Skip to content

Commit 7de8b07

Browse files
authored
[WasmFS] Implement additional versions of existing syscalls (#16421)
Including: - fstatat64 - mkdirat - fchdir - renameat - symlinkat - readlinkat - fchmodat Also reimplement the existing syscalls in terms of the new more general versions where possible to reduce code duplication and get free test coverage. As drive-by fixes, remove an unused debugging function, simplify the optional arguments to the path parsing functions, and validate the correctness of `flags` arguments to existing syscalls, and add various TODOs. Resolves #15972.
1 parent a29f668 commit 7de8b07

File tree

4 files changed

+151
-99
lines changed

4 files changed

+151
-99
lines changed

system/lib/libc/musl/arch/emscripten/syscall_arch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ long __syscall_unlinkat(long dirfd, long path, long flags);
101101
long __syscall_renameat(long olddirfd, long oldpath, long newdirfd, long newpath);
102102
long __syscall_linkat(long olddirfd, long oldpath, long newdirfd, long newpath, long flags);
103103
long __syscall_symlinkat(long target, long newdirfd, long linkpath);
104-
long __syscall_readlinkat(long dirfd, long path, long bug, long bufsize);
104+
long __syscall_readlinkat(long dirfd, long path, long buf, long bufsize);
105105
long __syscall_fchmodat(long dirfd, long path, long mode, ...);
106106
long __syscall_faccessat(long dirfd, long path, long amode, long flags);
107107
long __syscall_pselect6(long nfds, long readfds, long writefds, long exceptfds, long timeout, long sigmaks);

system/lib/wasmfs/paths.cpp

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,6 @@
1111

1212
namespace wasmfs::path {
1313

14-
#ifdef WASMFS_DEBUG
15-
// Print the absolute path of a file.
16-
std::string getAbsPath(std::shared_ptr<File> curr) {
17-
std::string result = "";
18-
19-
while (curr != wasmFS.getRootDirectory()) {
20-
auto parent = curr->locked().getParent();
21-
// Check if the parent exists. The parent may not exist if the CWD or one
22-
// of its ancestors has been unlinked.
23-
if (!parent) {
24-
return "unlinked";
25-
}
26-
27-
auto parentDir = parent->dynCast<Directory>();
28-
29-
auto name = parentDir->locked().getName(curr);
30-
result = '/' + name + result;
31-
curr = parentDir;
32-
}
33-
34-
// Check if the cwd is the root directory.
35-
if (result.empty()) {
36-
result = "/";
37-
}
38-
39-
return result;
40-
}
41-
#endif
42-
4314
static ParsedFile getChild(std::shared_ptr<Directory> dir,
4415
std::string_view name) {
4516
auto child = dir->locked().getChild(std::string(name));
@@ -50,8 +21,7 @@ static ParsedFile getChild(std::shared_ptr<Directory> dir,
5021
return child;
5122
}
5223

53-
ParsedParent parseParent(std::string_view path,
54-
std::optional<__wasi_fd_t> baseFD) {
24+
ParsedParent parseParent(std::string_view path, __wasi_fd_t basefd) {
5525
// Empty paths never exist.
5626
if (path.empty()) {
5727
return {-ENOENT};
@@ -62,17 +32,17 @@ ParsedParent parseParent(std::string_view path,
6232
if (path.front() == '/') {
6333
curr = wasmFS.getRootDirectory();
6434
path.remove_prefix(1);
65-
} else if (baseFD && *baseFD != AT_FDCWD) {
66-
auto openFile = wasmFS.getFileTable().locked().getEntry(*baseFD);
35+
} else if (basefd == AT_FDCWD) {
36+
curr = wasmFS.getCWD();
37+
} else {
38+
auto openFile = wasmFS.getFileTable().locked().getEntry(basefd);
6739
if (!openFile) {
6840
return -EBADF;
6941
}
7042
curr = openFile->locked().getFile()->dynCast<Directory>();
7143
if (!curr) {
7244
return {-ENOTDIR};
7345
}
74-
} else {
75-
curr = wasmFS.getCWD();
7646
}
7747

7848
// Ignore trailing '/'.
@@ -114,8 +84,8 @@ ParsedParent parseParent(std::string_view path,
11484
}
11585
}
11686

117-
ParsedFile parseFile(std::string_view path, std::optional<__wasi_fd_t> baseFD) {
118-
auto parsed = parseParent(path, baseFD);
87+
ParsedFile parseFile(std::string_view path, __wasi_fd_t basefd) {
88+
auto parsed = parseParent(path, basefd);
11989
if (auto err = parsed.getError()) {
12090
return {err};
12191
}

system/lib/wasmfs/paths.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include <cassert>
9+
#include <fcntl.h>
910
#include <memory>
1011
#include <string_view>
1112
#include <variant>
@@ -45,8 +46,7 @@ struct ParsedParent {
4546
}
4647
};
4748

48-
ParsedParent parseParent(std::string_view path,
49-
std::optional<__wasi_fd_t> baseFD = {});
49+
ParsedParent parseParent(std::string_view path, __wasi_fd_t basefd = AT_FDCWD);
5050

5151
struct ParsedFile {
5252
private:
@@ -71,7 +71,6 @@ struct ParsedFile {
7171
}
7272
};
7373

74-
ParsedFile parseFile(std::string_view path,
75-
std::optional<__wasi_fd_t> baseFD = {});
74+
ParsedFile parseFile(std::string_view path, __wasi_fd_t basefd = AT_FDCWD);
7675

7776
} // namespace wasmfs::path

0 commit comments

Comments
 (0)