@@ -136,6 +136,9 @@ private func cleanupTemporaryDirectory(at inPath: String?) {
136
136
}
137
137
138
138
/// 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
139
142
private func createTemporaryFile( at destinationPath: String , inPath: PathOrURL , prefix: String , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String ) {
140
143
#if os(WASI)
141
144
// WASI does not have temp directories
@@ -200,7 +203,14 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL,
200
203
201
204
/// Returns `(file descriptor, temporary file path, temporary directory path)`
202
205
/// 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
203
209
private 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
204
214
#if FOUNDATION_FRAMEWORK
205
215
if _foundation_sandbox_check ( getpid ( ) , nil ) != 0 {
206
216
// Convert the path back into a string
@@ -240,6 +250,7 @@ private func createProtectedTemporaryFile(at destinationPath: String, inPath: Pa
240
250
let temporaryDirectoryPath = destinationPath. deletingLastPathComponent ( )
241
251
let ( fd, auxFile) = try createTemporaryFile ( at: temporaryDirectoryPath, inPath: inPath, prefix: " .dat.nosync " , options: options, variant: variant)
242
252
return ( fd, auxFile, nil )
253
+ #endif // os(WASI)
243
254
}
244
255
245
256
private 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
314
325
}
315
326
316
327
internal 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
317
331
if options. contains ( . atomic) {
318
332
try writeToFileAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
319
333
} else {
320
334
try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
321
335
}
336
+ #endif
322
337
}
323
338
324
339
/// 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
325
343
private 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
326
348
assert ( options. contains ( . atomic) )
327
349
328
350
// 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
495
517
496
518
cleanupTemporaryDirectory ( at: temporaryDirectoryPath)
497
519
498
- #if !os(WASI) // WASI does not support fchmod for now
499
520
if let mode {
500
521
// Try to change the mode if the path has not changed. Do our best, but don't report an error.
501
522
#if FOUNDATION_FRAMEWORK
@@ -519,16 +540,18 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
519
540
fchmod ( fd, mode)
520
541
#endif
521
542
}
522
- #endif // os(WASI)
523
543
}
524
544
}
525
545
}
526
546
#endif
547
+ #endif // os(WASI)
527
548
}
528
549
529
550
/// Create a new file out of `Data` at a path, not using atomic writing.
530
551
private 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
531
553
assert ( !options. contains ( . atomic) )
554
+ #endif
532
555
533
556
#if os(Windows)
534
557
try inPath. path. withNTPathRepresentation { pwszPath in
0 commit comments