Michael Tsai
Michael Tsai

Reputation: 2030

How can I tell if a file or folder is busy, e.g. the Finder is busy copying to it?

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.

Upvotes: 5

Views: 7419

Answers (5)

Graham Perrin
Graham Perrin

Reputation: 534

File business

In Mac OS X v10.4 and later:

Finder and data integrity

Whilst Finder progresses a copy of a file (not afterwards) the copy has:

  • HFS type code brok
  • HFS creator code MACS

— together, those two things signify file business.

MACS is the ID of Finder.

If the copy is to a file system without support for attributes such as those

Whilst Finder progresses a copy of a file (not afterwards) the copy has a counterpart:

  • a dot underscore ._ file that includes the necessary extended attribute (xattr).

Related

Why are dot underscore ._ files created, and how can I avoid them? — Ask Different

Finder compatibility with a broad range of file systems and operating systems

If a copy using Finder is interrupted ungracefully, then the presence of brok MACS should ensure that breakage/business is recognisable to:

  • all versions of OS X
  • all versions of Mac OS X
  • Mac OS 9 and some earlier classic versions of the operating system.

Example

A file big file.dmg with no extended attribute.

Before copying from JHFS+ to an empty example directory on a volume that uses MS-DOS (FAT32):

[macbookpro08:~] gjp22% date
Fri 11 May 2012 17:24:29 BST
[macbookpro08:~] gjp22% ls -h@al /Users/gjp22/Documents/uk/ac/brighton/collaborate/bigfile.dmg
-rw-r--r--  1 gjp22  staff   1.4G 11 May 17:20 /Users/gjp22/Documents/uk/ac/brighton/collaborate/bigfile.dmg
[macbookpro08:~] gjp22% xattr /Users/gjp22/Documents/uk/ac/brighton/collaborate/bigfile.dmg
[macbookpro08:~] gjp22% diskutil list | grep FAT32
   1:                 DOS_FAT_32 FAT32                   2.0 GB     disk3s1
[macbookpro08:~] gjp22% ls -h@al /Volumes/FAT32/example 
total 16
drwxrwxrwx  1 gjp22  staff   4.0K 11 May 17:24 .
drwxrwxrwx  1 gjp22  staff   4.0K 11 May 11:32 ..

After using Finder to begin the copy, before completion

brokMACS within the value of extended attribute com.apple.FinderInfo:

[macbookpro08:~] gjp22% date
Fri 11 May 2012 17:25:08 BST
[macbookpro08:~] gjp22% ls -h@al /Volumes/FAT32/example
total 311320
drwxrwxrwx  1 gjp22  staff   4.0K 11 May 17:25 .
drwxrwxrwx  1 gjp22  staff   4.0K 11 May 11:32 ..
-rwxrwxrwx  1 gjp22  staff   4.0K 11 May 17:25 ._bigfile.dmg
-rwxrwxrwx@ 1 gjp22  staff   152M 11 May 17:25 bigfile.dmg
    com.apple.FinderInfo      32B 
[macbookpro08:~] gjp22% xattr -l /Volumes/FAT32/example/bigfile.dmg
com.apple.FinderInfo:
00000000  62 72 6F 6B 4D 41 43 53 00 00 00 00 00 00 00 00  |brokMACS........|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020

After Finder successfully completes the copy

That extended attribute is removed:

[macbookpro08:~] gjp22% date
Fri 11 May 2012 17:29:19 BST
[macbookpro08:~] gjp22% xattr -l /Volumes/FAT32/example/bigfile.dmg
[macbookpro08:~] gjp22% ls -h@al /Volumes/FAT32/example
total 3000016
drwxrwxrwx  1 gjp22  staff   4.0K 11 May 17:29 .
drwxrwxrwx  1 gjp22  staff   4.0K 11 May 11:32 ..
-rwxrwxrwx  1 gjp22  staff   1.4G 11 May 17:20 bigfile.dmg

Upvotes: 4

bdrister
bdrister

Reputation: 448

It's about 2 years too late for you now, but I thought for the sake of anybody finding this later I'd note that you can check the creation date of the file. Finder sets it to kMagicBusyCreationDate (1946-02-14 08:34:56 +0000) while it's copying.

Upvotes: 6

Miles
Miles

Reputation: 32468

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.

Upvotes: 2

Thomas Tempelmann
Thomas Tempelmann

Reputation: 12053

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.

Upvotes: 0

Brian Webster
Brian Webster

Reputation: 12045

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.

Upvotes: 0

Related Questions