If you really want to try hard linking directories, you actually can do that in Mac OS X Leopard and later. Mr_Noodle is correct -- Time Machine uses hard linked directories to simplify a lot of the backup work it has to do. The caveat is that it's not easy. You have to write and compile your own hard linking utility to do that, since the built-in ln command will check to see if you're trying to hard link a directory and then tell you that you can't. I'll post up the code for that utility at the end of this post, if you want to go down that road (which I don't suggest).
Right now, I'm just going to show you a script I wrote that attempts to mirror a directory structure using hard links.
You'll want to make sure this rule only works on folders and then those that Hazel hasn't matched before, so you would set the rule to match a folder with the date last matched being blank. The action is an embedded script, like before, except with the "shell" being /usr/bin/python and the following as its contents:
- Code: Select all
import os;
import sys;
import subprocess;
# replace the path in between the quotes with the folder where you want folders to end up
destinationPath = "/absolute/path/to/destination/";
sourcePathLength = len(sys.argv[1]);
for current, directories, files in os.walk(sys.argv[1]):
currentDestination = os.path.join(destinationPath, current[sourcePathLength:]);
if not os.path.exists(currentDestination):
os.mkdir(currentDestination);
for item in files:
subprocess.Popen(["ln", os.path.join(current, item), os.path.join(currentDestination, item)], stdout = subprocess.PIPE);
For files, another rule with a similar structure to the one given above (for symbolic links, so, in the case, omit the -s flag) will work.
If you have the Apple Developer Tools installed and you feel a bit adventurous, then you can write some C code that will let you hard link folders directly. Keep in mind, though, that this is a bit dangerous and I haven't tried it. Even though Apple tried to make this safe, I'm not sure if it really is. The danger is that you could create an infinite loop inside the file structure with a folder containing a hard link to itself, which will then contain that same hard link. The same thing can happen with symbolic links, but symbolic links are functionally different from the file itself. That is not the case with hard links and the operating system cannot tell them apart. I believe that Apple does try to keep you from shooting yourself in your foot, but again, use caution when using this method.
You'll need to, first, create the command that will hard link directories. You'll have to compile this command yourself. Open up a text editor (in plain text mode, in the case of TextEdit) and type in the following code:
- Code: Select all
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
if (argc != 3)
return 1;
int ret = link(argv[1], argv[2]);
if (ret != 0)
perror("link");
return ret;
}
Save this file as hlink.c to a convenient location, like your Desktop.
Next, you’ll have to compile that code. Open up Terminal.app (in /Applications/Utilities/) and type in the following (if you saved it to your Desktop, if not, then change your directory to the location you saved it):
- Code: Select all
cd ~/Desktop
gcc -o hlink hlink.c -Wall
chmod 755 hlink
This will give you another file on your Desktop called hlink. This command can be executed (because we changed its permissions) now. I would suggest placing the hlink file in a safe place (like /usr/bin/, since it’s probably already on your path) so you can access the command from anywhere on your computer.
Next, you can just invoke it like the ln command as seen in the previous symbolic link example referenced earlier.
If you want an exhaustive review of hard linking directories,
Exploring Leopard with DTrace by Greg Miller is an excellent place to start (all of the credit for the above C code goes to Greg), if you scroll all the way to the last few paragraphs of the article.