-
Notifications
You must be signed in to change notification settings - Fork 1.2k
FileManager: Fix copyItem(atPath:toPath:) for directories and symlinks. #1515
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -442,16 +442,34 @@ open class FileManager : NSObject { | |
else { | ||
return | ||
} | ||
|
||
func copyNonDirectory(srcPath: String, dstPath: String, fileType: FileAttributeType) throws { | ||
if fileType == .typeSymbolicLink { | ||
let destination = try destinationOfSymbolicLink(atPath: srcPath) | ||
try createSymbolicLink(atPath: dstPath, withDestinationPath: destination) | ||
} else if fileType == .typeRegular { | ||
if createFile(atPath: dstPath, contents: contents(atPath: srcPath), attributes: nil) == false { | ||
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue, userInfo: [NSFilePathErrorKey : NSString(string: dstPath)]) | ||
} | ||
} | ||
} | ||
|
||
if fileType == .typeDirectory { | ||
try createDirectory(atPath: dstPath, withIntermediateDirectories: false, attributes: nil) | ||
let subpaths = try subpathsOfDirectory(atPath: srcPath) | ||
for subpath in subpaths { | ||
try copyItem(atPath: srcPath + "/" + subpath, toPath: dstPath + "/" + subpath) | ||
let src = srcPath + "/" + subpath | ||
let dst = dstPath + "/" + subpath | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I don't recall—Do we not have an equivalent of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only on |
||
if let attrs = try? attributesOfItem(atPath: src), let fileType = attrs[.type] as? FileAttributeType { | ||
if fileType == .typeDirectory { | ||
try createDirectory(atPath: dst, withIntermediateDirectories: false, attributes: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks wrong. No recursion at all means we only go one extra level deep, no? (There should be a test for this.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original implementation was wrong because it was recursing in every directory and then additional recursing in every sub directory. It now uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. I misread this as Please note that |
||
} else { | ||
try copyNonDirectory(srcPath: src, dstPath: dst, fileType: fileType) | ||
} | ||
} | ||
} | ||
} else { | ||
if createFile(atPath: dstPath, contents: contents(atPath: srcPath), attributes: nil) == false { | ||
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue, userInfo: [NSFilePathErrorKey : NSString(string: dstPath)]) | ||
} | ||
try copyNonDirectory(srcPath: srcPath, dstPath: dstPath, fileType: fileType) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but a proper copy engine will read then write a limited portion of the source file at a time. For large files, this will result in an extreme high-water mark for memory usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, this was how the previous version was doing it but I will create a PR to fix it.