Files
cloudron-box/src/storage/filesystem.js

229 lines
8.5 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
upload: upload,
download: download,
downloadDir: downloadDir,
copy: copy,
remove: remove,
removeDir: removeDir,
2016-09-16 11:21:08 +02:00
testConfig: testConfig
};
var assert = require('assert'),
async = require('async'),
BackupsError = require('../backups.js').BackupsError,
debug = require('debug')('box:storage/filesystem'),
EventEmitter = require('events'),
fs = require('fs'),
mkdirp = require('mkdirp'),
2017-04-18 15:32:59 +02:00
path = require('path'),
readdirp = require('readdirp'),
safe = require('safetydance'),
shell = require('../shell.js');
2017-04-18 15:32:59 +02:00
// storage api
function upload(apiConfig, backupFilePath, sourceStream, callback) {
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof sourceStream, 'object');
assert.strictEqual(typeof callback, 'function');
mkdirp(path.dirname(backupFilePath), function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2016-09-16 11:21:08 +02:00
2017-09-22 14:40:37 -07:00
safe.fs.unlinkSync(backupFilePath); // remove any hardlink
var fileStream = fs.createWriteStream(backupFilePath);
2016-09-16 11:21:08 +02:00
// this pattern is required to ensure that the file got created before 'finish'
fileStream.on('open', function () {
sourceStream.pipe(fileStream);
});
2017-04-20 15:35:52 +02:00
fileStream.on('error', function (error) {
debug('[%s] upload: out stream error.', backupFilePath, error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-04-20 15:35:52 +02:00
});
2017-09-26 16:42:54 -07:00
fileStream.on('finish', function () {
2017-09-27 14:44:48 -07:00
// in test, upload() may or may not be called via sudo script
const BACKUP_UID = parseInt(process.env.SUDO_UID, 10) || process.getuid();
if (!safe.fs.chownSync(backupFilePath, BACKUP_UID, BACKUP_UID)) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, 'Unable to chown:' + safe.error.message));
if (!safe.fs.chownSync(path.dirname(backupFilePath), BACKUP_UID, BACKUP_UID)) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, 'Unable to chown:' + safe.error.message));
2017-09-27 14:44:48 -07:00
debug('upload %s: done.', backupFilePath);
2017-09-27 14:44:48 -07:00
callback(null);
});
});
2016-09-16 11:21:08 +02:00
}
function download(apiConfig, sourceFilePath, callback) {
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof sourceFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
debug('download: %s', sourceFilePath);
2017-09-28 14:26:39 -07:00
if (!safe.fs.existsSync(sourceFilePath)) return callback(new BackupsError(BackupsError.NOT_FOUND, 'File not found'));
var fileStream = fs.createReadStream(sourceFilePath);
2017-09-28 14:26:39 -07:00
callback(null, fileStream);
}
function listDir(apiConfig, dir, batchSize, iteratorCallback, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof dir, 'string');
assert.strictEqual(typeof batchSize, 'number');
assert.strictEqual(typeof iteratorCallback, 'function');
assert.strictEqual(typeof callback, 'function');
var entries = [];
var entryStream = readdirp({ root: dir, entryType: 'files' });
entryStream.on('data', function (data) {
entries.push(data.path);
if (entries.length < batchSize) return;
entryStream.pause();
iteratorCallback(entries, function (error) {
if (error) return callback(error);
entries = [];
entryStream.resume();
});
});
entryStream.on('warn', function (error) {
debug('listDir: warning ', error);
});
entryStream.on('end', function () {
iteratorCallback(entries, callback);
});
}
2017-10-11 12:41:40 -07:00
function downloadDir(apiConfig, backupFilePath, destDir) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof destDir, 'string');
2017-10-10 20:23:04 -07:00
var events = new EventEmitter();
events.emit('progress', `downloadDir: ${backupFilePath} to ${destDir}`);
function downloadFile(filePath, callback) {
const sourceFilePath = path.join(backupFilePath, filePath);
const destFilePath = path.join(destDir, filePath);
mkdirp(path.dirname(destFilePath), function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
let sourceStream = fs.createReadStream(sourceFilePath);
sourceStream.on('error', callback);
let destStream = fs.createWriteStream(destFilePath);
destStream.on('error', callback);
events.emit('progress', `downloadDir: Copying ${sourceFilePath} to ${destFilePath}`);
sourceStream.pipe(destStream, { end: true }).on('finish', callback);
});
}
listDir(apiConfig, backupFilePath, 1000, function (filePaths, done) {
async.each(filePaths, downloadFile, done);
}, function (error) {
2017-10-10 20:23:04 -07:00
if (error) return events.emit('done', new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-10-10 20:23:04 -07:00
events.emit('done', null);
});
2017-10-10 20:23:04 -07:00
return events;
}
function copy(apiConfig, oldFilePath, newFilePath) {
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof oldFilePath, 'string');
assert.strictEqual(typeof newFilePath, 'string');
2017-09-18 14:25:46 -07:00
debug('copy: %s -> %s', oldFilePath, newFilePath);
var events = new EventEmitter();
mkdirp(path.dirname(newFilePath), function (error) {
if (error) return events.emit('done', new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-09-18 12:42:42 -07:00
// this will hardlink backups saving space
var cpOptions = apiConfig.noHardlinks ? '-a' : '-al';
shell.exec('copy', '/bin/cp', [ cpOptions, oldFilePath, newFilePath ], { }, function (error) {
if (error) return events.emit('done', new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
events.emit('done', null);
});
2017-04-20 15:41:25 +02:00
});
return events;
}
function remove(apiConfig, filename, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof filename, 'string');
assert.strictEqual(typeof callback, 'function');
var stat = safe.fs.statSync(filename);
if (!stat) return callback();
if (stat.isFile()) {
2017-09-30 17:28:35 -07:00
if (!safe.fs.unlinkSync(filename)) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, safe.error.message));
} else if (stat.isDirectory()) {
2017-09-30 17:28:35 -07:00
if (!safe.fs.rmdirSync(filename)) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, safe.error.message));
}
2017-10-02 20:08:00 -07:00
callback(null);
}
2017-10-10 20:23:04 -07:00
function removeDir(apiConfig, pathPrefix) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof pathPrefix, 'string');
2017-10-10 20:23:04 -07:00
var events = new EventEmitter();
events.emit('progress', `downloadDir: ${pathPrefix}`);
shell.exec('removeDir', '/bin/rm', [ '-rf', pathPrefix ], { }, function (error) {
2017-10-10 20:23:04 -07:00
if (error) return events.emit('done', new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-10-10 20:23:04 -07:00
events.emit('done', null);
});
2017-10-10 20:23:04 -07:00
return events;
}
function testConfig(apiConfig, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof callback, 'function');
2017-09-19 12:43:13 -07:00
if (typeof apiConfig.backupFolder !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'backupFolder must be string'));
2017-09-19 12:43:13 -07:00
if (!apiConfig.backupFolder) return callback(new BackupsError(BackupsError.BAD_FIELD, 'backupFolder is required'));
if ('noHardlinks' in apiConfig && typeof apiConfig.noHardlinks !== 'boolean') return callback(new BackupsError(BackupsError.BAD_FIELD, 'noHardlinks must be boolean'));
if ('externalDisk' in apiConfig && typeof apiConfig.externalDisk !== 'boolean') return callback(new BackupsError(BackupsError.BAD_FIELD, 'externalDisk must be boolean'));
fs.stat(apiConfig.backupFolder, function (error, result) {
if (error) return callback(new BackupsError(BackupsError.BAD_FIELD, 'Directory does not exist or cannot be accessed: ' + error.message));
if (!result.isDirectory()) return callback(new BackupsError(BackupsError.BAD_FIELD, 'Backup location is not a directory'));
mkdirp(path.join(apiConfig.backupFolder, 'snapshot'), function (error) {
if (error && error.code === 'EACCES') return callback(new BackupsError(BackupsError.BAD_FIELD, `Access denied. Run "chown yellowtent:yellowtent ${apiConfig.backupFolder}" on the server`));
if (error) return callback(new BackupsError(BackupsError.BAD_FIELD, error.message));
callback(null);
});
});
}
2017-01-04 16:22:58 -08:00