Skip to content

Commit 4623330

Browse files
committed
fix realpath on directories
1 parent b186271 commit 4623330

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/library_fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ mergeInto(LibraryManager.library, {
350350
if (FS.isLink(node.mode)) {
351351
return ERRNO_CODES.ELOOP;
352352
} else if (FS.isDir(node.mode)) {
353-
if ((flags & {{{ cDefine('O_ACCMODE') }}}) !== {{{ cDefine('O_RDONLY')}}} || // opening for write
354-
(flags & {{{ cDefine('O_TRUNC') }}})) {
353+
if (FS.flagsToPermissionString(flags) !== 'r' || // opening for write
354+
(flags & {{{ cDefine('O_TRUNC') }}})) { // TODO: check for O_SEARCH? (== search for dir only)
355355
return ERRNO_CODES.EISDIR;
356356
}
357357
}

tests/test_other.py

+48
Original file line numberDiff line numberDiff line change
@@ -5321,6 +5321,54 @@ def test_realpath(self):
53215321
Popen([PYTHON, EMCC, 'src.c', '--embed-file', 'boot']).communicate()
53225322
self.assertContained('Resolved: /boot/README.txt', run_js('a.out.js'))
53235323

5324+
def test_realpath_2(self):
5325+
open('src.c', 'w').write(r'''
5326+
#include <stdlib.h>
5327+
#include <stdio.h>
5328+
#include <errno.h>
5329+
5330+
int testrealpath(const char* path) {
5331+
errno = 0;
5332+
char *t_realpath_buf = realpath(path, NULL);
5333+
if (NULL == t_realpath_buf) {
5334+
printf("Resolve failed: \"%s\"\n",path);fflush(stdout);
5335+
return 1;
5336+
} else {
5337+
printf("Resolved: \"%s\" => \"%s\"\n", path, t_realpath_buf);fflush(stdout);
5338+
free(t_realpath_buf);
5339+
return 0;
5340+
}
5341+
}
5342+
5343+
int main(int argc, char **argv)
5344+
{
5345+
// files:
5346+
testrealpath("testfile.txt");
5347+
testrealpath("Folder/testfile.txt");
5348+
testrealpath("testnonexistentfile.txt");
5349+
// folders
5350+
testrealpath("Folder");
5351+
testrealpath("/Folder");
5352+
testrealpath("./");
5353+
testrealpath("");
5354+
testrealpath("/");
5355+
return 0;
5356+
}
5357+
''')
5358+
open('testfile.txt', 'w').write('')
5359+
if not os.path.exists('Folder'): os.mkdir('Folder')
5360+
open(os.path.join('Folder', 'testfile.txt'), 'w').write('')
5361+
check_execute([PYTHON, EMCC, 'src.c', '--embed-file', 'testfile.txt', '--embed-file', 'Folder'])
5362+
self.assertContained('''Resolved: "testfile.txt" => "/testfile.txt"
5363+
Resolved: "Folder/testfile.txt" => "/Folder/testfile.txt"
5364+
Resolve failed: "testnonexistentfile.txt"
5365+
Resolved: "Folder" => "/Folder"
5366+
Resolved: "/Folder" => "/Folder"
5367+
Resolved: "./" => "/"
5368+
Resolve failed: ""
5369+
Resolved: "/" => "/"
5370+
''', run_js('a.out.js'))
5371+
53245372
def test_no_warnings(self):
53255373
out, err = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_libcxx.cpp')], stderr=PIPE).communicate()
53265374
assert err == '', err

0 commit comments

Comments
 (0)