#define DEBUG 1
#include <libc.h>
#include <stdint.h>
#include <inttypes.h>
#include <bless.h>
#include <AssertMacros.h>
void usage() {
fprintf(stderr, "Usage: %s [ mountpoint cnid | path ]\n", getprogname());
exit(1);
}
int main(int argc, char *argv[]) {
if(argc < 2 || argc > 3) {
usage();
}
if(argc == 2) {
char *path = argv[1];
int isHFS = 0;
struct stat sb;
struct statfs sf;
uint32_t fileID = 0;
char newpath[MAXPATHLEN];
require_noerr(lstat(path, &sb), cantStat);
if(S_ISLNK(sb.st_mode)) {
printf("Success: %s is a symlink\b", path);
return 0;
}
require_noerr(statfs(path, &sf), cantStat);
require_noerr(BLIsMountHFS(NULL, path, &isHFS), notHFS);
require(isHFS == 1, notHFS);
require_noerr(BLGetFileID(NULL, path, &fileID), error);
require(fileID == sb.st_ino, error);
require_noerr(BLLookupFileIDOnMount(NULL,
sf.f_mntonname,
fileID,
newpath), error);
require(strcmp(newpath, path) == 0, cantstrcmp);
printf("Success: %s\n", newpath);
return 0;
error:
printf("Error: %s\n", path);
return 1;
cantstrcmp:
printf("%s != %s\n", newpath, path);
return 1;
cantStat:
printf("%s: %s\n",path, strerror(errno));
return 1;
notHFS:
printf("%s not on an HFS+ filesystem\n", path);
return 1;
} else if(argc == 3) {
char *path = argv[1];
int isHFS = 0;
struct statfs sf;
uint32_t fileID = 0;
char newpath[MAXPATHLEN];
fileID = strtoul(argv[2], NULL, 0);
require(fileID > 0, error2);
require_noerr(statfs(path, &sf), cantStat2);
require_noerr(BLIsMountHFS(NULL, path, &isHFS), notHFS2);
require(isHFS == 1, notHFS2);
require_noerr(BLLookupFileIDOnMount(NULL,
sf.f_mntonname,
fileID,
newpath), error2);
printf("Success: %s\n", newpath);
return 0;
error2:
return 1;
cantStat2:
printf("%s: %s\n",path, strerror(errno));
return 1;
notHFS2:
printf("%s not on an HFS+ filesystem\n", path);
return 1;
}
return 0;
}