Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap more filesystem calls with fileSystemRepresentation() #1994

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,10 @@ extension FileHandle {
}

internal static func _openFileDescriptorForURL(_ url : URL, flags: Int32, reading: Bool) throws -> Int32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think its better to wrap this at the public API side where the call to _openFileDescriptorURL is made.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage of doing it here is you dont need to fix up every caller or any new callers.

Also the type for the file system representaion is not a URL or String so it would change the method signature.

let path = url.path
let fd = _CFOpenFile(path, flags)
let fd = url.withUnsafeFileSystemRepresentation( { (fsRep) -> Int32 in
guard let fsRep = fsRep else { return -1 }
return _CFOpenFile(fsRep, flags)
})
if fd < 0 {
throw _NSErrorWithErrno(errno, reading: reading, url: url)
}
Expand Down
85 changes: 41 additions & 44 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1450,61 +1450,58 @@ open class FileManager : NSObject {
#if os(Windows)
NSUnimplemented()
#else
if rmdir(path) == 0 {
return
} else if errno == ENOTEMPTY {

let stream = URL(fileURLWithPath: path).withUnsafeFileSystemRepresentation { (fsRep) -> UnsafeMutablePointer<FTS>? in
try _fileSystemRepresentation(withPath: path, { fsRep in
if rmdir(fsRep) == 0 {
return
} else if errno == ENOTEMPTY {
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
ps.advanced(by: 1).initialize(to: nil)
defer {
ps.deinitialize(count: 2)
ps.deallocate()
}
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
}

if stream != nil {
defer {
fts_close(stream)
}
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
ps.deallocate()

while let current = fts_read(stream)?.pointee {
let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
continue
if stream != nil {
defer {
fts_close(stream)
}

do {
switch Int32(current.fts_info) {
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
if unlink(current.fts_path) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)

while let current = fts_read(stream)?.pointee {
let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
continue
}

do {
switch Int32(current.fts_info) {
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
if unlink(current.fts_path) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
}
case FTS_DP:
if rmdir(current.fts_path) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
}
case FTS_DNR, FTS_ERR, FTS_NS:
throw _NSErrorWithErrno(current.fts_errno, reading: false, path: itemPath)
default:
break
}
case FTS_DP:
if rmdir(current.fts_path) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
} catch {
if !shouldProceedAfterError(error, removingItemAtPath: itemPath, isURL: isURL) {
throw error
}
case FTS_DNR, FTS_ERR, FTS_NS:
throw _NSErrorWithErrno(current.fts_errno, reading: false, path: itemPath)
default:
break
}
} catch {
if !shouldProceedAfterError(error, removingItemAtPath: itemPath, isURL: isURL) {
throw error
}
}
} else {
let _ = _NSErrorWithErrno(ENOTEMPTY, reading: false, path: path)
}
} else {
let _ = _NSErrorWithErrno(ENOTEMPTY, reading: false, path: path)
} else if errno != ENOTDIR {
throw _NSErrorWithErrno(errno, reading: false, path: path)
} else if unlink(fsRep) != 0 {
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
} else if errno != ENOTDIR {
throw _NSErrorWithErrno(errno, reading: false, path: path)
} else if _fileSystemRepresentation(withPath: path, { unlink($0) != 0 }) {
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
})
#endif
}

Expand Down