@@ -136,6 +136,9 @@ private func cleanupTemporaryDirectory(at inPath: String?) {
136136}
137137
138138/// Caller is responsible for calling `close` on the `Int32` file descriptor.
139+ #if os(WASI)
140+ @available ( * , unavailable, message: " WASI does not have temporary directories " )
141+ #endif
139142private func createTemporaryFile( at destinationPath: String , inPath: PathOrURL , prefix: String , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String ) {
140143#if os(WASI)
141144 // WASI does not have temp directories
@@ -200,7 +203,14 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL,
200203
201204/// Returns `(file descriptor, temporary file path, temporary directory path)`
202205/// Caller is responsible for calling `close` on the `Int32` file descriptor and calling `cleanupTemporaryDirectory` on the temporary directory path. The temporary directory path may be nil, if it does not need to be cleaned up.
206+ #if os(WASI)
207+ @available ( * , unavailable, message: " WASI does not have temporary directories " )
208+ #endif
203209private func createProtectedTemporaryFile( at destinationPath: String , inPath: PathOrURL , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String , String ? ) {
210+ #if os(WASI)
211+ // WASI does not have temp directories
212+ throw CocoaError ( . featureUnsupported)
213+ #else
204214#if FOUNDATION_FRAMEWORK
205215 if _foundation_sandbox_check ( getpid ( ) , nil ) != 0 {
206216 // Convert the path back into a string
@@ -240,6 +250,7 @@ private func createProtectedTemporaryFile(at destinationPath: String, inPath: Pa
240250 let temporaryDirectoryPath = destinationPath. deletingLastPathComponent ( )
241251 let ( fd, auxFile) = try createTemporaryFile ( at: temporaryDirectoryPath, inPath: inPath, prefix: " .dat.nosync " , options: options, variant: variant)
242252 return ( fd, auxFile, nil )
253+ #endif // os(WASI)
243254}
244255
245256private func write( buffer: UnsafeRawBufferPointer , toFileDescriptor fd: Int32 , path: PathOrURL , parentProgress: Progress ? ) throws {
@@ -314,15 +325,26 @@ internal func writeToFile(path inPath: PathOrURL, data: Data, options: Data.Writ
314325}
315326
316327internal func writeToFile( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] = [ : ] , reportProgress: Bool = false ) throws {
328+ #if os(WASI) // `.atomic` is unavailable on WASI
329+ try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
330+ #else
317331 if options. contains ( . atomic) {
318332 try writeToFileAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
319333 } else {
320334 try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
321335 }
336+ #endif
322337}
323338
324339/// Create a new file out of `Data` at a path, using atomic writing.
340+ #if os(WASI)
341+ @available ( * , unavailable, message: " atomic writing is unavailable in WASI because temporary files are not supported " )
342+ #endif
325343private func writeToFileAux( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] , reportProgress: Bool ) throws {
344+ #if os(WASI)
345+ // `.atomic` is unavailable on WASI
346+ throw CocoaError ( . featureUnsupported)
347+ #else
326348 assert ( options. contains ( . atomic) )
327349
328350 // TODO: Somehow avoid copying back and forth to a String to hold the path
@@ -495,7 +517,6 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
495517
496518 cleanupTemporaryDirectory ( at: temporaryDirectoryPath)
497519
498- #if !os(WASI) // WASI does not support fchmod for now
499520 if let mode {
500521 // Try to change the mode if the path has not changed. Do our best, but don't report an error.
501522#if FOUNDATION_FRAMEWORK
@@ -519,16 +540,18 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
519540 fchmod ( fd, mode)
520541#endif
521542 }
522- #endif // os(WASI)
523543 }
524544 }
525545 }
526546#endif
547+ #endif // os(WASI)
527548}
528549
529550/// Create a new file out of `Data` at a path, not using atomic writing.
530551private func writeToFileNoAux( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] , reportProgress: Bool ) throws {
552+ #if !os(WASI) // `.atomic` is unavailable on WASI
531553 assert ( !options. contains ( . atomic) )
554+ #endif
532555
533556#if os(Windows)
534557 try inPath. path. withNTPathRepresentation { pwszPath in
0 commit comments