Skip to content

Commit 7e0817f

Browse files
Follow-up fixes to make it work with wasi-libc (swiftlang#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 8aff429 commit 7e0817f

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
@@ -940,26 +940,30 @@ enum _FileOperations {
940940
#endif
941941
var statInfo = stat()
942942
if fstat(srcFD, &statInfo) == 0 {
943+
#if !os(WASI) // WASI doesn't have fchown for now
943944
// Copy owner/group
944945
if fchown(dstFD, statInfo.st_uid, statInfo.st_gid) != 0 {
945946
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
946947
}
948+
#endif
947949

948950
// Copy modification date
949-
let value = timeval(tv_sec: statInfo.st_mtim.tv_sec, tv_usec: statInfo.st_mtim.tv_nsec / 1000)
951+
let value = statInfo.st_mtim
950952
var tv = (value, value)
951953
try withUnsafePointer(to: &tv) {
952-
try $0.withMemoryRebound(to: timeval.self, capacity: 2) {
953-
if futimes(dstFD, $0) != 0 {
954+
try $0.withMemoryRebound(to: timespec.self, capacity: 2) {
955+
if futimens(dstFD, $0) != 0 {
954956
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
955957
}
956958
}
957959
}
958960

961+
#if !os(WASI) // WASI doesn't have fchmod for now
959962
// Copy permissions
960963
if fchmod(dstFD, statInfo.st_mode) != 0 {
961964
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
962965
}
966+
#endif
963967
} else {
964968
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
965969
}

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)