@@ -142,6 +142,9 @@ private func cleanupTemporaryDirectory(at inPath: String?) {
142142}
143143
144144/// Caller is responsible for calling `close` on the `Int32` file descriptor.
145+ #if os(WASI)
146+ @available ( * , unavailable, message: " WASI does not have temporary directories " )
147+ #endif
145148private func createTemporaryFile( at destinationPath: String , inPath: PathOrURL , prefix: String , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String ) {
146149#if os(WASI)
147150 // WASI does not have temp directories
@@ -206,7 +209,14 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL,
206209
207210/// Returns `(file descriptor, temporary file path, temporary directory path)`
208211/// 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.
212+ #if os(WASI)
213+ @available ( * , unavailable, message: " WASI does not have temporary directories " )
214+ #endif
209215private func createProtectedTemporaryFile( at destinationPath: String , inPath: PathOrURL , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String , String ? ) {
216+ #if os(WASI)
217+ // WASI does not have temp directories
218+ throw CocoaError ( . featureUnsupported)
219+ #else
210220#if FOUNDATION_FRAMEWORK
211221 if _foundation_sandbox_check ( getpid ( ) , nil ) != 0 {
212222 // Convert the path back into a string
@@ -248,6 +258,7 @@ private func createProtectedTemporaryFile(at destinationPath: String, inPath: Pa
248258 let temporaryDirectoryPath = destinationPath. deletingLastPathComponent ( )
249259 let ( fd, auxFile) = try createTemporaryFile ( at: temporaryDirectoryPath, inPath: inPath, prefix: " .dat.nosync " , options: options, variant: variant)
250260 return ( fd, auxFile, nil )
261+ #endif // os(WASI)
251262}
252263
253264private func write( buffer: UnsafeRawBufferPointer , toFileDescriptor fd: Int32 , path: PathOrURL , parentProgress: Progress ? ) throws {
@@ -322,15 +333,26 @@ internal func writeToFile(path inPath: PathOrURL, data: Data, options: Data.Writ
322333}
323334
324335internal func writeToFile( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] = [ : ] , reportProgress: Bool = false ) throws {
336+ #if os(WASI) // `.atomic` is unavailable on WASI
337+ try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
338+ #else
325339 if options. contains ( . atomic) {
326340 try writeToFileAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
327341 } else {
328342 try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
329343 }
344+ #endif
330345}
331346
332347/// Create a new file out of `Data` at a path, using atomic writing.
348+ #if os(WASI)
349+ @available ( * , unavailable, message: " atomic writing is unavailable in WASI because temporary files are not supported " )
350+ #endif
333351private func writeToFileAux( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] , reportProgress: Bool ) throws {
352+ #if os(WASI)
353+ // `.atomic` is unavailable on WASI
354+ throw CocoaError ( . featureUnsupported)
355+ #else
334356 assert ( options. contains ( . atomic) )
335357
336358 // TODO: Somehow avoid copying back and forth to a String to hold the path
@@ -503,7 +525,6 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
503525
504526 cleanupTemporaryDirectory ( at: temporaryDirectoryPath)
505527
506- #if !os(WASI) // WASI does not support fchmod for now
507528 if let mode {
508529 // Try to change the mode if the path has not changed. Do our best, but don't report an error.
509530#if FOUNDATION_FRAMEWORK
@@ -527,16 +548,18 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
527548 fchmod ( fd, mode)
528549#endif
529550 }
530- #endif // os(WASI)
531551 }
532552 }
533553 }
534554#endif
555+ #endif // os(WASI)
535556}
536557
537558/// Create a new file out of `Data` at a path, not using atomic writing.
538559private func writeToFileNoAux( path inPath: PathOrURL , buffer: UnsafeRawBufferPointer , options: Data . WritingOptions , attributes: [ String : Data ] , reportProgress: Bool ) throws {
560+ #if !os(WASI) // `.atomic` is unavailable on WASI
539561 assert ( !options. contains ( . atomic) )
562+ #endif
540563
541564#if os(Windows)
542565 try inPath. path. withNTPathRepresentation { pwszPath in
0 commit comments