@@ -180,7 +180,7 @@ extension FileManager {
180
180
try path. withCString ( encodedAs: UTF16 . self) {
181
181
if !CreateDirectoryW( $0, psaAttributes) {
182
182
// FIXME(compnerd) pass along path
183
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
183
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ path ] )
184
184
}
185
185
}
186
186
if let attr = attributes {
@@ -197,7 +197,7 @@ extension FileManager {
197
197
198
198
let hDirectory : HANDLE = FindFirstFileW ( $0, & ffd)
199
199
if hDirectory == INVALID_HANDLE_VALUE {
200
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
200
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
201
201
}
202
202
defer { FindClose ( hDirectory) }
203
203
@@ -208,7 +208,7 @@ extension FileManager {
208
208
}
209
209
}
210
210
if path != " . " && path != " .. " {
211
- try closure ( path, Int32 ( ffd. dwFileAttributes) )
211
+ try closure ( path. standardizingPath , Int32 ( ffd. dwFileAttributes) )
212
212
}
213
213
} while FindNextFileW ( hDirectory, & ffd)
214
214
}
@@ -222,7 +222,7 @@ extension FileManager {
222
222
if entryType & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY {
223
223
let subPath : String = joinPath ( prefix: path, suffix: entryName)
224
224
let entries = try subpathsOfDirectory ( atPath: subPath)
225
- contents. append ( contentsOf: entries. map { joinPath ( prefix: entryName, suffix: $0) } )
225
+ contents. append ( contentsOf: entries. map { joinPath ( prefix: entryName, suffix: $0) . standardizingPath } )
226
226
}
227
227
} )
228
228
return contents
@@ -232,7 +232,7 @@ extension FileManager {
232
232
var faAttributes : WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA ( )
233
233
return try path. withCString ( encodedAs: UTF16 . self) {
234
234
if !GetFileAttributesExW( $0, GetFileExInfoStandard, & faAttributes) {
235
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
235
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
236
236
}
237
237
return faAttributes
238
238
}
@@ -248,24 +248,24 @@ extension FileManager {
248
248
try path. withCString ( encodedAs: UTF16 . self) {
249
249
let dwLength : DWORD = GetFullPathNameW ( $0, 0 , nil , nil )
250
250
guard dwLength != 0 else {
251
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
251
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
252
252
}
253
253
var szVolumePath : [ WCHAR ] = Array < WCHAR > ( repeating: 0 , count: Int ( dwLength + 1 ) )
254
254
255
255
guard GetVolumePathNameW ( $0, & szVolumePath, dwLength) else {
256
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
256
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
257
257
}
258
258
259
259
var liTotal : ULARGE_INTEGER = ULARGE_INTEGER ( )
260
260
var liFree : ULARGE_INTEGER = ULARGE_INTEGER ( )
261
261
262
262
guard GetDiskFreeSpaceExW ( & szVolumePath, nil , & liTotal, & liFree) else {
263
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
263
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
264
264
}
265
265
266
266
var volumeSerialNumber : DWORD = 0
267
267
guard GetVolumeInformationW ( & szVolumePath, nil , 0 , & volumeSerialNumber, nil , nil , nil , 0 ) else {
268
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
268
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
269
269
}
270
270
271
271
result [ . systemSize] = NSNumber ( value: liTotal. QuadPart)
@@ -284,15 +284,17 @@ extension FileManager {
284
284
// other doesn't make a lot of sense, we allow it to throw, thus
285
285
// disallowing the creation of broken symlinks on Windows (unlike with
286
286
// POSIX).
287
- let faAttributes = try windowsFileAttributes ( atPath: destPath)
287
+ guard let faAttributes = try ? windowsFileAttributes ( atPath: destPath) else {
288
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths: [ path, destPath] )
289
+ }
288
290
if faAttributes. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_DIRECTORY) == DWORD ( FILE_ATTRIBUTE_DIRECTORY) {
289
291
dwFlags |= DWORD ( SYMBOLIC_LINK_FLAG_DIRECTORY)
290
292
}
291
293
292
294
try path. withCString ( encodedAs: UTF16 . self) { name in
293
295
try destPath. withCString ( encodedAs: UTF16 . self) { dest in
294
296
guard CreateSymbolicLinkW ( name, dest, dwFlags) != 0 else {
295
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
297
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path , destPath ] )
296
298
}
297
299
}
298
300
}
@@ -317,7 +319,7 @@ extension FileManager {
317
319
var szPath = Array < WCHAR > ( repeating: 0 , count: Int ( dwLength + 1 ) )
318
320
dwLength = GetFullPathNameW ( $0, DWORD ( szPath. count) , & szPath, nil )
319
321
guard dwLength > 0 && dwLength <= szPath. count else {
320
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
322
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
321
323
}
322
324
return String ( decodingCString: szPath, as: UTF16 . self)
323
325
}
@@ -335,7 +337,7 @@ extension FileManager {
335
337
try srcPath. withCString ( encodedAs: UTF16 . self) { src in
336
338
try dstPath. withCString ( encodedAs: UTF16 . self) { dst in
337
339
if !CopyFileW( src, dst, false ) {
338
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
340
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ srcPath , dstPath ] )
339
341
}
340
342
}
341
343
}
@@ -358,8 +360,7 @@ extension FileManager {
358
360
}
359
361
360
362
internal func _copyOrLinkDirectoryHelper( atPath srcPath: String , toPath dstPath: String , variant: String = " Copy " , _ body: ( String , String , FileAttributeType ) throws -> ( ) ) throws {
361
- var faAttributes : WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA ( )
362
- do { faAttributes = try windowsFileAttributes ( atPath: srcPath) } catch { return }
363
+ let faAttributes = try windowsFileAttributes ( atPath: srcPath)
363
364
364
365
var fileType = FileAttributeType ( attributes: faAttributes, atPath: srcPath)
365
366
if fileType == . typeDirectory {
@@ -372,7 +373,7 @@ extension FileManager {
372
373
let src = joinPath ( prefix: srcPath, suffix: item)
373
374
let dst = joinPath ( prefix: dstPath, suffix: item)
374
375
375
- do { faAttributes = try windowsFileAttributes ( atPath: src) } catch { return }
376
+ let faAttributes = try windowsFileAttributes ( atPath: src)
376
377
fileType = FileAttributeType ( attributes: faAttributes, atPath: srcPath)
377
378
if fileType == . typeDirectory {
378
379
try createDirectory ( atPath: dst, withIntermediateDirectories: false , attributes: nil )
@@ -397,7 +398,7 @@ extension FileManager {
397
398
try srcPath. withCString ( encodedAs: UTF16 . self) { src in
398
399
try dstPath. withCString ( encodedAs: UTF16 . self) { dst in
399
400
if !MoveFileExW( src, dst, DWORD ( MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH) ) {
400
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
401
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ srcPath , dstPath ] )
401
402
}
402
403
}
403
404
}
@@ -415,7 +416,7 @@ extension FileManager {
415
416
try srcPath. withCString ( encodedAs: UTF16 . self) { src in
416
417
try dstPath. withCString ( encodedAs: UTF16 . self) { dst in
417
418
if !CreateHardLinkW( dst, src, nil ) {
418
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
419
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ srcPath , dstPath ] )
419
420
}
420
421
}
421
422
}
@@ -436,6 +437,11 @@ extension FileManager {
436
437
guard alreadyConfirmed || shouldRemoveItemAtPath ( path, isURL: isURL) else {
437
438
return
438
439
}
440
+
441
+ guard path != " " else {
442
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadInvalidFileName. rawValue, userInfo: [ NSFilePathErrorKey : NSString ( path) ] )
443
+ }
444
+
439
445
let url = URL ( fileURLWithPath: path)
440
446
var fsrBuf : [ WCHAR ] = Array < WCHAR > ( repeating: 0 , count: Int ( MAX_PATH) )
441
447
_CFURLGetWideFileSystemRepresentation ( url. _cfObject, false , & fsrBuf, Int ( MAX_PATH) )
@@ -447,13 +453,13 @@ extension FileManager {
447
453
if faAttributes. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY {
448
454
let readableAttributes = faAttributes. dwFileAttributes & DWORD ( bitPattern: ~ FILE_ATTRIBUTE_READONLY)
449
455
guard fsrPath. withCString ( encodedAs: UTF16 . self, { SetFileAttributesW ( $0, readableAttributes) } ) else {
450
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
456
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ path ] )
451
457
}
452
458
}
453
459
454
460
if faAttributes. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_DIRECTORY) == 0 {
455
461
if !fsrPath. withCString ( encodedAs: UTF16 . self, DeleteFileW) {
456
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
462
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ path ] )
457
463
}
458
464
return
459
465
}
@@ -469,15 +475,15 @@ extension FileManager {
469
475
continue
470
476
}
471
477
guard GetLastError ( ) == ERROR_DIR_NOT_EMPTY else {
472
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
478
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ itemPath ] )
473
479
}
474
480
dirStack. append ( itemPath)
475
481
var ffd : WIN32_FIND_DATAW = WIN32_FIND_DATAW ( )
476
482
let h : HANDLE = ( itemPath + " \\ * " ) . withCString ( encodedAs: UTF16 . self, {
477
483
FindFirstFileW ( $0, & ffd)
478
484
} )
479
485
guard h != INVALID_HANDLE_VALUE else {
480
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
486
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ itemPath ] )
481
487
}
482
488
defer { FindClose ( h) }
483
489
@@ -491,7 +497,7 @@ extension FileManager {
491
497
if ffd. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY {
492
498
let readableAttributes = ffd. dwFileAttributes & DWORD ( bitPattern: ~ FILE_ATTRIBUTE_READONLY)
493
499
guard file. withCString ( encodedAs: UTF16 . self, { SetFileAttributesW ( $0, readableAttributes) } ) else {
494
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
500
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ file ] )
495
501
}
496
502
}
497
503
@@ -504,7 +510,7 @@ extension FileManager {
504
510
continue
505
511
}
506
512
if !itemPath. withCString ( encodedAs: UTF16 . self, DeleteFileW) {
507
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
513
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ file ] )
508
514
}
509
515
}
510
516
} while FindNextFileW ( h, & ffd)
@@ -604,7 +610,7 @@ extension FileManager {
604
610
/*hTemplateFile=*/nil )
605
611
}
606
612
if h == INVALID_HANDLE_VALUE {
607
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
613
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ path ] )
608
614
}
609
615
var info : BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION ( )
610
616
GetFileInformationByHandle ( h, & info)
@@ -675,12 +681,12 @@ extension FileManager {
675
681
nil , DWORD ( OPEN_EXISTING) , 0 , nil )
676
682
}
677
683
if hFile == INVALID_HANDLE_VALUE {
678
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
684
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ path ] )
679
685
}
680
686
defer { CloseHandle ( hFile) }
681
687
682
688
if !SetFileTime( hFile, nil , & atime, & mtime) {
683
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
689
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false , paths : [ path ] )
684
690
}
685
691
686
692
}
@@ -705,7 +711,7 @@ extension FileManager {
705
711
while let url = _stack. popLast ( ) {
706
712
if !FileManager. default. fileExists ( atPath: url. path, isDirectory: nil ) {
707
713
guard let handler = _errorHandler,
708
- handler ( url, _NSErrorWithWindowsError ( GetLastError ( ) , reading: true ) )
714
+ handler ( url, _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ url . path ] ) )
709
715
else { return nil }
710
716
continue
711
717
}
@@ -719,7 +725,7 @@ extension FileManager {
719
725
var isDir : ObjCBool = false
720
726
guard FileManager . default. fileExists ( atPath: _lastReturned. path, isDirectory: & isDir) else {
721
727
guard let handler = _errorHandler,
722
- handler ( _lastReturned, _NSErrorWithWindowsError ( GetLastError ( ) , reading: true ) )
728
+ handler ( _lastReturned, _NSErrorWithWindowsError ( GetLastError ( ) , reading: true , paths : [ _lastReturned . path ] ) )
723
729
else { return nil }
724
730
return firstValidItem ( )
725
731
}
0 commit comments