@@ -12,6 +12,10 @@ internal func &(left: UInt32, right: mode_t) -> mode_t {
12
12
return mode_t ( left) & right
13
13
}
14
14
#endif
15
+ #if os(Windows)
16
+ fileprivate let rmdir = _NS_rmdir
17
+ fileprivate let unlink = _NS_unlink
18
+ #endif
15
19
16
20
import CoreFoundation
17
21
@@ -1441,73 +1445,47 @@ open class FileManager : NSObject {
1441
1445
}
1442
1446
}
1443
1447
1444
- @available ( Windows, deprecated, message: " Not Yet Implemented " )
1445
1448
private func _removeItem( atPath path: String , isURL: Bool , alreadyConfirmed: Bool = false ) throws {
1446
1449
guard alreadyConfirmed || shouldRemoveItemAtPath ( path, isURL: isURL) else {
1447
1450
return
1448
1451
}
1449
1452
1450
- #if os(Windows)
1451
- NSUnimplemented ( )
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
1458
- let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>?> . allocate( capacity: 2 )
1459
- ps. initialize ( to: UnsafeMutablePointer ( mutating: fsRep) )
1460
- ps. advanced ( by: 1 ) . initialize ( to: nil )
1461
- defer {
1462
- ps. deinitialize ( count: 2 )
1463
- ps. deallocate ( )
1453
+ let urlItem = URL ( fileURLWithPath: path)
1454
+ if urlItem. hasDirectoryPath {
1455
+
1456
+ let stream = NSURLDirectoryEnumerator (
1457
+ url: urlItem,
1458
+ options: [ ] ,
1459
+ errorHandler: { ( url, err) in
1460
+ self . shouldProceedAfterError ( err,
1461
+ removingItemAtPath: url. absoluteString,
1462
+ isURL: true )
1463
+ } )
1464
+ while let itemPath = stream. nextObject ( ) as? String {
1465
+ do {
1466
+ guard alreadyConfirmed || shouldRemoveItemAtPath ( itemPath, isURL: true ) else {
1467
+ continue
1464
1468
}
1465
- return fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil )
1466
- }
1467
-
1468
- if stream != nil {
1469
- defer {
1470
- fts_close ( stream)
1469
+ if ( URL ( fileURLWithPath: itemPath) . hasDirectoryPath) {
1470
+ if rmdir ( itemPath) == - 1 {
1471
+ throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
1472
+ }
1473
+ } else {
1474
+ if unlink ( itemPath) == - 1 {
1475
+ throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
1476
+ }
1471
1477
}
1472
-
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
1477
- }
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)
1484
- }
1485
- case FTS_DP:
1486
- if rmdir ( current. fts_path) == - 1 {
1487
- throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
1488
- }
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
- }
1498
- }
1478
+ } catch {
1479
+ if !shouldProceedAfterError( error, removingItemAtPath: itemPath, isURL: true ) {
1480
+ throw error
1499
1481
}
1500
- } else {
1501
- let _ = _NSErrorWithErrno ( ENOTEMPTY, reading: false , path: path)
1482
+ }
1502
1483
}
1503
- } else if errno != ENOTDIR {
1504
- throw _NSErrorWithErrno ( errno, reading: false , path: path)
1505
1484
} else if _fileSystemRepresentation ( withPath: path, { unlink ( $0) != 0 } ) {
1506
1485
throw _NSErrorWithErrno ( errno, reading: false , path: path)
1507
1486
}
1508
- #endif
1509
1487
}
1510
-
1488
+
1511
1489
open func copyItem( atPath srcPath: String , toPath dstPath: String ) throws {
1512
1490
try _copyItem ( atPath: srcPath, toPath: dstPath, isURL: false )
1513
1491
}
0 commit comments