fileManager.copyItem with overwrite?

I'm would like to make a copy of a folder with files and folders in it before I do anything to them. When I naively fileManager.copyItem() I get errors about .DS_Store files already existing (I think the Finder might be creating them before the copy gets to it). The destination directory is empty at the start of the process. Is there any way to tell copyItem to overwrite?
Is replaceItemAt() a way to do this, with backupItemName set to the original name and withoutDeletingBackupItem set in options? That seems so convoluted. Is there a better way?

Replies

It’s hard to say what’s going on here without more details. Please post:

  • The details of the specific error you get

  • A small snippet of code that reproduces the problem

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The error I am getting is: Large file found, copy failed - Error Domain=NSCocoaErrorDomain Code=516 "“.DS_Store” couldn’t be copied to “Audio” because an item with the same name already exists." UserInfo={NSSourceFilePathErrorKey=/Volumes/RMP_Nearline/To_Archive/TAA/2019/190002_TA_Carnegie_Storytelling/Audio/.DS_Store, NSUserStringVariant=( Copy ), NSDestinationFilePath=/Volumes/RMP_Nearline/To_Archive/TAA/2019/To_Box/190002_TA_Carnegie_Storytelling/Audio/.DS_Store, NSFilePath=/Volumes/RMP_Nearline/To_Archive/TAA/2019/190002_TA_Carnegie_Storytelling/Audio/.DS_Store, NSUnderlyingError=0x60000195ce10 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}

But the output location is empty when I start the copy. The paths are very similar, but notice the To_Box in the destination path. I should also note these are large video project directories. This one is 5.3TB. I don't see the problem on small test directories. I made a small project https://github.com/ehemmete/CopyFolder that shows the same problem on the same folders. The error is always about .DS_Store files, so I expect that something (the Finder) is creating them before the copy gets to copying them over?

The error is always about .DS_Store files, so I expect that something (the Finder) is creating them before the copy gets to copying them over?

That seems like a reasonable theory.

This one is 5.3TB.

That makes me trying this for myself challenging (-:

Here’s a quick summary of that error:

NSSourceFilePathErrorKey /Volumes/RMP_Nearline/To_Archive/TAA/2019/190002_TA_Carnegie_Storytelling/Audio/.DS_Store
NSDestinationFilePath    /Volumes/RMP_Nearline/To_Archive/TAA/2019/To_Box/190002_TA_Carnegie_Storytelling/Audio/.DS_Store
NSFilePath               /Volumes/RMP_Nearline/To_Archive/TAA/2019/190002_TA_Carnegie_Storytelling/Audio/.DS_Store

What URL did you pass in as the source and destination of the copy?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The sourceFolderURL has path '/Volumes/RMP_Nearline/To_Archive/TAA/2019/190002_TA_Carnegie_Storytelling' The outputLocationURL has path '/Volumes/RMP_Nearline/To_Archive/TAA/2019/To_Box/' plus sourceFolderURL.lastPathComponent

In this case it copies about 3.5GB before failing.

Another note is that these directories are on Xsan volumes.

So there isn't an option to force file manager to overwrite? Is there another method that would get me that result?

First things first, please do file a bug about this. I’m still not 100% sure what’s going on but it definitely seems like an issue on the Apple side of things.

Make sure to include a sysdiagnose log taken shortly after reproducing the problem. See Bug Reporting > Profiles and Logs for more info on that.

I’d appreciate you posting your bug number, just for the record.


So there isn't an option to force file manager to overwrite?

No. FileManager thinks that it ‘owns’ this newly created area and thus there isn’t any reason for it to provide that.

Is there another method that would get me that result?

I can think of a bunch of potential options here, but let’s start you out with what I suspect will be the easiest one: Implement the fileManager(_:shouldProceedAfterError:movingItemAt:to:) delegate method and ‘swallow’ file exists errors (NSFileWriteFileExistsError) that involve a .DS_Store.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"