diff options
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 58 |
1 files changed, 23 insertions, 35 deletions
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) { | |||
| 35 | // 5- Delete the temp file | 35 | // 5- Delete the temp file |
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | int maketmp(const char *dest, const char *format, ...) { | 38 | int maketmp(const char *dest) { |
| 39 | char *filename = NULL, *fullpath = NULL; | 39 | int fd = open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); |
| 40 | struct stat fb; | 40 | if(fd < 0) |
| 41 | int fd = -1; | 41 | error(1, errno, "Couldn't open temp file at %s", dest); |
| 42 | va_list ap; | 42 | |
| 43 | |||
| 44 | // Make sure the destination exists and is a directory | ||
| 45 | if(stat(dest, &fb) < 0) | ||
| 46 | return -1; | ||
| 47 | if(!S_ISDIR(fb.st_mode)) | ||
| 48 | return -2; | ||
| 49 | |||
| 50 | // Get the first half of the filename | ||
| 51 | va_start(ap, format); | ||
| 52 | if(vasprintf(&filename, format, ap) < 0) | ||
| 53 | return -3; | ||
| 54 | va_end(ap); | ||
| 55 | |||
| 56 | // Get the second half of the filename | ||
| 57 | int fps = asprintf(&fullpath, "%s%s", dest, filename); // Hack to not duplicate `free(filename)` | ||
| 58 | free(filename); | ||
| 59 | if(fps < 0) | ||
| 60 | return -4; | ||
| 61 | |||
| 62 | // Open the temp file | ||
| 63 | if((fd = open(dest, (O_WRONLY | O_CLOEXEC | O_CREAT | O_TMPFILE), S_IWUSR)) < 0) | ||
| 64 | fd = -5; | ||
| 65 | |||
| 66 | free(fullpath); | ||
| 67 | return fd; | 43 | return fd; |
| 68 | } | 44 | } |
| 69 | 45 | ||
| 70 | 46 | ||
| 47 | #include <string.h> | ||
| 71 | 48 | ||
| 72 | int main(int argc, char *argv[]) { | 49 | int main(void) { |
| 73 | if(argc != 3) | 50 | const char *testmsg = "we do a little testing\n"; |
| 74 | error(1, 0, "USAGE: <dest> <filename>"); | ||
| 75 | 51 | ||
| 52 | int fd = maketmp("."); | ||
| 76 | 53 | ||
| 77 | int fd = maketmp(argv[1], "%s.test", argv[2]); | 54 | if(write(fd, testmsg, strlen(testmsg)) < 0) |
| 78 | if(fd < 0) | 55 | error(1, errno, "write broke"); |
| 79 | error(1, errno, "Couldn't open temp file"); | 56 | |
| 57 | // if(linkat(fd, "", AT_FDCWD, "kys/now", AT_EMPTY_PATH) < 0) | ||
| 58 | // error(1, errno, "linkat broke"); | ||
| 59 | |||
| 60 | char *path = NULL; | ||
| 61 | asprintf(&path, "/proc/self/fd/%d", fd); | ||
| 62 | linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); | ||
| 63 | |||
| 64 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages | ||
| 65 | |||
| 66 | if(close(fd) < 0) | ||
| 67 | error(1, errno, "close broke"); | ||
| 80 | 68 | ||
| 81 | return 0; | 69 | return 0; |
| 82 | } \ No newline at end of file | 70 | } \ No newline at end of file |
