forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverlayFS.ts
More file actions
1044 lines (931 loc) · 30 KB
/
OverlayFS.ts
File metadata and controls
1044 lines (931 loc) · 30 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {FileSystem, BaseFileSystem, BFSOneArgCallback, BFSCallback, FileSystemOptions} from '../core/file_system';
import {ApiError, ErrorCode} from '../core/api_error';
import {FileFlag, ActionType} from '../core/file_flag';
import {File} from '../core/file';
import {default as Stats} from '../core/node_fs_stats';
import PreloadFile from '../generic/preload_file';
import LockedFS from '../generic/locked_fs';
import * as path from 'path';
/**
* @hidden
*/
const deletionLogPath = '/.deletedFiles.log';
/**
* Given a read-only mode, makes it writable.
* @hidden
*/
function makeModeWritable(mode: number): number {
return 0o222 | mode;
}
/**
* @hidden
*/
function getFlag(f: string): FileFlag {
return FileFlag.getFileFlag(f);
}
/**
* Overlays a RO file to make it writable.
*/
class OverlayFile extends PreloadFile<UnlockedOverlayFS> implements File {
constructor(fs: UnlockedOverlayFS, path: string, flag: FileFlag, stats: Stats, data: Buffer) {
super(fs, path, flag, stats, data);
}
public sync(cb: BFSOneArgCallback): void {
if (!this.isDirty()) {
cb(null);
return;
}
this._fs._syncAsync(this, (err: ApiError) => {
this.resetDirty();
cb(err);
});
}
public syncSync(): void {
if (this.isDirty()) {
this._fs._syncSync(this);
this.resetDirty();
}
}
public close(cb: BFSOneArgCallback): void {
this.sync(cb);
}
public closeSync(): void {
this.syncSync();
}
}
/**
* *INTERNAL, DO NOT USE DIRECTLY!*
*
* Core OverlayFS class that contains no locking whatsoever. We wrap these objects
* in a LockedFS to prevent races.
*/
export class UnlockedOverlayFS extends BaseFileSystem implements FileSystem {
public static isAvailable(): boolean {
return true;
}
private _writable: FileSystem;
private _readable: FileSystem;
private _isInitialized: boolean = false;
private _initializeCallbacks: (BFSOneArgCallback)[] = [];
private _deletedFiles: {[path: string]: boolean} = {};
private _deleteLog: string = '';
// If 'true', we have scheduled a delete log update.
private _deleteLogUpdatePending: boolean = false;
// If 'true', a delete log update is needed after the scheduled delete log
// update finishes.
private _deleteLogUpdateNeeded: boolean = false;
// If there was an error updating the delete log...
private _deleteLogError: ApiError | null = null;
constructor(writable: FileSystem, readable: FileSystem) {
super();
this._writable = writable;
this._readable = readable;
if (this._writable.isReadOnly()) {
throw new ApiError(ErrorCode.EINVAL, "Writable file system must be writable.");
}
}
public getOverlayedFileSystems(): { readable: FileSystem; writable: FileSystem; } {
return {
readable: this._readable,
writable: this._writable
};
}
public _syncAsync(file: PreloadFile<UnlockedOverlayFS>, cb: BFSOneArgCallback): void {
this.createParentDirectoriesAsync(file.getPath(), (err?: ApiError) => {
if (err) {
return cb(err);
}
this._writable.writeFile(file.getPath(), file.getBuffer(), null, getFlag('w'), file.getStats().mode, cb);
});
}
public _syncSync(file: PreloadFile<UnlockedOverlayFS>): void {
this.createParentDirectories(file.getPath());
this._writable.writeFileSync(file.getPath(), file.getBuffer(), null, getFlag('w'), file.getStats().mode);
}
public getName() {
return OverlayFS.Name;
}
/**
* **INTERNAL METHOD**
*
* Called once to load up metadata stored on the writable file system.
*/
public _initialize(cb: BFSOneArgCallback): void {
const callbackArray = this._initializeCallbacks;
const end = (e?: ApiError): void => {
this._isInitialized = !e;
this._initializeCallbacks = [];
callbackArray.forEach(((cb) => cb(e)));
};
// if we're already initialized, immediately invoke the callback
if (this._isInitialized) {
return cb();
}
callbackArray.push(cb);
// The first call to initialize initializes, the rest wait for it to complete.
if (callbackArray.length !== 1) {
return;
}
// Read deletion log, process into metadata.
this._writable.readFile(deletionLogPath, 'utf8', getFlag('r'), (err: ApiError, data?: string) => {
if (err) {
// ENOENT === Newly-instantiated file system, and thus empty log.
if (err.errno !== ErrorCode.ENOENT) {
return end(err);
}
} else {
this._deleteLog = data!;
}
this._reparseDeletionLog();
end();
});
}
public isReadOnly(): boolean { return false; }
public supportsSynch(): boolean { return this._readable.supportsSynch() && this._writable.supportsSynch(); }
public supportsLinks(): boolean { return false; }
public supportsProps(): boolean { return this._readable.supportsProps() && this._writable.supportsProps(); }
public getDeletionLog(): string {
return this._deleteLog;
}
public restoreDeletionLog(log: string): void {
this._deleteLog = log;
this._reparseDeletionLog();
this.updateLog('');
}
public rename(oldPath: string, newPath: string, cb: BFSOneArgCallback): void {
if (!this.checkInitAsync(cb) || this.checkPathAsync(oldPath, cb) || this.checkPathAsync(newPath, cb)) {
return;
}
if (oldPath === deletionLogPath || newPath === deletionLogPath) {
return cb(ApiError.EPERM('Cannot rename deletion log.'));
}
// nothing to do if paths match
if (oldPath === newPath) {
return cb();
}
this.stat(oldPath, false, (oldErr: ApiError, oldStats?: Stats) => {
if (oldErr) {
return cb(oldErr);
}
return this.stat(newPath, false, (newErr: ApiError, newStats?: Stats) => {
const self = this;
// precondition: both oldPath and newPath exist and are dirs.
// decreases: |files|
// Need to move *every file/folder* currently stored on
// readable to its new location on writable.
function copyDirContents(files: string[]): void {
const file = files.shift();
if (!file) {
return cb();
}
const oldFile = path.resolve(oldPath, file);
const newFile = path.resolve(newPath, file);
// Recursion! Should work for any nested files / folders.
self.rename(oldFile, newFile, (err?: ApiError) => {
if (err) {
return cb(err);
}
copyDirContents(files);
});
}
let mode = 0o777;
// from linux's rename(2) manpage: oldpath can specify a
// directory. In this case, newpath must either not exist, or
// it must specify an empty directory.
if (oldStats!.isDirectory()) {
if (newErr) {
if (newErr.errno !== ErrorCode.ENOENT) {
return cb(newErr);
}
return this._writable.exists(oldPath, (exists: boolean) => {
// simple case - both old and new are on the writable layer
if (exists) {
return this._writable.rename(oldPath, newPath, cb);
}
this._writable.mkdir(newPath, mode, (mkdirErr?: ApiError) => {
if (mkdirErr) {
return cb(mkdirErr);
}
this._readable.readdir(oldPath, (err: ApiError, files?: string[]) => {
if (err) {
return cb();
}
copyDirContents(files!);
});
});
});
}
mode = newStats!.mode;
if (!newStats!.isDirectory()) {
return cb(ApiError.ENOTDIR(newPath));
}
this.readdir(newPath, (readdirErr: ApiError, files?: string[]) => {
if (files && files.length) {
return cb(ApiError.ENOTEMPTY(newPath));
}
this._readable.readdir(oldPath, (err: ApiError, files?: string[]) => {
if (err) {
return cb();
}
copyDirContents(files!);
});
});
}
if (newStats && newStats.isDirectory()) {
return cb(ApiError.EISDIR(newPath));
}
this.readFile(oldPath, null, getFlag('r'), (err: ApiError, data?: any) => {
if (err) {
return cb(err);
}
return this.writeFile(newPath, data, null, getFlag('w'), oldStats!.mode, (err: ApiError) => {
if (err) {
return cb(err);
}
return this.unlink(oldPath, cb);
});
});
});
});
}
public renameSync(oldPath: string, newPath: string): void {
this.checkInitialized();
this.checkPath(oldPath);
this.checkPath(newPath);
if (oldPath === deletionLogPath || newPath === deletionLogPath) {
throw ApiError.EPERM('Cannot rename deletion log.');
}
// Write newPath using oldPath's contents, delete oldPath.
const oldStats = this.statSync(oldPath, false);
if (oldStats.isDirectory()) {
// Optimization: Don't bother moving if old === new.
if (oldPath === newPath) {
return;
}
let mode = 0o777;
if (this.existsSync(newPath)) {
const stats = this.statSync(newPath, false);
mode = stats.mode;
if (stats.isDirectory()) {
if (this.readdirSync(newPath).length > 0) {
throw ApiError.ENOTEMPTY(newPath);
}
} else {
throw ApiError.ENOTDIR(newPath);
}
}
// Take care of writable first. Move any files there, or create an empty directory
// if it doesn't exist.
if (this._writable.existsSync(oldPath)) {
this._writable.renameSync(oldPath, newPath);
} else if (!this._writable.existsSync(newPath)) {
this._writable.mkdirSync(newPath, mode);
}
// Need to move *every file/folder* currently stored on readable to its new location
// on writable.
if (this._readable.existsSync(oldPath)) {
this._readable.readdirSync(oldPath).forEach((name) => {
// Recursion! Should work for any nested files / folders.
this.renameSync(path.resolve(oldPath, name), path.resolve(newPath, name));
});
}
} else {
if (this.existsSync(newPath) && this.statSync(newPath, false).isDirectory()) {
throw ApiError.EISDIR(newPath);
}
this.writeFileSync(newPath,
this.readFileSync(oldPath, null, getFlag('r')), null, getFlag('w'), oldStats.mode);
}
if (oldPath !== newPath && this.existsSync(oldPath)) {
this.unlinkSync(oldPath);
}
}
public stat(p: string, isLstat: boolean, cb: BFSCallback<Stats>): void {
if (!this.checkInitAsync(cb)) {
return;
}
this._writable.stat(p, isLstat, (err: ApiError, stat?: Stats) => {
if (err && err.errno === ErrorCode.ENOENT) {
if (this._deletedFiles[p]) {
cb(ApiError.ENOENT(p));
}
this._readable.stat(p, isLstat, (err: ApiError, stat?: Stats) => {
if (stat) {
// Make the oldStat's mode writable. Preserve the topmost
// part of the mode, which specifies if it is a file or a
// directory.
stat = Stats.clone(stat);
stat.mode = makeModeWritable(stat.mode);
}
cb(err, stat);
});
} else {
cb(err, stat);
}
});
}
public statSync(p: string, isLstat: boolean): Stats {
this.checkInitialized();
try {
return this._writable.statSync(p, isLstat);
} catch (e) {
if (this._deletedFiles[p]) {
throw ApiError.ENOENT(p);
}
const oldStat = Stats.clone(this._readable.statSync(p, isLstat));
// Make the oldStat's mode writable. Preserve the topmost part of the
// mode, which specifies if it is a file or a directory.
oldStat.mode = makeModeWritable(oldStat.mode);
return oldStat;
}
}
public open(p: string, flag: FileFlag, mode: number, cb: BFSCallback<File>): void {
if (!this.checkInitAsync(cb) || this.checkPathAsync(p, cb)) {
return;
}
this.stat(p, false, (err: ApiError, stats?: Stats) => {
if (stats) {
switch (flag.pathExistsAction()) {
case ActionType.TRUNCATE_FILE:
return this.createParentDirectoriesAsync(p, (err?: ApiError) => {
if (err) {
return cb(err);
}
this._writable.open(p, flag, mode, cb);
});
case ActionType.NOP:
return this._writable.exists(p, (exists: boolean) => {
if (exists) {
this._writable.open(p, flag, mode, cb);
} else {
// at this point we know the stats object we got is from
// the readable FS.
stats = Stats.clone(stats!);
stats.mode = mode;
this._readable.readFile(p, null, getFlag('r'), (readFileErr: ApiError, data?: any) => {
if (readFileErr) {
return cb(readFileErr);
}
if (stats!.size === -1) {
stats!.size = data.length;
}
const f = new OverlayFile(this, p, flag, stats!, data);
cb(null, f);
});
}
});
default:
return cb(ApiError.EEXIST(p));
}
} else {
switch (flag.pathNotExistsAction()) {
case ActionType.CREATE_FILE:
return this.createParentDirectoriesAsync(p, (err?: ApiError) => {
if (err) {
return cb(err);
}
return this._writable.open(p, flag, mode, cb);
});
default:
return cb(ApiError.ENOENT(p));
}
}
});
}
public openSync(p: string, flag: FileFlag, mode: number): File {
this.checkInitialized();
this.checkPath(p);
if (p === deletionLogPath) {
throw ApiError.EPERM('Cannot open deletion log.');
}
if (this.existsSync(p)) {
switch (flag.pathExistsAction()) {
case ActionType.TRUNCATE_FILE:
this.createParentDirectories(p);
return this._writable.openSync(p, flag, mode);
case ActionType.NOP:
if (this._writable.existsSync(p)) {
return this._writable.openSync(p, flag, mode);
} else {
// Create an OverlayFile.
const buf = this._readable.readFileSync(p, null, getFlag('r'));
const stats = Stats.clone(this._readable.statSync(p, false));
stats.mode = mode;
return new OverlayFile(this, p, flag, stats, buf);
}
default:
throw ApiError.EEXIST(p);
}
} else {
switch (flag.pathNotExistsAction()) {
case ActionType.CREATE_FILE:
this.createParentDirectories(p);
return this._writable.openSync(p, flag, mode);
default:
throw ApiError.ENOENT(p);
}
}
}
public unlink(p: string, cb: BFSOneArgCallback): void {
if (!this.checkInitAsync(cb) || this.checkPathAsync(p, cb)) {
return;
}
this.exists(p, (exists: boolean) => {
if (!exists) {
return cb(ApiError.ENOENT(p));
}
this._writable.exists(p, (writableExists: boolean) => {
if (writableExists) {
return this._writable.unlink(p, (err: ApiError) => {
if (err) {
return cb(err);
}
this.exists(p, (readableExists: boolean) => {
if (readableExists) {
this.deletePath(p);
}
cb(null);
});
});
} else {
// if this only exists on the readable FS, add it to the
// delete map.
this.deletePath(p);
cb(null);
}
});
});
}
public unlinkSync(p: string): void {
this.checkInitialized();
this.checkPath(p);
if (this.existsSync(p)) {
if (this._writable.existsSync(p)) {
this._writable.unlinkSync(p);
}
// if it still exists add to the delete log
if (this.existsSync(p)) {
this.deletePath(p);
}
} else {
throw ApiError.ENOENT(p);
}
}
public rmdir(p: string, cb: BFSOneArgCallback): void {
if (!this.checkInitAsync(cb)) {
return;
}
const rmdirLower = (): void => {
this.readdir(p, (err: ApiError, files: string[]): void => {
if (err) {
return cb(err);
}
if (files.length) {
return cb(ApiError.ENOTEMPTY(p));
}
this.deletePath(p);
cb(null);
});
};
this.exists(p, (exists: boolean) => {
if (!exists) {
return cb(ApiError.ENOENT(p));
}
this._writable.exists(p, (writableExists: boolean) => {
if (writableExists) {
this._writable.rmdir(p, (err: ApiError) => {
if (err) {
return cb(err);
}
this._readable.exists(p, (readableExists: boolean) => {
if (readableExists) {
rmdirLower();
} else {
cb();
}
});
});
} else {
rmdirLower();
}
});
});
}
public rmdirSync(p: string): void {
this.checkInitialized();
if (this.existsSync(p)) {
if (this._writable.existsSync(p)) {
this._writable.rmdirSync(p);
}
if (this.existsSync(p)) {
// Check if directory is empty.
if (this.readdirSync(p).length > 0) {
throw ApiError.ENOTEMPTY(p);
} else {
this.deletePath(p);
}
}
} else {
throw ApiError.ENOENT(p);
}
}
public mkdir(p: string, mode: number, cb: BFSCallback<Stats>): void {
if (!this.checkInitAsync(cb)) {
return;
}
this.exists(p, (exists: boolean) => {
if (exists) {
return cb(ApiError.EEXIST(p));
}
// The below will throw should any of the parent directories
// fail to exist on _writable.
this.createParentDirectoriesAsync(p, (err: ApiError) => {
if (err) {
return cb(err);
}
this._writable.mkdir(p, mode, cb);
});
});
}
public mkdirSync(p: string, mode: number): void {
this.checkInitialized();
if (this.existsSync(p)) {
throw ApiError.EEXIST(p);
} else {
// The below will throw should any of the parent directories fail to exist
// on _writable.
this.createParentDirectories(p);
this._writable.mkdirSync(p, mode);
}
}
public readdir(p: string, cb: BFSCallback<string[]>): void {
if (!this.checkInitAsync(cb)) {
return;
}
this.stat(p, false, (err: ApiError, dirStats?: Stats) => {
if (err) {
return cb(err);
}
if (!dirStats!.isDirectory()) {
return cb(ApiError.ENOTDIR(p));
}
this._writable.readdir(p, (err: ApiError, wFiles: string[]) => {
if (err && err.code !== 'ENOENT') {
return cb(err);
} else if (err || !wFiles) {
wFiles = [];
}
this._readable.readdir(p, (err: ApiError, rFiles: string[]) => {
// if the directory doesn't exist on the lower FS set rFiles
// here to simplify the following code.
if (err || !rFiles) {
rFiles = [];
}
// Readdir in both, check delete log on read-only file system's files, merge, return.
const seenMap: {[name: string]: boolean} = {};
const filtered: string[] = wFiles.concat(rFiles.filter((fPath: string) =>
!this._deletedFiles[`${p}/${fPath}`]
)).filter((fPath: string) => {
// Remove duplicates.
const result = !seenMap[fPath];
seenMap[fPath] = true;
return result;
});
cb(null, filtered);
});
});
});
}
public readdirSync(p: string): string[] {
this.checkInitialized();
const dirStats = this.statSync(p, false);
if (!dirStats.isDirectory()) {
throw ApiError.ENOTDIR(p);
}
// Readdir in both, check delete log on RO file system's listing, merge, return.
let contents: string[] = [];
try {
contents = contents.concat(this._writable.readdirSync(p));
} catch (e) {
// NOP.
}
try {
contents = contents.concat(this._readable.readdirSync(p).filter((fPath: string) =>
!this._deletedFiles[`${p}/${fPath}`]
));
} catch (e) {
// NOP.
}
const seenMap: {[name: string]: boolean} = {};
return contents.filter((fileP: string) => {
const result = !seenMap[fileP];
seenMap[fileP] = true;
return result;
});
}
public exists(p: string, cb: (exists: boolean) => void): void {
// Cannot pass an error back to callback, so throw an exception instead
// if not initialized.
this.checkInitialized();
this._writable.exists(p, (existsWritable: boolean) => {
if (existsWritable) {
return cb(true);
}
this._readable.exists(p, (existsReadable: boolean) => {
cb(existsReadable && this._deletedFiles[p] !== true);
});
});
}
public existsSync(p: string): boolean {
this.checkInitialized();
return this._writable.existsSync(p) || (this._readable.existsSync(p) && this._deletedFiles[p] !== true);
}
public chmod(p: string, isLchmod: boolean, mode: number, cb: BFSOneArgCallback): void {
if (!this.checkInitAsync(cb)) {
return;
}
this.operateOnWritableAsync(p, (err?: ApiError) => {
if (err) {
return cb(err);
} else {
this._writable.chmod(p, isLchmod, mode, cb);
}
});
}
public chmodSync(p: string, isLchmod: boolean, mode: number): void {
this.checkInitialized();
this.operateOnWritable(p, () => {
this._writable.chmodSync(p, isLchmod, mode);
});
}
public chown(p: string, isLchmod: boolean, uid: number, gid: number, cb: BFSOneArgCallback): void {
if (!this.checkInitAsync(cb)) {
return;
}
this.operateOnWritableAsync(p, (err?: ApiError) => {
if (err) {
return cb(err);
} else {
this._writable.chown(p, isLchmod, uid, gid, cb);
}
});
}
public chownSync(p: string, isLchown: boolean, uid: number, gid: number): void {
this.checkInitialized();
this.operateOnWritable(p, () => {
this._writable.chownSync(p, isLchown, uid, gid);
});
}
public utimes(p: string, atime: Date, mtime: Date, cb: BFSOneArgCallback): void {
if (!this.checkInitAsync(cb)) {
return;
}
this.operateOnWritableAsync(p, (err?: ApiError) => {
if (err) {
return cb(err);
} else {
this._writable.utimes(p, atime, mtime, cb);
}
});
}
public utimesSync(p: string, atime: Date, mtime: Date): void {
this.checkInitialized();
this.operateOnWritable(p, () => {
this._writable.utimesSync(p, atime, mtime);
});
}
private deletePath(p: string): void {
this._deletedFiles[p] = true;
this.updateLog(`d${p}\n`);
}
private updateLog(addition: string) {
this._deleteLog += addition;
if (this._deleteLogUpdatePending) {
this._deleteLogUpdateNeeded = true;
} else {
this._deleteLogUpdatePending = true;
this._writable.writeFile(deletionLogPath, this._deleteLog, 'utf8', FileFlag.getFileFlag('w'), 0o644, (e) => {
this._deleteLogUpdatePending = false;
if (e) {
this._deleteLogError = e;
} else if (this._deleteLogUpdateNeeded) {
this._deleteLogUpdateNeeded = false;
this.updateLog('');
}
});
}
}
private _reparseDeletionLog(): void {
this._deletedFiles = {};
this._deleteLog.split('\n').forEach((path: string) => {
// If the log entry begins w/ 'd', it's a deletion.
this._deletedFiles[path.slice(1)] = path.slice(0, 1) === 'd';
});
}
private checkInitialized(): void {
if (!this._isInitialized) {
throw new ApiError(ErrorCode.EPERM, "OverlayFS is not initialized. Please initialize OverlayFS using its initialize() method before using it.");
} else if (this._deleteLogError !== null) {
const e = this._deleteLogError;
this._deleteLogError = null;
throw e;
}
}
private checkInitAsync(cb: BFSOneArgCallback): boolean {
if (!this._isInitialized) {
cb(new ApiError(ErrorCode.EPERM, "OverlayFS is not initialized. Please initialize OverlayFS using its initialize() method before using it."));
return false;
} else if (this._deleteLogError !== null) {
const e = this._deleteLogError;
this._deleteLogError = null;
cb(e);
return false;
}
return true;
}
private checkPath(p: string): void {
if (p === deletionLogPath) {
throw ApiError.EPERM(p);
}
}
private checkPathAsync(p: string, cb: BFSOneArgCallback): boolean {
if (p === deletionLogPath) {
cb(ApiError.EPERM(p));
return true;
}
return false;
}
private createParentDirectoriesAsync(p: string, cb: BFSOneArgCallback): void {
let parent = path.dirname(p);
const toCreate: string[] = [];
const self = this;
this._writable.stat(parent, false, statDone);
function statDone(err: ApiError, stat?: Stats): void {
if (err) {
if (parent === "/") {
cb(new ApiError(ErrorCode.EBUSY, "Invariant failed: root does not exist!"));
} else {
toCreate.push(parent);
parent = path.dirname(parent);
self._writable.stat(parent, false, statDone);
}
} else {
createParents();
}
}
function createParents(): void {
if (!toCreate.length) {
return cb();
}
const dir = toCreate.pop();
self._readable.stat(dir!, false, (err: ApiError, stats?: Stats) => {
// stop if we couldn't read the dir
if (!stats) {
return cb();
}
self._writable.mkdir(dir!, stats.mode, (err?: ApiError) => {
if (err) {
return cb(err);
}
createParents();
});
});
}
}
/**
* With the given path, create the needed parent directories on the writable storage
* should they not exist. Use modes from the read-only storage.
*/
private createParentDirectories(p: string): void {
let parent = path.dirname(p), toCreate: string[] = [];
while (!this._writable.existsSync(parent)) {
toCreate.push(parent);
parent = path.dirname(parent);
}
toCreate = toCreate.reverse();
toCreate.forEach((p: string) => {
this._writable.mkdirSync(p, this.statSync(p, false).mode);
});
}
/**
* Helper function:
* - Ensures p is on writable before proceeding. Throws an error if it doesn't exist.
* - Calls f to perform operation on writable.
*/
private operateOnWritable(p: string, f: () => void): void {
if (this.existsSync(p)) {
if (!this._writable.existsSync(p)) {
// File is on readable storage. Copy to writable storage before
// changing its mode.
this.copyToWritable(p);
}
f();
} else {
throw ApiError.ENOENT(p);
}
}
private operateOnWritableAsync(p: string, cb: BFSOneArgCallback): void {
this.exists(p, (exists: boolean) => {
if (!exists) {
return cb(ApiError.ENOENT(p));
}
this._writable.exists(p, (existsWritable: boolean) => {
if (existsWritable) {
cb();
} else {
return this.copyToWritableAsync(p, cb);
}
});
});
}
/**
* Copy from readable to writable storage.
* PRECONDITION: File does not exist on writable storage.
*/
private copyToWritable(p: string): void {
const pStats = this.statSync(p, false);
if (pStats.isDirectory()) {
this._writable.mkdirSync(p, pStats.mode);
} else {
this.writeFileSync(p,
this._readable.readFileSync(p, null, getFlag('r')), null,
getFlag('w'), this.statSync(p, false).mode);
}
}
private copyToWritableAsync(p: string, cb: BFSOneArgCallback): void {
this.stat(p, false, (err: ApiError, pStats?: Stats) => {
if (err) {
return cb(err);
}
if (pStats!.isDirectory()) {
return this._writable.mkdir(p, pStats!.mode, cb);
}
// need to copy file.
this._readable.readFile(p, null, getFlag('r'), (err: ApiError, data?: Buffer) => {
if (err) {
return cb(err);
}
this.writeFile(p, data, null, getFlag('w'), pStats!.mode, cb);
});
});
}
}
/**
* Configuration options for OverlayFS instances.
*/
export interface OverlayFSOptions {
// The file system to write modified files to.
writable: FileSystem;
// The file system that initially populates this file system.
readable: FileSystem;
}
/**
* OverlayFS makes a read-only filesystem writable by storing writes on a second,
* writable file system. Deletes are persisted via metadata stored on the writable
* file system.
*/
export default class OverlayFS extends LockedFS<UnlockedOverlayFS> {
public static readonly Name = "OverlayFS";
public static readonly Options: FileSystemOptions = {
writable: {
type: "object",
description: "The file system to write modified files to."