Skip to content

Commit c0532f8

Browse files
Follow-up fixes to make it work with wasi-libc (#1095)
* Gate `fchown` and `fchmod` calls behind `os(WASI)` They are not available on WASI, so we gate them behind `os(WASI)`. * Add missing constant shims for wasi-libc * Use `futimens` instead of legacy `futimes` wasi-libc does not provide `futimes` as it is a legacy function. https://github.com/WebAssembly/wasi-libc/blob/574b88da481569b65a237cb80daf9a2d5aeaf82d/libc-top-half/musl/include/sys/time.h#L34
1 parent 6bf5c90 commit c0532f8

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,26 +980,30 @@ enum _FileOperations {
980980
#endif
981981
var statInfo = stat()
982982
if fstat(srcFD, &statInfo) == 0 {
983+
#if !os(WASI) // WASI doesn't have fchown for now
983984
// Copy owner/group
984985
if fchown(dstFD, statInfo.st_uid, statInfo.st_gid) != 0 {
985986
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
986987
}
988+
#endif
987989

988990
// Copy modification date
989-
let value = timeval(tv_sec: statInfo.st_mtim.tv_sec, tv_usec: statInfo.st_mtim.tv_nsec / 1000)
991+
let value = statInfo.st_mtim
990992
var tv = (value, value)
991993
try withUnsafePointer(to: &tv) {
992-
try $0.withMemoryRebound(to: timeval.self, capacity: 2) {
993-
if futimes(dstFD, $0) != 0 {
994+
try $0.withMemoryRebound(to: timespec.self, capacity: 2) {
995+
if futimens(dstFD, $0) != 0 {
994996
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
995997
}
996998
}
997999
}
9981000

1001+
#if !os(WASI) // WASI doesn't have fchmod for now
9991002
// Copy permissions
10001003
if fchmod(dstFD, mode_t(statInfo.st_mode)) != 0 {
10011004
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
10021005
}
1006+
#endif
10031007
} else {
10041008
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
10051009
}

Sources/FoundationEssentials/WASILibc+Extensions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,14 @@ internal var O_TRUNC: Int32 {
4949
internal var O_WRONLY: Int32 {
5050
return _platform_shims_O_WRONLY()
5151
}
52+
internal var O_RDONLY: Int32 {
53+
return _platform_shims_O_RDONLY()
54+
}
55+
internal var O_DIRECTORY: Int32 {
56+
return _platform_shims_O_DIRECTORY()
57+
}
58+
internal var O_NOFOLLOW: Int32 {
59+
return _platform_shims_O_NOFOLLOW()
60+
}
5261

5362
#endif // os(WASI)

Sources/_FoundationCShims/include/platform_shims.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ static inline int32_t _platform_shims_O_CREAT(void) { return O_CREAT; }
102102
static inline int32_t _platform_shims_O_EXCL(void) { return O_EXCL; }
103103
static inline int32_t _platform_shims_O_TRUNC(void) { return O_TRUNC; }
104104
static inline int32_t _platform_shims_O_WRONLY(void) { return O_WRONLY; }
105+
static inline int32_t _platform_shims_O_RDONLY(void) { return O_RDONLY; }
106+
static inline int32_t _platform_shims_O_DIRECTORY(void) { return O_DIRECTORY; }
107+
static inline int32_t _platform_shims_O_NOFOLLOW(void) { return O_NOFOLLOW; }
108+
105109
#endif
106110

107111
#endif /* CSHIMS_PLATFORM_SHIMS */

0 commit comments

Comments
 (0)