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

157 lines
5.5 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
upload: upload,
download: download,
downloadDir: downloadDir,
copy: copy,
remove: remove,
2016-09-16 11:21:08 +02:00
2017-01-04 16:22:58 -08:00
backupDone: backupDone,
testConfig: testConfig
};
var assert = require('assert'),
BackupsError = require('../backups.js').BackupsError,
config = require('../config.js'),
debug = require('debug')('box:storage/filesystem'),
fs = require('fs'),
mkdirp = require('mkdirp'),
PassThrough = require('stream').PassThrough,
2017-04-18 15:32:59 +02:00
path = require('path'),
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');
2017-09-27 10:25:36 -07:00
// in test, upload() may or may not be called via sudo script
const BACKUP_USER = config.TEST ? (process.env.SUDO_USER || process.env.USER) : 'yellowtent';
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
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 () {
shell.exec('upload', '/bin/chown', [ '-R', `${BACKUP_USER}:${BACKUP_USER}`, path.dirname(backupFilePath) ], { }, function (error) {
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
debug('upload %s: done.', backupFilePath);
callback(null);
});
});
sourceStream.pipe(fileStream);
});
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);
var ps = new PassThrough();
var fileStream = fs.createReadStream(sourceFilePath);
fileStream.on('error', function (error) {
ps.emit('error', new BackupsError(BackupsError.NOT_FOUND, error.message));
});
fileStream.pipe(ps);
callback(null, ps);
}
function downloadDir(apiConfig, backupFilePath, destDir, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof destDir, 'string');
assert.strictEqual(typeof callback, 'function');
debug('downloadDir: %s -> %s', backupFilePath, destDir);
shell.exec('downloadDir', '/bin/cp', [ '-r', backupFilePath + '/.', destDir ], { }, function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
callback();
});
}
2017-09-19 20:40:38 -07:00
function copy(apiConfig, oldFilePath, newFilePath, callback) {
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof oldFilePath, 'string');
assert.strictEqual(typeof newFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
2017-09-18 14:25:46 -07:00
debug('copy: %s -> %s', oldFilePath, newFilePath);
mkdirp(path.dirname(newFilePath), function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-09-18 12:42:42 -07:00
// this will hardlink backups saving space
shell.exec('copy', '/bin/cp', [ '-al', oldFilePath, newFilePath ], { }, function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
callback();
});
2017-04-20 15:41:25 +02:00
});
}
function remove(apiConfig, pathPrefix, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof pathPrefix, 'string');
assert.strictEqual(typeof callback, 'function');
shell.exec('remove', '/bin/rm', [ '-rf', pathPrefix ], { }, function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
safe.fs.rmdirSync(path.dirname(pathPrefix)); // try to cleanup empty directories
callback();
});
}
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'));
fs.stat(apiConfig.backupFolder, function (error, result) {
if (error) {
debug('testConfig: %s', apiConfig.backupFolder, error);
return callback(new BackupsError(BackupsError.BAD_FIELD, 'Directory does not exist or cannot be accessed'));
}
if (!result.isDirectory()) return callback(new BackupsError(BackupsError.BAD_FIELD, 'Backup location is not a directory'));
callback(null);
});
}
2017-01-04 16:22:58 -08:00
2017-09-26 12:28:33 -07:00
function backupDone(apiConfig, backupId, appBackupIds, callback) {
assert.strictEqual(typeof apiConfig, 'object');
2017-04-21 10:31:43 +02:00
assert.strictEqual(typeof backupId, 'string');
assert(Array.isArray(appBackupIds));
2017-01-04 16:22:58 -08:00
assert.strictEqual(typeof callback, 'function');
callback();
}