Skip to content

Commit 727e61b

Browse files
committed
Route rmdir/unlink directly to wasi's file_unlink_{directory,file}.
This way, if an application calls rmdir, it will only link in the file_unlink_directory, and not file_unlink_file too. Also, re-implement remove in terms of these new utilities.
1 parent d4efc3f commit 727e61b

8 files changed

Lines changed: 54 additions & 26 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
121121
%/rename.c \
122122
%/tmpnam.c %/tmpfile.c %/tempnam.c \
123123
%/popen.c %/pclose.c \
124+
%/remove.c \
124125
%/gets.c, \
125126
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/*.c)) \
126127
$(filter-out %/strsignal.c, \

expected/wasm32-wasi/defined-symbols.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ __uselocale
243243
__utc
244244
__wasilibc_fd_renumber
245245
__wasilibc_register_preopened_fd
246+
__wasilibc_rmdirat
247+
__wasilibc_rmfileat
246248
__wcscoll_l
247249
__wcsftime_l
248250
__wcsxfrm_l
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <common/errno.h>
2+
#include <wasi/core.h>
3+
#include <errno.h>
4+
#include <string.h>
5+
6+
int __wasilibc_rmdirat(int fd, const char *path) {
7+
size_t path_len = strlen(path);
8+
__wasi_errno_t error = __wasi_file_unlink_directory(fd, path, path_len);
9+
if (error != 0) {
10+
errno = errno_fixup_directory(fd, error);
11+
return -1;
12+
}
13+
return 0;
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <common/errno.h>
2+
#include <wasi/core.h>
3+
#include <errno.h>
4+
#include <string.h>
5+
6+
int __wasilibc_rmfileat(int fd, const char *path) {
7+
size_t path_len = strlen(path);
8+
__wasi_errno_t error = __wasi_file_unlink_file(fd, path, path_len);
9+
if (error != 0) {
10+
errno = error;
11+
return -1;
12+
}
13+
return 0;
14+
}

libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <common/errno.h>
66

77
#include <wasi/core.h>
8+
#ifdef __wasilibc_unmodified_upstream // unlink
9+
#else
10+
#include <wasi/libc.h>
11+
#endif
812
#include <errno.h>
913
#include <fcntl.h>
1014
#include <string.h>
@@ -21,22 +25,11 @@ int unlinkat(int fd, const char *path, int flag) {
2125
errno = errno_fixup_directory(fd, error);
2226
return -1;
2327
}
28+
return 0;
2429
#else
25-
__wasi_errno_t error;
26-
size_t path_len = strlen(path);
2730
if ((flag & AT_REMOVEDIR) != 0) {
28-
error = __wasi_file_unlink_directory(fd, path, path_len);
29-
if (error != 0) {
30-
errno = errno_fixup_directory(fd, error);
31-
return -1;
32-
}
33-
} else {
34-
error = __wasi_file_unlink_file(fd, path, path_len);
35-
if (error != 0) {
36-
errno = error;
37-
return -1;
38-
}
31+
return __wasilibc_rmdirat(fd, path);
3932
}
33+
return __wasilibc_rmfileat(fd, path);
4034
#endif
41-
return 0;
4235
}

libc-bottom-half/headers/public/wasi/libc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ extern "C" {
77

88
int __wasilibc_register_preopened_fd(int fd, const char *path);
99
int __wasilibc_fd_renumber(int fd, int newfd);
10+
int __wasilibc_rmfileat(int fd, const char *path);
11+
int __wasilibc_rmdirat(int fd, const char *path);
1012

1113
#ifdef __cplusplus
1214
}

libc-bottom-half/libpreopen/lib/po_libc_wrappers.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#ifdef __wasilibc_unmodified_upstream
5252
#else
5353
#include <errno.h>
54+
#include <wasi/libc.h>
5455
#endif
5556

5657
#include "internal.h"
@@ -318,15 +319,26 @@ unlink(const char *pathname)
318319
{
319320
struct po_relpath rel_pathname = find_relative(pathname, NULL);
320321

321-
return unlinkat(rel_pathname.dirfd, rel_pathname.relative_path, 0);
322+
return __wasilibc_rmfileat(rel_pathname.dirfd, rel_pathname.relative_path);
322323
}
323324

324325
int
325326
rmdir(const char *pathname)
326327
{
327328
struct po_relpath rel_pathname = find_relative(pathname, NULL);
328329

329-
return unlinkat(rel_pathname.dirfd, rel_pathname.relative_path, AT_REMOVEDIR);
330+
return __wasilibc_rmdirat(rel_pathname.dirfd, rel_pathname.relative_path);
331+
}
332+
333+
int
334+
remove(const char *pathname)
335+
{
336+
struct po_relpath rel_pathname = find_relative(pathname, NULL);
337+
338+
int r = __wasilibc_rmfileat(rel_pathname.dirfd, rel_pathname.relative_path);
339+
if (r != 0 && errno == EISDIR)
340+
r = __wasilibc_rmdirat(rel_pathname.dirfd, rel_pathname.relative_path);
341+
return r;
330342
}
331343

332344
int
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#ifdef __wasilibc_unmodified_upstream
2-
#else
3-
#include <unistd.h>
4-
#endif
51
#include <stdio.h>
62
#include <errno.h>
73
#include <fcntl.h>
84
#include "syscall.h"
95

106
int remove(const char *path)
117
{
12-
#ifdef __wasilibc_unmodified_upstream
138
#ifdef SYS_unlink
149
int r = __syscall(SYS_unlink, path);
1510
#else
@@ -21,9 +16,4 @@ int remove(const char *path)
2116
if (r==-EISDIR) r = __syscall(SYS_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
2217
#endif
2318
return __syscall_ret(r);
24-
#else
25-
int r = unlink(path);
26-
if (r != 0 && errno==EISDIR) r = rmdir(path);
27-
return r;
28-
#endif
2919
}

0 commit comments

Comments
 (0)