File size: 6,893 Bytes
00df61d | 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 | /**
* @license
* Copyright 2016 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
addToLibrary({
$PROXYFS__deps: ['$FS', '$PATH', '$ERRNO_CODES'],
$PROXYFS: {
mount(mount) {
return PROXYFS.createNode(null, '/', mount.opts.fs.lstat(mount.opts.root).mode, 0);
},
createNode(parent, name, mode, dev) {
if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
}
var node = FS.createNode(parent, name, mode);
node.node_ops = PROXYFS.node_ops;
node.stream_ops = PROXYFS.stream_ops;
return node;
},
realPath(node) {
var parts = [];
while (node.parent !== node) {
parts.push(node.name);
node = node.parent;
}
parts.push(node.mount.opts.root);
parts.reverse();
return PATH.join(...parts);
},
node_ops: {
getattr(node) {
var path = PROXYFS.realPath(node);
var stat;
try {
stat = node.mount.opts.fs.lstat(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
return {
dev: stat.dev,
ino: stat.ino,
mode: stat.mode,
nlink: stat.nlink,
uid: stat.uid,
gid: stat.gid,
rdev: stat.rdev,
size: stat.size,
atime: stat.atime,
mtime: stat.mtime,
ctime: stat.ctime,
blksize: stat.blksize,
blocks: stat.blocks
};
},
setattr(node, attr) {
var path = PROXYFS.realPath(node);
try {
if (attr.mode !== undefined) {
node.mount.opts.fs.chmod(path, attr.mode);
// update the common node structure mode as well
node.mode = attr.mode;
}
if (attr.atime || attr.mtime) {
var atime = new Date(attr.atime || attr.mtime);
var mtime = new Date(attr.mtime || attr.atime);
node.mount.opts.fs.utime(path, atime, mtime);
}
if (attr.size !== undefined) {
node.mount.opts.fs.truncate(path, attr.size);
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
lookup(parent, name) {
try {
var path = PATH.join2(PROXYFS.realPath(parent), name);
var mode = parent.mount.opts.fs.lstat(path).mode;
var node = PROXYFS.createNode(parent, name, mode);
return node;
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
mknod(parent, name, mode, dev) {
var node = PROXYFS.createNode(parent, name, mode, dev);
// create the backing node for this in the fs root as well
var path = PROXYFS.realPath(node);
try {
if (FS.isDir(node.mode)) {
node.mount.opts.fs.mkdir(path, node.mode);
} else {
node.mount.opts.fs.writeFile(path, '', { mode: node.mode });
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
return node;
},
rename(oldNode, newDir, newName) {
var oldPath = PROXYFS.realPath(oldNode);
var newPath = PATH.join2(PROXYFS.realPath(newDir), newName);
try {
oldNode.mount.opts.fs.rename(oldPath, newPath);
oldNode.name = newName;
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
unlink(parent, name) {
var path = PATH.join2(PROXYFS.realPath(parent), name);
try {
parent.mount.opts.fs.unlink(path);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
rmdir(parent, name) {
var path = PATH.join2(PROXYFS.realPath(parent), name);
try {
parent.mount.opts.fs.rmdir(path);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
readdir(node) {
var path = PROXYFS.realPath(node);
try {
return node.mount.opts.fs.readdir(path);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
symlink(parent, newName, oldPath) {
var newPath = PATH.join2(PROXYFS.realPath(parent), newName);
try {
parent.mount.opts.fs.symlink(oldPath, newPath);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
readlink(node) {
var path = PROXYFS.realPath(node);
try {
return node.mount.opts.fs.readlink(path);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
},
stream_ops: {
open(stream) {
var path = PROXYFS.realPath(stream.node);
try {
stream.nfd = stream.node.mount.opts.fs.open(path,stream.flags);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
close(stream) {
try {
stream.node.mount.opts.fs.close(stream.nfd);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
read(stream, buffer, offset, length, position) {
try {
return stream.node.mount.opts.fs.read(stream.nfd, buffer, offset, length, position);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
write(stream, buffer, offset, length, position) {
try {
return stream.node.mount.opts.fs.write(stream.nfd, buffer, offset, length, position);
} catch(e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
},
llseek(stream, offset, whence) {
var position = offset;
if (whence === {{{ cDefs.SEEK_CUR }}}) {
position += stream.position;
} else if (whence === {{{ cDefs.SEEK_END }}}) {
if (FS.isFile(stream.node.mode)) {
try {
var stat = stream.node.node_ops.getattr(stream.node);
position += stat.size;
} catch (e) {
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
}
}
}
if (position < 0) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
}
return position;
}
}
}
});
if (WASMFS) {
error("using -lproxyfs is not currently supported in WasmFS.");
}
|