Skip to content

Commit fefb1bf

Browse files
[wasm] Gate atomic write option usages behind platform check (#5198)
`Data.WritingOptions.atomic` is now unavailable on WASI: swiftlang/swift-foundation#992
1 parent ad782e2 commit fefb1bf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Diff for: Sources/Foundation/NSData.swift

+20-1
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,13 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
433433
#if os(WASI)
434434
// WASI does not have permission concept
435435
let permissions: Int? = nil
436+
// ReadingOptions.atomic won't be specified on WASI as it's marked unavailable
437+
var atomicWrite: Bool { false }
436438
#else
437439
let permissions = try? fm.attributesOfItem(atPath: path)[.posixPermissions] as? Int
440+
let atomicWrite = writeOptionsMask.contains(.atomic)
438441
#endif
439-
if writeOptionsMask.contains(.atomic) {
442+
if atomicWrite {
440443
let (newFD, auxFilePath) = try _NSCreateTemporaryFile(path)
441444
let fh = FileHandle(fileDescriptor: newFD, closeOnDealloc: true)
442445
do {
@@ -487,22 +490,38 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
487490

488491
/// Writes the data object's bytes to the file specified by a given path.
489492
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
493+
#if os(WASI)
494+
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
495+
#endif
490496
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
497+
#if os(WASI)
498+
// WASI does not support atomic file-writing as it does not have temporary directories
499+
return false
500+
#else
491501
do {
492502
try write(toFile: path, options: useAuxiliaryFile ? .atomic : [])
493503
} catch {
494504
return false
495505
}
496506
return true
507+
#endif
497508
}
498509

499510
/// Writes the data object's bytes to the location specified by a given URL.
500511
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
512+
#if os(WASI)
513+
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
514+
#endif
501515
open func write(to url: URL, atomically: Bool) -> Bool {
516+
#if os(WASI)
517+
// WASI does not support atomic file-writing as it does not have temporary directories
518+
return false
519+
#else
502520
if url.isFileURL {
503521
return write(toFile: url.path, atomically: atomically)
504522
}
505523
return false
524+
#endif
506525
}
507526

508527
/// Writes the data object's bytes to the location specified by a given URL.

Diff for: Sources/Foundation/NSString.swift

+13
Original file line numberDiff line numberDiff line change
@@ -1266,16 +1266,29 @@ extension NSString {
12661266
data = mData
12671267
}
12681268

1269+
#if os(WASI)
1270+
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
1271+
#endif
12691272
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
1273+
#if os(WASI)
1274+
throw CocoaError(.featureUnsupported)
1275+
#else
12701276
var data = Data()
12711277
try _getExternalRepresentation(&data, url, enc)
12721278
try data.write(to: url, options: useAuxiliaryFile ? .atomic : [])
1279+
#endif
12731280
}
12741281

1282+
#if os(WASI)
1283+
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
1284+
#endif
12751285
public func write(to url: URL, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {
12761286
try _writeTo(url, useAuxiliaryFile, enc)
12771287
}
12781288

1289+
#if os(WASI)
1290+
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
1291+
#endif
12791292
public func write(toFile path: String, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {
12801293
try _writeTo(URL(fileURLWithPath: path), useAuxiliaryFile, enc)
12811294
}

0 commit comments

Comments
 (0)