From 248e80cb5d5a411cc6362919f1b495f29fe57f9d Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 7 Jan 2025 18:17:42 -0600 Subject: Figure out how to link into the filesystem --- src/encryption.c | 58 ++++++++++++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 35 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index c7928d8..6da9603 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -35,48 +35,36 @@ int checkSodium(void) { // 5- Delete the temp file -int maketmp(const char *dest, const char *format, ...) { - char *filename = NULL, *fullpath = NULL; - struct stat fb; - int fd = -1; - va_list ap; - - // Make sure the destination exists and is a directory - if(stat(dest, &fb) < 0) - return -1; - if(!S_ISDIR(fb.st_mode)) - return -2; - - // Get the first half of the filename - va_start(ap, format); - if(vasprintf(&filename, format, ap) < 0) - return -3; - va_end(ap); - - // Get the second half of the filename - int fps = asprintf(&fullpath, "%s%s", dest, filename); // Hack to not duplicate `free(filename)` - free(filename); - if(fps < 0) - return -4; - - // Open the temp file - if((fd = open(dest, (O_WRONLY | O_CLOEXEC | O_CREAT | O_TMPFILE), S_IWUSR)) < 0) - fd = -5; - - free(fullpath); +int maketmp(const char *dest) { + int fd = open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); + if(fd < 0) + error(1, errno, "Couldn't open temp file at %s", dest); + return fd; } +#include -int main(int argc, char *argv[]) { - if(argc != 3) - error(1, 0, "USAGE: "); +int main(void) { + const char *testmsg = "we do a little testing\n"; + int fd = maketmp("."); - int fd = maketmp(argv[1], "%s.test", argv[2]); - if(fd < 0) - error(1, errno, "Couldn't open temp file"); + if(write(fd, testmsg, strlen(testmsg)) < 0) + error(1, errno, "write broke"); + + // if(linkat(fd, "", AT_FDCWD, "kys/now", AT_EMPTY_PATH) < 0) + // error(1, errno, "linkat broke"); + + char *path = NULL; + asprintf(&path, "/proc/self/fd/%d", fd); + linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); + + // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages + + if(close(fd) < 0) + error(1, errno, "close broke"); return 0; } \ No newline at end of file -- cgit v1.2.3