Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/libproxyfs.js
7085 views
1
/**
2
* @license
3
* Copyright 2016 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
addToLibrary({
8
$PROXYFS__deps: ['$FS', '$PATH', '$ERRNO_CODES'],
9
$PROXYFS: {
10
mount(mount) {
11
return PROXYFS.createNode(null, '/', mount.opts.fs.lstat(mount.opts.root).mode, 0);
12
},
13
createNode(parent, name, mode, dev) {
14
if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) {
15
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
16
}
17
var node = FS.createNode(parent, name, mode);
18
node.node_ops = PROXYFS.node_ops;
19
node.stream_ops = PROXYFS.stream_ops;
20
return node;
21
},
22
realPath(node) {
23
var parts = [];
24
while (node.parent !== node) {
25
parts.push(node.name);
26
node = node.parent;
27
}
28
parts.push(node.mount.opts.root);
29
parts.reverse();
30
return PATH.join(...parts);
31
},
32
node_ops: {
33
getattr(node) {
34
var path = PROXYFS.realPath(node);
35
var stat;
36
try {
37
stat = node.mount.opts.fs.lstat(path);
38
} catch (e) {
39
if (!e.code) throw e;
40
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
41
}
42
return {
43
dev: stat.dev,
44
ino: stat.ino,
45
mode: stat.mode,
46
nlink: stat.nlink,
47
uid: stat.uid,
48
gid: stat.gid,
49
rdev: stat.rdev,
50
size: stat.size,
51
atime: stat.atime,
52
mtime: stat.mtime,
53
ctime: stat.ctime,
54
blksize: stat.blksize,
55
blocks: stat.blocks
56
};
57
},
58
setattr(node, attr) {
59
var path = PROXYFS.realPath(node);
60
try {
61
if (attr.mode !== undefined) {
62
node.mount.opts.fs.chmod(path, attr.mode);
63
// update the common node structure mode as well
64
node.mode = attr.mode;
65
}
66
if (attr.atime || attr.mtime) {
67
var atime = new Date(attr.atime || attr.mtime);
68
var mtime = new Date(attr.mtime || attr.atime);
69
node.mount.opts.fs.utime(path, atime, mtime);
70
}
71
if (attr.size !== undefined) {
72
node.mount.opts.fs.truncate(path, attr.size);
73
}
74
} catch (e) {
75
if (!e.code) throw e;
76
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
77
}
78
},
79
lookup(parent, name) {
80
try {
81
var path = PATH.join2(PROXYFS.realPath(parent), name);
82
var mode = parent.mount.opts.fs.lstat(path).mode;
83
var node = PROXYFS.createNode(parent, name, mode);
84
return node;
85
} catch(e) {
86
if (!e.code) throw e;
87
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
88
}
89
},
90
mknod(parent, name, mode, dev) {
91
var node = PROXYFS.createNode(parent, name, mode, dev);
92
// create the backing node for this in the fs root as well
93
var path = PROXYFS.realPath(node);
94
try {
95
if (FS.isDir(node.mode)) {
96
node.mount.opts.fs.mkdir(path, node.mode);
97
} else {
98
node.mount.opts.fs.writeFile(path, '', { mode: node.mode });
99
}
100
} catch (e) {
101
if (!e.code) throw e;
102
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
103
}
104
return node;
105
},
106
rename(oldNode, newDir, newName) {
107
var oldPath = PROXYFS.realPath(oldNode);
108
var newPath = PATH.join2(PROXYFS.realPath(newDir), newName);
109
try {
110
oldNode.mount.opts.fs.rename(oldPath, newPath);
111
oldNode.name = newName;
112
} catch(e) {
113
if (!e.code) throw e;
114
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
115
}
116
},
117
unlink(parent, name) {
118
var path = PATH.join2(PROXYFS.realPath(parent), name);
119
try {
120
parent.mount.opts.fs.unlink(path);
121
} catch(e) {
122
if (!e.code) throw e;
123
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
124
}
125
},
126
rmdir(parent, name) {
127
var path = PATH.join2(PROXYFS.realPath(parent), name);
128
try {
129
parent.mount.opts.fs.rmdir(path);
130
} catch(e) {
131
if (!e.code) throw e;
132
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
133
}
134
},
135
readdir(node) {
136
var path = PROXYFS.realPath(node);
137
try {
138
return node.mount.opts.fs.readdir(path);
139
} catch(e) {
140
if (!e.code) throw e;
141
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
142
}
143
},
144
symlink(parent, newName, oldPath) {
145
var newPath = PATH.join2(PROXYFS.realPath(parent), newName);
146
try {
147
parent.mount.opts.fs.symlink(oldPath, newPath);
148
} catch(e) {
149
if (!e.code) throw e;
150
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
151
}
152
},
153
readlink(node) {
154
var path = PROXYFS.realPath(node);
155
try {
156
return node.mount.opts.fs.readlink(path);
157
} catch(e) {
158
if (!e.code) throw e;
159
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
160
}
161
},
162
},
163
stream_ops: {
164
open(stream) {
165
var path = PROXYFS.realPath(stream.node);
166
try {
167
stream.nfd = stream.node.mount.opts.fs.open(path,stream.flags);
168
} catch(e) {
169
if (!e.code) throw e;
170
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
171
}
172
},
173
close(stream) {
174
try {
175
stream.node.mount.opts.fs.close(stream.nfd);
176
} catch(e) {
177
if (!e.code) throw e;
178
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
179
}
180
},
181
read(stream, buffer, offset, length, position) {
182
try {
183
return stream.node.mount.opts.fs.read(stream.nfd, buffer, offset, length, position);
184
} catch(e) {
185
if (!e.code) throw e;
186
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
187
}
188
},
189
write(stream, buffer, offset, length, position) {
190
try {
191
return stream.node.mount.opts.fs.write(stream.nfd, buffer, offset, length, position);
192
} catch(e) {
193
if (!e.code) throw e;
194
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
195
}
196
},
197
llseek(stream, offset, whence) {
198
var position = offset;
199
if (whence === {{{ cDefs.SEEK_CUR }}}) {
200
position += stream.position;
201
} else if (whence === {{{ cDefs.SEEK_END }}}) {
202
if (FS.isFile(stream.node.mode)) {
203
try {
204
var stat = stream.node.node_ops.getattr(stream.node);
205
position += stat.size;
206
} catch (e) {
207
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
208
}
209
}
210
}
211
212
if (position < 0) {
213
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
214
}
215
216
return position;
217
}
218
}
219
}
220
});
221
222
if (WASMFS) {
223
error("using -lproxyfs is not currently supported in WasmFS.");
224
}
225
226