2017-11-03 02:09:34 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const { promiseChain } = require('lib/promise-utils.js');
|
|
|
|
const moment = require('moment');
|
2017-12-14 20:12:14 +02:00
|
|
|
const BaseItem = require('lib/models/BaseItem.js');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { time } = require('lib/time-utils.js');
|
2017-06-11 23:11:14 +02:00
|
|
|
|
2017-07-24 20:58:11 +02:00
|
|
|
// NOTE: when synchronising with the file system the time resolution is the second (unlike milliseconds for OneDrive for instance).
|
|
|
|
// What it means is that if, for example, client 1 changes a note at time t, and client 2 changes the same note within the same second,
|
|
|
|
// both clients will not know about each others updates during the next sync. They will simply both sync their note and whoever
|
|
|
|
// comes last will overwrite (on the remote storage) the note of the other client. Both client will then have a different note at
|
|
|
|
// that point and that will only be resolved if one of them changes the note and sync (if they don't change it, it will never get resolved).
|
|
|
|
//
|
|
|
|
// This is compound with the fact that we can't have a reliable delta API on the file system so we need to check all the timestamps
|
|
|
|
// every time and rely on this exclusively to know about changes.
|
|
|
|
//
|
|
|
|
// This explains occasional failures of the fuzzing program (it finds that the clients end up with two different notes after sync). To
|
|
|
|
// check that it is indeed the problem, check log-database.txt of both clients, search for the note ID, and most likely both notes
|
|
|
|
// will have been modified at the same exact second at some point. If not, it's another bug that needs to be investigated.
|
2017-07-16 14:53:59 +02:00
|
|
|
|
2017-07-24 20:58:11 +02:00
|
|
|
class FileApiDriverLocal {
|
2017-07-16 14:53:59 +02:00
|
|
|
|
2017-07-03 20:58:01 +02:00
|
|
|
fsErrorToJsError_(error) {
|
|
|
|
let msg = error.toString();
|
|
|
|
let output = new Error(msg);
|
|
|
|
if (error.code) output.code = error.code;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
stat(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.stat(path, (error, s) => {
|
|
|
|
if (error) {
|
2017-06-15 01:14:15 +02:00
|
|
|
if (error.code == 'ENOENT') {
|
|
|
|
resolve(null);
|
|
|
|
} else {
|
2017-07-03 20:58:01 +02:00
|
|
|
reject(this.fsErrorToJsError_(error));
|
2017-06-15 01:14:15 +02:00
|
|
|
}
|
2017-06-11 23:11:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-15 01:14:15 +02:00
|
|
|
resolve(this.metadataFromStats_(path, s));
|
2017-06-11 23:11:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-27 21:48:01 +02:00
|
|
|
statTimeToTimestampMs_(time) {
|
2017-06-11 23:11:14 +02:00
|
|
|
let m = moment(time, 'YYYY-MM-DDTHH:mm:ss.SSSZ');
|
|
|
|
if (!m.isValid()) {
|
|
|
|
throw new Error('Invalid date: ' + time);
|
|
|
|
}
|
2017-06-27 21:48:01 +02:00
|
|
|
return m.toDate().getTime();
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 23:56:27 +02:00
|
|
|
metadataFromStats_(path, stats) {
|
2017-06-11 23:11:14 +02:00
|
|
|
return {
|
2017-06-12 23:56:27 +02:00
|
|
|
path: path,
|
2017-06-27 21:48:01 +02:00
|
|
|
created_time: this.statTimeToTimestampMs_(stats.birthtime),
|
|
|
|
updated_time: this.statTimeToTimestampMs_(stats.mtime),
|
2017-06-19 00:06:10 +02:00
|
|
|
created_time_orig: stats.birthtime,
|
|
|
|
updated_time_orig: stats.mtime,
|
2017-06-11 23:11:14 +02:00
|
|
|
isDir: stats.isDirectory(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-27 21:48:01 +02:00
|
|
|
setTimestamp(path, timestampMs) {
|
2017-06-12 23:56:27 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-27 21:48:01 +02:00
|
|
|
let t = Math.floor(timestampMs / 1000);
|
|
|
|
fs.utimes(path, t, t, (error) => {
|
2017-06-12 23:56:27 +02:00
|
|
|
if (error) {
|
2017-07-03 20:58:01 +02:00
|
|
|
reject(this.fsErrorToJsError_(error));
|
2017-06-12 23:56:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:14:20 +02:00
|
|
|
async delta(path, options) {
|
2017-10-26 23:56:32 +02:00
|
|
|
const itemIds = await options.allItemIdsHandler();
|
|
|
|
|
2017-07-19 00:14:20 +02:00
|
|
|
try {
|
|
|
|
let items = await fs.readdir(path);
|
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
let stat = await this.stat(path + '/' + items[i]);
|
|
|
|
if (!stat) continue; // Has been deleted between the readdir() call and now
|
|
|
|
stat.path = items[i];
|
|
|
|
output.push(stat);
|
|
|
|
}
|
|
|
|
|
2017-10-26 23:56:32 +02:00
|
|
|
if (!Array.isArray(itemIds)) throw new Error('Delta API not supported - local IDs must be provided');
|
2017-07-19 00:14:20 +02:00
|
|
|
|
|
|
|
let deletedItems = [];
|
2017-10-26 23:56:32 +02:00
|
|
|
for (let i = 0; i < itemIds.length; i++) {
|
|
|
|
const itemId = itemIds[i];
|
2017-07-19 00:14:20 +02:00
|
|
|
let found = false;
|
|
|
|
for (let j = 0; j < output.length; j++) {
|
|
|
|
const item = output[j];
|
|
|
|
if (BaseItem.pathToId(item.path) == itemId) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
deletedItems.push({
|
|
|
|
path: BaseItem.systemPath(itemId),
|
|
|
|
isDeleted: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 16:11:44 +02:00
|
|
|
output = output.concat(deletedItems);
|
|
|
|
|
2017-07-19 00:14:20 +02:00
|
|
|
return {
|
|
|
|
hasMore: false,
|
|
|
|
context: null,
|
|
|
|
items: output,
|
|
|
|
};
|
|
|
|
} catch(error) {
|
|
|
|
throw this.fsErrorToJsError_(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 12:34:07 +02:00
|
|
|
async list(path, options) {
|
2017-07-03 20:58:01 +02:00
|
|
|
try {
|
|
|
|
let items = await fs.readdir(path);
|
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
let stat = await this.stat(path + '/' + items[i]);
|
|
|
|
if (!stat) continue; // Has been deleted between the readdir() call and now
|
|
|
|
stat.path = items[i];
|
|
|
|
output.push(stat);
|
|
|
|
}
|
2017-06-11 23:11:14 +02:00
|
|
|
|
2017-07-03 20:58:01 +02:00
|
|
|
return {
|
|
|
|
items: output,
|
|
|
|
hasMore: false,
|
|
|
|
context: null,
|
|
|
|
};
|
|
|
|
} catch(error) {
|
|
|
|
throw this.fsErrorToJsError_(error);
|
|
|
|
}
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 14:02:07 +02:00
|
|
|
async get(path, options) {
|
|
|
|
let output = null;
|
|
|
|
|
|
|
|
try {
|
2017-12-14 23:12:02 +02:00
|
|
|
if (options.target === 'file') {
|
|
|
|
output = await fs.copy(path, options.path, { overwrite: true });
|
2017-07-02 14:02:07 +02:00
|
|
|
} else {
|
2017-12-14 23:12:02 +02:00
|
|
|
output = await fs.readFile(path, options.encoding);
|
2017-07-02 14:02:07 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code == 'ENOENT') return null;
|
2017-07-03 20:58:01 +02:00
|
|
|
throw this.fsErrorToJsError_(error);
|
2017-07-02 14:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.exists(path, (exists) => {
|
|
|
|
if (exists) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:18:19 +02:00
|
|
|
fs.mkdirp(path, (error) => {
|
2017-06-11 23:11:14 +02:00
|
|
|
if (error) {
|
2017-07-03 20:58:01 +02:00
|
|
|
reject(this.fsErrorToJsError_(error));
|
2017-06-11 23:11:14 +02:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:47:25 +02:00
|
|
|
async put(path, content, options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
|
|
|
|
if (options.source === 'file') content = await fs.readFile(options.path);
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.writeFile(path, content, function(error) {
|
|
|
|
if (error) {
|
2017-07-03 20:58:01 +02:00
|
|
|
reject(this.fsErrorToJsError_(error));
|
2017-06-11 23:11:14 +02:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.unlink(path, function(error) {
|
|
|
|
if (error) {
|
|
|
|
if (error && error.code == 'ENOENT') {
|
|
|
|
// File doesn't exist - it's fine
|
|
|
|
resolve();
|
|
|
|
} else {
|
2017-07-03 20:58:01 +02:00
|
|
|
reject(this.fsErrorToJsError_(error));
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-02 12:34:07 +02:00
|
|
|
async move(oldPath, newPath) {
|
|
|
|
let lastError = null;
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
try {
|
|
|
|
let output = await fs.move(oldPath, newPath, { overwrite: true });
|
|
|
|
return output;
|
|
|
|
} catch (error) {
|
|
|
|
lastError = error;
|
|
|
|
// Normally cannot happen with the `overwrite` flag but sometime it still does.
|
|
|
|
// In this case, retry.
|
|
|
|
if (error.code == 'EEXIST') {
|
|
|
|
await time.sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-03 20:58:01 +02:00
|
|
|
throw this.fsErrorToJsError_(error);
|
2017-07-02 12:34:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw lastError;
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 22:58:17 +02:00
|
|
|
format() {
|
|
|
|
throw new Error('Not supported');
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:13:17 +02:00
|
|
|
module.exports = { FileApiDriverLocal };
|