From 9806d718d5aaffee95c74d346ae12a88e1ed50d4 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 25 Jun 2021 20:53:09 +0530 Subject: [PATCH] implement block archival --- lib/commands/block.js | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/commands/block.js b/lib/commands/block.js index 8180cb68e..e405ede85 100644 --- a/lib/commands/block.js +++ b/lib/commands/block.js @@ -86,6 +86,20 @@ var createLoginBlockPath = function (Env, publicKey) { // FIXME BLOCKS return Path.join(Env.paths.block, safeKey.slice(0, 2), safeKey); }; +var createLoginBlockArchivePath = function (Env, publicKey) { + // prepare publicKey to be used as a file name + var safeKey = Util.escapeKeyCharacters(publicKey); + + // validate safeKey + if (typeof(safeKey) !== 'string') { + return; + } + + // derive the full path + // /home/cryptpad/cryptpad/block/fg/fg32kefksjdgjkewrjksdfksjdfsdfskdjfsfd + return Path.join(Env.paths.archive, 'block', safeKey.slice(0, 2), safeKey); +}; + Block.validateAncestorProof = function (Env, proof, _cb) { var cb = Util.once(Util.mkAsync(_cb)); /* prove that you own an existing block by signing for its publicKey */ @@ -216,7 +230,7 @@ Block.writeLoginBlock = function (Env, safeKey, msg, _cb) { // FIXME BLOCKS information, we can just sign some constant and use that as proof. */ -Block.removeLoginBlock = function (Env, safeKey, msg, cb) { // FIXME BLOCKS +Block.removeLoginBlock = function (Env, safeKey, msg, cb) { var publicKey = msg[0]; var signature = msg[1]; var block = Nacl.util.decodeUTF8('DELETE_BLOCK'); // clients and the server will have to agree on this constant @@ -230,21 +244,28 @@ Block.removeLoginBlock = function (Env, safeKey, msg, cb) { // FIXME BLOCKS validateLoginBlock(Env, publicKey, signature, block, function (e /*::, validatedBlock */) { if (e) { return void cb(e); } // derive the filepath - var path = createLoginBlockPath(Env, publicKey); + var currentPath = createLoginBlockPath(Env, publicKey); // make sure the path is valid - if (typeof(path) !== 'string') { + if (typeof(currentPath) !== 'string') { return void cb('E_INVALID_BLOCK_PATH'); } - // FIXME COLDSTORAGE - Fs.unlink(path, function (err) { - Env.Log.info('DELETION_BLOCK_BY_OWNER_RPC', { + var archivePath = createLoginBlockArchivePath(Env, publicKey); + // make sure the path is valid + if (typeof(archivePath) !== 'string') { + return void cb('E_INVALID_BLOCK_ARCHIVAL_PATH'); + } + + Fse.move(currentPath, archivePath, { + overwrite: true, + }, function (err) { + Env.Log.info('ARCHIVAL_BLOCK_BY_OWNER_RPC', { publicKey: publicKey, - path: path, + currentPath: currentPath, + archivePath: archivePath, status: err? String(err): 'SUCCESS', }); - if (err) { return void cb(err); } cb(); });