Compare commits

...

6 Commits

2 changed files with 41 additions and 24 deletions

View File

@ -93,17 +93,17 @@ export function TplFS(client, parent, desc) {
} else throw new TplFSErrorInvalid('open', (this.path != '' ? this.path + '/' : '') + path.join('/'), 'Invalid path'); } else throw new TplFSErrorInvalid('open', (this.path != '' ? this.path + '/' : '') + path.join('/'), 'Invalid path');
}; };
this.mkdir = async (path, tries=5, delay=1000) => { this.mkdir = async (path, optimistic=true, tries=5, delay=1000) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/'); if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
var node = this; var node = this;
for(var i = 0; i < path.length; ++i) node = await node.raw_mkdir(path[i], tries, delay); for(var i = 0; i < path.length; ++i) node = await node.raw_mkdir(path[i], optimistic, tries, delay);
return node; return node;
}; };
this.create = async (path, data, mkdir=false, tries=5, delay=1000) => { this.create = async (path, data, optimistic=true, mkdir=false, tries=5, delay=1000) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/'); if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
if(path.length > 1) return await (mkdir ? await this.mkdir(path.slice(0, -1), tries, delay) : await this.open(path.slice(0, -1))).create(path.slice(-1), data, tries, delay); if(path.length > 1) return await (mkdir ? await this.mkdir(path.slice(0, -1), optimistic, tries, delay) : await this.open(path.slice(0, -1))).create(path.slice(-1), data, optimistic, mkdir, tries, delay);
else if(path.length == 1) return await this.raw_create(path[0], data, tries, delay); else if(path.length == 1) return await this.raw_create(path[0], data, optimistic, tries, delay);
else throw new TplFSErrorInvalid('create', (this.path != '' ? this.path + '/' : '') + path.join('/'), 'Invalid path'); else throw new TplFSErrorInvalid('create', (this.path != '' ? this.path + '/' : '') + path.join('/'), 'Invalid path');
}; };
@ -116,30 +116,35 @@ export function TplFS(client, parent, desc) {
this.update = async (path, data) => { this.update = async (path, data) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/'); if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
if(path.length > 0) return await (await this.open(path)).update([], data); if((path) && (path.length > 0)) return await (await this.open(path)).update([], data);
else return await this.raw_update(data); else return await this.raw_update(data);
}; };
this.cat = async (path=[]) => { this.cat = async (path) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/'); if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
if(path.length > 0) return await (await this.open(path)).raw_cat(); if((path) && (path.length > 0)) return await (await this.open(path)).raw_cat();
else return await this.raw_cat(path[0]); else return await this.raw_cat();
}; };
this.raw_mkdir = async (name, tries=5, delay=1000) => { this.raw_mkdir = async (name, optimistic=true, tries=5, delay=1000) => {
if((desc.type == 'C') || (desc.type == 'P')) { if((desc.type == 'C') || (desc.type == 'P')) {
if(name == '.') return this; if(name == '.') return this;
else if(name == '..') return this.parent ? this.parent : this; else if(name == '..') return this.parent ? this.parent : this;
if(optimistic) {
var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN);
var item = items.find(x => x.name == name);
if(item) return new TplFS(client, this, item);
}
if(await lock(tries, delay) == '1') try { if(await lock(tries, delay) == '1') try {
var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN); var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN);
var item = items.find(x => x.name == name); var item = items.find(x => x.name == name);
if(item) return new TplFS(client, this, item); if(item) return new TplFS(client, this, item);
var nodeid = await client.TIU_TEMPLATE_CREATE_MODIFY(0, { '.01': name, '.02': '@', '.03': 'C', '.04': 'I', '.05': '0', '.08': '0', '.09': '0', '.1': '0', '.11': '0', '.12': '0', '.13': '0', '.14': '0', '.15': '@', '.16': '0', '.17': '@', '.18': '@', '.19': '@', '.06': desc.personal_owner, '2,1': '@', '5,1': '@' }); var nodeid = await client.TIU_TEMPLATE_CREATE_MODIFY(0, { '.01': name, '.02': '@', '.03': 'C', '.04': 'I', '.05': '0', '.08': '0', '.09': '0', '.1': '0', '.11': '0', '.12': '0', '.13': '0', '.14': '0', '.15': '@', '.16': '0', '.17': '@', '.18': '@', '.19': '@', '.06': desc.personal_owner, '2,1': '@', '5,1': '@' });
var nodes = (await client.TIU_TEMPLATE_GETITEMS(desc.IEN)).map(x => x.IEN); var nodes = items.map(x => x.IEN);
nodes.push(nodeid); nodes.push(nodeid);
await client.TIU_TEMPLATE_SET_ITEMS(desc.IEN, nodes); await client.TIU_TEMPLATE_SET_ITEMS(desc.IEN, nodes);
nodeid = (await client.TIU_TEMPLATE_GETITEMS(desc.IEN)).find(x => (x.type == 'C') && (x.name == name)); item = (await client.TIU_TEMPLATE_GETITEMS(desc.IEN)).find(x => (x.type == 'C') && (x.name == name));
if(nodeid) return new TplFS(client, this, nodeid); if(item) return new TplFS(client, this, item);
else throw new TplFSErrorPerm('mkdir', (this.path != '' ? this.path + '/' : '') + name, 'Failed to create directory'); else throw new TplFSErrorPerm('mkdir', (this.path != '' ? this.path + '/' : '') + name, 'Failed to create directory');
} finally { } finally {
await client.TIU_TEMPLATE_UNLOCK(desc.IEN); await client.TIU_TEMPLATE_UNLOCK(desc.IEN);
@ -147,9 +152,14 @@ export function TplFS(client, parent, desc) {
} else throw new TplFSErrorInvalid('mkdir', this.path, 'Not a directory'); } else throw new TplFSErrorInvalid('mkdir', this.path, 'Not a directory');
}; };
this.raw_create = async (name, data, tries=5, delay=1000) => { this.raw_create = async (name, data, optimistic=true, tries=5, delay=1000) => {
if((desc.type == 'C') || (desc.type == 'P')) { if((desc.type == 'C') || (desc.type == 'P')) {
if((name == '.') || (name == '..')) throw new TplFSErrorInvalid('create', (this.path != '' ? this.path + '/' : '') + name, 'Invalid path'); if((name == '.') || (name == '..')) throw new TplFSErrorInvalid('create', (this.path != '' ? this.path + '/' : '') + name, 'Invalid path');
if(optimistic) {
var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN);
var item = items.find(x => x.name == name);
if(item) return (new TplFS(client, this, item)).raw_update(data);
}
if(await lock(tries, delay) == '1') try { if(await lock(tries, delay) == '1') try {
var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN); var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN);
var item = items.find(x => x.name == name); var item = items.find(x => x.name == name);
@ -217,9 +227,9 @@ export function EncFS(fs, data_encrypt, data_decrypt, path_encrypt, path_decrypt
this.list = async (filter=null) => (await fs.list(filter)).map(x => new EncFS(x, data_encrypt, data_decrypt, path_encrypt, path_decrypt)); this.list = async (filter=null) => (await fs.list(filter)).map(x => new EncFS(x, data_encrypt, data_decrypt, path_encrypt, path_decrypt));
this.open = async (path) => new EncFS(await fs.open(await path_encrypt(path)), data_encrypt, data_decrypt, path_encrypt, path_decrypt); this.open = async (path) => new EncFS(await fs.open(await path_encrypt(path)), data_encrypt, data_decrypt, path_encrypt, path_decrypt);
this.mkdir = async (path, tries=5, delay=1000) => new EncFS(await fs.mkdir(await path_encrypt(path), tries=5, delay=1000), data_encrypt, data_decrypt, path_encrypt, path_decrypt); this.mkdir = async (path, optimistic=true, tries=5, delay=1000) => new EncFS(await fs.mkdir(await path_encrypt(path), optimistic, tries, delay), data_encrypt, data_decrypt, path_encrypt, path_decrypt);
this.create = async (path, data, mkdir=false, tries=5, delay=1000) => new EncFS(await fs.create(await path_encrypt(path), await data_encrypt(data), mkdir=false, tries=5, delay=1000), data_encrypt, data_decrypt, path_encrypt, path_decrypt); this.create = async (path, data, optimistic=true, mkdir=false, tries=5, delay=1000) => new EncFS(await fs.create(await path_encrypt(path), await data_encrypt(data), optimistic, mkdir, tries, delay), data_encrypt, data_decrypt, path_encrypt, path_decrypt);
this.remove = async (path, tries=5, delay=1000) => await fs.remove(await path_encrypt(path), tries=5, delay=1000); this.remove = async (path, tries=5, delay=1000) => await fs.remove(await path_encrypt(path), tries, delay);
this.update = async (path, data) => new EncFS(await fs.update(await path_encrypt(path), await data_encrypt(data)), data_encrypt, data_decrypt, path_encrypt, path_decrypt); this.update = async (path, data) => new EncFS(await fs.update(await path_encrypt(path), await data_encrypt(data)), data_encrypt, data_decrypt, path_encrypt, path_decrypt);
this.cat = async (path) => await data_decrypt(await fs.cat(await path_encrypt(path))); this.cat = async (path) => await data_decrypt(await fs.cat(await path_encrypt(path)));
} }
@ -279,11 +289,17 @@ EncFS.fromPassword = async function(fs, password) {
return String.fromCharCode.apply(null, new Uint8Array(res)); return String.fromCharCode.apply(null, new Uint8Array(res));
} }
async function path_encrypt(data) { async function path_encrypt(data) {
return (await Promise.all(data.split('/').map(x => path_component_encrypt(x)))).join('/'); if(data) {
if(data.constructor === Array) return await Promise.all(data.map(x => path_component_encrypt(x)));
else return (await Promise.all(data.split('/').map(x => path_component_encrypt(x)))).join('/');
} else return data;
} }
async function path_decrypt(data) { async function path_decrypt(data) {
if(data.startsWith('/')) return '/' + await path_decrypt(data.replace(/^\/+/g, '')); if(data) {
return (await Promise.all(data.split('/').map(x => path_component_decrypt(x)))).join('/'); if(data.constructor === Array) return await Promise.all(data.map(x => path_component_decrypt(x)));
else if(data.startsWith('/')) return '/' + await path_decrypt(data.replace(/^\/+/g, ''));
else return (await Promise.all(data.split('/').map(x => path_component_decrypt(x)))).join('/');
} else return data;
} }
return new EncFS((await fs.mkdir('ZZZE ' + uint8array_to_b64(new Uint8Array(await digest('SHA-1', password))))).chroot(), data_encrypt, data_decrypt, path_encrypt, path_decrypt); return new EncFS((await fs.mkdir('ZZZE ' + uint8array_to_b64(new Uint8Array(await digest('SHA-1', password))))).chroot(), data_encrypt, data_decrypt, path_encrypt, path_decrypt);
}; };

View File

@ -153,15 +153,16 @@ export function Client(cid, secret) {
this.remotestate = reactive({}); this.remotestate = reactive({});
this.setup_remotestate = async () => { this.setup_remotestate = async () => {
var fs = await this.encfs(); var fs = await this.encfs(), file;
try { try {
Object.assign(this.remotestate, JSON.parse(await fs.cat('state'))); file = await fs.open('state');
Object.assign(this.remotestate, JSON.parse(await file.cat(null)));
} catch(ex) { } catch(ex) {
console.error(ex); console.error(ex);
await fs.create('state', JSON.stringify(this.remotestate)); file = await fs.create('state', JSON.stringify(this.remotestate));
} }
watch(this.remotestate, debounce(function(value) { watch(this.remotestate, debounce(function(value) {
fs.update('state', JSON.stringify(value)); file.update(null, JSON.stringify(value));
}, 1000), { deep: true }); }, 1000), { deep: true });
if((!this.remotestate.resources) && (localstate.resources)) this.remotestate.resources = localstate.resources; if((!this.remotestate.resources) && (localstate.resources)) this.remotestate.resources = localstate.resources;
if((!this.remotestate.practitioner) && (localstate.practitioner)) this.remotestate.practitioner = localstate.practitioner; if((!this.remotestate.practitioner) && (localstate.practitioner)) this.remotestate.practitioner = localstate.practitioner;