forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_workerfs.js
141 lines (141 loc) · 4.85 KB
/
library_workerfs.js
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
mergeInto(LibraryManager.library, {
$WORKERFS__deps: ['$FS'],
$WORKERFS: {
DIR_MODE: {{{ cDefine('S_IFDIR') }}} | 511 /* 0777 */,
FILE_MODE: {{{ cDefine('S_IFREG') }}} | 511 /* 0777 */,
reader: null,
mount: function (mount) {
assert(ENVIRONMENT_IS_WORKER);
if (!WORKERFS.reader) WORKERFS.reader = new FileReaderSync();
var root = WORKERFS.createNode(null, '/', WORKERFS.DIR_MODE, 0);
var createdParents = {};
function ensureParent(path) {
// return the parent node, creating subdirs as necessary
var parts = path.split('/');
var parent = root;
for (var i = 0; i < parts.length-1; i++) {
var curr = parts.slice(0, i+1).join('/');
if (!createdParents[curr]) {
createdParents[curr] = WORKERFS.createNode(parent, curr, WORKERFS.DIR_MODE, 0);
}
parent = createdParents[curr];
}
return parent;
}
function base(path) {
var parts = path.split('/');
return parts[parts.length-1];
}
// We also accept FileList here, by using Array.prototype
Array.prototype.forEach.call(mount.opts["files"] || [], function(file) {
WORKERFS.createNode(ensureParent(file.name), base(file.name), WORKERFS.FILE_MODE, 0, file, file.lastModifiedDate);
});
(mount.opts["blobs"] || []).forEach(function(obj) {
WORKERFS.createNode(ensureParent(obj["name"]), base(obj["name"]), WORKERFS.FILE_MODE, 0, obj["data"]);
});
(mount.opts["packages"] || []).forEach(function(pack) {
pack['metadata'].files.forEach(function(file) {
var name = file.filename.substr(1); // remove initial slash
WORKERFS.createNode(ensureParent(name), base(name), WORKERFS.FILE_MODE, 0, pack['blob'].slice(file.start, file.end));
});
});
return root;
},
createNode: function (parent, name, mode, dev, contents, mtime) {
var node = FS.createNode(parent, name, mode);
node.mode = mode;
node.node_ops = WORKERFS.node_ops;
node.stream_ops = WORKERFS.stream_ops;
node.timestamp = (mtime || new Date).getTime();
assert(WORKERFS.FILE_MODE !== WORKERFS.DIR_MODE);
if (mode === WORKERFS.FILE_MODE) {
node.size = contents.size;
node.contents = contents;
} else {
node.size = 4096;
node.contents = {};
}
if (parent) {
parent.contents[name] = node;
}
return node;
},
node_ops: {
getattr: function(node) {
return {
dev: 1,
ino: undefined,
mode: node.mode,
nlink: 1,
uid: 0,
gid: 0,
rdev: undefined,
size: node.size,
atime: new Date(node.timestamp),
mtime: new Date(node.timestamp),
ctime: new Date(node.timestamp),
blksize: 4096,
blocks: Math.ceil(node.size / 4096),
};
},
setattr: function(node, attr) {
if (attr.mode !== undefined) {
node.mode = attr.mode;
}
if (attr.timestamp !== undefined) {
node.timestamp = attr.timestamp;
}
},
lookup: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.ENOENT);
},
mknod: function (parent, name, mode, dev) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
rename: function (oldNode, newDir, newName) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
unlink: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
rmdir: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
readdir: function(node) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
symlink: function(parent, newName, oldPath) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
readlink: function(node) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
},
stream_ops: {
read: function (stream, buffer, offset, length, position) {
if (position >= stream.node.size) return 0;
var chunk = stream.node.contents.slice(position, position + length);
var ab = WORKERFS.reader.readAsArrayBuffer(chunk);
buffer.set(new Uint8Array(ab), offset);
return chunk.size;
},
write: function (stream, buffer, offset, length, position) {
throw new FS.ErrnoError(ERRNO_CODES.EIO);
},
llseek: function (stream, offset, whence) {
var position = offset;
if (whence === 1) { // SEEK_CUR.
position += stream.position;
} else if (whence === 2) { // SEEK_END.
if (FS.isFile(stream.node.mode)) {
position += stream.node.size;
}
}
if (position < 0) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
}
return position;
},
},
},
});