From 3e5ca27fdfd4737b353dbeee2671252e366fc052 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 2 Mar 2016 13:16:18 -0800 Subject: [PATCH] Initial file cleanup. --- adsbus/file.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/adsbus/file.c b/adsbus/file.c index 7844fac..237dfd3 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -9,6 +9,15 @@ #include "file.h" +static void file_open_new(char *path, struct flow *flow, void *passthrough, int flags) { + int fd = open(path, flags | O_CLOEXEC, S_IRUSR | S_IWUSR); + if (fd == -1) { + // TODO: log error; retry? + return; + } + file_fd_new(fd, flow, passthrough); +} + void file_fd_new(int fd, struct flow *flow, void *passthrough) { flow->new(fd, passthrough, NULL); // TODO: log error; retry? @@ -16,28 +25,13 @@ void file_fd_new(int fd, struct flow *flow, void *passthrough) { } void file_read_new(char *path, struct flow *flow, void *passthrough) { - int fd = open(path, O_RDONLY | O_CLOEXEC); - if (fd == -1) { - // TODO: log error; retry? - return; - } - file_fd_new(fd, flow, passthrough); + file_open_new(path, flow, passthrough, O_RDONLY); } void file_write_new(char *path, struct flow *flow, void *passthrough) { - int fd = open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, S_IRUSR | S_IWUSR); - if (fd == -1) { - // TODO: log error; retry? - return; - } - file_fd_new(fd, flow, passthrough); + file_open_new(path, flow, passthrough, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC); } void file_append_new(char *path, struct flow *flow, void *passthrough) { - int fd = open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU); - if (fd == -1) { - // TODO: log error; retry? - return; - } - flow->new(fd, flow, passthrough); + file_open_new(path, flow, passthrough, O_WRONLY | O_CREAT | O_NOFOLLOW); }