2015-12-02 21:39:40 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-27 09:06:23 +02:00
|
|
|
var StorageBase = require('./storage-base'),
|
|
|
|
Launcher = require('../comp/launcher');
|
2015-12-02 21:39:40 +01:00
|
|
|
|
2016-02-06 11:59:57 +01:00
|
|
|
var fileWatchers = {};
|
|
|
|
|
2016-03-27 09:06:23 +02:00
|
|
|
var StorageFile = StorageBase.extend({
|
2015-12-02 21:39:40 +01:00
|
|
|
name: 'file',
|
2016-03-12 12:22:35 +01:00
|
|
|
icon: 'hdd-o',
|
2015-12-02 21:39:40 +01:00
|
|
|
enabled: !!Launcher,
|
2016-03-13 17:08:25 +01:00
|
|
|
system: true,
|
2016-08-21 18:07:59 +02:00
|
|
|
backup: true,
|
2015-12-02 21:39:40 +01:00
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
load: function(path, opts, callback) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Load', path);
|
|
|
|
var ts = this.logger.ts();
|
2015-12-02 21:39:40 +01:00
|
|
|
try {
|
|
|
|
var data = Launcher.readFile(path);
|
2016-02-06 11:59:57 +01:00
|
|
|
var rev = Launcher.statFile(path).mtime.getTime().toString();
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Loaded', path, rev, this.logger.ts(ts));
|
2016-02-06 11:59:57 +01:00
|
|
|
if (callback) { callback(null, data.buffer, { rev: rev }); }
|
2015-12-02 21:39:40 +01:00
|
|
|
} catch (e) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.error('Error reading local file', path, e);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e, null); }
|
2015-12-02 21:39:40 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
stat: function(path, opts, callback) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Stat', path);
|
|
|
|
var ts = this.logger.ts();
|
2015-12-02 21:39:40 +01:00
|
|
|
try {
|
2016-02-06 11:59:57 +01:00
|
|
|
var stat = Launcher.statFile(path);
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Stat done', path, this.logger.ts(ts));
|
2016-02-06 11:59:57 +01:00
|
|
|
if (callback) { callback(null, { rev: stat.mtime.getTime().toString() }); }
|
|
|
|
} catch (e) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.error('Error stat local file', path, e);
|
2016-02-06 11:59:57 +01:00
|
|
|
if (e.code === 'ENOENT') {
|
|
|
|
e.notFound = true;
|
|
|
|
}
|
|
|
|
if (callback) { callback(e, null); }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
save: function(path, opts, data, callback, rev) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Save', path, rev);
|
|
|
|
var ts = this.logger.ts();
|
2016-02-06 11:59:57 +01:00
|
|
|
try {
|
|
|
|
if (rev) {
|
|
|
|
try {
|
|
|
|
var stat = Launcher.statFile(path);
|
|
|
|
var fileRev = stat.mtime.getTime().toString();
|
|
|
|
if (fileRev !== rev) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Save mtime differs', rev, fileRev);
|
2016-02-06 11:59:57 +01:00
|
|
|
if (callback) { callback({ revConflict: true }, { rev: fileRev }); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// file doesn't exist or we cannot stat it: don't care and overwrite
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:39:40 +01:00
|
|
|
Launcher.writeFile(path, data);
|
2016-02-06 11:59:57 +01:00
|
|
|
var newRev = Launcher.statFile(path).mtime.getTime().toString();
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Saved', path, this.logger.ts(ts));
|
2016-02-06 11:59:57 +01:00
|
|
|
if (callback) { callback(undefined, { rev: newRev }); }
|
2015-12-02 21:39:40 +01:00
|
|
|
} catch (e) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.error('Error writing local file', path, e);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e); }
|
2015-12-02 21:39:40 +01:00
|
|
|
}
|
2016-02-06 11:59:57 +01:00
|
|
|
},
|
|
|
|
|
2016-08-21 18:07:59 +02:00
|
|
|
mkdir: function(path, callback) {
|
|
|
|
this.logger.debug('Make dir', path);
|
|
|
|
var ts = this.logger.ts();
|
|
|
|
try {
|
|
|
|
Launcher.mkdir(path);
|
|
|
|
this.logger.debug('Made dir', path, this.logger.ts(ts));
|
|
|
|
if (callback) { callback(); }
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.error('Error making local dir', path, e);
|
|
|
|
if (callback) { callback(e); }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-06 11:59:57 +01:00
|
|
|
watch: function(path, callback) {
|
|
|
|
var names = Launcher.parsePath(path);
|
|
|
|
if (!fileWatchers[names.dir]) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Watch dir', names.dir);
|
2016-02-06 11:59:57 +01:00
|
|
|
var fsWatcher = Launcher.createFsWatcher(names.dir);
|
|
|
|
fsWatcher.on('change', this.fsWatcherChange.bind(this, names.dir));
|
|
|
|
fileWatchers[names.dir] = { fsWatcher: fsWatcher, callbacks: [] };
|
|
|
|
}
|
|
|
|
fileWatchers[names.dir].callbacks.push({ file: names.file, callback: callback });
|
|
|
|
},
|
|
|
|
|
|
|
|
unwatch: function(path) {
|
|
|
|
var names = Launcher.parsePath(path);
|
|
|
|
var watcher = fileWatchers[names.dir];
|
|
|
|
if (watcher) {
|
2016-07-17 13:30:38 +02:00
|
|
|
var ix = watcher.callbacks.findIndex(cb => cb.file === names.file);
|
2016-02-06 11:59:57 +01:00
|
|
|
if (ix >= 0) {
|
|
|
|
watcher.callbacks.splice(ix, 1);
|
|
|
|
}
|
|
|
|
if (!watcher.callbacks.length) {
|
2016-03-27 09:06:23 +02:00
|
|
|
this.logger.debug('Stop watch dir', names.dir);
|
2016-02-06 11:59:57 +01:00
|
|
|
watcher.fsWatcher.close();
|
|
|
|
delete fileWatchers[names.dir];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fsWatcherChange: function(dirname, evt, fileName) {
|
|
|
|
var watcher = fileWatchers[dirname];
|
|
|
|
if (watcher) {
|
2016-07-17 13:30:38 +02:00
|
|
|
watcher.callbacks.forEach(cb => {
|
2016-02-06 11:59:57 +01:00
|
|
|
if (cb.file === fileName && typeof cb.callback === 'function') {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('File changed', dirname, evt, fileName);
|
2016-02-06 11:59:57 +01:00
|
|
|
cb.callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-12-02 21:39:40 +01:00
|
|
|
}
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2015-12-02 21:39:40 +01:00
|
|
|
|
2016-03-27 09:46:43 +02:00
|
|
|
module.exports = new StorageFile();
|