Skip to content

Commit

Permalink
Handling files deleted from the repository when walking current files…
Browse files Browse the repository at this point in the history
…ystem
  • Loading branch information
mroeder committed Jun 23, 2015
1 parent bd31f86 commit 621e115
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
37 changes: 33 additions & 4 deletions file-import-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions sqlite-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 621e115

Please sign in to comment.