-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
25 lines (21 loc) · 787 Bytes
/
index.js
File metadata and controls
25 lines (21 loc) · 787 Bytes
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
fs = require("fs"),
mmap = require("./build/Release/mmap"), real_map = mmap.map;
function mmap_wrapper(size, protection, flags, fd, offset) {
if(typeof fd === "number" || fd instanceof Number)
return real_map(size,protection,flags,fd,offset);
var writep = (protection & mmap.PROT_WRITE);
if(writep) fs.closeSync(fs.openSync(fd, "a"));
fd = fs.openSync(fd, writep? "r+" : "r");
var stat = fs.fstatSync(fd);
if(!size) {
size = stat.size;
} else if (writep && size > stat.size) {
fs.ftruncateSync(fd, size); // extend if needed
}
var buffer = real_map(size, protection, flags, fd, offset);
fs.closeSync(fd); // close-behind
return buffer;
};
for(var k in mmap) mmap_wrapper[k] = mmap[k];
mmap_wrapper.map = mmap_wrapper;
module.exports = mmap_wrapper;