|
| 1 | +From 6e3a9e2ced5688bfe9e209633735bf6bdc85d64c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Yuta Saito < [email protected]> |
| 3 | +Date: Mon, 31 Mar 2025 11:07:42 +0000 |
| 4 | +Subject: [PATCH] [wasm] Gate atomic write option usages behind platform check |
| 5 | + |
| 6 | +`Data.WritingOptions.atomic` is now unavailable on WASI: https://github.com/swiftlang/swift-foundation/pull/992 |
| 7 | +--- |
| 8 | + Sources/Foundation/NSData.swift | 11 +++++++++-- |
| 9 | + Sources/Foundation/NSString.swift | 7 ++++++- |
| 10 | + 2 files changed, 15 insertions(+), 3 deletions(-) |
| 11 | + |
| 12 | +diff --git a/Sources/Foundation/NSData.swift b/Sources/Foundation/NSData.swift |
| 13 | +index 1a076bef..e1499ae2 100644 |
| 14 | +--- a/Sources/Foundation/NSData.swift |
| 15 | ++++ b/Sources/Foundation/NSData.swift |
| 16 | +@@ -433,10 +433,12 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding { |
| 17 | + #if os(WASI) |
| 18 | + // WASI does not have permission concept |
| 19 | + let permissions: Int? = nil |
| 20 | ++ var atomicWrite: Bool { false } |
| 21 | + #else |
| 22 | + let permissions = try? fm.attributesOfItem(atPath: path)[.posixPermissions] as? Int |
| 23 | ++ let atomicWrite = writeOptionsMask.contains(.atomic) |
| 24 | + #endif |
| 25 | +- if writeOptionsMask.contains(.atomic) { |
| 26 | ++ if atomicWrite { |
| 27 | + let (newFD, auxFilePath) = try _NSCreateTemporaryFile(path) |
| 28 | + let fh = FileHandle(fileDescriptor: newFD, closeOnDealloc: true) |
| 29 | + do { |
| 30 | +@@ -489,7 +491,12 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding { |
| 31 | + /// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes |
| 32 | + open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool { |
| 33 | + do { |
| 34 | +- try write(toFile: path, options: useAuxiliaryFile ? .atomic : []) |
| 35 | ++ #if os(WASI) |
| 36 | ++ let options: WritingOptions = [] |
| 37 | ++ #else |
| 38 | ++ let options: WritingOptions = useAuxiliaryFile ? .atomic : [] |
| 39 | ++ #endif |
| 40 | ++ try write(toFile: path, options: options) |
| 41 | + } catch { |
| 42 | + return false |
| 43 | + } |
| 44 | +diff --git a/Sources/Foundation/NSString.swift b/Sources/Foundation/NSString.swift |
| 45 | +index ccd0ae08..fd1de6f3 100644 |
| 46 | +--- a/Sources/Foundation/NSString.swift |
| 47 | ++++ b/Sources/Foundation/NSString.swift |
| 48 | +@@ -1269,7 +1269,12 @@ extension NSString { |
| 49 | + internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws { |
| 50 | + var data = Data() |
| 51 | + try _getExternalRepresentation(&data, url, enc) |
| 52 | +- try data.write(to: url, options: useAuxiliaryFile ? .atomic : []) |
| 53 | ++ #if os(WASI) |
| 54 | ++ let options: Data.WritingOptions = [] |
| 55 | ++ #else |
| 56 | ++ let options: Data.WritingOptions = useAuxiliaryFile ? .atomic : [] |
| 57 | ++ #endif |
| 58 | ++ try data.write(to: url, options: options) |
| 59 | + } |
| 60 | + |
| 61 | + public func write(to url: URL, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws { |
| 62 | +-- |
| 63 | +2.48.1 |
| 64 | + |
0 commit comments