-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WasmFS JS API: Implement truncate #19543
Changes from 14 commits
e0d56a1
f305495
58bd56a
cfc0213
b354716
408fcc3
5eaaeba
08773f5
044e8e4
a0a3aa6
ad54b31
23c4326
e857699
c1b3f46
d3a8326
279f9a5
c924202
750a978
a0e4ca5
b1703ff
0cabd03
b262202
058dfa6
f057bc4
1d18411
bce2613
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,25 +126,25 @@ int _wasmfs_mkdir(char* path, int mode) { | |
int _wasmfs_rmdir(char* path){ return __syscall_unlinkat(AT_FDCWD, (intptr_t)path, AT_REMOVEDIR); } | ||
|
||
int _wasmfs_open(char* path, int flags, mode_t mode) { | ||
int err = __syscall_openat(AT_FDCWD, (intptr_t)path, flags, mode); | ||
if (err == -1) { | ||
return -errno; | ||
} | ||
return err; | ||
return __syscall_openat(AT_FDCWD, (intptr_t)path, flags, mode); | ||
} | ||
|
||
int _wasmfs_mknod(char* path, mode_t mode, dev_t dev) { | ||
int err = __syscall_mknodat(AT_FDCWD, (intptr_t)path, mode, dev); | ||
if (err == -1) { | ||
return errno; | ||
} | ||
return err; | ||
return __syscall_mknodat(AT_FDCWD, (intptr_t)path, mode, dev); | ||
} | ||
|
||
int _wasmfs_unlink(char* path) { | ||
return __syscall_unlinkat(AT_FDCWD, (intptr_t)path, 0); | ||
} | ||
|
||
int _wasmfs_truncate(char *path, long size) { | ||
return __syscall_truncate64((intptr_t)path, size); | ||
} | ||
|
||
int _wasmfs_ftruncate(int fd, long size) { | ||
return __syscall_ftruncate64(fd, size); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there's no extra logic necessary here, is it possible to skip these wrappers and just call the underlying syscall directly from the JS? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, although I'm having trouble figuring out how to import the syscalls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe that could be a followup. When calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this as a follow-up sounds good to me. |
||
int _wasmfs_chdir(char* path) { return __syscall_chdir((intptr_t)path); } | ||
|
||
int _wasmfs_symlink(char* old_path, char* new_path) { | ||
|
@@ -190,11 +190,7 @@ int _wasmfs_lchmod(char* path, mode_t mode) { | |
} | ||
|
||
int _wasmfs_rename(char* oldpath, char* newpath) { | ||
int err = __syscall_renameat(AT_FDCWD, (intptr_t)oldpath, AT_FDCWD, (intptr_t)newpath); | ||
if (err == -1) { | ||
return errno; | ||
} | ||
return err; | ||
return __syscall_renameat(AT_FDCWD, (intptr_t)oldpath, AT_FDCWD, (intptr_t)newpath); | ||
}; | ||
|
||
int _wasmfs_read(int fd, void *buf, size_t count) { | ||
|
@@ -228,19 +224,11 @@ int _wasmfs_close(int fd) { | |
} | ||
|
||
int _wasmfs_stat(char* path, struct stat* statBuf) { | ||
int err = __syscall_stat64((intptr_t)path, (intptr_t)statBuf); | ||
if (err == -1) { | ||
return errno; | ||
} | ||
return err; | ||
return __syscall_stat64((intptr_t)path, (intptr_t)statBuf); | ||
} | ||
|
||
int _wasmfs_lstat(char* path, struct stat* statBuf) { | ||
int err = __syscall_lstat64((intptr_t)path, (intptr_t)statBuf); | ||
if (err == -1) { | ||
return errno; | ||
} | ||
return err; | ||
return __syscall_lstat64((intptr_t)path, (intptr_t)statBuf); | ||
} | ||
|
||
// Helper method that identifies what a path is: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,76 @@ int main() { | |
assert(createFileNotHere && createFileNotHere.fd >= 0); | ||
); | ||
|
||
/********** test FS.truncate() **********/ | ||
EM_ASM( | ||
FS.writeFile('truncatetest', 'a=1\nb=2\n'); | ||
); | ||
|
||
struct stat s; | ||
stat("truncatetest", &s); | ||
assert(s.st_size == 8); | ||
|
||
EM_ASM( | ||
FS.truncate('truncatetest', 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with all these changes it would be good to look for other tests that already use this API, if any? If we were previously disabling, or not running, those tests under WASMFS it would be good to enable them now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't see anything explicitly calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it would only be in the test dir. I guess we had not tests for that API. Thanks for looking into it. Hopefully after we are done with this we will have much better test coverage for the JS API! |
||
); | ||
stat("truncatetest", &s); | ||
assert(s.st_size == 2); | ||
|
||
EM_ASM( | ||
FS.truncate('truncatetest', 10); | ||
); | ||
stat("truncatetest", &s); | ||
assert(s.st_size == 10); | ||
|
||
EM_ASM( | ||
var truncateStream = FS.open('truncatetest', 'w'); | ||
FS.ftruncate(truncateStream.fd, 4); | ||
); | ||
stat("truncatetest", &s); | ||
assert(s.st_size == 4); | ||
|
||
EM_ASM( | ||
var ex; | ||
try { | ||
FS.truncate('truncatetest', -10); | ||
} catch(err) { | ||
ex = err; | ||
} | ||
|
||
assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */); | ||
); | ||
|
||
EM_ASM( | ||
var ex; | ||
try { | ||
var truncateStream = FS.open('truncatetest', 'w'); | ||
FS.ftruncate(truncateStream.fd, -10); | ||
} catch(err) { | ||
ex = err; | ||
} | ||
|
||
assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */); | ||
); | ||
|
||
EM_ASM( | ||
var ex; | ||
try { | ||
FS.truncate('nonexistent', 10); | ||
} catch(err) { | ||
ex = err; | ||
} | ||
assert(ex.name === "ErrnoError" && ex.errno === 44 /* ENOENT */); | ||
|
||
var ex; | ||
try { | ||
FS.ftruncate(99, 10); | ||
} catch(err) { | ||
ex = err; | ||
} | ||
|
||
assert(ex.name === "ErrnoError" && ex.errno === 8 /* EBADF */); | ||
); | ||
|
||
/********** test FS.rename() **********/ | ||
EM_ASM( | ||
FS.mkdir('renamedir'); | ||
|
@@ -151,6 +221,7 @@ int main() { | |
|
||
remove("mknodtest"); | ||
remove("createtest"); | ||
remove("truncatetest"); | ||
remove("testfile"); | ||
remove("renametestfile"); | ||
remove("readtestfile"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these bugfixes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We realized that this check isn't needed, since the syscalls do not actually set
errno
. The tests were passing before becauseerr
was never equal to -1, so thejs_api.cpp
function directly returned the error without using errno, giving the desired effect.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make a separate PR for this cleanup stuff?