@@ -1450,61 +1450,58 @@ open class FileManager : NSObject {
1450
1450
#if os(Windows)
1451
1451
NSUnimplemented ( )
1452
1452
#else
1453
- if rmdir ( path) == 0 {
1454
- return
1455
- } else if errno == ENOTEMPTY {
1456
-
1457
- let stream = URL ( fileURLWithPath: path) . withUnsafeFileSystemRepresentation { ( fsRep) -> UnsafeMutablePointer < FTS > ? in
1453
+ try _fileSystemRepresentation ( withPath: path, { fsRep in
1454
+ if rmdir ( fsRep) == 0 {
1455
+ return
1456
+ } else if errno == ENOTEMPTY {
1458
1457
let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>?> . allocate( capacity: 2 )
1459
1458
ps. initialize ( to: UnsafeMutablePointer ( mutating: fsRep) )
1460
1459
ps. advanced ( by: 1 ) . initialize ( to: nil )
1461
- defer {
1462
- ps. deinitialize ( count: 2 )
1463
- ps. deallocate ( )
1464
- }
1465
- return fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil )
1466
- }
1467
-
1468
- if stream != nil {
1469
- defer {
1470
- fts_close ( stream)
1471
- }
1460
+ let stream = fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil )
1461
+ ps. deinitialize ( count: 2 )
1462
+ ps. deallocate ( )
1472
1463
1473
- while let current = fts_read ( stream) ? . pointee {
1474
- let itemPath = string ( withFileSystemRepresentation: current. fts_path, length: Int ( current. fts_pathlen) )
1475
- guard alreadyConfirmed || shouldRemoveItemAtPath ( itemPath, isURL: isURL) else {
1476
- continue
1464
+ if stream != nil {
1465
+ defer {
1466
+ fts_close ( stream)
1477
1467
}
1478
-
1479
- do {
1480
- switch Int32 ( current. fts_info) {
1481
- case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
1482
- if unlink ( current. fts_path) == - 1 {
1483
- throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
1468
+
1469
+ while let current = fts_read ( stream) ? . pointee {
1470
+ let itemPath = string ( withFileSystemRepresentation: current. fts_path, length: Int ( current. fts_pathlen) )
1471
+ guard alreadyConfirmed || shouldRemoveItemAtPath ( itemPath, isURL: isURL) else {
1472
+ continue
1473
+ }
1474
+
1475
+ do {
1476
+ switch Int32 ( current. fts_info) {
1477
+ case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
1478
+ if unlink ( current. fts_path) == - 1 {
1479
+ throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
1480
+ }
1481
+ case FTS_DP:
1482
+ if rmdir ( current. fts_path) == - 1 {
1483
+ throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
1484
+ }
1485
+ case FTS_DNR, FTS_ERR, FTS_NS:
1486
+ throw _NSErrorWithErrno ( current. fts_errno, reading: false , path: itemPath)
1487
+ default :
1488
+ break
1484
1489
}
1485
- case FTS_DP :
1486
- if rmdir ( current . fts_path ) == - 1 {
1487
- throw _NSErrorWithErrno ( errno , reading : false , path : itemPath )
1490
+ } catch {
1491
+ if !shouldProceedAfterError ( error , removingItemAtPath : itemPath , isURL : isURL ) {
1492
+ throw error
1488
1493
}
1489
- case FTS_DNR, FTS_ERR, FTS_NS:
1490
- throw _NSErrorWithErrno ( current. fts_errno, reading: false , path: itemPath)
1491
- default :
1492
- break
1493
- }
1494
- } catch {
1495
- if !shouldProceedAfterError( error, removingItemAtPath: itemPath, isURL: isURL) {
1496
- throw error
1497
1494
}
1498
1495
}
1496
+ } else {
1497
+ let _ = _NSErrorWithErrno ( ENOTEMPTY, reading: false , path: path)
1499
1498
}
1500
- } else {
1501
- let _ = _NSErrorWithErrno ( ENOTEMPTY, reading: false , path: path)
1499
+ } else if errno != ENOTDIR {
1500
+ throw _NSErrorWithErrno ( errno, reading: false , path: path)
1501
+ } else if unlink ( fsRep) != 0 {
1502
+ throw _NSErrorWithErrno ( errno, reading: false , path: path)
1502
1503
}
1503
- } else if errno != ENOTDIR {
1504
- throw _NSErrorWithErrno ( errno, reading: false , path: path)
1505
- } else if _fileSystemRepresentation ( withPath: path, { unlink ( $0) != 0 } ) {
1506
- throw _NSErrorWithErrno ( errno, reading: false , path: path)
1507
- }
1504
+ } )
1508
1505
#endif
1509
1506
}
1510
1507
0 commit comments