@@ -142,6 +142,9 @@ private func cleanupTemporaryDirectory(at inPath: String?) {
142
142
}
143
143
144
144
/// 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
145
148
private func createTemporaryFile( at destinationPath: String , inPath: PathOrURL , prefix: String , options: Data . WritingOptions , variant: String ? = nil ) throws -> ( Int32 , String ) {
146
149
#if os(WASI)
147
150
// WASI does not have temp directories
@@ -206,7 +209,14 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL,
206
209
207
210
/// Returns `(file descriptor, temporary file path, temporary directory path)`
208
211
/// 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
209
215
private 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
210
220
#if FOUNDATION_FRAMEWORK
211
221
if _foundation_sandbox_check ( getpid ( ) , nil ) != 0 {
212
222
// Convert the path back into a string
@@ -248,6 +258,7 @@ private func createProtectedTemporaryFile(at destinationPath: String, inPath: Pa
248
258
let temporaryDirectoryPath = destinationPath. deletingLastPathComponent ( )
249
259
let ( fd, auxFile) = try createTemporaryFile ( at: temporaryDirectoryPath, inPath: inPath, prefix: " .dat.nosync " , options: options, variant: variant)
250
260
return ( fd, auxFile, nil )
261
+ #endif // os(WASI)
251
262
}
252
263
253
264
private 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
322
333
}
323
334
324
335
internal 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
325
339
if options. contains ( . atomic) {
326
340
try writeToFileAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
327
341
} else {
328
342
try writeToFileNoAux ( path: inPath, buffer: buffer, options: options, attributes: attributes, reportProgress: reportProgress)
329
343
}
344
+ #endif
330
345
}
331
346
332
347
/// 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
333
351
private 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
334
356
assert ( options. contains ( . atomic) )
335
357
336
358
// 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
503
525
504
526
cleanupTemporaryDirectory ( at: temporaryDirectoryPath)
505
527
506
- #if !os(WASI) // WASI does not support fchmod for now
507
528
if let mode {
508
529
// Try to change the mode if the path has not changed. Do our best, but don't report an error.
509
530
#if FOUNDATION_FRAMEWORK
@@ -527,16 +548,18 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
527
548
fchmod ( fd, mode)
528
549
#endif
529
550
}
530
- #endif // os(WASI)
531
551
}
532
552
}
533
553
}
534
554
#endif
555
+ #endif // os(WASI)
535
556
}
536
557
537
558
/// Create a new file out of `Data` at a path, not using atomic writing.
538
559
private 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
539
561
assert ( !options. contains ( . atomic) )
562
+ #endif
540
563
541
564
#if os(Windows)
542
565
try inPath. path. withNTPathRepresentation { pwszPath in
0 commit comments