File: FileManager%2BFiles.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (987 lines) | stat: -rw-r--r-- 40,712 bytes parent folder | download
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if FOUNDATION_FRAMEWORK
internal import Foundation_Private.NSFileManager
internal import DarwinPrivate.sys.content_protection
#endif

#if canImport(Darwin)
import Darwin
#elseif os(Android)
import Android
import posix_filesystem
#elseif canImport(Glibc)
import Glibc
internal import _FoundationCShims
#elseif canImport(Musl)
import Musl
internal import _FoundationCShims
#elseif os(Windows)
import CRT
import WinSDK
#elseif os(WASI)
internal import _FoundationCShims
import WASILibc
#endif

extension Date {
    fileprivate init(seconds: TimeInterval, nanoSeconds: TimeInterval) {
        self.init(timeIntervalSinceReferenceDate: seconds - Self.timeIntervalBetween1970AndReferenceDate + nanoSeconds / 1_000_000_000.0 )
    }
}

#if !os(Windows)
extension mode_t {
    private var _fileType: FileAttributeType {
        switch self & S_IFMT {
        case S_IFCHR: .typeCharacterSpecial
        case S_IFDIR: .typeDirectory
        case S_IFBLK: .typeBlockSpecial
        case S_IFREG: .typeRegular
        case S_IFLNK: .typeSymbolicLink
        case S_IFSOCK: .typeSocket
        default: .typeUnknown
        }
    }
    
    #if FOUNDATION_FRAMEWORK
    // Since FileAttributeType is an NS_TYPED_ENUM, clients rely on being able to cast values to both String and FileAttributeType
    // Store NSString values in attribute dictionaries to support both of these casting behaviors
    fileprivate var fileType: NSString { _fileType as NSString }
    #else
    // In swift-foundation, use FileAttributeType values instead since NSString doesn't exist
    fileprivate var fileType: FileAttributeType { _fileType }
    #endif
}
#endif

func _readFileAttributePrimitive<T: BinaryInteger>(_ value: Any?, as type: T.Type) -> T? {
    guard let value else { return nil }
    #if FOUNDATION_FRAMEWORK
    if let nsNumber = value as? NSNumber, let result = nsNumber as? T {
        return result
    }
    #endif
    
    if let exact = value as? T {
        return exact
    } else if let binInt = value as? (any BinaryInteger), let result = T(exactly: binInt) {
        return result
    }
    return nil
}

func _readFileAttributePrimitive(_ value: Any?, as type: Bool.Type) -> Bool? {
    guard let value else { return nil }
    #if FOUNDATION_FRAMEWORK
    if let nsNumber = value as? NSNumber, let result = nsNumber as? Bool {
        return result
    }
    #endif
    
    if let boolValue = value as? Bool {
        return boolValue
    } else if let binInt = value as? (any BinaryInteger), let result = Int(exactly: binInt) {
        switch result {
        case 0: return false
        case 1: return true
        default: return nil
        }
    }
    return nil
}

#if !FOUNDATION_FRAMEWORK
@_spi(SwiftCorelibsFoundation)
public protocol _NSNumberInitializer {
    static func initialize(value: Bool) -> Any
    static func initialize(value: some BinaryInteger) -> Any
}

@_spi(SwiftCorelibsFoundation)
dynamic public func _nsNumberInitializer() -> (any _NSNumberInitializer.Type)? {
    // Dynamically replaced by swift-corelibs-foundation
    return nil
}
#endif

func _writeFileAttributePrimitive<T: BinaryInteger, U: BinaryInteger>(_ value: T, as type: U.Type) -> Any {
    #if FOUNDATION_FRAMEWORK
    if let int = Int64(exactly: value) {
        NSNumber(value: int)
    } else {
        NSNumber(value: UInt64(value))
    }
    #else
    if let ns = _nsNumberInitializer()?.initialize(value: value) {
        return ns
    } else {
        return U(value)
    }
    #endif
}

func _writeFileAttributePrimitive(_ value: Bool) -> Any {
    #if FOUNDATION_FRAMEWORK
    NSNumber(value: value)
    #else
    if let ns = _nsNumberInitializer()?.initialize(value: value) {
        return ns
    } else {
        return value
    }
    #endif
}

#if !os(Windows)
extension stat {
    var modificationDate: Date {
        #if canImport(Darwin)
        Date(seconds: TimeInterval(st_mtimespec.tv_sec), nanoSeconds: TimeInterval(st_mtimespec.tv_nsec))
        #else
        Date(seconds: TimeInterval(st_mtim.tv_sec), nanoSeconds: TimeInterval(st_mtim.tv_nsec))
        #endif
    }
    
    var creationDate: Date {
        #if canImport(Darwin)
        Date(seconds: TimeInterval(st_ctimespec.tv_sec), nanoSeconds: TimeInterval(st_ctimespec.tv_nsec))
        #else
        Date(seconds: TimeInterval(st_ctim.tv_sec), nanoSeconds: TimeInterval(st_ctim.tv_nsec))
        #endif
    }
    
    fileprivate var fileAttributes: [FileAttributeKey : Any] {
        let fileType = st_mode.fileType
        var result: [FileAttributeKey : Any] = [
            .size : _writeFileAttributePrimitive(st_size, as: UInt.self),
            .modificationDate : modificationDate,
            .creationDate : creationDate,
            .posixPermissions : _writeFileAttributePrimitive(st_mode & 0o7777, as: UInt.self),
            .referenceCount : _writeFileAttributePrimitive(st_nlink, as: UInt.self),
            .systemNumber : _writeFileAttributePrimitive(st_dev, as: UInt.self),
            .systemFileNumber : _writeFileAttributePrimitive(st_ino, as: UInt64.self),
            .type : fileType,
            .ownerAccountID : _writeFileAttributePrimitive(st_uid, as: UInt.self),
            .groupOwnerAccountID : _writeFileAttributePrimitive(st_gid, as: UInt.self)
        ]
        #if !os(WASI)
        if let userName = Platform.name(forUID: st_uid) {
            result[.ownerAccountName] = userName
        }
        if let groupName = Platform.name(forGID: st_gid) {
            result[.groupOwnerAccountName] = groupName
        }
        #endif
        switch fileType as FileAttributeType {
        case .typeBlockSpecial, .typeCharacterSpecial:
            result[.deviceIdentifier] = _writeFileAttributePrimitive(st_rdev, as: UInt.self)
        default:
            // Do nothing
            break
        }
        #if canImport(Darwin)
        let immutable = (st_flags & UInt32(UF_IMMUTABLE)) != 0 || (st_flags & UInt32(SF_IMMUTABLE)) != 0
        result[.immutable] = _writeFileAttributePrimitive(immutable)
        let appendOnly = (st_flags & UInt32(UF_APPEND)) != 0 || (st_flags & UInt32(SF_APPEND)) != 0
        result[.appendOnly] = _writeFileAttributePrimitive(appendOnly)
        #endif
        return result
    }
}

#if FOUNDATION_FRAMEWORK
extension FileProtectionType {
    var intValue: Int32? {
        switch self {
        case .complete: PROTECTION_CLASS_A
        case .init(rawValue: "NSFileProtectionWriteOnly"), .completeUnlessOpen: PROTECTION_CLASS_B
        case .init(rawValue: "NSFileProtectionCompleteUntilUserAuthentication"), .completeUntilFirstUserAuthentication: PROTECTION_CLASS_C
        case .none: PROTECTION_CLASS_D
        #if !os(macOS)
        case .completeWhenUserInactive: PROTECTION_CLASS_CX
        #endif
        default: nil
        }
    }
    
    init?(intValue value: Int32) {
        switch value {
        case PROTECTION_CLASS_A: self = .complete
        case PROTECTION_CLASS_B: self = .completeUnlessOpen
        case PROTECTION_CLASS_C: self = .completeUntilFirstUserAuthentication
        case PROTECTION_CLASS_D: self = .none
        #if !os(macOS)
        case PROTECTION_CLASS_CX: self = .completeWhenUserInactive
        #endif
        default: return nil
        }
    }
}
#endif
#endif

extension FileAttributeKey {
    fileprivate static var _extendedAttributes: Self { Self("NSFileExtendedAttributes") }
}

extension _FileManagerImpl {
    func createFile(
        atPath path: String,
        contents data: Data?,
        attributes attr: [FileAttributeKey : Any]? = nil
    ) -> Bool {
        #if (os(iOS) || os(watchOS) || os(tvOS)) && FOUNDATION_FRAMEWORK
        // Creating a file with a specific file protection class must have that class specified at open() time. Special-case NSFileProtectionKey here so that we can pass it as an NSDataWritingOption instead. 21998573.
        var opts = Data.WritingOptions.atomic
        var attr = attr
        if let protection = attr?[.protectionKey] as? String {
            let option: Data.WritingOptions? = switch FileProtectionType(rawValue: protection) {
            case .none: .noFileProtection
            case .complete: .completeFileProtection
            case .completeUnlessOpen: .completeFileProtectionUnlessOpen
            case .completeUntilFirstUserAuthentication: .completeFileProtectionUntilFirstUserAuthentication
            case .completeWhenUserInactive: .completeFileProtectionWhenUserInactive
            default: nil
            }
            if let option {
                opts.insert(option)
            }
            attr?[.protectionKey] = nil
        }
        #else
        let opts = Data.WritingOptions.atomic
        #endif
        
        do {
            try (data ?? .init()).write(to: URL(fileURLWithPath: path), options: opts)
        } catch {
            return false
        }
        if let attr {
            try? fileManager.setAttributes(attr, ofItemAtPath: path)
        }
        return true
    }
    
    func removeItem(at url: URL) throws {
        guard url.isFileURL else {
            throw CocoaError.errorWithFilePath(.fileReadUnsupportedScheme, url)
        }
        
        let path = url.path
        guard !path.isEmpty else {
            throw CocoaError.errorWithFilePath(.fileNoSuchFile, url)
        }
        
        try removeItem(atPath: path)
    }
    
    func removeItem(atPath path: String) throws {
        try _FileOperations.removeFile(path, with: fileManager)
    }
    
    func copyItem(
        at srcURL: URL,
        to dstURL: URL,
        options: NSFileManagerCopyOptions
    ) throws {
        guard srcURL.isFileURL else {
            throw CocoaError.errorWithFilePath(.fileReadUnsupportedScheme, srcURL)
        }
        guard dstURL.isFileURL else {
            throw CocoaError.errorWithFilePath(.fileReadUnsupportedScheme, dstURL)
        }
        
        let srcPath = srcURL.path
        guard !srcPath.isEmpty else {
            throw CocoaError.errorWithFilePath(.fileNoSuchFile, srcURL)
        }
        let dstPath = dstURL.path
        guard !dstPath.isEmpty else {
            throw CocoaError.errorWithFilePath(.fileNoSuchFile, dstURL)
        }
        
        try copyItem(atPath: srcPath, toPath: dstPath, options: options)
    }
    
    func copyItem(
        atPath srcPath: String,
        toPath dstPath: String,
        options: NSFileManagerCopyOptions
    ) throws {
        try _FileOperations.copyFile(srcPath, to: dstPath, with: fileManager, options: options)
    }
    
    func moveItem(
        at srcURL: URL,
        to dstURL: URL,
        options: NSFileManagerMoveOptions
    ) throws {
        guard srcURL.isFileURL else {
            throw CocoaError.errorWithFilePath(.fileReadUnsupportedScheme, srcURL)
        }
        guard dstURL.isFileURL else {
            throw CocoaError.errorWithFilePath(.fileReadUnsupportedScheme, dstURL)
        }
        
        let srcPath = srcURL.path
        guard !srcPath.isEmpty else {
            throw CocoaError.errorWithFilePath(.fileNoSuchFile, srcURL)
        }
        let dstPath = dstURL.path
        guard !dstPath.isEmpty else {
            throw CocoaError.errorWithFilePath(.fileNoSuchFile, dstURL)
        }
        
        try moveItem(atPath: srcPath, toPath: dstPath, options: options)
    }
    
    func moveItem(
        atPath srcPath: String,
        toPath dstPath: String,
        options: NSFileManagerMoveOptions
    ) throws {
        try _FileOperations.moveFile(
            URL(fileURLWithPath: srcPath),
            to: URL(fileURLWithPath: dstPath),
            with: fileManager,
            options: options
        )
    }
    
    private func _fileExists(_ path: String) -> (exists: Bool, isDirectory: Bool) {
#if os(Windows)
        guard !path.isEmpty else { return (false, false) }
        return (try? path.withNTPathRepresentation { pwszPath in
            let handle = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nil)
            if handle == INVALID_HANDLE_VALUE {
                return (false, false)
            }
            defer { CloseHandle(handle) }

            var info: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
            guard GetFileInformationByHandle(handle, &info) else {
                return (false, false)
            }

            return (true, info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY)
        }) ?? (false, false)
#else
        path.withFileSystemRepresentation { rep -> (Bool, Bool) in
            guard let rep else {
                return (false, false)
            }

            var fileInfo = stat()
            guard stat(rep, &fileInfo) == 0 else {
                return (false, false)
            }
            let isDir = (fileInfo.st_mode & S_IFMT) == S_IFDIR
            return (true, isDir)
        }
#endif
    }
    
    func fileExists(atPath path: String) -> Bool {
        _fileExists(path).exists
    }
    
    func fileExists(
        atPath path: String,
        isDirectory: inout Bool
    ) -> Bool {
        let result = _fileExists(path)
        guard result.exists else { return false }
        isDirectory = result.isDirectory
        return true
    }

#if !os(Windows)
    private func _fileAccessibleForMode(_ path: String, _ mode: Int32) -> Bool {
        path.withFileSystemRepresentation { ptr in
            guard let ptr else { return false }
            return access(ptr, mode) == 0
        }
    }
#endif

    func isReadableFile(atPath path: String) -> Bool {
#if os(Windows)
        return (try? path.withNTPathRepresentation {
            var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
            return GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes)
        }) ?? false
#else
        _fileAccessibleForMode(path, R_OK)
#endif
    }
    
    func isWritableFile(atPath path: String) -> Bool {
#if os(Windows)
        return (try? path.withNTPathRepresentation {
            var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
            guard GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes) else {
                return false
            }
            return faAttributes.dwFileAttributes & FILE_ATTRIBUTE_READONLY != FILE_ATTRIBUTE_READONLY
        }) ?? false
#else
        _fileAccessibleForMode(path, W_OK)
#endif
    }
    
    func isExecutableFile(atPath path: String) -> Bool {
#if os(Windows)
        return (try? path.withNTPathRepresentation {
            var dwBinaryType: DWORD = 0
            return GetBinaryTypeW($0, &dwBinaryType)
        }) ?? false
#else
        _fileAccessibleForMode(path, X_OK)
#endif
    }
    
    func isDeletableFile(atPath path: String) -> Bool {
        var parent = path.deletingLastPathComponent()
        if parent.isEmpty {
            parent = fileManager.currentDirectoryPath
        }

#if os(Windows) || os(WASI)
        return fileManager.isWritableFile(atPath: parent) && fileManager.isWritableFile(atPath: path)
#else
        guard fileManager.isWritableFile(atPath: parent),
              let dirInfo = fileManager._fileStat(parent) else {
            return false
        }
        
        if ((dirInfo.st_mode & S_ISVTX) != 0) && fileManager.fileExists(atPath: path) {
            // its sticky so verify that we own the file
            // otherwise we answer YES on the principle that if
            // we create files we can delete them
            
            guard let fileInfo = fileManager._fileStat(path) else {
                return false
            }
            return fileInfo.st_uid == getuid();
        } else {
            return true
        }
#endif
    }

#if !os(Windows) && !os(WASI)
    private func _extendedAttribute(_ key: UnsafePointer<CChar>, at path: UnsafePointer<CChar>, followSymlinks: Bool) throws -> Data? {
        #if canImport(Darwin)
        var size = getxattr(path, key, nil, 0, 0, followSymlinks ? 0 : XATTR_NOFOLLOW)
        #else
        var size = followSymlinks ? getxattr(path, key, nil, 0) : lgetxattr(path, key, nil, 0)
        #endif
        guard size != -1 else {
            throw CocoaError.errorWithFilePath(String(cString: path), errno: errno, reading: true)
        }
        // Historically we've omitted extended attribute keys with no associated data value
        guard size > 0 else { return nil }
        // Deallocated below in the Data deallocator
        let buffer = malloc(size)!
        #if canImport(Darwin)
        size = getxattr(path, key, buffer, size, 0, followSymlinks ? 0 : XATTR_NOFOLLOW)
        #else
        size = followSymlinks ? getxattr(path, key, buffer, size) : lgetxattr(path, key, buffer, size)
        #endif
        guard size != -1 else {
            free(buffer)
            throw CocoaError.errorWithFilePath(String(cString: path), errno: errno, reading: true)
        }
        // Check size again in case something has changed between the two getxattr calls
        guard size > 0 else {
            free(buffer)
            return nil
        }
        return Data(bytesNoCopy: buffer, count: size, deallocator: .free)
    }
    
    private func _extendedAttributes(at path: UnsafePointer<CChar>, followSymlinks: Bool) throws -> [String : Data]? {
        #if canImport(Darwin)
        var size = listxattr(path, nil, 0, 0)
        #else
        var size = listxattr(path, nil, 0)
        #endif
        guard size > 0 else { return nil }
        let keyList = UnsafeMutableBufferPointer<CChar>.allocate(capacity: size)
        defer { keyList.deallocate() }
        #if canImport(Darwin)
        size = listxattr(path, keyList.baseAddress!, size, 0)
        #else
        size = listxattr(path, keyList.baseAddress!, size)
        #endif
        guard size > 0 else { return nil }
        
        var extendedAttrs: [String : Data] = [:]
        var current = keyList.baseAddress!
        let end = keyList.baseAddress!.advanced(by: keyList.count)
        while current < end {
            let currentKey = String(cString: current)
            defer { current = current.advanced(by: currentKey.utf8.count) + 1 /* pass null byte */ }
            
            #if canImport(Darwin)
            if currentKey == XATTR_RESOURCEFORK_NAME || currentKey == XATTR_FINDERINFO_NAME || currentKey == "system.Security" {
                continue
            }
            #endif
            
            if let value = try _extendedAttribute(current, at: path, followSymlinks: false) {
                extendedAttrs[currentKey] = value
            }
        }
        return extendedAttrs
    }
#endif

    func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
#if os(Windows)
        return try path.withNTPathRepresentation { pwszPath in
            let hFile = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nil)
            if hFile == INVALID_HANDLE_VALUE {
                throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
            }
            defer { CloseHandle(hFile) }

            var info: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
            guard GetFileInformationByHandle(hFile, &info) else {
              throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
            }

            let dwFileType = GetFileType(hFile)
            var fatType: FileAttributeType = switch (dwFileType) {
                case FILE_TYPE_CHAR: FileAttributeType.typeCharacterSpecial
                case FILE_TYPE_DISK:
                    info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY
                            ? FileAttributeType.typeDirectory
                            : FileAttributeType.typeRegular
                case FILE_TYPE_PIPE: FileAttributeType.typeSocket
                case FILE_TYPE_UNKNOWN: FileAttributeType.typeUnknown
                default: FileAttributeType.typeUnknown
            }

            if info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == FILE_ATTRIBUTE_REPARSE_POINT {
                // This could by a symlink, check if that's the case and update fatType if necessary
                var tagInfo = FILE_ATTRIBUTE_TAG_INFO()
                if GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, &tagInfo, DWORD(MemoryLayout<FILE_ATTRIBUTE_TAG_INFO>.size)) {
                    if tagInfo.ReparseTag == IO_REPARSE_TAG_SYMLINK {
                        fatType = .typeSymbolicLink
                    }
                }
            }

            let systemNumber = UInt64(info.dwVolumeSerialNumber)
            let systemFileNumber = UInt64(info.nFileIndexHigh << 32) | UInt64(info.nFileIndexLow)
            let referenceCount = UInt64(info.nNumberOfLinks)

            let isReadOnly = info.dwFileAttributes & FILE_ATTRIBUTE_READONLY != 0
            // Directories are always considered executable, but we check for other types
            let isExecutable = fatType == .typeDirectory || SaferiIsExecutableFileType(pwszPath, 0)
            var posixPermissions = UInt16(_S_IREAD)
            if !isReadOnly {
                posixPermissions |= UInt16(_S_IWRITE)
            }
            if isExecutable {
                posixPermissions |= UInt16(_S_IEXEC)
            }

            let size: UInt64 = (UInt64(info.nFileSizeHigh) << 32) | UInt64(info.nFileSizeLow)
            let creation: Date = Date(timeIntervalSince1970: info.ftCreationTime.timeIntervalSince1970)
            let modification: Date = Date(timeIntervalSince1970: info.ftLastWriteTime.timeIntervalSince1970)
            return [
                .size: _writeFileAttributePrimitive(size, as: UInt.self),
                .modificationDate: modification,
                .creationDate: creation,
                .type: fatType,
                .systemNumber: _writeFileAttributePrimitive(systemNumber, as: UInt.self),
                .systemFileNumber: _writeFileAttributePrimitive(systemFileNumber, as: UInt.self),
                .posixPermissions: _writeFileAttributePrimitive(posixPermissions, as: UInt.self),
                .referenceCount: _writeFileAttributePrimitive(referenceCount, as: UInt.self),

                // Uid is always 0 on Windows systems
                .ownerAccountID: _writeFileAttributePrimitive(0, as: UInt.self),

                // Group id is always 0 on Windows
                .groupOwnerAccountID: _writeFileAttributePrimitive(0, as: UInt.self)

                // TODO: Support .deviceIdentifier
            ]
        }
#else
        try fileManager.withFileSystemRepresentation(for: path) { fsRep in
            guard let fsRep else {
                throw CocoaError.errorWithFilePath(.fileReadUnknown, path)
            }
            
            var statAtPath = stat()
            guard lstat(fsRep, &statAtPath) == 0 else {
                throw CocoaError.errorWithFilePath(path, errno: errno, reading: true)
            }
            
            var attributes = statAtPath.fileAttributes
            try? Self._catInfo(for: URL(filePath: path, directoryHint: .isDirectory), statInfo: statAtPath, into: &attributes)
            #if !os(WASI) // WASI does not support extended attributes
            if let extendedAttrs = try? _extendedAttributes(at: fsRep, followSymlinks: false) {
                attributes[._extendedAttributes] = extendedAttrs
            }
            #endif
            
            #if !targetEnvironment(simulator) && FOUNDATION_FRAMEWORK
            if statAtPath.isRegular || statAtPath.isDirectory {
                if let protectionClass = Self._fileProtectionValueForPath(fsRep), let pType = FileProtectionType(intValue: protectionClass) {
                    // Cast to NSString here so that clients can cast this value to both String and FileProtectionType
                    attributes[.protectionKey] = pType as NSString
                } else {
                    attributes[.protectionKey] = nil
                }
            }
            #endif
            return attributes
        }
#endif
    }
    
    func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
#if os(Windows)
        return try path.withNTPathRepresentation { pwszPath in
            var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
            guard GetFileAttributesExW(pwszPath, GetFileExInfoStandard, &faAttributes) else {
                throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
            }

            let dwLength: DWORD = GetFullPathNameW(pwszPath, 0, nil, nil)
            guard dwLength > 0 else {
                throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
            }

            return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) { szVolumeName in
                guard GetVolumePathNameW(pwszPath, szVolumeName.baseAddress, dwLength) else {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
                }

                var liTotal: ULARGE_INTEGER = .init()
                var liFree: ULARGE_INTEGER = .init()
                guard GetDiskFreeSpaceExW(szVolumeName.baseAddress, nil, &liTotal, &liFree) else {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
                }

                let hr: HRESULT = PathCchStripToRoot(szVolumeName.baseAddress, szVolumeName.count)
                guard hr == S_OK || hr == S_FALSE else {
                    throw CocoaError.errorWithFilePath(path, win32: DWORD(hr & 0xffff), reading: true)
                }

                var dwVolumeSerialNumber: DWORD = 0
                guard GetVolumeInformationW(szVolumeName.baseAddress, nil, 0, &dwVolumeSerialNumber, nil, nil, nil, 0) else {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
                }

                return [
                    .systemSize: _writeFileAttributePrimitive(liTotal.QuadPart, as: UInt64.self),
                    .systemFreeSize: _writeFileAttributePrimitive(liFree.QuadPart, as: UInt64.self),
                    .systemNumber: _writeFileAttributePrimitive(dwVolumeSerialNumber, as: UInt.self),

                    // TODO(compnerd) support these attributes, remapping the Windows semantics...
                    // .systemNodes: ...,
                    // .systemFreeNodes: ...,
                ]
            }
        }
#elseif os(WASI)
        // WASI does not support file system attributes
        return [:]
#else
        try fileManager.withFileSystemRepresentation(for: path) { rep in
            guard let rep else {
                throw CocoaError.errorWithFilePath(.fileReadUnknown, path)
            }
            
            #if canImport(Darwin)
            var result = statfs()
            let statfsReturnValue = statfs(rep, &result)
            #else
            var result = statvfs()
            let statfsReturnValue = statvfs(rep, &result)
            #endif
            guard statfsReturnValue == 0 else {
                throw CocoaError.errorWithFilePath(path, errno: errno, reading: true)
            }
            
            #if canImport(Darwin)
            let fsNumber = result.f_fsid.val.0
            let blockSize = UInt64(result.f_bsize)
            #else
            let fsNumber = result.f_fsid
            let blockSize = UInt(result.f_frsize)
            #endif
            var totalSizeBytes = result.f_blocks * blockSize
            var availSizeBytes = result.f_bavail * blockSize
            var totalFiles = result.f_files
            var availFiles = result.f_ffree
            
            
            #if canImport(Darwin)
            func QCMD(_ cmd: Int32, _ type: Int32) -> Int32 {
                (cmd << SUBCMDSHIFT) | (type & SUBCMDMASK)
            }
            
            func _quotactl<T>(_ path: UnsafePointer<CChar>, _ cmd: Int32, _ type: Int32, _ uid: uid_t, init: T) -> T? {
                var res = `init`
                let success = withUnsafeMutableBytes(of: &res) { buffer in
                    quotactl(path, QCMD(cmd, type), Int32(uid), buffer.baseAddress!) == 0
                }
                return success ? res : nil
            }
            
            withUnsafeBytes(of: &result.f_mntonname) { mntonnameBuffer in
                let mntonname = mntonnameBuffer.baseAddress!.assumingMemoryBound(to: CChar.self)
                // If a quota is enabled, get quota info
                let userID = geteuid()
                if let isQuotaOn = _quotactl(mntonname, Q_QUOTASTAT, USRQUOTA, userID, init: 0),
                   isQuotaOn != 0,
                   let quotaInfo = _quotactl(mntonname, Q_GETQUOTA, USRQUOTA, userID, init: dqblk()) {
                    // For each value (total/available bytes, total/available files) report the smaller of the quota hard limit and the statfs value.
                    if quotaInfo.dqb_bhardlimit > 0 {
                        totalSizeBytes = min(quotaInfo.dqb_bhardlimit, totalSizeBytes)
                        availSizeBytes = min(quotaInfo.dqb_bhardlimit - quotaInfo.dqb_curbytes, availSizeBytes)
                    }
                    if (quotaInfo.dqb_ihardlimit > 0) {
                        totalFiles = min(UInt64(quotaInfo.dqb_ihardlimit), totalFiles)
                        availFiles = min(UInt64(quotaInfo.dqb_ihardlimit - quotaInfo.dqb_curinodes), availFiles)
                    }
                }
            }
            #endif
            
            return [
                .systemSize : _writeFileAttributePrimitive(totalSizeBytes, as: UInt64.self),
                .systemFreeSize : _writeFileAttributePrimitive(availSizeBytes, as: UInt64.self),
                .systemNodes : _writeFileAttributePrimitive(totalFiles, as: UInt64.self),
                .systemFreeNodes : _writeFileAttributePrimitive(availFiles, as: UInt64.self),
                .systemNumber : _writeFileAttributePrimitive(fsNumber, as: UInt.self)
            ]
        }
#endif
    }
    
    func setAttributes(
        _ attributes: [FileAttributeKey : Any],
        ofItemAtPath path: String
    ) throws {
        let mode = _readFileAttributePrimitive(attributes[.posixPermissions], as: UInt.self)
        let immutable = _readFileAttributePrimitive(attributes[.immutable], as: Bool.self)
        let appendOnly = _readFileAttributePrimitive(attributes[.appendOnly], as: Bool.self)

#if os(Windows)
        try path.withNTPathRepresentation {
            if immutable != nil || appendOnly != nil {
                // Setting these flags is not supported on this platform
                throw CocoaError.errorWithFilePath(.featureUnsupported, path)
            }

            var attributesToSet: DWORD?
            if let mode {
                let existingAttributes = GetFileAttributesW($0)
                guard existingAttributes != INVALID_FILE_ATTRIBUTES else {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
                }
                let isReadOnly = (existingAttributes & FILE_ATTRIBUTE_READONLY) != 0
                let requestedReadOnly = (mode & UInt(_S_IWRITE)) == 0
                if isReadOnly && !requestedReadOnly {
                    guard SetFileAttributesW($0, existingAttributes & ~FILE_ATTRIBUTE_READONLY) else {
                        throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: false)
                    }
                } else if !isReadOnly && requestedReadOnly {
                    // Make the file read-only later after setting other attributes
                    attributesToSet = existingAttributes | FILE_ATTRIBUTE_READONLY
                }
            }

            if let modification = attributes[.modificationDate] as? Date {
                let seconds = modification.timeIntervalSince1601

                var uiTime: ULARGE_INTEGER = .init()
                guard let converted = UInt64(exactly: seconds * 10000000.0) else {
                    return
                }
                uiTime.QuadPart = converted

                var ftTime: FILETIME = .init()
                ftTime.dwLowDateTime = uiTime.LowPart
                ftTime.dwHighDateTime = uiTime.HighPart

                let hFile: HANDLE = CreateFileW($0, GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, nil)
                if hFile == INVALID_HANDLE_VALUE {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true)
                }
                defer { CloseHandle(hFile) }

                guard SetFileTime(hFile, nil, nil, &ftTime) else {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: false)
                }
            }

            // Finally, make the file read-only if requested
            if let attributesToSet {
                guard SetFileAttributesW($0, attributesToSet) else {
                    throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: false)
                }
            }
        }
#else
        try fileManager.withFileSystemRepresentation(for: path) { fileSystemRepresentation in
            guard let fileSystemRepresentation else {
                throw CocoaError.errorWithFilePath(.fileWriteUnknown, path)
            }
            
            // Use Result instead of throwing var to avoid compiler hang (rdar://119035093)
            lazy var statAtPath: Result<stat, CocoaError> = {
                var result = stat()
                if lstat(fileSystemRepresentation, &result) != 0 {
                    return .failure(CocoaError.errorWithFilePath(path, errno: errno, reading: false))
                }
                return .success(result)
            }()
            
            // Set the flags first if we could be clearing the immutable bit. Set them last if we could be setting the immutable bit.
            var setFlags: (() throws -> Void)?
            if (immutable != nil || appendOnly != nil) {
                #if canImport(Darwin)
                setFlags = {
                    var flags = try statAtPath.get().st_flags
                    if let appendOnly {
                        if appendOnly {
                            flags |= UInt32(UF_APPEND)
                        } else {
                            flags &= ~UInt32(UF_APPEND)
                        }
                    }
                    if let immutable {
                        if immutable {
                            flags |= UInt32(UF_IMMUTABLE)
                        } else {
                            flags &= ~UInt32(UF_IMMUTABLE)
                        }
                    }
                    
                    if chflags(fileSystemRepresentation, flags) != 0 {
                        throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
                    }
                }
                
                if !(immutable ?? false) {
                    try setFlags?()
                    setFlags = nil
                }
                #else
                // Setting these flags is not supported on this platform
                throw CocoaError.errorWithFilePath(.featureUnsupported, path)
                #endif
            }
            
            // Like the immutable flag, if write permissions are being set, do it first. If they are being unset, do it last.
            var setMode: (() throws -> Void)?
            if let mode {
                #if os(WASI)
                // WASI does not have the concept of permissions
                throw CocoaError.errorWithFilePath(.featureUnsupported, path)
                #else
                setMode = {
                    if chmod(fileSystemRepresentation, mode_t(mode)) != 0 {
                        throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
                    }
                }
                
                if mode_t(mode) & S_IWUSR != 0 {
                    try setMode?()
                    setMode = nil
                }
                #endif
            }
            
            let user = attributes[.ownerAccountName] as? String
            let userID = _readFileAttributePrimitive(attributes[.ownerAccountID], as: UInt.self)
            let group = attributes[.groupOwnerAccountName] as? String
            let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self)
            
            if user != nil || userID != nil || group != nil || groupID != nil {
                #if os(WASI)
                // WASI does not have the concept of users or groups
                throw CocoaError.errorWithFilePath(.featureUnsupported, path)
                #else
                // Bias toward userID & groupID - try to prevent round trips to getpwnam if possible.
                var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) }
                let rawUserID = userID.flatMap(uid_t.init) ?? user.flatMap(Platform.uid(forName:)) ?? leaveUnchanged
                let rawGroupID = groupID.flatMap(gid_t.init) ?? group.flatMap(Platform.gid(forName:)) ?? leaveUnchanged
                if chown(fileSystemRepresentation, rawUserID, rawGroupID) != 0 {
                    throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
                }
                #endif
            }
            
            try Self._setCatInfoAttributes(attributes, path: path)
            
            if let extendedAttrs = attributes[.init("NSFileExtendedAttributes")] as? [String : Data] {
                #if os(WASI)
                // WASI does not support extended attributes
                throw CocoaError.errorWithFilePath(.featureUnsupported, path)
                #elseif canImport(Android)
                // Android doesn't allow setting this for normal apps, so just skip it.
                #else
                try Self._setAttributes(extendedAttrs, at: fileSystemRepresentation, followSymLinks: false)
                #endif
            }
            
            if let date = attributes[.modificationDate] as? Date {
                let (isecs, fsecs) = modf(date.timeIntervalSince1970)
                if let tv_sec = time_t(exactly: isecs),
                   let tv_usec = suseconds_t(exactly: round(fsecs * 1000000.0)) {
                    var timevals = (timeval(), timeval())
                    timevals.0.tv_sec = tv_sec
                    timevals.0.tv_usec = tv_usec
                    timevals.1 = timevals.0
                    try withUnsafePointer(to: timevals) {
                        try $0.withMemoryRebound(to: timeval.self, capacity: 2) {
                            if utimes(fileSystemRepresentation, $0) != 0 {
                                throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
                            }
                        }
                    }
                }
            }
            
            // Remove write permissions if it has been requested. This must be done before setting the immutable bit.
            try setMode?()
            
            // Set flags now, if we postponed it until now.
            try setFlags?()
            
            #if !targetEnvironment(simulator) && FOUNDATION_FRAMEWORK
            // Set per-file protection class on embedded.
            let fileProtection = attributes[.protectionKey] as? FileProtectionType
            if let fileProtection, let fileProtectionClass = fileProtection.intValue {
                // Only set protection class on regular files and directories.
                if try statAtPath.get().isRegular || statAtPath.get().isDirectory {
                    // Finally, set the class.
                    try Self._setFileProtectionValueForPath(path, fileSystemRepresentation, newValue: fileProtectionClass)
                }
            }
            #endif
        }
#endif
    }
}