Skip to content

[wasm] Gate atomic write option usages behind platform check #5198

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

Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion Sources/Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,13 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
#if os(WASI)
// WASI does not have permission concept
let permissions: Int? = nil
// ReadingOptions.atomic won't be specified on WASI as it's marked unavailable
var atomicWrite: Bool { false }
#else
let permissions = try? fm.attributesOfItem(atPath: path)[.posixPermissions] as? Int
let atomicWrite = writeOptionsMask.contains(.atomic)
#endif
if writeOptionsMask.contains(.atomic) {
if atomicWrite {
let (newFD, auxFilePath) = try _NSCreateTemporaryFile(path)
let fh = FileHandle(fileDescriptor: newFD, closeOnDealloc: true)
do {
Expand Down Expand Up @@ -487,22 +490,38 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {

/// Writes the data object's bytes to the file specified by a given path.
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
#if os(WASI)
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
#endif
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
#if os(WASI)
// WASI does not support atomic file-writing as it does not have temporary directories
return false
#else
do {
try write(toFile: path, options: useAuxiliaryFile ? .atomic : [])
} catch {
return false
}
return true
#endif
}

/// Writes the data object's bytes to the location specified by a given URL.
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
#if os(WASI)
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
#endif
open func write(to url: URL, atomically: Bool) -> Bool {
#if os(WASI)
// WASI does not support atomic file-writing as it does not have temporary directories
return false
#else
if url.isFileURL {
return write(toFile: url.path, atomically: atomically)
}
return false
#endif
}

/// Writes the data object's bytes to the location specified by a given URL.
Expand Down
13 changes: 13 additions & 0 deletions Sources/Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1266,16 +1266,29 @@ extension NSString {
data = mData
}

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

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

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