From 621e115853e11e32d038868123de744b361c2368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Ro=CC=88der?= Date: Tue, 23 Jun 2015 16:12:11 -0700 Subject: [PATCH] Handling files deleted from the repository when walking current filesystem --- file-import-task.js | 37 +++++++++++++++++++++++++++++++++---- sqlite-storage.js | 1 + 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/file-import-task.js b/file-import-task.js index 33a28bb..d325a6d 100644 --- a/file-import-task.js +++ b/file-import-task.js @@ -16,8 +16,24 @@ var d = require('./domain'); */ function processFile(lvfs, fi, thenDo) { - var rootDir = lvfs.getRootDirectory(); - fs.readFile(path.join(rootDir, fi.path), handleReadResult); + var rootDir = lvfs.getRootDirectory(), + fullPath = path.join(rootDir, fi.path); + fs.exists(fullPath, handleExistsResult); + + function handleExistsResult(exists) { + if (exists) + fs.readFile(fullPath, handleReadResult); + else { + lvfs.createVersionRecord({ + change: 'deletion', + date: new Date().toISOString().replace(/[0-9]{3}Z/, '000Z'), + path: fi.path, + stat: null, + content: null + }, thenDo); + } + } + function handleReadResult(err, content) { if (!err) { lvfs.createVersionRecord({ @@ -53,9 +69,12 @@ function createBatches(files, thenDo) { function filterFilesThatAreInStorage(lvfs, files, thenDo) { // files = [{path: STRING, stat: {mtime: DATE, ...}}] - var queryLimit = 3, allNewFiles = []; + var queryLimit = 3, + allNewFiles = [], + allFiles = []; var cargo = async.cargo(function(files, next) { var paths = files.map(function(f) { return f.path; }); + allFiles = allFiles.concat(paths); lvfs.getRecords({paths: paths, newest: true, attributes: ['path','date']}, function(err, versionRecords) { if (err) { console.error('error in filterFilesThatAreInStorage: ', err); @@ -80,7 +99,17 @@ function filterFilesThatAreInStorage(lvfs, files, thenDo) { }, queryLimit); cargo.push(files); cargo.drain = function() { - thenDo(null, allNewFiles); }; + lvfs.getRecords({ newest: true, exists: true, attributes: ['path'] }, function(err, versionRecords) { + var recordFiles = versionRecords.map(function(rec) { return rec.path; }), + deletedFiles = recordFiles.filter(function(i) { + return allFiles.indexOf(i) < 0; + }).map(function(file) { + console.log('Deleting non-existing file %s.', file); + return { path: file, stat: { size: 0 } }; // fake fileinfo + }); + thenDo(null, allNewFiles.concat(deletedFiles)); + }); + }; } function runTask(lvfs, thenDo) { diff --git a/sqlite-storage.js b/sqlite-storage.js index 0069a14..7752faa 100644 --- a/sqlite-storage.js +++ b/sqlite-storage.js @@ -213,6 +213,7 @@ util._extend(SQLiteStore.prototype, d.bindMethods({ // groupByPaths: BOOL, -- return an object with rows grouped (keys of result) // attributes: [STRING], -- which attributes to return from stored records // newest: BOOL, -- only return most recent version of a record + // exists: BOOL, -- only return records that still exist // paths: [STRING], -- filter records by path names // pathPatterns: [STRING], -- pattern to match paths // version: [STRING|NUMBER], -- the version number