-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
fs-driver-node.js
193 lines (169 loc) · 4.72 KB
/
fs-driver-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const fs = require('fs-extra');
const { time } = require('lib/time-utils.js');
const FsDriverBase = require('lib/fs-driver-base');
class FsDriverNode extends FsDriverBase {
fsErrorToJsError_(error, path = null) {
let msg = error.toString();
if (path !== null) msg += `. Path: ${path}`;
let output = new Error(msg);
if (error.code) output.code = error.code;
return output;
}
appendFileSync(path, string) {
return fs.appendFileSync(path, string);
}
async appendFile(path, string, encoding = 'base64') {
try {
return await fs.appendFile(path, string, { encoding: encoding });
} catch (error) {
throw this.fsErrorToJsError_(error, path);
}
}
async writeBinaryFile(path, content) {
try {
// let buffer = new Buffer(content);
let buffer = Buffer.from(content);
return await fs.writeFile(path, buffer);
} catch (error) {
throw this.fsErrorToJsError_(error, path);
}
}
async writeFile(path, string, encoding = 'base64') {
try {
if (encoding === 'buffer') {
return await fs.writeFile(path, string);
} else {
return await fs.writeFile(path, string, { encoding: encoding });
}
} catch (error) {
throw this.fsErrorToJsError_(error, path);
}
}
// same as rm -rf
async remove(path) {
try {
const r = await fs.remove(path);
return r;
} catch (error) {
throw this.fsErrorToJsError_(error, path);
}
}
async move(source, dest) {
let lastError = null;
for (let i = 0; i < 5; i++) {
try {
const output = await fs.move(source, dest, { 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;
}
throw this.fsErrorToJsError_(error);
}
}
throw lastError;
}
exists(path) {
return fs.pathExists(path);
}
async mkdir(path) {
// Note that mkdirp() does not throw an error if the directory
// could not be created. This would make the synchroniser to
// incorrectly try to sync with a non-existing dir:
// https://github.com/laurent22/joplin/issues/2117
const r = await fs.mkdirp(path);
if (!(await this.exists(path))) throw new Error(`Could not create directory: ${path}`);
return r;
}
async stat(path) {
try {
const stat = await fs.stat(path);
return {
birthtime: stat.birthtime,
mtime: stat.mtime,
isDirectory: () => stat.isDirectory(),
path: path,
size: stat.size,
};
} catch (error) {
if (error.code == 'ENOENT') return null;
throw error;
}
}
async setTimestamp(path, timestampDate) {
return fs.utimes(path, timestampDate, timestampDate);
}
async readDirStats(path, options = null) {
if (!options) options = {};
if (!('recursive' in options)) options.recursive = false;
let items = [];
try {
items = await fs.readdir(path);
} catch (error) {
throw this.fsErrorToJsError_(error);
}
let output = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
let stat = await this.stat(`${path}/${item}`);
if (!stat) continue; // Has been deleted between the readdir() call and now
stat.path = stat.path.substr(path.length + 1);
output.push(stat);
output = await this.readDirStatsHandleRecursion_(path, stat, output, options);
}
return output;
}
async open(path, mode) {
try {
return await fs.open(path, mode);
} catch (error) {
throw this.fsErrorToJsError_(error, path);
}
}
async close(handle) {
try {
return await fs.close(handle);
} catch (error) {
throw this.fsErrorToJsError_(error, '');
}
}
async readFile(path, encoding = 'utf8') {
try {
if (encoding === 'Buffer') return await fs.readFile(path); // Returns the raw buffer
return await fs.readFile(path, encoding);
} catch (error) {
throw this.fsErrorToJsError_(error, path);
}
}
// Always overwrite destination
async copy(source, dest) {
try {
return await fs.copy(source, dest, { overwrite: true });
} catch (error) {
throw this.fsErrorToJsError_(error, source);
}
}
async unlink(path) {
try {
await fs.unlink(path);
} catch (error) {
if (error.code === 'ENOENT') return; // Don't throw if the file does not exist
throw error;
}
}
async readFileChunk(handle, length, encoding = 'base64') {
// let buffer = new Buffer(length);
let buffer = Buffer.alloc(length);
const result = await fs.read(handle, buffer, 0, length, null);
if (!result.bytesRead) return null;
buffer = buffer.slice(0, result.bytesRead);
if (encoding === 'base64') return buffer.toString('base64');
if (encoding === 'ascii') return buffer.toString('ascii');
throw new Error(`Unsupported encoding: ${encoding}`);
}
}
module.exports.FsDriverNode = FsDriverNode;