From 12e2c46d6a778f131c1ca6a7134af1cd5e722dbe Mon Sep 17 00:00:00 2001 From: Edward Tomasz Napierala Date: Sat, 29 Jul 2017 12:28:49 +0100 Subject: Add wrappers to reduce #ifdefs. Signed-off-by: Edward Tomasz Napierala --- src/core/capsicum.c | 17 +++++++++++++++++ src/core/capsicum.h | 12 +++++++++++- src/core/log.c | 23 ++--------------------- src/core/rawlog.c | 29 ++++++----------------------- src/fe-common/core/fe-log.c | 11 +---------- 5 files changed, 37 insertions(+), 55 deletions(-) diff --git a/src/core/capsicum.c b/src/core/capsicum.c index e87704dd..99a4e795 100644 --- a/src/core/capsicum.c +++ b/src/core/capsicum.c @@ -161,6 +161,14 @@ int capsicum_open(const char *path, int flags, int mode) return (fd); } +int capsicum_open_wrapper(const char *path, int flags, int mode) +{ + if (capsicum_enabled()) { + return capsicum_open(path, flags, mode); + } + return open(path, flags, mode); +} + void capsicum_mkdir_with_parents(const char *path, int mode) { char *component, *copy, *tofree; @@ -201,6 +209,15 @@ void capsicum_mkdir_with_parents(const char *path, int mode) close(fd); } +void capsicum_mkdir_with_parents_wrapper(const char *path, int mode) +{ + if (capsicum_enabled()) { + capsicum_mkdir_with_parents(path, mode); + return; + } + g_mkdir_with_parents(path, mode); +} + nvlist_t *symbiont_connect(const nvlist_t *request) { nvlist_t *response; diff --git a/src/core/capsicum.h b/src/core/capsicum.h index 1eb56402..3c1234e1 100644 --- a/src/core/capsicum.h +++ b/src/core/capsicum.h @@ -7,7 +7,17 @@ int capsicum_net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6); int capsicum_open(const char *path, int flags, int mode); void capsicum_mkdir_with_parents(const char *path, int mode); +#ifdef HAVE_CAPSICUM +int capsicum_open_wrapper(const char *path, int flags, int mode); +void capsicum_mkdir_with_parents_wrapper(const char *path, int mode); +#else +#define capsicum_open_wrapper(P, F, M) \ + open(P, F, M) +#define capsicum_mkdir_with_parents_wrapper(P, M) \ + g_mkdir_with_parents(P, M) +#endif + void capsicum_init(void); void capsicum_deinit(void); -#endif +#endif /* !__CAPSICUM_H */ diff --git a/src/core/log.c b/src/core/log.c index 9394263f..ea2f1a1d 100644 --- a/src/core/log.c +++ b/src/core/log.c @@ -26,9 +26,7 @@ #include "servers.h" #include "log.h" #include "write-buffer.h" -#ifdef HAVE_CAPSICUM #include "capsicum.h" -#endif #include "lib-config/iconfig.h" #include "settings.h" @@ -76,16 +74,6 @@ static void log_write_timestamp(int handle, const char *format, if (text != NULL) write_buffer(handle, text, strlen(text)); } -static int log_open_wrapper(const char *path, int flags, int mode) -{ -#ifdef HAVE_CAPSICUM - if (capsicum_enabled()) - return capsicum_open(path, flags, mode); -#endif - - return open(path, flags, mode); -} - static char *log_filename(LOG_REC *log) { char *str, fname[1024]; @@ -127,19 +115,12 @@ int log_start_logging(LOG_REC *log) /* path may contain variables (%time, $vars), make sure the directory is created */ dir = g_path_get_dirname(log->real_fname); -#ifdef HAVE_CAPSICUM - if (capsicum_enabled()) - capsicum_mkdir_with_parents(dir, log_dir_create_mode); - else - g_mkdir_with_parents(dir, log_dir_create_mode); -#else - g_mkdir_with_parents(dir, log_dir_create_mode); -#endif + capsicum_mkdir_with_parents_wrapper(dir, log_dir_create_mode); g_free(dir); } log->handle = log->real_fname == NULL ? -1 : - log_open_wrapper(log->real_fname, O_WRONLY | O_APPEND | O_CREAT, + capsicum_open_wrapper(log->real_fname, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode); if (log->handle == -1) { signal_emit("log create failed", 1, log); diff --git a/src/core/rawlog.c b/src/core/rawlog.c index dd86af7c..8f674e6b 100644 --- a/src/core/rawlog.c +++ b/src/core/rawlog.c @@ -27,25 +27,13 @@ #include "misc.h" #include "write-buffer.h" #include "settings.h" -#ifdef HAVE_CAPSICUM #include "capsicum.h" -#endif #include "servers.h" static int rawlog_lines; static int signal_rawlog; -static int rawlog_open_wrapper(const char *path, int flags, int mode) -{ -#ifdef HAVE_CAPSICUM - if (capsicum_enabled()) - return capsicum_open(path, flags, mode); -#endif - - return open(path, flags, mode); -} - RAWLOG_REC *rawlog_create(void) { RAWLOG_REC *rec; @@ -139,8 +127,9 @@ void rawlog_open(RAWLOG_REC *rawlog, const char *fname) return; path = convert_home(fname); - rawlog->handle = rawlog_open_wrapper(path, O_WRONLY | O_APPEND | O_CREAT, - log_file_create_mode); + rawlog->handle = capsicum_open_wrapper(path, + O_WRONLY | O_APPEND | O_CREAT, + log_file_create_mode); g_free(path); if (rawlog->handle == -1) { @@ -167,18 +156,12 @@ void rawlog_save(RAWLOG_REC *rawlog, const char *fname) int f; dir = g_path_get_dirname(fname); -#ifdef HAVE_CAPSICUM - if (capsicum_enabled()) - capsicum_mkdir_with_parents(dir, log_dir_create_mode); - else - g_mkdir_with_parents(dir, log_dir_create_mode); -#else - g_mkdir_with_parents(dir, log_dir_create_mode); -#endif + capsicum_mkdir_with_parents_wrapper(dir, log_dir_create_mode); g_free(dir); path = convert_home(fname); - f = rawlog_open_wrapper(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode); + f = capsicum_open_wrapper(path, O_WRONLY | O_APPEND | O_CREAT, + log_file_create_mode); g_free(path); if (f < 0) { diff --git a/src/fe-common/core/fe-log.c b/src/fe-common/core/fe-log.c index deb70991..3d8910c9 100644 --- a/src/fe-common/core/fe-log.c +++ b/src/fe-common/core/fe-log.c @@ -30,9 +30,7 @@ #include "special-vars.h" #include "settings.h" #include "lib-config/iconfig.h" -#ifdef HAVE_CAPSICUM #include "capsicum.h" -#endif #include "fe-windows.h" #include "window-items.h" @@ -454,14 +452,7 @@ static void autolog_open(SERVER_REC *server, const char *server_tag, log_item_add(log, LOG_ITEM_TARGET, target, server_tag); dir = g_path_get_dirname(log->real_fname); -#ifdef HAVE_CAPSICUM - if (capsicum_enabled()) - capsicum_mkdir_with_parents(dir, log_dir_create_mode); - else - g_mkdir_with_parents(dir, log_dir_create_mode); -#else - g_mkdir_with_parents(dir, log_dir_create_mode); -#endif + capsicum_mkdir_with_parents_wrapper(dir, log_dir_create_mode); g_free(dir); log->temp = TRUE; -- cgit v1.2.3