Following up on discussions and examples
here and
here, I created two tools to create and remove hard links to directories on HFS+ filesystems. The code was copied from the discussion sites, but in keeping with the admonitions to be very, very careful in using such links I augmented the code to be a bit more precise. The code for creating a directory link is
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr,"Usage: hlink_dir <src_dir> <target_dir>\n");
return 1;
}
struct stat st;
stat(argv[1], &st);
if ( st.st_mode & S_IFDIR ) {
int ret = link(argv[1], argv[2]);
if (ret != 0) perror("link");
return ret;
} else {
fprintf(stderr,"Usage: hlink_dir <src_dir> <target_dir>\n");
fprintf(stderr,"<src_dir> must be a directory\n");
return 2;
}
}
which was turned into an executable via
gcc -o /usr/local/bin/hlink_dir hlink.c -Wall
and the code for the link remover is
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr,"Usage: unhlink_dir <target_dir>\n");
return 1;
}
struct stat st;
stat(argv[1], &st);
if ( st.st_mode & S_IFDIR ) {
int ret = unlink(argv[1]);
if (ret != 0) perror("link");
return ret;
} else {
fprintf(stderr,"Usage: unhlink_dir <target_dir>\n");
fprintf(stderr,"<target_dir> must be a directory\n");
return 2;
}
}
which was similarly turned to an executable via
gcc -o /usr/local/bin/unhlink_dir