On Sep 21, 2009, at 3:26 PM, dct wrote:

Prior to installing OS X 10.6 and Xcode 3.2 (64-bit), a bit of FileManager code for replacing one file with another, to wit: if( [mgr fileExistsAtPath:path1] ) [mgr removeFileAtPath:path1 handler:nil];
  [mgr movePath:path0 toPath:path1 handler:nil];
run without any problem.

That algorithm does have a race condition, in that another thread/ process can create a file at that path in between the first and second line, causing the move to fail.

If you do this using the Unix rename() call, the item at the destination will be deleted if it exists, making the first line unnecessary (and making the whole operation atomic.) It won't work if the move is cross-volume or if the item at the destination is a directory, though.

Otherwise, the safest approach is probably something like
        while( movePath fails because there's something at the destination) {
                remove the item at the destination;
                if the remove failed, return an error
        }

With the new OS X and Xcode I changed the code to:
if( [mgr fileExistsAtPath:path1] ) [mgr removeItemAtPath:path1 error:NULL];
  [mgr moveItemAtPath:path0 toPath:path1 error:NULL];

That looks like comparable code to the original version; I'd expect it to work the same way...

which fails in that:
 a. FileManager is aware of a file at the new location, i.e.,
      (BOOL)fileOK = [mgr fileExistsAtPath:path1];  equals YES, but
 b. the file now at path1 is empty, i.e.,
       (int)nn = [hndl1 seekToEndOfFile];  equals 0
whereas the original File at path0 contained several thousand bytes.

Weird. Are you sure the -removeItemAtPath call succeeded? You're not checking the return value or error. It could fail if you don't have write permission for the file or the parent directory, for instance.

Have you single-stepped through this code and used a shell to look at what's going on in the filesystem after each call?

—Jens_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to