Skip to content

Commit 77a2d87

Browse files
committed
Reorganize the stat/fstat API to make imports more fine-grained.
This more closely aligns the imports for stat/fstat with the actual capabilities. Split fd_stat_put into fd_stat_set_flags and fd_stat_set_rights. Split file_stat_fput into file_fstat_set_times and file_fstat_set_size. Rename file_stat_put to file_stat_set_times. And other minor API adjustments for consistency and readability. This also eliminates the need for __wasi_fdsflags_t. And overall, hopefully this makes it easier for us to remember the differences between fd_stat_get/file_stat_fget/file_stat_get.
1 parent b1fa89f commit 77a2d87

11 files changed

Lines changed: 133 additions & 55 deletions

File tree

expected/wasm32-wasi/predefined-macros.txt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,13 +3510,6 @@
35103510
#define __WASI_FDFLAG_NONBLOCK (UINT16_C(0x0004))
35113511
#define __WASI_FDFLAG_RSYNC (UINT16_C(0x0008))
35123512
#define __WASI_FDFLAG_SYNC (UINT16_C(0x0010))
3513-
#define __WASI_FDSTAT_FLAGS (UINT16_C(0x0001))
3514-
#define __WASI_FDSTAT_RIGHTS (UINT16_C(0x0002))
3515-
#define __WASI_FILESTAT_ATIM (UINT16_C(0x0001))
3516-
#define __WASI_FILESTAT_ATIM_NOW (UINT16_C(0x0002))
3517-
#define __WASI_FILESTAT_MTIM (UINT16_C(0x0004))
3518-
#define __WASI_FILESTAT_MTIM_NOW (UINT16_C(0x0008))
3519-
#define __WASI_FILESTAT_SIZE (UINT16_C(0x0010))
35203513
#define __WASI_FILETYPE_BLOCK_DEVICE (UINT8_C(1))
35213514
#define __WASI_FILETYPE_CHARACTER_DEVICE (UINT8_C(2))
35223515
#define __WASI_FILETYPE_DIRECTORY (UINT8_C(3))
@@ -3525,6 +3518,11 @@
35253518
#define __WASI_FILETYPE_SOCKET_STREAM (UINT8_C(6))
35263519
#define __WASI_FILETYPE_SYMBOLIC_LINK (UINT8_C(7))
35273520
#define __WASI_FILETYPE_UNKNOWN (UINT8_C(0))
3521+
#define __WASI_FILE_STAT_ATIM (UINT16_C(0x0001))
3522+
#define __WASI_FILE_STAT_ATIM_NOW (UINT16_C(0x0002))
3523+
#define __WASI_FILE_STAT_MTIM (UINT16_C(0x0004))
3524+
#define __WASI_FILE_STAT_MTIM_NOW (UINT16_C(0x0008))
3525+
#define __WASI_FILE_STAT_SIZE (UINT16_C(0x0010))
35283526
#define __WASI_LOOKUP_SYMLINK_FOLLOW (UINT32_C(0x00000001))
35293527
#define __WASI_O_CREAT (UINT16_C(0x0001))
35303528
#define __WASI_O_DIRECTORY (UINT16_C(0x0002))
@@ -3548,15 +3546,13 @@
35483546
#define __WASI_RIGHT_FILE_READLINK (UINT64_C(0x0000000000008000))
35493547
#define __WASI_RIGHT_FILE_RENAME_SOURCE (UINT64_C(0x0000000000010000))
35503548
#define __WASI_RIGHT_FILE_RENAME_TARGET (UINT64_C(0x0000000000020000))
3551-
#define __WASI_RIGHT_FILE_STAT_FGET (UINT64_C(0x0000000000040000))
3552-
#define __WASI_RIGHT_FILE_STAT_FPUT_SIZE (UINT64_C(0x0000000000080000))
3553-
#define __WASI_RIGHT_FILE_STAT_FPUT_TIMES (UINT64_C(0x0000000000100000))
3554-
#define __WASI_RIGHT_FILE_STAT_GET (UINT64_C(0x0000000000200000))
3555-
#define __WASI_RIGHT_FILE_STAT_PUT_TIMES (UINT64_C(0x0000000000400000))
3556-
#define __WASI_RIGHT_FILE_SYMLINK (UINT64_C(0x0000000000800000))
3557-
#define __WASI_RIGHT_FILE_UNLINK (UINT64_C(0x0000000001000000))
3558-
#define __WASI_RIGHT_POLL_FD_READWRITE (UINT64_C(0x0000000002000000))
3559-
#define __WASI_RIGHT_SOCK_SHUTDOWN (UINT64_C(0x0000000004000000))
3549+
#define __WASI_RIGHT_FILE_STAT_GET (UINT64_C(0x0000000000040000))
3550+
#define __WASI_RIGHT_FILE_STAT_SET_SIZE (UINT64_C(0x0000000000080000))
3551+
#define __WASI_RIGHT_FILE_STAT_SET_TIMES (UINT64_C(0x0000000000100000))
3552+
#define __WASI_RIGHT_FILE_SYMLINK (UINT64_C(0x0000000000200000))
3553+
#define __WASI_RIGHT_FILE_UNLINK (UINT64_C(0x0000000000400000))
3554+
#define __WASI_RIGHT_POLL_FD_READWRITE (UINT64_C(0x0000000000800000))
3555+
#define __WASI_RIGHT_SOCK_SHUTDOWN (UINT64_C(0x0000000001000000))
35603556
#define __WASI_SHUT_RD (UINT8_C(0x01))
35613557
#define __WASI_SHUT_WR (UINT8_C(0x02))
35623558
#define __WASI_SIGABRT (UINT8_C(6))

expected/wasm32-wasi/undefined-symbols.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,22 @@ __wasi_fd_read
3333
__wasi_fd_renumber
3434
__wasi_fd_seek
3535
__wasi_fd_stat_get
36-
__wasi_fd_stat_put
36+
__wasi_fd_stat_set_flags
3737
__wasi_fd_sync
3838
__wasi_fd_write
3939
__wasi_file_advise
4040
__wasi_file_allocate
41+
__wasi_file_fstat_get
42+
__wasi_file_fstat_set_size
43+
__wasi_file_fstat_set_times
4144
__wasi_file_link
4245
__wasi_file_mkdir
4346
__wasi_file_open
4447
__wasi_file_readdir
4548
__wasi_file_readlink
4649
__wasi_file_rename
47-
__wasi_file_stat_fget
48-
__wasi_file_stat_fput
4950
__wasi_file_stat_get
50-
__wasi_file_stat_put
51+
__wasi_file_stat_set_times
5152
__wasi_file_symlink
5253
__wasi_file_unlink
5354
__wasi_poll_oneoff

libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ int fcntl(int fildes, int cmd, ...) {
5050
int flags = va_arg(ap, int);
5151
va_end(ap);
5252

53+
#ifdef __wasilibc_unmodified_upstream // fstat
5354
__wasi_fdstat_t fds = {.fs_flags = flags & 0xfff};
5455
__wasi_errno_t error =
5556
__wasi_fd_stat_put(fildes, &fds, __WASI_FDSTAT_FLAGS);
57+
#else
58+
__wasi_fdflags_t fs_flags = flags & 0xfff;
59+
__wasi_errno_t error =
60+
__wasi_fd_stat_set_flags(fildes, fs_flags);
61+
#endif
5662
if (error != 0) {
5763
errno = error;
5864
return -1;

libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ int openat(int fd, const char *path, int oflag, ...) {
2929
__wasi_rights_t max =
3030
~(__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_READ |
3131
__WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FILE_ALLOCATE |
32+
#ifdef __wasilibc_unmodified_upstream // fstat
3233
__WASI_RIGHT_FILE_READDIR | __WASI_RIGHT_FILE_STAT_FPUT_SIZE |
34+
#else
35+
__WASI_RIGHT_FILE_READDIR | __WASI_RIGHT_FILE_STAT_SET_SIZE |
36+
#endif
3337
#ifdef __wasilibc_unmodified_upstream // RIGHT_MEM_MAP_EXEC
3438
__WASI_RIGHT_MEM_MAP_EXEC);
3539
#else
@@ -55,7 +59,11 @@ int openat(int fd, const char *path, int oflag, ...) {
5559
min |= __WASI_RIGHT_FD_SEEK;
5660
max |= __WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_WRITE |
5761
__WASI_RIGHT_FILE_ALLOCATE |
62+
#ifdef __wasilibc_unmodified_upstream // fstat
5863
__WASI_RIGHT_FILE_STAT_FPUT_SIZE;
64+
#else
65+
__WASI_RIGHT_FILE_STAT_SET_SIZE;
66+
#endif
5967
}
6068
break;
6169
case O_EXEC:

libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ int ioctl(int fildes, int request, ...) {
8787
va_end(ap);
8888

8989
// Update the file descriptor flags.
90+
#ifdef __wasilibc_unmodified_upstream // fstat
9091
error = __wasi_fd_stat_put(fildes, &fds, __WASI_FDSTAT_FLAGS);
92+
#else
93+
error = __wasi_fd_stat_set_flags(fildes, fds.fs_flags);
94+
#endif
9195
if (error != 0) {
9296
errno = error;
9397
return -1;

libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
int fstat(int fildes, struct stat *buf) {
1313
__wasi_filestat_t internal_stat;
14+
#ifdef __wasilibc_unmodified_upstream // fstat
1415
__wasi_errno_t error = __wasi_file_stat_fget(fildes, &internal_stat);
16+
#else
17+
__wasi_errno_t error = __wasi_file_fstat_get(fildes, &internal_stat);
18+
#endif
1519
if (error != 0) {
1620
errno = error;
1721
return -1;

libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@
1111

1212
int futimens(int fd, const struct timespec *times) {
1313
// Convert timestamps and extract NOW/OMIT flags.
14+
#ifdef __wasilibc_unmodified_upstream // fstat
1415
__wasi_filestat_t fs;
1516
__wasi_fsflags_t flags;
1617
if (!utimens_get_timestamps(times, &fs, &flags)) {
18+
#else
19+
__wasi_timestamp_t st_atim;
20+
__wasi_timestamp_t st_mtim;
21+
__wasi_fstflags_t flags;
22+
if (!utimens_get_timestamps(times, &st_atim, &st_mtim, &flags)) {
23+
#endif
1724
errno = EINVAL;
1825
return -1;
1926
}
2027

2128
// Perform system call.
29+
#ifdef __wasilibc_unmodified_upstream // fstat
2230
__wasi_errno_t error = __wasi_file_stat_fput(fd, &fs, flags);
31+
#else
32+
__wasi_errno_t error = __wasi_file_fstat_set_times(fd, st_atim, st_mtim, flags);
33+
#endif
2334
if (error != 0) {
2435
errno = error;
2536
return -1;

libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,64 @@ static inline void to_public_stat(const __wasi_filestat_t *in,
8585
}
8686

8787
static inline bool utimens_get_timestamps(const struct timespec *times,
88+
#ifdef __wasilibc_unmodified_upstream // fstat
8889
__wasi_filestat_t *fs,
8990
__wasi_fsflags_t *flags) {
91+
#else
92+
__wasi_timestamp_t *st_atim,
93+
__wasi_timestamp_t *st_mtim,
94+
__wasi_fstflags_t *flags) {
95+
#endif
9096
if (times == NULL) {
9197
// Update both timestamps.
98+
#ifdef __wasilibc_unmodified_upstream // fstat
9299
*flags = __WASI_FILESTAT_ATIM_NOW | __WASI_FILESTAT_MTIM_NOW;
100+
#else
101+
*flags = __WASI_FILE_STAT_ATIM_NOW | __WASI_FILE_STAT_MTIM_NOW;
102+
#endif
93103
} else {
94104
// Set individual timestamps.
95105
*flags = 0;
96106
switch (times[0].tv_nsec) {
97107
case UTIME_NOW:
108+
#ifdef __wasilibc_unmodified_upstream // fstat
98109
*flags |= __WASI_FILESTAT_ATIM_NOW;
110+
#else
111+
*flags |= __WASI_FILE_STAT_ATIM_NOW;
112+
#endif
99113
break;
100114
case UTIME_OMIT:
101115
break;
102116
default:
117+
#ifdef __wasilibc_unmodified_upstream // fstat
103118
*flags |= __WASI_FILESTAT_ATIM;
104119
if (!timespec_to_timestamp_exact(&times[0], &fs->st_atim))
120+
#else
121+
*flags |= __WASI_FILE_STAT_ATIM;
122+
if (!timespec_to_timestamp_exact(&times[0], st_atim))
123+
#endif
105124
return false;
106125
break;
107126
}
108127

109128
switch (times[1].tv_nsec) {
110129
case UTIME_NOW:
130+
#ifdef __wasilibc_unmodified_upstream // fstat
111131
*flags |= __WASI_FILESTAT_MTIM_NOW;
132+
#else
133+
*flags |= __WASI_FILE_STAT_MTIM_NOW;
134+
#endif
112135
break;
113136
case UTIME_OMIT:
114137
break;
115138
default:
139+
#ifdef __wasilibc_unmodified_upstream // fstat
116140
*flags |= __WASI_FILESTAT_MTIM;
117141
if (!timespec_to_timestamp_exact(&times[1], &fs->st_mtim))
142+
#else
143+
*flags |= __WASI_FILE_STAT_MTIM;
144+
if (!timespec_to_timestamp_exact(&times[1], st_mtim))
145+
#endif
118146
return false;
119147
break;
120148
}

libc-bottom-half/cloudlibc/src/libc/sys/stat/utimensat.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616
int utimensat(int fd, const char *path, const struct timespec times[2],
1717
int flag) {
1818
// Convert timestamps and extract NOW/OMIT flags.
19+
#ifdef __wasilibc_unmodified_upstream // fstat
1920
__wasi_filestat_t fs;
2021
__wasi_fsflags_t flags;
2122
if (!utimens_get_timestamps(times, &fs, &flags)) {
23+
#else
24+
__wasi_timestamp_t st_atim;
25+
__wasi_timestamp_t st_mtim;
26+
__wasi_fstflags_t flags;
27+
if (!utimens_get_timestamps(times, &st_atim, &st_mtim, &flags)) {
28+
#endif
2229
errno = EINVAL;
2330
return -1;
2431
}
@@ -38,10 +45,10 @@ int utimensat(int fd, const char *path, const struct timespec times[2],
3845

3946
// Perform system call.
4047
__wasi_errno_t error =
41-
#ifdef __wasilibc_unmodified_upstream // split out __wasi_lookup_t
48+
#ifdef __wasilibc_unmodified_upstream // split out __wasi_lookup_t, fstat
4249
__wasi_file_stat_put(lookup, path, strlen(path), &fs, flags);
4350
#else
44-
__wasi_file_stat_put(fd, lookup_flags, path, strlen(path), &fs, flags);
51+
__wasi_file_stat_set_times(fd, lookup_flags, path, strlen(path), st_atim, st_mtim, flags);
4552
#endif
4653
if (error != 0) {
4754
errno = errno_fixup_directory(fd, error);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ int ftruncate(int fildes, off_t length) {
1111
errno = EINVAL;
1212
return -1;
1313
}
14+
#ifdef __wasilibc_unmodified_upstream // fstat
1415
__wasi_filestat_t fs = {
1516
.st_size = length,
1617
};
18+
#else
19+
__wasi_filesize_t st_size = length;
20+
#endif
1721
__wasi_errno_t error =
22+
#ifdef __wasilibc_unmodified_upstream // fstat
1823
__wasi_file_stat_fput(fildes, &fs, __WASI_FILESTAT_SIZE);
24+
#else
25+
__wasi_file_fstat_set_size(fildes, st_size);
26+
#endif
1927
if (error != 0) {
2028
errno = error;
2129
return -1;

0 commit comments

Comments
 (0)