Compare commits

..

No commits in common. "f8a50c31b600c5a33c40347d80a928b4788b70d0" and "a30ee4a3da40555f9886f44bef7e233cb7ed3075" have entirely different histories.

2 changed files with 24 additions and 41 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');
};
this.mkdir = async (path, optimistic=true, tries=5, delay=1000) => {
this.mkdir = async (path, tries=5, delay=1000) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
var node = this;
for(var i = 0; i < path.length; ++i) node = await node.raw_mkdir(path[i], optimistic, tries, delay);
for(var i = 0; i < path.length; ++i) node = await node.raw_mkdir(path[i], tries, delay);
return node;
};
this.create = async (path, data, optimistic=true, mkdir=false, tries=5, delay=1000) => {
this.create = async (path, data, mkdir=false, tries=5, delay=1000) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
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, optimistic, tries, delay);
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);
else if(path.length == 1) return await this.raw_create(path[0], data, tries, delay);
else throw new TplFSErrorInvalid('create', (this.path != '' ? this.path + '/' : '') + path.join('/'), 'Invalid path');
};
@ -116,35 +116,30 @@ export function TplFS(client, parent, desc) {
this.update = async (path, data) => {
if((typeof path === 'string') || (path instanceof String)) path = path.split('/');
if((path) && (path.length > 0)) return await (await this.open(path)).update([], data);
if(path.length > 0) return await (await this.open(path)).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((path) && (path.length > 0)) return await (await this.open(path)).raw_cat();
else return await this.raw_cat();
if(path.length > 0) return await (await this.open(path)).raw_cat();
else return await this.raw_cat(path[0]);
};
this.raw_mkdir = async (name, optimistic=true, tries=5, delay=1000) => {
this.raw_mkdir = async (name, tries=5, delay=1000) => {
if((desc.type == 'C') || (desc.type == 'P')) {
if(name == '.') return 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 {
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);
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 = items.map(x => x.IEN);
var nodes = (await client.TIU_TEMPLATE_GETITEMS(desc.IEN)).map(x => x.IEN);
nodes.push(nodeid);
await client.TIU_TEMPLATE_SET_ITEMS(desc.IEN, nodes);
item = (await client.TIU_TEMPLATE_GETITEMS(desc.IEN)).find(x => (x.type == 'C') && (x.name == name));
if(item) return new TplFS(client, this, item);
nodeid = (await client.TIU_TEMPLATE_GETITEMS(desc.IEN)).find(x => (x.type == 'C') && (x.name == name));
if(nodeid) return new TplFS(client, this, nodeid);
else throw new TplFSErrorPerm('mkdir', (this.path != '' ? this.path + '/' : '') + name, 'Failed to create directory');
} finally {
await client.TIU_TEMPLATE_UNLOCK(desc.IEN);
@ -152,14 +147,9 @@ export function TplFS(client, parent, desc) {
} else throw new TplFSErrorInvalid('mkdir', this.path, 'Not a directory');
};
this.raw_create = async (name, data, optimistic=true, tries=5, delay=1000) => {
this.raw_create = async (name, data, tries=5, delay=1000) => {
if((desc.type == 'C') || (desc.type == 'P')) {
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 {
var items = await client.TIU_TEMPLATE_GETITEMS(desc.IEN);
var item = items.find(x => x.name == name);
@ -227,9 +217,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.open = async (path) => new EncFS(await fs.open(await path_encrypt(path)), 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, 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, delay);
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.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.remove = async (path, tries=5, delay=1000) => await fs.remove(await path_encrypt(path), tries=5, delay=1000);
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)));
}
@ -289,17 +279,11 @@ EncFS.fromPassword = async function(fs, password) {
return String.fromCharCode.apply(null, new Uint8Array(res));
}
async function path_encrypt(data) {
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;
return (await Promise.all(data.split('/').map(x => path_component_encrypt(x)))).join('/');
}
async function path_decrypt(data) {
if(data) {
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;
if(data.startsWith('/')) return '/' + await path_decrypt(data.replace(/^\/+/g, ''));
return (await Promise.all(data.split('/').map(x => path_component_decrypt(x)))).join('/');
}
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,16 +153,15 @@ export function Client(cid, secret) {
this.remotestate = reactive({});
this.setup_remotestate = async () => {
var fs = await this.encfs(), file;
var fs = await this.encfs();
try {
file = await fs.open('state');
Object.assign(this.remotestate, JSON.parse(await file.cat(null)));
Object.assign(this.remotestate, JSON.parse(await fs.cat('state')));
} catch(ex) {
console.error(ex);
file = await fs.create('state', JSON.stringify(this.remotestate));
await fs.create('state', JSON.stringify(this.remotestate));
}
watch(this.remotestate, debounce(function(value) {
file.update(null, JSON.stringify(value));
fs.update('state', JSON.stringify(value));
}, 1000), { deep: true });
if((!this.remotestate.resources) && (localstate.resources)) this.remotestate.resources = localstate.resources;
if((!this.remotestate.practitioner) && (localstate.practitioner)) this.remotestate.practitioner = localstate.practitioner;