|
| 1 | +From 7e0817f4c7dbc748fc70c45c20edd5acc98a34d7 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Yuta Saito < [email protected]> |
| 3 | +Date: Tue, 17 Dec 2024 04:19:39 +0900 |
| 4 | +Subject: [PATCH 2/2] Follow-up fixes to make it work with wasi-libc (#1095) |
| 5 | + |
| 6 | +* Gate `fchown` and `fchmod` calls behind `os(WASI)` |
| 7 | + |
| 8 | +They are not available on WASI, so we gate them behind `os(WASI)`. |
| 9 | + |
| 10 | +* Add missing constant shims for wasi-libc |
| 11 | + |
| 12 | +* Use `futimens` instead of legacy `futimes` |
| 13 | + |
| 14 | +wasi-libc does not provide `futimes` as it is a legacy function. |
| 15 | +https://github.com/WebAssembly/wasi-libc/blob/574b88da481569b65a237cb80daf9a2d5aeaf82d/libc-top-half/musl/include/sys/time.h#L34 |
| 16 | +--- |
| 17 | + .../FileManager/FileOperations.swift | 10 +++++++--- |
| 18 | + Sources/FoundationEssentials/WASILibc+Extensions.swift | 9 +++++++++ |
| 19 | + Sources/_FoundationCShims/include/platform_shims.h | 4 ++++ |
| 20 | + 3 files changed, 20 insertions(+), 3 deletions(-) |
| 21 | + |
| 22 | +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift |
| 23 | +index ce4de44..96ee566 100644 |
| 24 | +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift |
| 25 | ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift |
| 26 | +@@ -940,26 +940,30 @@ enum _FileOperations { |
| 27 | + #endif |
| 28 | + var statInfo = stat() |
| 29 | + if fstat(srcFD, &statInfo) == 0 { |
| 30 | ++ #if !os(WASI) // WASI doesn't have fchown for now |
| 31 | + // Copy owner/group |
| 32 | + if fchown(dstFD, statInfo.st_uid, statInfo.st_gid) != 0 { |
| 33 | + try delegate.throwIfNecessary(errno, srcPath(), dstPath()) |
| 34 | + } |
| 35 | ++ #endif |
| 36 | + |
| 37 | + // Copy modification date |
| 38 | +- let value = timeval(tv_sec: statInfo.st_mtim.tv_sec, tv_usec: statInfo.st_mtim.tv_nsec / 1000) |
| 39 | ++ let value = statInfo.st_mtim |
| 40 | + var tv = (value, value) |
| 41 | + try withUnsafePointer(to: &tv) { |
| 42 | +- try $0.withMemoryRebound(to: timeval.self, capacity: 2) { |
| 43 | +- if futimes(dstFD, $0) != 0 { |
| 44 | ++ try $0.withMemoryRebound(to: timespec.self, capacity: 2) { |
| 45 | ++ if futimens(dstFD, $0) != 0 { |
| 46 | + try delegate.throwIfNecessary(errno, srcPath(), dstPath()) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | ++ #if !os(WASI) // WASI doesn't have fchmod for now |
| 52 | + // Copy permissions |
| 53 | + if fchmod(dstFD, statInfo.st_mode) != 0 { |
| 54 | + try delegate.throwIfNecessary(errno, srcPath(), dstPath()) |
| 55 | + } |
| 56 | ++ #endif |
| 57 | + } else { |
| 58 | + try delegate.throwIfNecessary(errno, srcPath(), dstPath()) |
| 59 | + } |
| 60 | +diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift |
| 61 | +index 351fe19..44f3f93 100644 |
| 62 | +--- a/Sources/FoundationEssentials/WASILibc+Extensions.swift |
| 63 | ++++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift |
| 64 | +@@ -49,5 +49,14 @@ internal var O_TRUNC: Int32 { |
| 65 | + internal var O_WRONLY: Int32 { |
| 66 | + return _platform_shims_O_WRONLY() |
| 67 | + } |
| 68 | ++internal var O_RDONLY: Int32 { |
| 69 | ++ return _platform_shims_O_RDONLY() |
| 70 | ++} |
| 71 | ++internal var O_DIRECTORY: Int32 { |
| 72 | ++ return _platform_shims_O_DIRECTORY() |
| 73 | ++} |
| 74 | ++internal var O_NOFOLLOW: Int32 { |
| 75 | ++ return _platform_shims_O_NOFOLLOW() |
| 76 | ++} |
| 77 | + |
| 78 | + #endif // os(WASI) |
| 79 | +diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h |
| 80 | +index 6bc0a0e..e02b581 100644 |
| 81 | +--- a/Sources/_FoundationCShims/include/platform_shims.h |
| 82 | ++++ b/Sources/_FoundationCShims/include/platform_shims.h |
| 83 | +@@ -102,6 +102,10 @@ static inline int32_t _platform_shims_O_CREAT(void) { return O_CREAT; } |
| 84 | + static inline int32_t _platform_shims_O_EXCL(void) { return O_EXCL; } |
| 85 | + static inline int32_t _platform_shims_O_TRUNC(void) { return O_TRUNC; } |
| 86 | + static inline int32_t _platform_shims_O_WRONLY(void) { return O_WRONLY; } |
| 87 | ++static inline int32_t _platform_shims_O_RDONLY(void) { return O_RDONLY; } |
| 88 | ++static inline int32_t _platform_shims_O_DIRECTORY(void) { return O_DIRECTORY; } |
| 89 | ++static inline int32_t _platform_shims_O_NOFOLLOW(void) { return O_NOFOLLOW; } |
| 90 | ++ |
| 91 | + #endif |
| 92 | + |
| 93 | + #endif /* CSHIMS_PLATFORM_SHIMS */ |
| 94 | +-- |
| 95 | +2.46.0 |
| 96 | + |
0 commit comments