Cannot copy a file from one location to another

I have a Mac app using Core Data with SQLite. In this case, the database is in 3 files, the storedata file, the .shm file, and the .wal file. I want to back these up from the app to a location of the user's choosing. My code asks the user for the location, then creates URL's representing the sources and destinations. I check for the file's existence and remove if necessary before copying.


NSError* error;

if ([[NSFileManager defaultManager] fileExistsAtPath:backupDBURL.path]) {

/

NSError* error;

[[NSFileManager defaultManager] removeItemAtURL:backupDBURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

}

if ([[NSFileManager defaultManager] fileExistsAtPath:backupShmURL.path]) {

/

NSError* error;

[[NSFileManager defaultManager] removeItemAtURL:backupShmURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

}

if ([[NSFileManager defaultManager] fileExistsAtPath:backupWalURL.path]) {

/

NSError* error;

[[NSFileManager defaultManager] removeItemAtURL:backupWalURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

}


Then I copy each one with:


[[NSFileManager defaultManager]copyItemAtURL:appDelegate.storeDataURL toURL:backupDBURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

[[NSFileManager defaultManager]copyItemAtURL:appDelegate.storeDataURL toURL:backupShmURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

[[NSFileManager defaultManager]copyItemAtURL:appDelegate.walURL toURL:backupWalURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}


The really strange this is that the first file copies fine, which is the main database file. However, each of the others failes with a message like: "couldn’t be copied because you don’t have permission to access". I have tried copying to many locations including the desktop, the documents folder, and others. Same error.


Here's the full error message attempting to copy the second file to my user folder:


2015-07-26 09:56:13.425 Mac Inventory[54658:7880735] error: Error Domain=NSCocoaErrorDomain Code=513 "“Inventory.storedata” couldn’t be copied because you don’t have permission to access “myNameHere”." UserInfo=0x6080000f5100 {NSSourceFilePathErrorKey=/Users/rickschlueter/Library/Containers/com.rschluet.MacInventory/Data/Library/Application Support/Inventory.storedata, NSUserStringVariant=(

Copy

), NSDestinationFilePath=/Users/myNameHere/Inventory.storedata-shm, NSFilePath=/Users/myNameHere/Library/Containers/com.rschluet.appNameHere/Data/Library/Application Support/Inventory.storedata, NSUnderlyingError=0x608000257580 "The operation couldn’t be completed. Operation not permitted"}

Accepted Reply

It's probably not the underlying reason, but you're using that API incorrectly. You should be checking the return value of the method, and only looking at the error if the method returned NO.


Sorry I don't know how the OS X sandbox works so I can't suggest anything for the actual problem. Do you have some antivirus or something running that would prevent writing the file types? Are you positive you're constructing the URLs correctly?

Replies

Sorry, 2nd file copy is:

[[NSFileManager defaultManager]copyItemAtURL:appDelegate.shmURL toURL:backupShmURL error:&error];


I had been messing with different things to see what was causing the issue.

OK, I saw an issue, I had not reinitialized error before each call. However, same result, just not as many error messages:

error = nil;

[[NSFileManager defaultManager]copyItemAtURL:appDelegate.shmURL toURL:backupShmURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

error = nil;

[[NSFileManager defaultManager]copyItemAtURL:appDelegate.storeDataURL toURL:backupDBURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}

error = nil;

[[NSFileManager defaultManager]copyItemAtURL:appDelegate.walURL toURL:backupWalURL error:&error];

if (error) {

NSLog(@"error: %@", error);

}


I also changed the order of the copy's in case that was an issue. It wasn't. The db file is copied 2nd now and still is the only one that successfully copies.

Seems very strange to me.

It's probably not the underlying reason, but you're using that API incorrectly. You should be checking the return value of the method, and only looking at the error if the method returned NO.


Sorry I don't know how the OS X sandbox works so I can't suggest anything for the actual problem. Do you have some antivirus or something running that would prevent writing the file types? Are you positive you're constructing the URLs correctly?

It is unlikely that an app would have sandbox rights to modify files in two different users' home directories. Unless you edited that log message, I think you're constructing the destination path wrong.

I guess it was malformed URL. I actually ended up rewriting this routine as now I wanted to create a folder and copy the files into it. This worked, but I didn't go back and try to figure out why the initial error occurred.

So thanks for suggesting the URL issue.