UNPKG

3.35 kBJavaScriptView Raw
1(function (require, exports) {
2
3var Q = require("q");
4var BOOT = require("fs-boot");
5var COMMON = require("./common");
6
7var Fs = exports.Fs = function (outer, root) {
8 var inner = Object.create(BOOT);
9
10 function attenuate(path) {
11
12 // the machinations of projecting a path inside a
13 // subroot
14 var actual;
15 // if it's absolute, we want the path relative to
16 // the root of the inner file system
17 if (outer.isAbsolute(path)) {
18 actual = outer.relativeFromDirectory(outer.ROOT, path);
19 } else {
20 actual = path;
21 }
22 // we join the path onto the root of the inner file
23 // system so that parent references from the root
24 // return to the root, emulating standard unix
25 // behavior
26 actual = outer.join(outer.ROOT, actual);
27 // then we reconstruct the path relative to the
28 // inner root
29 actual = outer.relativeFromDirectory(outer.ROOT, actual);
30 // and rejoin it on the outer root
31 actual = outer.join(root, actual);
32 // and find the corresponding real path
33 actual = outer.canonical(actual);
34 return Q.when(actual, function (actual) {
35 // and verify that the outer canonical path is
36 // actually inside the inner canonical path, to
37 // prevent break-outs
38 if (outer.contains(root, actual)) {
39 return {
40 "inner": outer.join(outer.ROOT, outer.relativeFromDirectory(root, actual)),
41 "outer": actual
42 };
43 } else {
44 return Q.reject("No such file: " + JSON.stringify(path));
45 }
46 });
47 }
48
49 function workingDirectory() {
50 return outer.ROOT;
51 }
52
53 COMMON.update(inner, workingDirectory);
54
55 inner.list = function (path) {
56 return Q.when(attenuate(path), function (path) {
57 return outer.list(path.outer);
58 }).then(null, function (reason) {
59 return Q.reject("Can't list " + JSON.stringify(path));
60 });
61 };
62
63 inner.open = function (path, flags, charset) {
64 return Q.when(attenuate(path), function (path) {
65 return outer.open(path.outer, flags, charset);
66 }).then(null, function (reason) {
67 return Q.reject("Can't open " + JSON.stringify(path));
68 });
69 };
70
71 inner.stat = function (path) {
72 return Q.when(attenuate(path), function (path) {
73 return outer.stat(path.outer);
74 }).then(null, function (reason) {
75 return Q.reject("Can't stat " + JSON.stringify(path));
76 });
77 };
78
79 inner.canonical = function (path) {
80 return Q.when(attenuate(path), function (path) {
81 return path.inner;
82 }).then(null, function (reason) {
83 return Q.reject("Can't find canonical of " + JSON.stringify(path));
84 });
85 };
86
87 return Q.when(outer.canonical(root), function (_root) {
88 root = _root;
89 return inner;
90 });
91};
92
93}).apply(null, typeof exports !== "undefined" ?
94 [require, exports] :
95 [
96 function (id) {
97 id = id.toUpperCase()
98 .replace(".", "Q_FS")
99 .replace("/", "$")
100 .replace("-", "_");
101 return window[id];
102 },
103 Q_FS$ROOT
104 ]
105)