forked from jvilk/BrowserFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.js
139 lines (128 loc) · 3.27 KB
/
replay.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
function ReplayStart() {
/**
* Replays a log file.
*/
var fname = 'log_0.txt',
descHash = {}, parsedData, i = 0, inNode = false,
fs = require('fs'), Buffer, getTime = (function() {
return typeof performance !== 'undefined' ? function() { return performance.now(); } : function() { return Date.now(); };
})(),
timeNow = getTime();
if (typeof BrowserFS === 'undefined') {
inNode = true;
fs.mkdirSync('tmp');
Buffer = require('buffer').Buffer;
} else {
var Buffer = BrowserFS.BFSRequire('buffer').Buffer;
}
function fixPath2(p) {
// Maps browser path to local path, if needed.
var parts = p.split('/');
// In the browser, fix some relative paths.
if (!inNode) {
switch(parts[1]) {
case 'tmp':
case 'sys':
// Path is fine.
return p;
case 'vendor':
case 'benchmark':
// Append /sys
return '/sys' + p;
default:
// Append /tmp
return '/tmp' + p;
}
}
// First part is empty (all paths begin w/ '/'), second part has dir.
switch(parts[1]) {
case 'tmp':
// Slice off root part.
return p.slice(1);
case 'sys':
// Slice off /sys/
return p.slice(5);
case 'vendor':
return p.slice(1);
case 'benchmark':
return p.slice(1);
default:
// From a FD (path local to FS backend); append /tmp mountpoint.
return 'tmp' + p;
}
}
// XXX: hackfix for /sys statting
function fixPath(p) {
p = fixPath2(p);
if (p === '') {
return '.';
}
return p;
}
function getFd(p) {
if (descHash.hasOwnProperty(p)) {
return descHash[p];
}
throw new Error("File " + p + " isn't open.");
}
function putFd(p, fd) {
if (descHash.hasOwnProperty(p)) {
throw new Error("File " + p + " is already open.");
}
descHash[p] = fd;
}
function delFd(p, fd) {
if (descHash.hasOwnProperty(p)) {
delete descHash[p];
} else {
throw new Error("File " + p + " is not open.");
}
}
var errors = [];
function next(e) {
if (e) errors.push(i-1);
if (i === parsedData.length) {
// done
console.log("Benchmark time: " + (getTime() - timeNow) + " ms");
console.log(JSON.stringify(errors));
return;
}
var cmd = parsedData[i++];
if (cmd.length > 1) {
var p = fixPath(cmd[1]);
}
switch(cmd[0]) {
case 'readFile':
case 'stat':
case 'readdir':
case 'mkdir':
return fs[cmd[0]](p, next);
case 'open':
return fs.open(p, cmd[2], function(e, fd) {
if (e) console.error(e);
else {
putFd(p, fd);
}
next();
});
case 'fstat':
return fs.fstat(getFd(p), next);
case 'read':
case 'write':
var data = new Buffer(cmd[2]);
return fs[cmd[0]](getFd(p), data, 0, cmd[2], null, next);
case 'close':
var fd = getFd(p);
delFd(p);
return fs.close(fd, next);
default:
throw new Error("Unrecognized command: " + cmd[0]);
}
}
fs.readFile(fname, {encoding: 'utf8'}, function(err, data) {
if (err) throw err;
parsedData = JSON.parse(data);
next();
});
}
ReplayStart();