Last active
October 22, 2021 11:41
-
-
Save Alhadis/546de1e86ab1f641fabc563d4360b5c6 to your computer and use it in GitHub Desktop.
Existence-Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/broken.lnk | |
/exists | |
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <sys/errno.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
int main(int argc, const char *argv[]){ | |
const char *path; | |
for(int i = 1; i < argc; ++i){ | |
path = argv[i]; | |
#if defined(faccessat) && defined(AT_FDCWD) && defined(AT_SYMLINK_NOFOLLOW) | |
if(faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW)) | |
goto nope; | |
#else | |
int fd; | |
if(-1 == open(path, O_RDONLY | O_SYMLINK, &fd)) | |
goto nope; | |
close(fd); | |
#endif | |
printf("%s exists\n", path); | |
} | |
return 0; | |
nope: | |
perror(argv[0]); | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* @fileoverview fs_usage(1) output | |
*/ | |
import {existsSync, accessSync, constants, openSync, closeSync} from "fs"; | |
closeSync(openSync("broken.lnk", constants.O_RDONLY | constants.O_SYMLINK)); | |
// open F=20 (R________S_X) broken.lnk | |
// close F=20 | |
existsSync(process.argv[0]); // access (___F) /usr/local/Cellar/node/16.7.0/bin/node | |
existsSync("/tmp/naaaah"); // access [ 2] (___F) private/tmp/naaaah | |
existsSync("/tmp/a.css"); // access (___F) private/tmp/a.css | |
existsSync("broken.lnk"); // access [ 2] (___F) nowhere | |
accessSync("/tmp/a.css", constants.W_OK); // access (_W__) private/tmp/a.css | |
accessSync("/tmp/a.css", constants.R_OK); // access (R___) private/tmp/a.css | |
accessSync("/tmp/a.css", constants.F_OK); // access (___F) private/tmp/a.css | |
accessSync("/tmp/a.css", constants.X_OK); // access [ 13] (__X_) private/tmp/a.css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: broken.lnk exists | |
broken.lnk: | |
ln -s nowhere $@ | |
exists: exists.c | |
cc -o $@ $^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment