@@ -175,22 +175,20 @@ extension FileManager {
175
175
}
176
176
}
177
177
178
- var saAttributes : SECURITY_ATTRIBUTES =
179
- SECURITY_ATTRIBUTES ( nLength: DWORD ( MemoryLayout< SECURITY_ATTRIBUTES> . size) ,
180
- lpSecurityDescriptor: nil ,
181
- bInheritHandle: false )
182
- let psaAttributes : UnsafeMutablePointer < SECURITY_ATTRIBUTES > =
183
- UnsafeMutablePointer < SECURITY_ATTRIBUTES > ( & saAttributes)
184
-
185
-
186
- try path. withCString ( encodedAs: UTF16 . self) {
187
- if !CreateDirectoryW( $0, psaAttributes) {
188
- // FIXME(compnerd) pass along path
189
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ path] )
178
+ try FileManager . default. _fileSystemRepresentation ( withPath: path) { fsr in
179
+ var saAttributes : SECURITY_ATTRIBUTES =
180
+ SECURITY_ATTRIBUTES ( nLength: DWORD ( MemoryLayout< SECURITY_ATTRIBUTES> . size) ,
181
+ lpSecurityDescriptor: nil ,
182
+ bInheritHandle: false )
183
+ try withUnsafeMutablePointer ( to: & saAttributes) {
184
+ if !CreateDirectoryW( fsr, $0) {
185
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ path] )
190
186
}
191
- }
192
- if let attr = attributes {
187
+ }
188
+
189
+ if let attr = attributes {
193
190
try self . setAttributes ( attr, ofItemAtPath: path)
191
+ }
194
192
}
195
193
}
196
194
@@ -236,15 +234,15 @@ extension FileManager {
236
234
}
237
235
238
236
internal func windowsFileAttributes( atPath path: String ) throws -> WIN32_FILE_ATTRIBUTE_DATA {
237
+ return try FileManager . default. _fileSystemRepresentation ( withPath: path) {
239
238
var faAttributes : WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA ( )
240
- return try path. withCString ( encodedAs: UTF16 . self) {
241
- if !GetFileAttributesExW( $0, GetFileExInfoStandard, & faAttributes) {
242
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ path] )
243
- }
244
- return faAttributes
245
- }
239
+ if !GetFileAttributesExW( $0, GetFileExInfoStandard, & faAttributes) {
240
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ path] )
241
+ }
242
+ return faAttributes
243
+ }
246
244
}
247
-
245
+
248
246
internal func _attributesOfFileSystemIncludingBlockSize( forPath path: String ) throws -> ( attributes: [ FileAttributeKey : Any ] , blockSize: UInt64 ? ) {
249
247
return ( attributes: try _attributesOfFileSystem ( forPath: path) , blockSize: nil )
250
248
}
@@ -292,9 +290,10 @@ extension FileManager {
292
290
case . some( false ) :
293
291
break ;
294
292
case . none:
295
- let resolvedDest = destPath. isAbsolutePath
296
- ? destPath
297
- : try joinPath ( prefix: path. deletingLastPathComponent, suffix: destPath)
293
+ let resolvedDest =
294
+ destPath. isAbsolutePath ? destPath
295
+ : joinPath ( prefix: path. deletingLastPathComponent,
296
+ suffix: destPath)
298
297
guard let faAttributes = try ? windowsFileAttributes ( atPath: resolvedDest) else {
299
298
// Note: windowsfileAttributes will throw if the destPath is not found.
300
299
// Since on Windows, you are required to know the type of the symlink
@@ -309,12 +308,10 @@ extension FileManager {
309
308
}
310
309
}
311
310
312
- try path. withCString ( encodedAs: UTF16 . self) { name in
313
- try destPath. withCString ( encodedAs: UTF16 . self) { dest in
314
- guard CreateSymbolicLinkW ( name, dest, dwFlags) != 0 else {
315
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ path, destPath] )
316
- }
317
- }
311
+ try FileManager . default. _fileSystemRepresentation ( withPath: path, andPath: destPath) {
312
+ guard CreateSymbolicLinkW ( $0, $1, dwFlags) != 0 else {
313
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ path, destPath] )
314
+ }
318
315
}
319
316
}
320
317
@@ -494,12 +491,10 @@ extension FileManager {
494
491
throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileWriteFileExists. rawValue, userInfo: [ NSFilePathErrorKey : NSString ( dstPath) ] )
495
492
}
496
493
497
- try srcPath. withCString ( encodedAs: UTF16 . self) { src in
498
- try dstPath. withCString ( encodedAs: UTF16 . self) { dst in
499
- if !MoveFileExW( src, dst, DWORD ( MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH) ) {
500
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ srcPath, dstPath] )
501
- }
502
- }
494
+ try FileManager . default. _fileSystemRepresentation ( withPath: srcPath, andPath: dstPath) {
495
+ if !MoveFileExW( $0, $1, DWORD ( MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH) ) {
496
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ srcPath, dstPath] )
497
+ }
503
498
}
504
499
}
505
500
@@ -512,12 +507,10 @@ extension FileManager {
512
507
do {
513
508
switch fileType {
514
509
case . typeRegular:
515
- try srcPath. withCString ( encodedAs: UTF16 . self) { src in
516
- try dstPath. withCString ( encodedAs: UTF16 . self) { dst in
517
- if !CreateHardLinkW( dst, src, nil ) {
518
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ srcPath, dstPath] )
519
- }
520
- }
510
+ try FileManager . default. _fileSystemRepresentation ( withPath: srcPath, andPath: dstPath) {
511
+ if !CreateHardLinkW( $1, $0, nil ) {
512
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ srcPath, dstPath] )
513
+ }
521
514
}
522
515
case . typeSymbolicLink:
523
516
try _copySymlink ( atPath: srcPath, toPath: dstPath)
@@ -637,12 +630,14 @@ extension FileManager {
637
630
var szDirectory : [ WCHAR ] = Array < WCHAR > ( repeating: 0 , count: Int ( dwLength + 1 ) )
638
631
639
632
GetCurrentDirectoryW ( dwLength, & szDirectory)
640
- return String ( decodingCString: & szDirectory, as: UTF16 . self)
633
+ return String ( decodingCString: & szDirectory, as: UTF16 . self) . standardizingPath
641
634
}
642
635
643
636
@discardableResult
644
637
internal func _changeCurrentDirectoryPath( _ path: String ) -> Bool {
645
- return path. withCString ( encodedAs: UTF16 . self) { SetCurrentDirectoryW ( $0) }
638
+ return try ! FileManager . default. _fileSystemRepresentation ( withPath: path) {
639
+ SetCurrentDirectoryW ( $0)
640
+ }
646
641
}
647
642
648
643
internal func _fileExists( atPath path: String , isDirectory: UnsafeMutablePointer < ObjCBool > ? ) -> Bool {
@@ -701,8 +696,8 @@ extension FileManager {
701
696
return true
702
697
}
703
698
704
- internal func _lstatFile( atPath path: String , withFileSystemRepresentation fsRep: UnsafePointer < Int8 > ? = nil ) throws -> stat {
705
- let _fsRep : UnsafePointer < Int8 >
699
+ internal func _lstatFile( atPath path: String , withFileSystemRepresentation fsRep: UnsafePointer < NativeFSRCharType > ? = nil ) throws -> stat {
700
+ let _fsRep : UnsafePointer < NativeFSRCharType >
706
701
if fsRep == nil {
707
702
_fsRep = try __fileSystemRepresentation ( withPath: path)
708
703
} else {
@@ -714,20 +709,20 @@ extension FileManager {
714
709
}
715
710
716
711
var statInfo = stat ( )
717
- let h = path. withCString ( encodedAs: UTF16 . self) {
718
- CreateFileW ( /*lpFileName=*/$0,
719
- /*dwDesiredAccess=*/DWORD ( 0 ) ,
720
- /*dwShareMode=*/DWORD ( FILE_SHARE_READ) ,
721
- /*lpSecurityAttributes=*/nil ,
722
- /*dwCreationDisposition=*/DWORD ( OPEN_EXISTING) ,
723
- /*dwFlagsAndAttributes=*/DWORD ( FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS) ,
712
+ let handle =
713
+ CreateFileW ( _fsRep, /*dwDesiredAccess=*/DWORD ( 0 ) ,
714
+ DWORD ( FILE_SHARE_READ) , /*lpSecurityAttributes=*/nil ,
715
+ DWORD ( OPEN_EXISTING) ,
716
+ DWORD ( FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS) ,
724
717
/*hTemplateFile=*/nil )
725
- }
726
- if h == INVALID_HANDLE_VALUE {
718
+ if handle == INVALID_HANDLE_VALUE {
727
719
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths: [ path] )
728
720
}
721
+ defer { CloseHandle ( handle) }
722
+
729
723
var info : BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION ( )
730
- GetFileInformationByHandle ( h, & info)
724
+ GetFileInformationByHandle ( handle, & info)
725
+
731
726
// Group id is always 0 on Windows
732
727
statInfo. st_gid = 0
733
728
statInfo. st_atime = info. ftLastAccessTime. time_t
@@ -751,13 +746,12 @@ extension FileManager {
751
746
752
747
statInfo. st_mtime = info. ftLastWriteTime. time_t
753
748
statInfo. st_nlink = Int16 ( info. nNumberOfLinks)
754
- if info. nFileSizeHigh != 0 {
749
+ guard info. nFileSizeHigh == 0 else {
755
750
throw _NSErrorWithErrno ( EOVERFLOW, reading: true , path: path)
756
751
}
757
752
statInfo. st_size = Int32 ( info. nFileSizeLow)
758
753
// Uid is always 0 on Windows systems
759
754
statInfo. st_uid = 0
760
- CloseHandle ( h)
761
755
return statInfo
762
756
}
763
757
@@ -858,7 +852,7 @@ extension FileManager {
858
852
}
859
853
860
854
internal func _updateTimes( atPath path: String ,
861
- withFileSystemRepresentation fsr: UnsafePointer < Int8 > ,
855
+ withFileSystemRepresentation fsr: UnsafePointer < NativeFSRCharType > ,
862
856
creationTime: Date ? = nil ,
863
857
accessTime: Date ? = nil ,
864
858
modificationTime: Date ? = nil ) throws {
@@ -869,10 +863,9 @@ extension FileManager {
869
863
var mtime : FILETIME =
870
864
FILETIME ( from: time_t ( ( modificationTime ?? stat. lastModificationDate) . timeIntervalSince1970) )
871
865
872
- let hFile : HANDLE = String ( utf8String : fsr ) ! . withCString ( encodedAs : UTF16 . self ) {
873
- CreateFileW ( $0 , DWORD ( GENERIC_WRITE) , DWORD ( FILE_SHARE_WRITE) ,
866
+ let hFile : HANDLE =
867
+ CreateFileW ( fsr , DWORD ( GENERIC_WRITE) , DWORD ( FILE_SHARE_WRITE) ,
874
868
nil , DWORD ( OPEN_EXISTING) , 0 , nil )
875
- }
876
869
if hFile == INVALID_HANDLE_VALUE {
877
870
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ path] )
878
871
}
@@ -927,10 +920,10 @@ extension FileManager {
927
920
if isDir && ( level == 0 || !_options. contains ( . skipsSubdirectoryDescendants) ) {
928
921
var ffd = WIN32_FIND_DATAW ( )
929
922
let dirPath = joinPath ( prefix: _lastReturned. path, suffix: " * " )
930
- let handle = dirPath . withCString ( encodedAs : UTF16 . self ) {
923
+ let handle = try ? FileManager . default . _fileSystemRepresentation ( withPath : dirPath ) {
931
924
FindFirstFileW ( $0, & ffd)
932
925
}
933
- guard handle != INVALID_HANDLE_VALUE else { return firstValidItem ( ) }
926
+ if handle == INVALID_HANDLE_VALUE { return firstValidItem ( ) }
934
927
defer { FindClose ( handle) }
935
928
936
929
repeat {
0 commit comments