-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfslib.js
More file actions
508 lines (484 loc) · 19.3 KB
/
Copy pathfslib.js
File metadata and controls
508 lines (484 loc) · 19.3 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// jshint ignore: start
/*global globalObject*/
/*eslint no-console: 0*/
/*eslint strict: ["error", "global"]*/
// Ensure globalObject is available in both browser and web worker contexts
// virtualfs.js sets this up, but we need a fallback for safety
if (typeof globalObject === 'undefined') {
if (typeof window !== 'undefined') {
window.globalObject = window;
} else if (typeof self !== 'undefined') {
self.globalObject = self;
}
}
const {ERR_CODES, Errors} = require('./errno');
const {NativeFS} = require('./fslib_native');
const {TauriFS} = require('./fslib_tauri');
const {ElectronFS} = require('./fslib_electron');
const {NodeTauriFS} = require('./fslib_node_ws');
const {FilerFSModified} = require('./fslib_filer');
const {Constants} = require('./constants');
const {Mounts} = require('./fslib_mounts');
const {FsWatch} = require('./fslib_watch');
const {globalCopy} = require('./filerlib_copy.js');
const picomatch = require('picomatch/posix');
const ignore = require('ignore');
import * as iconv from 'iconv-lite';
let filerLib = null;
let filerShell = null;
/**
* Offers functionality similar to mkdir -p
*
* Asynchronous operation. No arguments other than a possible exception
* are given to the completion callback.
*/
function _mkdir_p (fsLib, path, mode, callback, _position) {
const osSep = '/';
const parts = filerLib.path.normalize(path).split(osSep);
mode = mode || process.umask();
_position = _position || 0;
if (_position >= parts.length) {
return callback(null);
}
var directory = parts.slice(0, _position + 1).join(osSep) || osSep;
fsLib.stat(directory, function(err) {
if (err === null) {
_mkdir_p(fsLib, path, mode, callback, _position + 1);
} else {
fsLib.mkdir(directory, mode, function (error) {
if (error && error.code !== 'EEXIST') {
return callback(error);
} else {
_mkdir_p(fsLib, path, mode, callback, _position + 1);
}
});
}
});
}
function _ensure_mount_directory() {
fileSystemLib.mkdirs(Constants.MOUNT_POINT_ROOT);
fileSystemLib.mkdirs(Constants.TAURI_ROOT);
NativeFS.refreshMountPoints();
}
function _getFirstFunctionIndex(argsArray) {
for(let i=0; i<argsArray.length; i++){
if (typeof argsArray[i] === 'function') {
return i;
}
}
return -1;
}
function _isSubPathOf(dir, subDir) {
const relative = filerLib.path.relative(dir, subDir);
return relative && !relative.startsWith('..') && !filerLib.path.isAbsolute(relative);
}
const fileSystemLib = {
mountNativeFolder: async function (...args) {
// Opens a file picker(or use provided handle) to open the folder in the system with fs access api.
// to be used in browsers like chrome/edge that supports fs access apis
return NativeFS.mountNativeFolder(...args);
},
openTauriFilePickerAsync: function (options) {
if(globalObject.__ELECTRON__) {
return ElectronFS.openElectronFilePickerAsync(options);
}
return TauriFS.openTauriFilePickerAsync(options);
},
openTauriFileSaveDialogueAsync: function (options) {
if(globalObject.__ELECTRON__) {
return ElectronFS.openElectronFileSaveDialogueAsync(options);
}
return TauriFS.openTauriFileSaveDialogueAsync(options);
},
getTauriPlatformPath: function (virtualPath) {
if(TauriFS.isTauriPath(virtualPath) || TauriFS.isTauriSubPath(virtualPath)) {
return TauriFS.getTauriPlatformPath(virtualPath);
}
return null;
},
getTauriVirtualPath: function (platformPath) {
return TauriFS.getTauriVirtualPath(platformPath);
},
readdir: function (...args) { // (path, options, callback)
let path = args[0];
if(TauriFS.isTauriPath(path) || TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.readdir(...args);
}
return TauriFS.readdir(...args);
}
if(Mounts.isMountPath(path) || Mounts.isMountSubPath(path)) {
return NativeFS.readdir(...args);
}
return filerLib.fs.readdir(...args);
},
stat: function (...args) { // (path, callback)
let path = args[0];
if(typeof path !== 'string') {
let callback = args[_getFirstFunctionIndex(args)];
return callback(new Errors.EINVAL(`Error Invalid path for stat: ${path}`));
}
if(TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.stat(...args);
}
return TauriFS.stat(...args);
}
if(Mounts.isMountSubPath(path)) {
return NativeFS.stat(...args);
}
return filerLib.fs.stat(...args);
},
readFile: function (...args) { // (path, options, callback)
let path = args[0];
if(TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.readFile(...args);
}
return TauriFS.readFile(...args);
} else if(Mounts.isMountSubPath(path)) {
return NativeFS.readFile(...args);
}
return FilerFSModified.readFile(...args);
},
writeFile: function (...args) { // (path, data, options, callback)
let path = args[0];
let newFileCreated = false;
function callbackInterceptor(...interceptedArgs) {
let err = interceptedArgs.length >= 1 ? interceptedArgs[0] : null;
if(!err){
if(newFileCreated){
FsWatch.reportCreateEvent(path, false);
} else {
FsWatch.reportChangeEvent(path);
}
}
if(args.originalCallback){
args.originalCallback(...interceptedArgs);
}
}
let callbackIndex = _getFirstFunctionIndex(args);
if(callbackIndex !== -1) {
args.originalCallback = args[callbackIndex];
args[callbackIndex] = callbackInterceptor;
}
if(TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.writeFile(...args);
}
return TauriFS.writeFile(...args);
}
fileSystemLib.stat(path, (err)=>{
if(err && err.code === ERR_CODES.ERROR_CODES.ENOENT){
newFileCreated = true;
}
if(Mounts.isMountSubPath(path)) {
return NativeFS.writeFile(...args);
}
return FilerFSModified.writeFile(...args);
});
},
mkdir: function (...args) { // (path, mode, callback)
let path = args[0];
function callbackInterceptor(...interceptedArgs) {
let err = interceptedArgs.length >= 1 ? interceptedArgs[0] : null;
if(!err){
FsWatch.reportCreateEvent(path, true);
}
if(args.originalCallback){
args.originalCallback(...interceptedArgs);
}
}
let callbackIndex = _getFirstFunctionIndex(args);
if(callbackIndex !== -1) {
args.originalCallback = args[callbackIndex];
args[callbackIndex] = callbackInterceptor;
}
if(TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.mkdirs(...args);
}
return TauriFS.mkdirs(...args);
}
if(Mounts.isMountSubPath(path)) {
return NativeFS.mkdir(...args);
}
return filerLib.fs.mkdir(...args);
},
rename: function (oldPath, newPath, cb) {
function callbackInterceptor(...args) {
let err = args.length >= 1 ? args[0] : null;
if(!err){
if(!TauriFS.isTauriSubPath(oldPath) || !TauriFS.isTauriSubPath(newPath)) {
// we only need to manually handle watch events for non tauri paths as tauri watch is done at node
fileSystemLib.stat(newPath, (err, stat)=>{
const isDir = !err && stat.isDirectory();
if(!TauriFS.isTauriSubPath(oldPath)) {
FsWatch.reportUnlinkEvent(oldPath, isDir);
}
if(!TauriFS.isTauriSubPath(newPath)) {
FsWatch.reportCreateEvent(newPath, isDir);
}
});
}
}
if(cb){
cb(...args);
}
}
if (_isSubPathOf(oldPath, newPath) || _isSubPathOf(newPath, oldPath)){
callbackInterceptor(new Errors.EINVAL(`Error renaming as one is a sub-path of other: ${newPath}, ${oldPath}`));
return ;
}
if(Mounts.isMountPath(oldPath) || Mounts.isMountPath(newPath)) {
cb(new Errors.EPERM('Mount root directory cannot be renamed.'));
return;
} else if(TauriFS.isTauriPath(oldPath) || TauriFS.isTauriPath(newPath)) {
cb(new Errors.EPERM('Tauri root directory cannot be renamed.'));
return;
}
if(oldPath !== newPath && oldPath.toLowerCase() === newPath.toLowerCase()) {
// in windows, we should be able to rename "a.txt" to "A.txt". Since windows is case-insensitive,
// the below stat(A.txt) will return a stat for "a.txt" which is not what we want.
if(TauriFS.isTauriSubPath(oldPath) && TauriFS.isTauriSubPath(newPath)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.rename(oldPath, newPath, callbackInterceptor);
}
return TauriFS.rename(oldPath, newPath, callbackInterceptor);
} else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) {
return NativeFS.renameSameNameDiffCase(oldPath, newPath, callbackInterceptor);
}
}
fileSystemLib.stat(newPath, (err)=>{
if(!err){
// the destination folder/file exists and we should not rename
cb(new Errors.EEXIST('Cannot rename, The destination path exists: ' + newPath));
return ;
}
if(TauriFS.isTauriSubPath(oldPath) && TauriFS.isTauriSubPath(newPath)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.rename(oldPath, newPath, callbackInterceptor);
}
return TauriFS.rename(oldPath, newPath, callbackInterceptor);
} else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) {
return NativeFS.rename(oldPath, newPath, callbackInterceptor);
}
return filerLib.fs.rename(oldPath, newPath, callbackInterceptor);
});
},
unlink: function (path, cb) {
let isDir;
function callbackInterceptor(...args) {
let err = args.length >= 1 ? args[0] : null;
if(!err){
FsWatch.reportUnlinkEvent(path, isDir);
}
if(cb){
cb(...args);
}
}
if(Mounts.isMountPath(path) || TauriFS.isTauriPath(path)) {
callbackInterceptor(new Errors.EPERM('Mount root directory cannot be deleted.'));
return ;
} else if(TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.unlink(path, callbackInterceptor);
}
return TauriFS.unlink(path, callbackInterceptor);
}
fileSystemLib.stat(path, (err, stat)=>{
isDir = !err && stat.isDirectory();
// we do this to emit fs watch events for non-tauri path unlinks.
// We only need to manually handle watch events for non tauri paths as tauri watch is done at node
if(Mounts.isMountSubPath(path)) {
return NativeFS.unlink(path, callbackInterceptor);
}
if (typeof path !== 'string') {
callbackInterceptor(new Errors.EINVAL('Invalid arguments.'));
return;
}
return filerShell.rm(path, { recursive: true }, callbackInterceptor);
});
},
copy: function (src, dst, cb) {
function callbackInterceptor(...args) {
let err = args.length >= 1 ? args[0] : null;
if(!err){
fileSystemLib.stat(dst, (err, stat)=>{
const isDir = !err && stat.isDirectory();
FsWatch.reportCreateEvent(dst, isDir);
});
}
if(cb){
cb(...args);
}
}
if (_isSubPathOf(src, dst)){
callbackInterceptor(new Errors.EINVAL(`Error copying: ${dst} cannot be a subpath of ${src}`));
return ;
}
// we have two implementation here even though the globalCopy fn is capable of copying anywhere. Native has its
// own impl to prevent large number of file node io in fs access impl.
// Ideally we should have a tauri copy impl too, but to unblock main thread while copy, we need to
// spawn different threads in rust and write tauri handlers which is pretty complex atm. so instead we will
// fall back to global copy here and will use node Tauri web socket fs adapter as and when it becomes available.
if(Mounts.isMountSubPath(src) && Mounts.isMountSubPath(dst)) {
return NativeFS.copy(src, dst, callbackInterceptor);
} else if(TauriFS.isTauriSubPath(src) && TauriFS.isTauriSubPath(dst)) {
if(globalObject.__ELECTRON__ && ElectronFS.canCopy()) {
return ElectronFS.copy(src, dst, callbackInterceptor);
}
if(TauriFS.canCopy()) {
return TauriFS.copy(src, dst, callbackInterceptor);
}
return globalCopy(src, dst, callbackInterceptor);
} else {
return globalCopy(src, dst, callbackInterceptor);
}
},
showSaveDialog: function (options) {
if(globalObject.__ELECTRON__){
return fileSystemLib.openElectronFileSaveDialogueAsync(options);
}
// Default to Tauri for native environments (including workers via WebSocket)
return fileSystemLib.openTauriFileSaveDialogueAsync(options);
},
watchAsync: function (path, gitIgnorePaths="") {
if(TauriFS.isTauriPath(path)) {
throw new Errors.EPERM('Cannot watch root directory!', path);
} else if(TauriFS.isTauriSubPath(path)) {
return NodeTauriFS.watchAsync(path, gitIgnorePaths);
}
return FsWatch.watchAsync(path, gitIgnorePaths);
},
unwatchAsync: function (eventEmitter) {
if(eventEmitter.eventEmitterID) {
return NodeTauriFS.unwatchAsync(eventEmitter);
}
return FsWatch.unwatchAsync(eventEmitter);
},
moveToTrash: function () {
throw new Errors.ENOSYS('Phoenix fs moveToTrash function not yet supported.');
},
mkdirs: function (path, mode, recursive, callback) {
// Determine if 'mode' is provided
if (typeof mode !== 'number') {
callback = recursive;
recursive = mode;
mode = 0o777; // Default mode (or any other default you'd like to set)
}
// Determine if 'recursive' is provided
if (typeof recursive !== 'boolean') {
callback = recursive;
recursive = false;
}
// Determine if 'callback' is provided
if (typeof callback !== 'function') {
callback = function () {
// Do Nothing
};
}
if (!recursive) {
fileSystemLib.mkdir(path, mode, callback);
} else if(TauriFS.isTauriSubPath(path)) {
if(globalObject.__ELECTRON__) {
return ElectronFS.mkdirs(path, mode, true, callback);
}
return TauriFS.mkdirs(path, mode, true, callback);
} else {
_mkdir_p(fileSystemLib, path, mode, callback);
}
},
testNodeWsEndpoint: function (wsEndPoint, echoData, echoBuffer) {
return NodeTauriFS.testNodeWsEndpoint(wsEndPoint, echoData, echoBuffer);
},
setNodeWSEndpoint: function (wsEndPoint) {
return NodeTauriFS.setNodeWSEndpoint(wsEndPoint);
},
stopNodeWSEndpoint: function (wsEndPoint) {
return NodeTauriFS.stopNodeWSEndpoint(wsEndPoint);
},
getNodeWSEndpoint: function () {
return NodeTauriFS.getNodeWSEndpoint();
},
forceUseNodeWSEndpoint: function (use) {
if(globalObject.__ELECTRON__) {
return ElectronFS.forceUseNodeWSEndpoint(use);
}
return TauriFS.forceUseNodeWSEndpoint(use);
},
preferNodeWSEndpoint: function (use) {
if(globalObject.__ELECTRON__) {
return ElectronFS.preferNodeWSEndpoint(use);
}
return TauriFS.preferNodeWSEndpoint(use);
},
BYTE_ARRAY_ENCODING: Constants.BYTE_ARRAY_ENCODING,
MOUNT_POINT_ROOT: Constants.MOUNT_POINT_ROOT,
TAURI_ROOT: Constants.TAURI_ROOT,
ERR_CODES: {},
WATCH_EVENTS: Constants.WATCH_EVENTS,
isEncodingSupported: function (encoding) {
if(encoding.toLowerCase() === Constants.BYTE_ARRAY_ENCODING){
return true;
}
return iconv.encodingExists(encoding);
},
utils: {
iconv,
picomatch,
ignore
}
};
for(let errCode of Object.values(ERR_CODES.FS_ERROR_CODES)){
fileSystemLib.ERR_CODES[errCode] = errCode;
}
fileSystemLib.copyFile = fileSystemLib.copy;
fileSystemLib.name = 'phoenixFS';
function _populateSupportedEncodings() {
if(iconv.encodingExists('utf8')) {
// we do this as iconv bootstraps this
const SUPPORTED_ENCODINGS = [];
for(let encoding of Object.keys(iconv.encodings)) {
// the Object.keys list contains encoding functions and privates. so filter
if(iconv.encodingExists(encoding)) {
SUPPORTED_ENCODINGS.push(encoding);
}
}
SUPPORTED_ENCODINGS.push(fileSystemLib.BYTE_ARRAY_ENCODING);
fileSystemLib.SUPPORTED_ENCODINGS = SUPPORTED_ENCODINGS;
}
}
function initFsLib(FilerLib) {
filerLib = FilerLib;
FilerFSModified.initFilerLib(filerLib);
filerShell = new filerLib.fs.Shell();
globalObject.path = FilerLib.path;
globalObject.Buffer = FilerLib.Buffer;
globalObject.iconv = iconv;
globalObject.fs = fileSystemLib;
globalObject.fs.path = FilerLib.path;
globalObject.fs.Buffer = FilerLib.Buffer;
_populateSupportedEncodings();
_ensure_mount_directory();
}
module.exports ={
initFsLib
};