Skip to content

Commit 7224d52

Browse files
authored
[nodefs] Fix execute permission bit on Windows when retrieving mode attribute (#21902)
1 parent 799a1cb commit 7224d52

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

src/library_nodefs.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,19 @@ addToLibrary({
123123
var path = NODEFS.realPath(node);
124124
var stat;
125125
NODEFS.tryFSOperation(() => stat = fs.lstatSync(path));
126-
// node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake
127-
// them with default blksize of 4096.
128-
// See http://support.microsoft.com/kb/140365
129-
if (NODEFS.isWindows && !stat.blksize) {
130-
stat.blksize = 4096;
131-
}
132-
if (NODEFS.isWindows && !stat.blocks) {
133-
stat.blocks = (stat.size+stat.blksize-1)/stat.blksize|0;
126+
if (NODEFS.isWindows) {
127+
// node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake
128+
// them with default blksize of 4096.
129+
// See http://support.microsoft.com/kb/140365
130+
if (!stat.blksize) {
131+
stat.blksize = 4096;
132+
}
133+
if (!stat.blocks) {
134+
stat.blocks = (stat.size+stat.blksize-1)/stat.blksize|0;
135+
}
136+
// Node.js on Windows never represents permission bit 'x', so
137+
// propagate read bits to execute bits.
138+
stat.mode |= (stat.mode & {{{ cDefs.S_IRUSR | cDefs.S_IRGRP | cDefs.S_IROTH }}}) >> 2;
134139
}
135140
return {
136141
dev: stat.dev,

test/test_other.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14286,6 +14286,51 @@ def test_windows_batch_file_dp0_expansion_bug(self):
1428614286
create_file('build_with_quotes.bat', f'@"emcc" {test_file("hello_world.c")}')
1428714287
self.run_process(['build_with_quotes.bat'])
1428814288

14289+
@only_windows('Check that directory permissions are properly retrieved on Windows')
14290+
@requires_node
14291+
def test_windows_nodefs_execution_permission(self):
14292+
src = r'''
14293+
#include <assert.h>
14294+
#include <emscripten.h>
14295+
#include <sys/stat.h>
14296+
#include <string.h>
14297+
#include <stdio.h>
14298+
14299+
void setup() {
14300+
EM_ASM(
14301+
FS.mkdir('/working');
14302+
FS.mount(NODEFS, { root: '.' }, '/working');
14303+
FS.mkdir('/working/new-dir');
14304+
FS.writeFile('/working/new-dir/test.txt', 'test');
14305+
);
14306+
}
14307+
14308+
void test() {
14309+
int err;
14310+
struct stat s;
14311+
memset(&s, 0, sizeof(s));
14312+
err = stat("/working/new-dir", &s);
14313+
assert(S_ISDIR(s.st_mode));
14314+
assert(s.st_mode & S_IXUSR);
14315+
assert(s.st_mode & S_IXGRP);
14316+
assert(s.st_mode & S_IXOTH);
14317+
14318+
err = stat("/working/new-dir/test.txt", &s);
14319+
assert(s.st_mode & S_IXUSR);
14320+
assert(s.st_mode & S_IXGRP);
14321+
assert(s.st_mode & S_IXOTH);
14322+
14323+
puts("success");
14324+
}
14325+
14326+
int main(int argc, char * argv[]) {
14327+
setup();
14328+
test();
14329+
return EXIT_SUCCESS;
14330+
}
14331+
'''
14332+
self.do_run(src, emcc_args=['-lnodefs.js'])
14333+
1428914334
@parameterized({
1429014335
'wasm2js': (True,),
1429114336
'': (False,)

0 commit comments

Comments
 (0)