Handle fs errors

This commit is contained in:
Girish Ramakrishnan
2017-09-26 11:59:45 -07:00
parent 12083f5608
commit 4e68abe51d
4 changed files with 21 additions and 15 deletions

6
npm-shrinkwrap.json generated
View File

@@ -4495,9 +4495,9 @@
"optional": true "optional": true
}, },
"safetydance": { "safetydance": {
"version": "0.4.0", "version": "0.6.0",
"from": "safetydance@0.4.0", "from": "safetydance@0.6.0",
"resolved": "https://registry.npmjs.org/safetydance/-/safetydance-0.4.0.tgz" "resolved": "https://registry.npmjs.org/safetydance/-/safetydance-0.6.0.tgz"
}, },
"sass-graph": { "sass-graph": {
"version": "2.2.4", "version": "2.2.4",

View File

@@ -59,7 +59,7 @@
"progress-stream": "^2.0.0", "progress-stream": "^2.0.0",
"proxy-middleware": "^0.13.0", "proxy-middleware": "^0.13.0",
"s3-block-read-stream": "^0.2.0", "s3-block-read-stream": "^0.2.0",
"safetydance": "^0.4.0", "safetydance": "^0.6.0",
"semver": "^4.3.6", "semver": "^4.3.6",
"showdown": "^1.6.0", "showdown": "^1.6.0",
"split": "^1.0.0", "split": "^1.0.0",

View File

@@ -225,7 +225,11 @@ function sync(backupConfig, backupId, dataDir, callback) {
} else if (task.operation === 'remove') { } else if (task.operation === 'remove') {
api(backupConfig.provider).remove(backupConfig, getBackupFilePath(backupConfig, backupId, task.path), iteratorCallback); api(backupConfig.provider).remove(backupConfig, getBackupFilePath(backupConfig, backupId, task.path), iteratorCallback);
} }
}, callback); }, function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
callback();
});
} }
function saveEmptyDirs(appDataDir, callback) { function saveEmptyDirs(appDataDir, callback) {

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
var assert = require('assert'), var assert = require('assert'),
fs = require('fs'), debug = require('debug')('box:syncer'),
path = require('path'), path = require('path'),
paths = require('./paths.js'), paths = require('./paths.js'),
safe = require('safetydance'); safe = require('safetydance');
@@ -15,7 +15,7 @@ function readCache(cacheFile) {
var cache = safe.fs.readFileSync(cacheFile, 'utf8'); var cache = safe.fs.readFileSync(cacheFile, 'utf8');
if (!cache) return [ ]; if (!cache) return [ ];
var result = cache.split('\n').map(JSON.parse); var result = cache.trim().split('\n').map(JSON.parse);
return result; return result;
} }
@@ -25,8 +25,7 @@ function readTree(dir) {
var list = safe.fs.readdirSync(dir).sort(); var list = safe.fs.readdirSync(dir).sort();
if (!list) return [ ]; if (!list) return [ ];
// TODO: handle lstat errors return list.map(function (e) { return { stat: safe.fs.lstatSync(path.join(dir, e)), name: e }; });
return list.map(function (e) { return { stat: fs.lstatSync(path.join(dir, e)), name: e }; });
} }
// TODO: concurrency // TODO: concurrency
@@ -42,7 +41,8 @@ function sync(dir, taskProcessor, callback) {
var cache = readCache(cacheFile); var cache = readCache(cacheFile);
var newCacheFd = fs.openSync(newCacheFile, 'w'); // truncates any existing file var newCacheFd = safe.fs.openSync(newCacheFile, 'w'); // truncates any existing file
if (newCacheFd === -1) return callback(new Error('Error opening new cache file: ' + safe.error.message));
var dummyCallback = function() { }; var dummyCallback = function() { };
@@ -59,6 +59,7 @@ function sync(dir, taskProcessor, callback) {
var entryPath = path.join(relpath, entries[i].name); var entryPath = path.join(relpath, entries[i].name);
var stat = entries[i].stat; var stat = entries[i].stat;
if (!stat) continue; // some stat error
if (!stat.isDirectory() && !stat.isFile()) continue; if (!stat.isDirectory() && !stat.isFile()) continue;
if (stat.isSymbolicLink()) continue; if (stat.isSymbolicLink()) continue;
@@ -67,7 +68,7 @@ function sync(dir, taskProcessor, callback) {
continue; continue;
} }
fs.appendFileSync(newCacheFd, JSON.stringify({ path: entryPath, mtime: stat.mtime.getTime() }) + '\n'); safe.fs.appendFileSync(newCacheFd, JSON.stringify({ path: entryPath, mtime: stat.mtime.getTime() }) + '\n');
advanceCache(entryPath); advanceCache(entryPath);
@@ -83,12 +84,13 @@ function sync(dir, taskProcessor, callback) {
} }
traverse(''); traverse('');
advanceCache(''); // remove rest of the cache entries advanceCache(''); // remove rest of the cache entries
// move the new cache file // move the new cache file
fs.closeSync(newCacheFd); safe.fs.closeSync(newCacheFd);
fs.unlinkSync(cacheFile); safe.fs.unlinkSync(cacheFile);
fs.renameSync(cacheFile, newCacheFd);
if (!safe.fs.renameSync(newCacheFile, cacheFile)) debug('Unable to save new cache file');
callback(); callback();
} }