Skip to content

Commit 591661f

Browse files
committed
Implement _removeItem in terms of DirectoryEnumerator
1 parent 448641f commit 591661f

File tree

1 file changed

+32
-54
lines changed

1 file changed

+32
-54
lines changed

Foundation/FileManager.swift

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ internal func &(left: UInt32, right: mode_t) -> mode_t {
1212
return mode_t(left) & right
1313
}
1414
#endif
15+
#if os(Windows)
16+
fileprivate let rmdir = _NS_rmdir
17+
fileprivate let unlink = _NS_unlink
18+
#endif
1519

1620
import CoreFoundation
1721

@@ -1441,73 +1445,47 @@ open class FileManager : NSObject {
14411445
}
14421446
}
14431447

1444-
@available(Windows, deprecated, message: "Not Yet Implemented")
14451448
private func _removeItem(atPath path: String, isURL: Bool, alreadyConfirmed: Bool = false) throws {
14461449
guard alreadyConfirmed || shouldRemoveItemAtPath(path, isURL: isURL) else {
14471450
return
14481451
}
14491452

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
14641468
}
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+
}
14711477
}
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
14991481
}
1500-
} else {
1501-
let _ = _NSErrorWithErrno(ENOTEMPTY, reading: false, path: path)
1482+
}
15021483
}
1503-
} else if errno != ENOTDIR {
1504-
throw _NSErrorWithErrno(errno, reading: false, path: path)
15051484
} else if _fileSystemRepresentation(withPath: path, { unlink($0) != 0 }) {
15061485
throw _NSErrorWithErrno(errno, reading: false, path: path)
15071486
}
1508-
#endif
15091487
}
1510-
1488+
15111489
open func copyItem(atPath srcPath: String, toPath dstPath: String) throws {
15121490
try _copyItem(atPath: srcPath, toPath: dstPath, isURL: false)
15131491
}

0 commit comments

Comments
 (0)