@@ -140,6 +140,9 @@ private func cleanupTemporaryDirectory(at inPath: String?) {
140140}
141141
142142/// Caller is responsible for calling `close` on the `Int32` file descriptor.
143+ #if os(WASI)
144+ @available ( * , unavailable, message: " WASI does not have temporary directories " )
145+ #endif
143146private func createTemporaryFile( at destinationPath: String , inPath: PathOrURL , prefix: String , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String ) {
144147#if os(WASI)
145148 // WASI does not have temp directories
@@ -204,7 +207,14 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL,
204207
205208/// Returns `(file descriptor, temporary file path, temporary directory path)`
206209/// 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.
210+ #if os(WASI)
211+ @available ( * , unavailable, message: " WASI does not have temporary directories " )
212+ #endif
207213private func createProtectedTemporaryFile( at destinationPath: String , inPath: PathOrURL , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String , String ? ) {
214+ #if os(WASI)
215+ // WASI does not have temp directories
216+ throw CocoaError ( . featureUnsupported)
217+ #else
208218#if FOUNDATION_FRAMEWORK
209219 if _foundation_sandbox_check ( getpid ( ) , nil ) != 0 {
210220 // Convert the path back into a string
@@ -244,6 +254,7 @@ private func createProtectedTemporaryFile(at destinationPath: String, inPath: Pa
244254 let temporaryDirectoryPath = destinationPath. deletingLastPathComponent ( )
245255 let ( fd, auxFile) = try createTemporaryFile ( at: temporaryDirectoryPath, inPath: inPath, prefix: " .dat.nosync " , options: options, variant: variant)
246256 return ( fd, auxFile, nil )
257+ #endif // os(WASI)
247258}
248259
249260private func write( buffer: UnsafeRawBufferPointer , toFileDescriptor fd: Int32 , path: PathOrURL , parentProgress: Progress ? ) throws {
@@ -318,15 +329,26 @@ internal func writeToFile(path inPath: PathOrURL, data: Data, options: Data.Writ
318329}
319330
320331internal func writeToFile( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] = [ : ] , reportProgress: Bool = false ) throws {
332+ #if os(WASI) // `.atomic` is unavailable on WASI
333+ try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
334+ #else
321335 if options. contains ( . atomic) {
322336 try writeToFileAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
323337 } else {
324338 try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
325339 }
340+ #endif
326341}
327342
328343/// Create a new file out of `Data` at a path, using atomic writing.
344+ #if os(WASI)
345+ @available ( * , unavailable, message: " atomic writing is unavailable in WASI because temporary files are not supported " )
346+ #endif
329347private func writeToFileAux( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] , reportProgress: Bool ) throws {
348+ #if os(WASI)
349+ // `.atomic` is unavailable on WASI
350+ throw CocoaError ( . featureUnsupported)
351+ #else
330352 assert ( options. contains ( . atomic) )
331353
332354 // TODO: Somehow avoid copying back and forth to a String to hold the path
@@ -499,7 +521,6 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
499521
500522 cleanupTemporaryDirectory ( at: temporaryDirectoryPath)
501523
502- #if !os(WASI) // WASI does not support fchmod for now
503524 if let mode {
504525 // Try to change the mode if the path has not changed. Do our best, but don't report an error.
505526#if FOUNDATION_FRAMEWORK
@@ -523,16 +544,18 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
523544 fchmod ( fd, mode)
524545#endif
525546 }
526- #endif // os(WASI)
527547 }
528548 }
529549 }
530550#endif
551+ #endif // os(WASI)
531552}
532553
533554/// Create a new file out of `Data` at a path, not using atomic writing.
534555private func writeToFileNoAux( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] , reportProgress: Bool ) throws {
556+ #if !os(WASI) // `.atomic` is unavailable on WASI
535557 assert ( !options. contains ( . atomic) )
558+ #endif
536559
537560#if os(Windows)
538561 try inPath. path. withNTPathRepresentation { pwszPath in
0 commit comments