Permissions and USB key drives

May 24, '04 10:29:00AM

Contributed by: SOX

I just got my first USB 'key disk' and tried using it with 10.3 to move my files between home and work. Since I work in the Terminal a lot, I immediately noticed that the MS-DOS FAT format of the key drive changes all your permissions on the files to 777 (world read-write-execute). And that when you copy the file off the drive, these permissions don't revert to their original form.

This has three bad consequences. First if you move documents this way, they are readable by all users, including the unprivileged "nobody" on your computer. Second, all your documents become binary "executables." That is, you could try to "run" them from the command line, and if they lack descriptive suffixes, then the Finder changes their icons from generic documents to the black window icon used for unix terminal commands. Third, if you click on them, the computer may try to execute the document, not open it!!! I'll note this last operation will nearly always "crash" harmlessly, but who knows. More importantly, if your Terminal window has "./" in its path, your command line may try to run the document in preference to any similarly named command in your /bin directory.

Changing back to the correct permissions is non trivial. You can't simply execute a chmod to remove the executable status, because that will make your folders inaccessible. Plus you may have actual executables you want to preserve this status on. If all of the files you are transferring are indeed plain documents or folders, then after you copy them to your HFS or unix formatted disk, you can simply pull up a Termninal widow and type:

find /yourfolder -type f -exec chmod 640 {} \;
find /yourfolder -type -d -exec chmod 755 {} \;
This will recursively set files so that they are readable by you and the group, and folders so that they are openable by anyone. It removes write/delete privileges from everyone but you. Note: don't use this blindly on an application; normally 644 would be preferred for any .app file.

An alternative approach is to instead use Disk Utility to create an HFS-formatted sparse diskimage on your USB key drive. A sparse image can expand and contract as needed, so make it the same size as your drive so it can expand to fill the drive if need be. This is less hassle than it might seem. The sparse image will be small and mount quickly when you click on it. So the extra step of opening it is not much worse than if it were just a folder on the drive, plus it has the added advantage that you can encrypt it if you like. Encrypted or not, anything placed there will retain its privileges and ownership.

Finally, you could of course reformat the USB drive to HFS but then it would not be PC compatible.

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2004052014151826