Skip to content

Commit 98c3925

Browse files
authoredMar 27, 2019
Merge pull request swiftlang#1994 from spevans/pr_more_fsrep
2 parents 9c7e85d + 8a85f44 commit 98c3925

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed
 

‎Foundation/FileHandle.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,10 @@ extension FileHandle {
782782
}
783783

784784
internal static func _openFileDescriptorForURL(_ url : URL, flags: Int32, reading: Bool) throws -> Int32 {
785-
let path = url.path
786-
let fd = _CFOpenFile(path, flags)
785+
let fd = url.withUnsafeFileSystemRepresentation( { (fsRep) -> Int32 in
786+
guard let fsRep = fsRep else { return -1 }
787+
return _CFOpenFile(fsRep, flags)
788+
})
787789
if fd < 0 {
788790
throw _NSErrorWithErrno(errno, reading: reading, url: url)
789791
}

‎Foundation/FileManager.swift

+41-44
Original file line numberDiff line numberDiff line change
@@ -1450,61 +1450,58 @@ open class FileManager : NSObject {
14501450
#if os(Windows)
14511451
NSUnimplemented()
14521452
#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 {
14581457
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
14591458
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
14601459
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()
14721463

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)
14771467
}
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
14841489
}
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
14881493
}
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
14971494
}
14981495
}
1496+
} else {
1497+
let _ = _NSErrorWithErrno(ENOTEMPTY, reading: false, path: path)
14991498
}
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)
15021503
}
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+
})
15081505
#endif
15091506
}
15101507

0 commit comments

Comments
 (0)