Skip to content

Commit

Permalink
Add support for AT_REMOVEDIR in unlinkat syscall (emscripten-core#6466)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom authored and kripken committed Apr 24, 2018
1 parent 13ee7f4 commit c8d9b35
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -1174,9 +1174,14 @@ var SyscallsLibrary = {
Module.printErr('warning: untested syscall');
#endif
var dirfd = SYSCALLS.get(), path = SYSCALLS.getStr(), flags = SYSCALLS.get();
assert(flags === 0);
path = SYSCALLS.calculateAt(dirfd, path);
FS.unlink(path);
if (flags === 0) {
FS.unlink(path);
} else if (flags === {{{ cDefine('AT_REMOVEDIR') }}}) {
FS.rmdir(path);
} else {
abort('Invalid flags passed to unlinkat');
}
return 0;
},
__syscall302: function(which, varargs) { // renameat
Expand Down
2 changes: 1 addition & 1 deletion src/struct_info.compiled.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
"F_SETLK",
"O_WRONLY",
"AT_FDCWD",
"AT_SYMLINK_NOFOLLOW"
"AT_SYMLINK_NOFOLLOW",
"AT_REMOVEDIR"
],
"structs": {
"flock": [
Expand Down
47 changes: 47 additions & 0 deletions tests/other/unlink/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
const char *filename = "test.dat";
const char *dirname = "test";

// Create a file
FILE *f = fopen(filename, "wb");
if (f == NULL) {
return 1;
}
if (fclose(f)) {
return 1;
}
// Check it exists
if (access(filename, F_OK) != 0) {
return 1;
}
// Delete the file
if (unlinkat(AT_FDCWD, filename, 0)) {
return 1;
}
// Check that it doesn't exist
if (access(filename, F_OK) != -1) {
return 1;
}

// Create a directory
if (mkdir(dirname, 0700)) {
return 1;
}
// Delete the directory
if (unlinkat(AT_FDCWD, dirname, AT_REMOVEDIR)) {
return 1;
}
// Check that it doesn't exist
if (access(dirname, F_OK) != -1) {
return 1;
}

printf("ok\n");

return 0;
}
1 change: 1 addition & 0 deletions tests/other/unlink/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok
3 changes: 3 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3800,6 +3800,9 @@ def test_init_file_at_offset(self):
Popen([PYTHON, EMCC, 'src.cpp']).communicate()
self.assertContained('read: 0\nfile size is 104\n', run_js('a.out.js'))

def test_unlink(self):
self.do_other_test(os.path.join('other', 'unlink'))

def test_argv0_node(self):
open('code.cpp', 'w').write(r'''
#include <stdio.h>
Expand Down

0 comments on commit c8d9b35

Please sign in to comment.