@@ -140,6 +140,9 @@ private func cleanupTemporaryDirectory(at inPath: String?) {
140
140
}
141
141
142
142
/// 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
143
146
private func createTemporaryFile( at destinationPath: String , inPath: PathOrURL , prefix: String , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String ) {
144
147
#if os(WASI)
145
148
// WASI does not have temp directories
@@ -204,7 +207,14 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL,
204
207
205
208
/// Returns `(file descriptor, temporary file path, temporary directory path)`
206
209
/// 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
207
213
private 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
208
218
#if FOUNDATION_FRAMEWORK
209
219
if _foundation_sandbox_check ( getpid ( ) , nil ) != 0 {
210
220
// Convert the path back into a string
@@ -244,6 +254,7 @@ private func createProtectedTemporaryFile(at destinationPath: String, inPath: Pa
244
254
let temporaryDirectoryPath = destinationPath. deletingLastPathComponent ( )
245
255
let ( fd, auxFile) = try createTemporaryFile ( at: temporaryDirectoryPath, inPath: inPath, prefix: " .dat.nosync " , options: options, variant: variant)
246
256
return ( fd, auxFile, nil )
257
+ #endif // os(WASI)
247
258
}
248
259
249
260
private 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
318
329
}
319
330
320
331
internal 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
321
335
if options. contains ( . atomic) {
322
336
try writeToFileAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
323
337
} else {
324
338
try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
325
339
}
340
+ #endif
326
341
}
327
342
328
343
/// 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
329
347
private 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
330
352
assert ( options. contains ( . atomic) )
331
353
332
354
// 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
499
521
500
522
cleanupTemporaryDirectory ( at: temporaryDirectoryPath)
501
523
502
- #if !os(WASI) // WASI does not support fchmod for now
503
524
if let mode {
504
525
// Try to change the mode if the path has not changed. Do our best, but don't report an error.
505
526
#if FOUNDATION_FRAMEWORK
@@ -523,16 +544,18 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
523
544
fchmod ( fd, mode)
524
545
#endif
525
546
}
526
- #endif // os(WASI)
527
547
}
528
548
}
529
549
}
530
550
#endif
551
+ #endif // os(WASI)
531
552
}
532
553
533
554
/// Create a new file out of `Data` at a path, not using atomic writing.
534
555
private 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
535
557
assert ( !options. contains ( . atomic) )
558
+ #endif
536
559
537
560
#if os(Windows)
538
561
try inPath. path. withNTPathRepresentation { pwszPath in
0 commit comments