diff options
author | juj <jujjyl@gmail.com> | 2013-10-22 16:06:55 -0700 |
---|---|---|
committer | juj <jujjyl@gmail.com> | 2013-10-22 16:06:55 -0700 |
commit | 98a12c04b0e8fe43303c75d3b96b6ad8a0bd9fa9 (patch) | |
tree | 4c48819be2184c8c77c167f5f39578edecc96e11 /src | |
parent | e5b15abb510e020ba2a76436d61428cb1b664471 (diff) | |
parent | 3c0549acd9d7b900dda86b2827a485d889bf4cdd (diff) |
Merge pull request #1722 from juj/fix_nodefs_windows
Fix nodefs windows
Diffstat (limited to 'src')
-rw-r--r-- | src/library_nodefs.js | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/src/library_nodefs.js b/src/library_nodefs.js index d8df1689..2be54076 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -1,7 +1,11 @@ mergeInto(LibraryManager.library, { $NODEFS__deps: ['$FS', '$PATH'], - $NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { var fs = require("fs"); }', + $NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { var fs = require("fs"); NODEFS.staticInit(); }', $NODEFS: { + isWindows: false, + staticInit: function() { + NODEFS.isWindows = !!process.platform.match(/^win/); + }, mount: function (mount) { assert(ENVIRONMENT_IS_NODE); return NODEFS.createNode(null, '/', NODEFS.getMode(mount.opts.root), 0); @@ -19,6 +23,11 @@ mergeInto(LibraryManager.library, { var stat; try { stat = fs.lstatSync(path); + if (NODEFS.isWindows) { + // On Windows, directories return permission bits 'rw-rw-rw-', even though they have 'rwxrwxrwx', so + // propagate write bits to execute bits. + stat.mode = stat.mode | ((stat.mode & 146) >> 1); + } } catch (e) { if (!e.code) throw e; throw new FS.ErrnoError(ERRNO_CODES[e.code]); @@ -35,6 +44,41 @@ mergeInto(LibraryManager.library, { parts.reverse(); return PATH.join.apply(null, parts); }, + // This maps the integer permission modes from http://linux.die.net/man/3/open + // to node.js-specific file open permission strings at http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback + flagsToPermissionStringMap: { + 0/*O_RDONLY*/: 'r', + 1/*O_WRONLY*/: 'r+', + 2/*O_RDWR*/: 'r+', + 64/*O_CREAT*/: 'r', + 65/*O_WRONLY|O_CREAT*/: 'r+', + 66/*O_RDWR|O_CREAT*/: 'r+', + 129/*O_WRONLY|O_EXCL*/: 'rx+', + 193/*O_WRONLY|O_CREAT|O_EXCL*/: 'rx+', + 514/*O_RDWR|O_TRUNC*/: 'w+', + 577/*O_WRONLY|O_CREAT|O_TRUNC*/: 'w', + 578/*O_CREAT|O_RDWR|O_TRUNC*/: 'w+', + 705/*O_WRONLY|O_CREAT|O_EXCL|O_TRUNC*/: 'wx', + 706/*O_RDWR|O_CREAT|O_EXCL|O_TRUNC*/: 'wx+', + 1024/*O_APPEND*/: 'a', + 1025/*O_WRONLY|O_APPEND*/: 'a', + 1026/*O_RDWR|O_APPEND*/: 'a+', + 1089/*O_WRONLY|O_CREAT|O_APPEND*/: 'a', + 1090/*O_RDWR|O_CREAT|O_APPEND*/: 'a+', + 1153/*O_WRONLY|O_EXCL|O_APPEND*/: 'ax', + 1154/*O_RDWR|O_EXCL|O_APPEND*/: 'ax+', + 1217/*O_WRONLY|O_CREAT|O_EXCL|O_APPEND*/: 'ax', + 1218/*O_RDWR|O_CREAT|O_EXCL|O_APPEND*/: 'ax+', + 4096/*O_RDONLY|O_DSYNC*/: 'rs', + 4098/*O_RDWR|O_DSYNC*/: 'rs+' + }, + flagsToPermissionString: function(flags) { + if (flags in NODEFS.flagsToPermissionStringMap) { + return NODEFS.flagsToPermissionStringMap[flags]; + } else { + return flags; + } + }, node_ops: { getattr: function(node) { var path = NODEFS.realPath(node); @@ -45,6 +89,14 @@ mergeInto(LibraryManager.library, { if (!e.code) throw e; throw new FS.ErrnoError(ERRNO_CODES[e.code]); } + // node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake them with default blksize of 4096. + // See http://support.microsoft.com/kb/140365 + if (NODEFS.isWindows && !stat.blksize) { + stat.blksize = 4096; + } + if (NODEFS.isWindows && !stat.blocks) { + stat.blocks = (stat.size+stat.blksize-1)/stat.blksize|0; + } return { dev: stat.dev, ino: stat.ino, @@ -163,7 +215,7 @@ mergeInto(LibraryManager.library, { var path = NODEFS.realPath(stream.node); try { if (FS.isFile(stream.node.mode)) { - stream.nfd = fs.openSync(path, stream.flags); + stream.nfd = fs.openSync(path, NODEFS.flagsToPermissionString(stream.flags)); } } catch (e) { if (!e.code) throw e; @@ -172,7 +224,7 @@ mergeInto(LibraryManager.library, { }, close: function (stream) { try { - if (FS.isFile(stream.node.mode)) { + if (FS.isFile(stream.node.mode) && stream.nfd) { fs.closeSync(stream.nfd); } } catch (e) { |