Re: Does "- replaceItemAtURL: withItemAtURL: backupItemName: options: resultingItemURL: error:" handle cross-volume moves?
Re: Does "- replaceItemAtURL: withItemAtURL: backupItemName: options: resultingItemURL: error:" handle cross-volume moves?
- Subject: Re: Does "- replaceItemAtURL: withItemAtURL: backupItemName: options: resultingItemURL: error:" handle cross-volume moves?
- From: Kevin Perry <email@hidden>
- Date: Mon, 30 Mar 2015 17:37:02 -0700
> On Mar 30, 2015, at 5:17 PM, Daryle Walker <email@hidden> wrote:
>
>> On Mar 30, 2015, at 7:59 PM, Kevin Perry <email@hidden <mailto:email@hidden>> wrote:
>>
>> -replaceItemAtURL:… relies on the atomicity of the POSIX rename() function in order to safely do its operation. Since rename() doesn’t work across volumes (returning EXDEV), the two URLs must be on the same volume.
>>
>> If you’re using a temporary directory with replaceItemAtURL:…, you want to use -[NSFileManager URLForDirectory:inDomain:appropriateForURL:create:error:] with NSItemReplacementDirectory, passing it the to-be-replaced item URL. This will get you an appropriate temporary directory on the target volume to ensure -replaceItemAtURL:… won’t fail due to rename's EXDEV.
>>
>>> On Mar 30, 2015, at 4:44 PM, Daryle Walker <email@hidden <mailto:email@hidden>> wrote:
>>>
>>> NSFileManager’s “- moveItemAtURL: toURL: error:” method does a copy if the source and destination file-URLs are on different volumes. Does “- replaceItemAtURL: withItemAtURL: backupItemName: options: resultingItemURL: error:” do similar? The docs for the latter don’t mention it. I’m worried since those docs do mention temporary directories, that we have to do different-volume detection (possibly harder than in the pre-X days) and copying manually. I don’t want to do that, since Apple already includes that logic in the former method.
>
> So I have to: (1) call URLForDirectory… to make sure there’s an appropriate temporary directory, (2) call moveItemAtURL… to relocate the downloaded file from the volume NSURLSession used to the one made in the first step, and (3) call replaceItemAtURL… for the final move, right? Is there a way to easily determine if two file objects are in the same volume, to skip the second step if unneeded? (Or is there a way to tell NSURLSession which volume to put its temporary files in advance?…)
I’m not personally familiar with NSURLSession, but don’t see any API to specify a volume/directory for the download destination.
You can check if two URLs are on the same volume by comparing values for NSURLVolumeIdentifierKey. If they are on the same volume, you can probably skip both (1) and (2). The API doesn’t require a specific temporary directory to work.
>
> —
> Daryle Walker
> Mac, Internet, and Video Game Junkie
> darylew AT mac DOT com
>
_______________________________________________
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