Re: Strange NSFileManager file replacement issue
Re: Strange NSFileManager file replacement issue
- Subject: Re: Strange NSFileManager file replacement issue
- From: Sixten Otto <email@hidden>
- Date: Fri, 19 Aug 2011 08:29:47 -0700
On Thu, Aug 18, 2011 at 10:14 PM, Quincey Morris
<email@hidden> wrote:
> c. Can you show us the actual line of code that does the replacement?
Here's the original code (plus the addition of an assert on the file
manager). The property self.filePath has the path to the current
version of the file that's already on disk (from a previous pass
through this code). In my current test case, the paths look something
like:
self.filePath: /Users/sixten/Library/Application Support/iPhone Simulator/4.3.2/Applications/840B5926-8107-458E-87ED-ABF0F084BC12/Documents/Subdir/MyFile.pdf
tempFilePath: /var/folders/KP/KPl-d+TMHGaIJ6QIjMYNQ++++TI/-Tmp-/2212
// start original code
NSFileManager* fileManager = [[NSFileManager alloc] init];
NSAssert(fileManager != nil, @"File manager wasn't created.");
NSString* directoryPath = [fileManager
rd_documentsSubdirectory:self.document.volume];
BOOL isDirectory;
NSAssert1([fileManager fileExistsAtPath:tempFilePath
isDirectory:&isDirectory] && isDirectory == NO, @"Bad temp file path
%@", tempFilePath);
NSAssert1([fileManager fileExistsAtPath:directoryPath
isDirectory:&isDirectory] && isDirectory == YES, @"Bad document
directory %@", directoryPath);
if( [desiredName length] == 0 ) {
if( self.filePath ) {
desiredName = [self.filePath lastPathComponent];
}
else {
desiredName = [tempFilePath lastPathComponent];
}
}
NSString* finalPath = [directoryPath
stringByAppendingPathComponent:desiredName];
NSError* error = nil;
if( [finalPath isEqualToString:self.filePath] || [self.filePath
hasPrefix:directoryPath] ) {
NSURL* existingFileURL = [NSURL fileURLWithPath:finalPath];
NSURL* newFileURL = [NSURL fileURLWithPath:tempFilePath];
NSString* backupItemName = [NSString
stringWithFormat:@"__%@__.bak", self.documentId];
NSURL* resultURL = nil;
// *** 1
if( [fileManager replaceItemAtURL:existingFileURL
withItemAtURL:newFileURL backupItemName:backupItemName options:0
resultingItemURL:&resultURL error:&error] ) {
self.filePath = [resultURL path];
success = YES;
}
else {
// *** ends up here, with no error == nil
LOG_GENERAL(LOG_PRIORITY_HIGHEST, @"Error attempting to
replace »%@« with »%@«: %@\n%@", tempFilePath, finalPath, [error
localizedDescription]);
NSAssert(NO, @"Couldn't move file to designated location");
}
// *** 2
}
else {
// ... move the temp file to finalPath, which works just fine,
and update self.filePath
}
[fileManager release];
// end original code
This morning, I replaced the code between the "*** 1" and "*** 2" with
the following, which still failed in exactly the same way. It does get
create a replacement directory and return it, my temp file moves into
that directory without issue, but the swap still mysteriously fails.
The value of swapURL looks like:
file://localhost/Users/sixten/Library/Application Support/iPhone Simulator/4.3.2/Applications/840B5926-8107-458E-87ED-ABF0F084BC12/Documents/Subdir/(A Document Being Saved By MyApp)/MyFile.pdf
// *** 1
NSURL* swapURL = [fileManager
URLForDirectory:NSItemReplacementDirectory inDomain:NSUserDomainMask
appropriateForURL:existingFileURL create:YES error:&error];
if( swapURL ) {
swapURL = [swapURL URLByAppendingPathComponent:desiredName];
if( [fileManager moveItemAtURL:newFileURL toURL:swapURL error:NULL] ) {
if( [fileManager replaceItemAtURL:existingFileURL
withItemAtURL:swapURL backupItemName:backupItemName options:0
resultingItemURL:&resultURL error:&error] ) {
self.filePath = [resultURL path];
success = YES;
}
else {
// *** still gets here, with no error set!
LOG_GENERAL(LOG_PRIORITY_HIGHEST, @"Error attempting to
replace »%@« with »%@«: %@\n%@", tempFilePath, finalPath, [error
localizedDescription]);
NSAssert(NO, @"Couldn't move file to designated location");
}
}
}
else {
LOG_GENERAL(LOG_PRIORITY_HIGHEST, @"Error attempting to
replace »%@« with »%@«: %@\n%@", tempFilePath, finalPath, [error
localizedDescription]);
NSAssert(NO, @"Couldn't find/create swap location");
}
// *** 2
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden