tags:

views:

296

answers:

3

I have tried:

  1. Checking the FSCatalogInfo nodeFlags to see if kFSNodeForkOpenBit is set (using kFSNodeForkOpenMask).

  2. Checking whether the creator code is between kFirstMagicBusyFiletype and kLastMagicBusyFiletype.

  3. Checking the ExtendedFileInfo extendedFinderFlags to see if kExtendedFlagObjectIsBusy is set.

  4. Running GetFileInfo -ab from the shell.

All of these report that the file the Finder is copying to is not open.

Using lsof does detect that the file is open, but (a) I don't want to call lsof from my application, and (b) my understanding is that it relies on private API so looking at its source wouldn't help.

A: 

This method might be a little kludgey, but I've used it for similar purposes and it may work for you. The basic idea is to attempt opening the file with an exclusive lock, check to see if the open was successful, and then immediately close it again. So, this would look something like:

char* pathToFile;
int result;

result = open(pathToFile, O_RDWR | O_NONBLOCK | O_EXLOCK);
if (result != -1)
{
    //The file is not busy
    close(result);
}
else
{
    //The file is busy
}

I've never tried this with a file being copied by the Finder, but it does work when a file is open by another application on the system. I'm not sure if this same method works if you open it as read-only instead of read/write, so that might be another gotcha depending on your requirements.

Brian Webster
Thanks. This does detect files being copied by the Finder. Unfortunately, it won't work for folders or for files that the current process doesn't have write permission for.
Michael Tsai
A: 

I don't know the exact answer but... Below the POSIX and Carbon file mgr APIs there's another layer that is used by both. It's pretty close to VFS, and uses all lowercase names. You see those calls when you trace FS calls using the command "fs_usage", IIRC. You may find a working function in those calls. Unfortunately, they're not well documented. Hope that helps.

Thomas Tempelmann
A: 

It looks to me like partially copied files have a file type of "brok" and a creator code of "MACS".

I don't believe that the Finder marks in any way the copies of folders that it is in the process of making. The "grayed out" representation is strictly confined to the Finder process that is doing the copying. You can verify this by using Fast User Switching while a folder is copying: as a different user, the folder being copied has a normal appearance in the Finder, and you can open it and watch as sub-files and folders appear. The Finder doesn't seem to reveal any differences about the folder through AppleScript either, and I can't think of any other way to get that information.

Miles