Skip to content
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

[6.1] Fix WASI build of _copyDirectoryMetadata #1099

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Sources/FoundationEssentials/FileManager/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ enum _FileOperations {

#if !canImport(Darwin)
private static func _copyDirectoryMetadata(srcFD: CInt, srcPath: @autoclosure () -> String, dstFD: CInt, dstPath: @autoclosure () -> String, delegate: some LinkOrCopyDelegate) throws {
#if !os(WASI)
// Copy extended attributes
var size = flistxattr(srcFD, nil, 0)
if size > 0 {
Expand All @@ -936,28 +937,33 @@ enum _FileOperations {
}
}
}
#endif
var statInfo = stat()
if fstat(srcFD, &statInfo) == 0 {
#if !os(WASI) // WASI doesn't have fchown for now
// Copy owner/group
if fchown(dstFD, statInfo.st_uid, statInfo.st_gid) != 0 {
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
}
#endif

// Copy modification date
let value = timeval(tv_sec: statInfo.st_mtim.tv_sec, tv_usec: statInfo.st_mtim.tv_nsec / 1000)
let value = statInfo.st_mtim
var tv = (value, value)
try withUnsafePointer(to: &tv) {
try $0.withMemoryRebound(to: timeval.self, capacity: 2) {
if futimes(dstFD, $0) != 0 {
try $0.withMemoryRebound(to: timespec.self, capacity: 2) {
if futimens(dstFD, $0) != 0 {
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
}
}
}

#if !os(WASI) // WASI doesn't have fchmod for now
// Copy permissions
if fchmod(dstFD, statInfo.st_mode) != 0 {
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
}
#endif
} else {
try delegate.throwIfNecessary(errno, srcPath(), dstPath())
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/FoundationEssentials/WASILibc+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,14 @@ internal var O_TRUNC: Int32 {
internal var O_WRONLY: Int32 {
return _platform_shims_O_WRONLY()
}
internal var O_RDONLY: Int32 {
return _platform_shims_O_RDONLY()
}
internal var O_DIRECTORY: Int32 {
return _platform_shims_O_DIRECTORY()
}
internal var O_NOFOLLOW: Int32 {
return _platform_shims_O_NOFOLLOW()
}

#endif // os(WASI)
4 changes: 4 additions & 0 deletions Sources/_FoundationCShims/include/platform_shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ static inline int32_t _platform_shims_O_CREAT(void) { return O_CREAT; }
static inline int32_t _platform_shims_O_EXCL(void) { return O_EXCL; }
static inline int32_t _platform_shims_O_TRUNC(void) { return O_TRUNC; }
static inline int32_t _platform_shims_O_WRONLY(void) { return O_WRONLY; }
static inline int32_t _platform_shims_O_RDONLY(void) { return O_RDONLY; }
static inline int32_t _platform_shims_O_DIRECTORY(void) { return O_DIRECTORY; }
static inline int32_t _platform_shims_O_NOFOLLOW(void) { return O_NOFOLLOW; }

#endif

#endif /* CSHIMS_PLATFORM_SHIMS */