aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2025-12-30 11:16:32 +0100
committerTristan Riehs <tristan.riehs@inria.fr>2025-12-30 11:16:32 +0100
commit965129eb3b23b64f00472a4853dd4151dc3dee75 (patch)
treead4b9ef77c7692c1f9ea4a5ab2f3fbbca186ff99 /src/main.c
parent742ebb0218ce278b9e15e0f683db50c717559192 (diff)
Create routine for copying a file
Also restrict access rights.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c83
1 files changed, 38 insertions, 45 deletions
diff --git a/src/main.c b/src/main.c
index c13c14a..d193601 100644
--- a/src/main.c
+++ b/src/main.c
@@ -79,6 +79,42 @@ static void assert_db_exists(void)
exit(EXIT_FAILURE);
}
+/* Copy the file whose path is IN at path OUT. OUT is created or overwritten if
+ * needed. */
+static void copy_file(const char *in, const char *out)
+{
+ int in_fd;
+ int out_fd;
+ int rc;
+ ssize_t written_bytes;
+ in_fd = open(in, O_RDONLY);
+ if (in_fd == -1) {
+ fprintf(stderr, "open: %s:", in);
+ perror("");
+ exit(EXIT_FAILURE);
+ }
+ out_fd = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ if (out_fd == -1) {
+ fprintf(stderr, "open: %s:", out);
+ perror("");
+ exit(EXIT_FAILURE);
+ }
+ while ((written_bytes = sendfile(out_fd, in_fd, NULL, 4096)) == 4096)
+ ;
+ if (written_bytes == -1) {
+ perror("sendfile");
+ exit(EXIT_FAILURE);
+ }
+ rc = fchmod(out_fd, 0400);
+ if (rc == -1) {
+ fprintf(stderr, "chmod: %s: ", out);
+ perror("");
+ exit(EXIT_FAILURE);
+ }
+ close(in_fd);
+ close(out_fd);
+}
+
/* Prompt the user for yes or no (default is yes). Before calling, a prompt
* should be printed to stdout, eventually not with an ending newline. */
static int prompt_yes_no(void)
@@ -326,32 +362,14 @@ static void ftag_export(int argc, char **argv)
ssize_t line_len;
char *line = malloc(line_size);
char in[512];
- int in_fd;
char out[128];
char extension[16];
- int out_fd;
while ((line_len = getline(&line, &line_size, stdin)) != -1) {
remove_ending_newline(line);
ftag_file_get_extension(extension, sizeof(extension), line);
strbuild(in, "%s/files/%s", FTAG_ROOT, line);
strbuild(out, "%s/%s.%s", tmp_dir, line, extension);
- in_fd = open(in, O_RDONLY);
- if (in_fd == -1) {
- fprintf(stderr, "open: %s:", in);
- perror("");
- exit(EXIT_FAILURE);
- }
- out_fd = open(out, O_WRONLY | O_CREAT, 0644);
- if (out_fd == -1) {
- fprintf(stderr, "open: %s:", out);
- perror("");
- exit(EXIT_FAILURE);
- }
- ssize_t written_bytes;
- while ((written_bytes = sendfile(out_fd, in_fd, NULL, 4096)) == 4096)
- ;
- close(in_fd);
- close(out_fd);
+ copy_file(in, out);
}
free(line);
@@ -557,33 +575,8 @@ ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum
char new_path[512];
memset(new_path, 0, sizeof(new_path));
strbuild(new_path, "%s/files/%s", FTAG_ROOT, canonical_name);
- int out_fd = open(new_path, O_WRONLY | O_CREAT, 0644);
-
- if (out_fd == -1) {
- fprintf(stderr, "open: \"%s\": ", new_path);
- perror("");
- exit(EXIT_FAILURE);
- }
-
- int in_fd = open(file, O_RDONLY);
-
- if (in_fd == -1) {
- fprintf(stderr, "open: \"%s\": ", file);
- perror("");
- exit(EXIT_FAILURE);
- }
-
- ssize_t written_bytes;
- while ((written_bytes = sendfile(out_fd, in_fd, NULL, 4096)) == 4096)
- ;
+ copy_file(file, new_path);
- if (written_bytes == -1) {
- perror("sendfile");
- exit(EXIT_FAILURE);
- }
-
- close(in_fd);
- close(out_fd);
free(date_str);
free(full_name);
free(canonical_name);