Skip to content

Commit bca2f46

Browse files
[6.1] Fix WASI build of _copyDirectoryMetadata (#1099)
* Fix WASI build of `_copyDirectoryMetadata` (#1094) Extended attributes don't exist in WASI, so we need to exclude the use of xattr-related APIs including `flistxattr`. * 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 * Don't try to set extended attributes on Android (#1106) Normal users don't have permission to change these, even for their own files. --------- Co-authored-by: finagolfin <finagolfin@tuta.io>
1 parent ca7070f commit bca2f46

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Sources/FoundationEssentials/FileManager/FileOperations.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ enum _FileOperations {
911911

912912
#if !canImport(Darwin)
913913
private static func _copyDirectoryMetadata(srcFD: CInt, srcPath: @autoclosure () -> String, dstFD: CInt, dstPath: @autoclosure () -> String, delegate: some LinkOrCopyDelegate) throws {
914+
#if !os(WASI) && !os(Android)
914915
// Copy extended attributes
915916
var size = flistxattr(srcFD, nil, 0)
916917
if size > 0 {
@@ -936,28 +937,33 @@ enum _FileOperations {
936937
}
937938
}
938939
}
940+
#endif
939941
var statInfo = stat()
940942
if fstat(srcFD, &statInfo) == 0 {
943+
#if !os(WASI) // WASI doesn't have fchown for now
941944
// Copy owner/group
942945
if fchown(dstFD, statInfo.st_uid, statInfo.st_gid) != 0 {
943946
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
944947
}
948+
#endif
945949

946950
// Copy modification date
947-
let value = timeval(tv_sec: statInfo.st_mtim.tv_sec, tv_usec: statInfo.st_mtim.tv_nsec / 1000)
951+
let value = statInfo.st_mtim
948952
var tv = (value, value)
949953
try withUnsafePointer(to: &tv) {
950-
try $0.withMemoryRebound(to: timeval.self, capacity: 2) {
951-
if futimes(dstFD, $0) != 0 {
954+
try $0.withMemoryRebound(to: timespec.self, capacity: 2) {
955+
if futimens(dstFD, $0) != 0 {
952956
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
953957
}
954958
}
955959
}
956960

961+
#if !os(WASI) // WASI doesn't have fchmod for now
957962
// Copy permissions
958963
if fchmod(dstFD, statInfo.st_mode) != 0 {
959964
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
960965
}
966+
#endif
961967
} else {
962968
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
963969
}

Sources/FoundationEssentials/WASILibc+Extensions.swift

+9
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

+4
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)