Skip to content

Commit 9c8bfdc

Browse files
gmittertJason Mittertreiner
authored andcommitted
Implement _removeItem in terms of DirectoryEnumerator
1 parent 3a4e7fb commit 9c8bfdc

File tree

1 file changed

+46
-56
lines changed

1 file changed

+46
-56
lines changed

Foundation/FileManager.swift

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,73 +1441,63 @@ open class FileManager : NSObject {
14411441
}
14421442
}
14431443

1444-
@available(Windows, deprecated, message: "Not Yet Implemented")
14451444
private func _removeItem(atPath path: String, isURL: Bool, alreadyConfirmed: Bool = false) throws {
14461445
guard alreadyConfirmed || shouldRemoveItemAtPath(path, isURL: isURL) else {
14471446
return
14481447
}
14491448

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()
1449+
let urlItem = URL(fileURLWithPath: path)
1450+
if urlItem.hasDirectoryPath {
1451+
1452+
let stream = NSURLDirectoryEnumerator(
1453+
url: urlItem,
1454+
options: [],
1455+
errorHandler: {(url, err) in
1456+
self.shouldProceedAfterError(err,
1457+
removingItemAtPath: url.absoluteString,
1458+
isURL: true)
1459+
})
1460+
while let itemPath = stream.nextObject() as? String {
1461+
do {
1462+
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: true) else {
1463+
continue
14641464
}
1465-
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
1466-
}
1467-
1468-
if stream != nil {
1469-
defer {
1470-
fts_close(stream)
1465+
if (URL(fileURLWithPath: itemPath).hasDirectoryPath) {
1466+
#if os(Windows)
1467+
let result = _NS_rmdir(itemPath)
1468+
#else
1469+
let result = rmdir(itemPath)
1470+
#endif
1471+
if result == -1 {
1472+
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
1473+
}
1474+
} else {
1475+
#if os(Windows)
1476+
let result = _NS_unlink(itemPath)
1477+
#else
1478+
let result = unlink(itemPath)
1479+
#endif
1480+
if result == -1 {
1481+
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
1482+
}
14711483
}
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-
}
1484+
} catch {
1485+
if !shouldProceedAfterError(error, removingItemAtPath: itemPath, isURL: true) {
1486+
throw error
14991487
}
1500-
} else {
1501-
let _ = _NSErrorWithErrno(ENOTEMPTY, reading: false, path: path)
1488+
}
15021489
}
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)
1490+
} else if _fileSystemRepresentation(withPath: path, {path in
1491+
#if os(Windows)
1492+
return _NS_unlink(path) != 0
1493+
#else
1494+
return unlink(path) != 0
1495+
#endif
1496+
}) {
1497+
throw _NSErrorWithErrno(errno, reading: false, path: path)
15071498
}
1508-
#endif
15091499
}
1510-
1500+
15111501
open func copyItem(atPath srcPath: String, toPath dstPath: String) throws {
15121502
try _copyItem(atPath: srcPath, toPath: dstPath, isURL: false)
15131503
}

0 commit comments

Comments
 (0)