+ struct stat stat;
+ if (lstat(target, &stat) == -1)
+ return false;
+ if (!S_ISDIR(stat.st_mode))
+ return false;
+
+ char temp[] = "/var/stash/_.XXXXXX";
+ if (mkdtemp(temp) == NULL)
+ return false;
+
+ if (false) undo: {
+ unlink(temp);
+ return false;
+ }
+
+ if (chmod(temp, 0755) == -1)
+ goto undo;
+
+ char destiny[strlen(temp) + 32];
+ sprintf(destiny, "%s%s", temp, APPLICATIONS);
+
+ if (unlink(APPLICATIONS) == -1)
+ goto undo;
+
+ if (rename(target, destiny) == -1) {
+ if (symlink(target, APPLICATIONS) == -1)
+ fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
+ goto undo;
+ } else {
+ if (symlink(destiny, APPLICATIONS) == -1)
+ fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
+
+ // unneccessary, but feels better (I'm nervous)
+ symlink(destiny, target);
+
+ [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
+ return true;
+ }
+}