-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathFileSystemProtocol.swift
More file actions
823 lines (774 loc) · 36.7 KB
/
Copy pathFileSystemProtocol.swift
File metadata and controls
823 lines (774 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import SystemPackage
/// The interface for interacting with a file system.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public protocol FileSystemProtocol: Sendable {
/// The type of ``ReadableFileHandleProtocol`` to return when opening files for reading.
associatedtype ReadFileHandle: ReadableFileHandleProtocol
/// The type of ``WritableFileHandleProtocol`` to return when opening files for writing.
associatedtype WriteFileHandle: WritableFileHandleProtocol
/// The type of ``ReadableAndWritableFileHandleProtocol`` to return when opening files for reading and writing.
associatedtype ReadWriteFileHandle: ReadableAndWritableFileHandleProtocol
/// The type of ``DirectoryFileHandleProtocol`` to return when opening directories.
associatedtype DirectoryFileHandle: DirectoryFileHandleProtocol
where
DirectoryFileHandle.ReadFileHandle == ReadFileHandle,
DirectoryFileHandle.ReadWriteFileHandle == ReadWriteFileHandle,
DirectoryFileHandle.WriteFileHandle == WriteFileHandle
// MARK: - File access
/// Opens the file at `path` for reading and returns a handle to it.
///
/// The file being opened must exist otherwise this function will throw a ``FileSystemError``
/// with code ``FileSystemError/Code-swift.struct/notFound``.
///
/// - Parameters:
/// - path: The path of the file to open.
/// - options: How the file should be opened.
/// - Returns: A readable handle to the opened file.
func openFile(
forReadingAt path: FilePath,
options: OpenOptions.Read
) async throws -> ReadFileHandle
/// Opens the file at `path` for writing and returns a handle to it.
///
/// - Parameters:
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
/// - Returns: A writable handle to the opened file.
func openFile(
forWritingAt path: FilePath,
options: OpenOptions.Write
) async throws -> WriteFileHandle
/// Opens the file at `path` for reading and writing and returns a handle to it.
///
/// - Parameters:
/// - path: The path of the file to open relative to the open file.
/// - options: How the file should be opened.
func openFile(
forReadingAndWritingAt path: FilePath,
options: OpenOptions.Write
) async throws -> ReadWriteFileHandle
/// Opens the directory at `path` and returns a handle to it.
///
/// The directory being opened must already exist otherwise this function will throw an error.
/// Use ``createDirectory(at:withIntermediateDirectories:permissions:)`` to create directories.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - options: How the directory should be opened.
/// - Returns: A handle to the opened directory.
func openDirectory(
atPath path: FilePath,
options: OpenOptions.Directory
) async throws -> DirectoryFileHandle
/// Create a directory at the given path.
///
/// If a directory (or file) already exists at `path` then an error will be thrown. If
/// `createIntermediateDirectories` is `false` then the full prefix of `path` must already
/// exist. If set to `true` then all intermediate directories will be created.
///
/// Related system calls: `mkdir(2)`.
///
/// - Parameters:
/// - path: The directory to create.
/// - createIntermediateDirectories: Whether intermediate directories should be created.
/// - permissions: The permissions to set on the new directory; default permissions will be
/// used if not specified.
func createDirectory(
at path: FilePath,
withIntermediateDirectories createIntermediateDirectories: Bool,
permissions: FilePermissions?
) async throws
// MARK: - Common directories
/// Returns the current working directory.
var currentWorkingDirectory: FilePath { get async throws }
/// Returns the current user's home directory.
var homeDirectory: FilePath { get async throws }
/// Returns the path of the temporary directory.
var temporaryDirectory: FilePath { get async throws }
/// Create a temporary directory at the given path, from a template.
///
/// The template for the path of the temporary directory must end in at least
/// three 'X's, which will be replaced with a unique alphanumeric combination.
/// The template can contain intermediary directories which will be created
/// if they do not exist already.
///
/// Related system calls: `mkdir(2)`.
///
/// - Parameters:
/// - template: The template for the path of the temporary directory.
/// - Returns:
/// - The path to the new temporary directory.
func createTemporaryDirectory(
template: FilePath
) async throws -> FilePath
// MARK: - File information
/// Returns information about the file at the given path, if it exists; nil otherwise.
///
/// - Parameters:
/// - path: The path to get information about.
/// - infoAboutSymbolicLink: If the file is a symbolic link and this parameter is `true` then
/// information about the link will be returned, otherwise information about the
/// destination of the symbolic link is returned.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
func info(
forFileAt path: NIOFilePath,
infoAboutSymbolicLink: Bool
) async throws -> FileInfo?
// MARK: - Symbolic links
/// Creates a symbolic link that points to the destination.
///
/// If a file or directory exists at `path` then an error is thrown.
///
/// - Parameters:
/// - path: The path at which to create the symbolic link.
/// - destinationPath: The path that contains the item that the symbolic link points to.`
func createSymbolicLink(
at path: FilePath,
withDestination destinationPath: FilePath
) async throws
/// Returns the path of the item pointed to by a symbolic link.
///
/// - Parameter path: The path of a file or directory.
/// - Returns: The path of the file or directory to which the symbolic link points to.
func destinationOfSymbolicLink(
at path: FilePath
) async throws -> FilePath
// MARK: - File copying, removal, and moving
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy (when `replaceExisting` is `false`) or its parent directory does not exist.
///
/// Note that other errors may also be thrown.
///
/// If `sourcePath` is a symbolic link then only the link is copied. The copied file will
/// preserve permissions and any extended attributes (if supported by the file system).
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - copyStrategy: How to deal with concurrent aspects of the copy, only relevant to directories.
/// - replaceExisting: If `true`, atomically replace any existing file at `destinationPath`.
/// - shouldProceedAfterError: A closure which is executed to determine whether to continue
/// copying files if an error is encountered during the operation. See Errors section for full details.
/// - shouldCopyItem: A closure which is executed before each copy to determine whether each
/// item should be copied. See Filtering section for full details
///
/// #### Errors
///
/// No errors should be throw by implementors without first calling `shouldProceedAfterError`,
/// if that returns without throwing this is taken as permission to continue and the error is swallowed.
/// If instead the closure throws then ``copyItem(at:to:strategy:replaceExisting:shouldProceedAfterError:shouldCopyItem:)``
/// will throw and copying will stop, though the precise semantics of this can depend on the `strategy`.
///
/// if using ``CopyStrategy/parallel(maxDescriptors:)``
/// Already started work may continue for an indefinite period of time. In particular, after throwing an error
/// it is possible that invocations of `shouldCopyItem` may continue to occur!
///
/// If using ``CopyStrategy/sequential`` only one invocation of any of the `should*` closures will occur at a time,
/// and an error will immediately stop further activity.
///
/// The specific error thrown from copyItem is undefined, it does not have to be the same error thrown from
/// `shouldProceedAfterError`.
/// In the event of any errors (ignored or otherwise) implementations are under no obbligation to
/// attempt to 'tidy up' after themselves. The state of the file system within `destinationPath`
/// after an aborted copy should is undefined.
///
/// When calling `shouldProceedAfterError` implementations of this method
/// MUST:
/// - Do so once and only once per item.
/// - Not hold any locks when doing so.
/// MAY:
/// - invoke the function multiple times concurrently (except when using ``CopyStrategy/sequential``)
///
/// #### Filtering
///
/// When invoking `shouldCopyItem` implementations of this method
/// MUST:
/// - Do so once and only once per item.
/// - Do so before attempting any operations related to the copy (including determining if they can do so).
/// - Not hold any locks when doing so.
/// - Check parent directories *before* items within them,
/// * if a parent is ignored no items within it should be considered or checked
/// - Skip all contents of a directory which is filtered out.
/// - Invoke it for the `sourcePath` itself.
/// MAY:
/// - invoke the function multiple times concurrently (except when using ``CopyStrategy/sequential``)
/// - invoke the function an arbitrary point before actually trying to copy the file
func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
strategy copyStrategy: CopyStrategy,
replaceExisting: Bool,
shouldProceedAfterError:
@escaping @Sendable (
_ source: DirectoryEntry,
_ error: Error
) async throws -> Void,
shouldCopyItem:
@escaping @Sendable (
_ source: DirectoryEntry,
_ destination: FilePath
) async -> Bool
) async throws
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory and `removeItemRecursively` is `true` then the
/// contents of all of its subdirectories will be removed recursively before the directory at
/// `path`. Symbolic links are removed (but their targets are not deleted).
///
/// - Parameters:
/// - path: The path to delete.
/// - removalStrategy: Whether to delete files sequentially (one-by-one), or perform a
/// concurrent scan of the tree at `path` and delete files when they are found. Ignored if
/// the item being removed isn't a directory.
/// - removeItemRecursively: If the item being removed is a directory, remove it by
/// recursively removing its children. Setting this to `true` is synonymous with calling
/// `rm -r`, setting this false is synonymous to calling `rmdir`. Ignored if the item
/// being removed isn't a directory.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
func removeItem(
at path: FilePath,
strategy removalStrategy: RemovalStrategy,
recursively removeItemRecursively: Bool
) async throws -> Int
/// Moves the file or directory at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown.
///
/// If the file at `sourcePath` is a symbolic link then only the link is moved to the new path.
///
/// - Parameters:
/// - sourcePath: The path to the item to move.
/// - destinationPath: The path at which to place the item.
func moveItem(at sourcePath: FilePath, to destinationPath: FilePath) async throws
/// Replaces the item at `destinationPath` with the item at `existingPath`.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `existingPath` does
/// not exist,
/// - ``FileSystemError/Code-swift.struct/io`` if the file at `existingPath` was successfully
/// copied to `destinationPath` but an error occurred while removing it from `existingPath.`
///
/// Note that other errors may also be thrown.
///
/// The item at `destinationPath` is not required to exist. Note that it is possible to replace
/// a file with a directory and vice versa. After the file or directory at `destinationPath`
/// has been replaced, the item at `existingPath` will be removed.
///
/// > Note: This method behaves identically to ``FileSystemProtocol/moveItem(at:to:)``, except that it will replace
/// the item at `destinationPath` if it already exists. The item at `existingPath` is moved to the new location and
/// will no longer exist at its original path.
///
/// - Parameters:
/// - destinationPath: The path of the file or directory to replace.
/// - existingPath: The path of the existing file or directory.
func replaceItem(at destinationPath: FilePath, withItemAt existingPath: FilePath) async throws
}
// MARK: - Open existing files/directories
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension FileSystemProtocol {
/// Opens the file at the given path and provides scoped read-only access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns.
/// Files may also be opened in read-write or write-only mode by calling
/// ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)`` and
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
/// - options: How the file should be opened.
/// - execute: A closure which provides read-only access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forReadingAt path: FilePath,
options: OpenOptions.Read = OpenOptions.Read(),
execute: (_ read: ReadFileHandle) async throws -> Result
) async throws -> Result {
let handle = try await self.openFile(forReadingAt: path, options: options)
return try await withUncancellableTearDown {
try await execute(handle)
} tearDown: { _ in
try await handle.close()
}
}
/// Opens the file at the given path and provides scoped write-only access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the call returns.
/// Files may also be opened in read-write or write-only mode by calling
/// ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)`` and
/// ``FileSystemProtocol/withFileHandle(forWritingAt:options:execute:)``.
///
/// - Parameters:
/// - path: The path of the file to open for reading.
/// - options: How the file should be opened.
/// - execute: A closure which provides write-only access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forWritingAt path: FilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute: (_ write: WriteFileHandle) async throws -> Result
) async throws -> Result {
let handle = try await self.openFile(forWritingAt: path, options: options)
return try await withUncancellableTearDown {
try await execute(handle)
} tearDown: { result in
switch result {
case .success:
try await handle.close()
case .failure:
try await handle.close(makeChangesVisible: false)
}
}
}
/// Opens the file at the given path and provides scoped read-write access to it.
///
/// The file remains open during lifetime of the `execute` block and will be closed
/// automatically before the function returns.
/// Files may also be opened in read-only or
/// write-only mode by with ``FileSystemProtocol/withFileHandle(forReadingAt:options:execute:)`` and
/// ``FileSystemProtocol/withFileHandle(forReadingAndWritingAt:options:execute:)``.
///
/// - Parameters:
/// - path: The path of the file to open for reading and writing.
/// - options: How the file should be opened.
/// - execute: A closure which provides access to the open file. The file is closed
/// automatically after the closure exits.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withFileHandle<Result>(
forReadingAndWritingAt path: FilePath,
options: OpenOptions.Write = .newFile(replaceExisting: false),
execute: (_ readWrite: ReadWriteFileHandle) async throws -> Result
) async throws -> Result {
let handle = try await self.openFile(forReadingAndWritingAt: path, options: options)
return try await withUncancellableTearDown {
try await execute(handle)
} tearDown: { _ in
try await handle.close()
}
}
/// Opens the directory at the given path and provides scoped access to it.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - options: How the file should be opened.
/// - execute: A closure which provides access to the directory.
/// - Important: The handle passed to `execute` must not escape the closure.
/// - Returns: The result of the `execute` closure.
public func withDirectoryHandle<Result>(
atPath path: FilePath,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute: (_ directory: DirectoryFileHandle) async throws -> Result
) async throws -> Result {
let handle = try await self.openDirectory(atPath: path, options: options)
return try await withUncancellableTearDown {
try await execute(handle)
} tearDown: { _ in
try await handle.close()
}
}
}
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension FileSystemProtocol {
/// Opens the file at `path` for reading and returns a handle to it.
///
/// The file being opened must exist otherwise this function will throw a ``FileSystemError``
/// with code ``FileSystemError/Code-swift.struct/notFound``.
///
/// - Parameters:
/// - path: The path of the file to open.
/// - Returns: A readable handle to the opened file.
public func openFile(
forReadingAt path: FilePath
) async throws -> ReadFileHandle {
try await self.openFile(forReadingAt: path, options: OpenOptions.Read())
}
/// Opens the directory at `path` and returns a handle to it.
///
/// The directory being opened must already exist otherwise this function will throw an error.
/// Use ``createDirectory(at:withIntermediateDirectories:permissions:)`` to create directories.
///
/// - Parameters:
/// - path: The path of the directory to open.
/// - Returns: A handle to the opened directory.
public func openDirectory(
atPath path: FilePath
) async throws -> DirectoryFileHandle {
try await self.openDirectory(atPath: path, options: OpenOptions.Directory())
}
/// Returns information about the file at the given path, if it exists; nil otherwise.
///
/// Calls ``info(forFileAt:infoAboutSymbolicLink:)`` setting `infoAboutSymbolicLink` to `false`.
///
/// - Parameters:
/// - path: The path to get information about.
/// - Returns: Information about the file at the given path or `nil` if no file exists.
public func info(forFileAt path: NIOFilePath) async throws -> FileInfo? {
try await self.info(forFileAt: path, infoAboutSymbolicLink: false)
}
/// Copies the item at the specified path to a new location.
///
/// Calls through to
/// ``copyItem(at:to:strategy:replaceExisting:shouldProceedAfterError:shouldCopyItem:)``
/// with `replaceExisting` set to `false`.
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
strategy copyStrategy: CopyStrategy,
shouldProceedAfterError:
@escaping @Sendable (
_ source: DirectoryEntry,
_ error: Error
) async throws -> Void,
shouldCopyItem:
@escaping @Sendable (
_ source: DirectoryEntry,
_ destination: FilePath
) async -> Bool
) async throws {
try await self.copyItem(
at: sourcePath,
to: destinationPath,
strategy: copyStrategy,
replaceExisting: false,
shouldProceedAfterError: shouldProceedAfterError,
shouldCopyItem: shouldCopyItem
)
}
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown. If any error is encountered during the copy
/// then the copy is aborted. You can modify the behaviour with the `shouldProceedAfterError`
/// parameter of ``FileSystemProtocol/copyItem(at:to:strategy:replaceExisting:shouldProceedAfterError:shouldCopyItem:)``.
///
/// If the file at `sourcePath` is a symbolic link then only the link is copied to the new path.
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - copyStrategy: This controls the concurrency used if the file at `sourcePath` is a directory.
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
strategy copyStrategy: CopyStrategy = .platformDefault
) async throws {
try await self.copyItem(
at: sourcePath,
to: destinationPath,
strategy: copyStrategy,
replaceExisting: false,
shouldProceedAfterError: { _, error in
throw error
},
shouldCopyItem: { _, _ in
true
}
)
}
/// Copies the item at the specified path to a new location.
///
/// The item to be copied must be a:
/// - regular file,
/// - symbolic link, or
/// - directory.
///
/// If `sourcePath` is a symbolic link then only the link is copied. The copied file will
/// preserve permissions and any extended attributes (if supported by the file system).
///
/// #### Errors
///
/// Error codes thrown include:
/// - ``FileSystemError/Code-swift.struct/notFound`` if `sourcePath` doesn't exist.
/// - ``FileSystemError/Code-swift.struct/fileAlreadyExists`` if `destinationPath` exists.
///
/// #### Backward Compatibility details
///
/// This is implemented in terms of ``copyItem(at:to:strategy:replaceExisting:shouldProceedAfterError:shouldCopyItem:)``
/// using ``CopyStrategy/sequential`` to avoid changing the concurrency semantics of the should callbacks
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - shouldProceedAfterError: Determines whether to continue copying files if an error is
/// thrown during the operation. This error does not have to match the error passed
/// to the closure.
/// - shouldCopyFile: A closure which is executed before each file to determine whether the
/// file should be copied.
@available(*, deprecated, message: "please use copyItem overload taking CopyStrategy")
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
shouldProceedAfterError:
@escaping @Sendable (
_ entry: DirectoryEntry,
_ error: Error
) async throws -> Void,
shouldCopyFile:
@escaping @Sendable (
_ source: FilePath,
_ destination: FilePath
) async -> Bool
) async throws {
try await self.copyItem(
at: sourcePath,
to: destinationPath,
strategy: .sequential,
replaceExisting: false,
shouldProceedAfterError: shouldProceedAfterError,
shouldCopyItem: { (source, destination) in
await shouldCopyFile(source.path, destination)
}
)
}
/// Copies the item at the specified path to a new location.
///
/// The following error codes may be thrown:
/// - ``FileSystemError/Code-swift.struct/notFound`` if the item at `sourcePath` does not exist,
/// - ``FileSystemError/Code-swift.struct/invalidArgument`` if an item at `destinationPath`
/// exists prior to the copy or its parent directory does not exist.
///
/// Note that other errors may also be thrown.
///
/// If `sourcePath` is a symbolic link then only the link is copied. The copied file will
/// preserve permissions and any extended attributes (if supported by the file system).
///
/// - Parameters:
/// - sourcePath: The path to the item to copy.
/// - destinationPath: The path at which to place the copy.
/// - shouldProceedAfterError: A closure which is executed to determine whether to continue
/// copying files if an error is encountered during the operation. See Errors section for full details.
/// - shouldCopyItem: A closure which is executed before each copy to determine whether each
/// item should be copied. See Filtering section for full details
///
/// #### Parallelism
///
/// This overload uses ``CopyStrategy/platformDefault`` which is likely to result in multiple concurrency domains being used
/// in the event of copying a directory.
/// See the detailed description on ``copyItem(at:to:strategy:replaceExisting:shouldProceedAfterError:shouldCopyItem:)``
/// for the implications of this with respect to the `shouldProceedAfterError` and `shouldCopyItem` callbacks
public func copyItem(
at sourcePath: FilePath,
to destinationPath: FilePath,
shouldProceedAfterError:
@escaping @Sendable (
_ source: DirectoryEntry,
_ error: Error
) async throws -> Void,
shouldCopyItem:
@escaping @Sendable (
_ source: DirectoryEntry,
_ destination: FilePath
) async -> Bool
) async throws {
try await self.copyItem(
at: sourcePath,
to: destinationPath,
strategy: .platformDefault,
replaceExisting: false,
shouldProceedAfterError: shouldProceedAfterError,
shouldCopyItem: shouldCopyItem
)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory then the contents of all of its subdirectories will
/// be removed recursively before the directory at `path`. Symbolic links are removed (but their
/// targets are not deleted).
///
/// The strategy for deletion will be determined automatically depending on the discovered
/// platform.
///
/// - Parameters:
/// - path: The path to delete.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath
) async throws -> Int {
try await self.removeItem(at: path, strategy: .platformDefault, recursively: true)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory then the contents of all of its subdirectories will
/// be removed recursively before the directory at `path`. Symbolic links are removed (but their
/// targets are not deleted).
///
/// The strategy for deletion will be determined automatically depending on the discovered
/// platform.
///
/// - Parameters:
/// - path: The path to delete.
/// - removeItemRecursively: If the item being removed is a directory, remove it by
/// recursively removing its children. Setting this to `true` is synonymous with calling
/// `rm -r`, setting this false is synonymous to calling `rmdir`. Ignored if the item
/// being removed isn't a directory.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath,
recursively removeItemRecursively: Bool
) async throws -> Int {
try await self.removeItem(at: path, strategy: .platformDefault, recursively: removeItemRecursively)
}
/// Deletes the file or directory (and its contents) at `path`.
///
/// The item to be removed must be a regular file, symbolic link or directory. If no file exists
/// at the given path then this function returns zero.
///
/// If the item at the `path` is a directory then the contents of all of its subdirectories will
/// be removed recursively before the directory at `path`. Symbolic links are removed (but their
/// targets are not deleted).
///
/// - Parameters:
/// - path: The path to delete.
/// - removalStrategy: Whether to delete files sequentially (one-by-one), or perform a
/// concurrent scan of the tree at `path` and delete files when they are found.
/// - Returns: The number of deleted items which may be zero if `path` did not exist.
@discardableResult
public func removeItem(
at path: FilePath,
strategy removalStrategy: RemovalStrategy
) async throws -> Int {
try await self.removeItem(at: path, strategy: removalStrategy, recursively: true)
}
/// Create a directory at the given path.
///
/// If a directory (or file) already exists at `path` then an error will be thrown. If
/// `createIntermediateDirectories` is `false` then the full prefix of `path` must already
/// exist. If set to `true` then all intermediate directories will be created.
///
/// New directories will be given read-write-execute owner permissions and read-execute group
/// and other permissions.
///
/// Related system calls: `mkdir(2)`.
///
/// - Parameters:
/// - path: The directory to create.
/// - createIntermediateDirectories: Whether intermediate directories should be created.
public func createDirectory(
at path: FilePath,
withIntermediateDirectories createIntermediateDirectories: Bool
) async throws {
try await self.createDirectory(
at: path,
withIntermediateDirectories: createIntermediateDirectories,
permissions: .defaultsForDirectory
)
}
/// Create a temporary directory and removes it once the function returns.
///
/// You can use `prefix` to specify the directory in which the temporary directory should
/// be created. If `prefix` is `nil` then the value of ``temporaryDirectory`` is used as
/// the prefix.
///
/// The temporary directory, and all of its contents, is removed once `execute` returns.
///
/// - Parameters:
/// - prefix: The prefix to use for the path of the temporary directory.
/// - options: Options used to create the directory.
/// - execute: A closure which provides access to the directory and its path.
/// - Returns: The result of `execute`.
public func withTemporaryDirectory<Result>(
prefix: FilePath? = nil,
options: OpenOptions.Directory = OpenOptions.Directory(),
execute: (_ directory: DirectoryFileHandle, _ path: FilePath) async throws -> Result
) async throws -> Result {
let template: FilePath
if let prefix = prefix {
template = prefix.appending("XXXXXXXX")
} else {
template = try await self.temporaryDirectory.appending("XXXXXXXX")
}
let directory = try await self.createTemporaryDirectory(template: template)
return try await withUncancellableTearDown {
try await withDirectoryHandle(atPath: directory, options: options) { handle in
try await execute(handle, directory)
}
} tearDown: { _ in
try await self.removeItem(at: directory, strategy: .platformDefault, recursively: true)
}
}
}
// MARK: - File attributes
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension FileSystemProtocol {
/// Sets the file's last access and last data modification times to the given times.
///
/// - Parameters:
/// - path: The path of the file to modify.
/// - lastAccess: The new value of the file's last access time, as time elapsed since the Epoch.
/// - lastDataModification: The new value of the file's last data modification time, as time elapsed since the Epoch.
public func setTimes(
forFileAt path: NIOFilePath,
lastAccess: FileInfo.Timespec?,
lastDataModification: FileInfo.Timespec?
) async throws {
try await self.withFileHandle(forReadingAt: path) { handle in
try await handle.setTimes(lastAccess: lastAccess, lastDataModification: lastDataModification)
}
}
/// Sets the file's last access time to the given time.
///
/// - Parameters:
/// - path: The path of the file to modify.
/// - time: The time to which the file's last access time should be set.
public func setLastAccessTime(
forFileAt path: NIOFilePath,
to time: FileInfo.Timespec
) async throws {
try await self.setTimes(forFileAt: path, lastAccess: time, lastDataModification: nil)
}
/// Sets the file's last data modification time to the given time.
///
/// - Parameters:
/// - path: The path of the file to modify.
/// - time: The time to which the file's last data modification time should be set.
public func setLastDataModificationTime(
forFileAt path: NIOFilePath,
to time: FileInfo.Timespec
) async throws {
try await self.setTimes(forFileAt: path, lastAccess: nil, lastDataModification: time)
}
}