From 2bfab10b5ce4d860c45a81b13162d3a9d8f6f0fb Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Sun, 15 Oct 2006 21:51:25 +0000 Subject: [PATCH] The device directory is now automatically created if it didn't exist. --- src/conf.h | 9 +++++++-- src/pad.c | 41 +++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/conf.h b/src/conf.h index b1981fe..2538d44 100644 --- a/src/conf.h +++ b/src/conf.h @@ -22,6 +22,11 @@ # define CONF_USER_XPATH "//configuration/users/user[@id='%s']/%s" # define CONF_SERVICE_XPATH "//configuration/services/service[@id='%s']/%s" # define CONF_USER_MAXLEN 32 +# include +# include +# ifndef PATH_MAX +# define PATH_MAX 4096 +# endif typedef struct pusb_device { @@ -41,8 +46,8 @@ typedef struct pusb_options int color_log; int one_time_pad; char hostname[32]; - char system_pad_directory[128]; - char device_pad_directory[32]; + char system_pad_directory[PATH_MAX]; + char device_pad_directory[PATH_MAX]; t_pusb_device device; } t_pusb_options; diff --git a/src/pad.c b/src/pad.c index c707de1..129f051 100644 --- a/src/pad.c +++ b/src/pad.c @@ -36,25 +36,31 @@ static FILE *pusb_pad_open_device(t_pusb_options *opts, const char *mode) { FILE *f; - char *path; - size_t path_size; + char path[PATH_MAX]; const char *mnt_point; mnt_point = (char *)libhal_volume_get_mount_point(volume); if (!mnt_point) return (NULL); - path_size = strlen(mnt_point) + 1 + strlen(opts->device_pad_directory) + - 1 + strlen(user) + 1 + strlen(opts->hostname) + strlen(".pad") + 1; - if (!(path = malloc(path_size))) + memset(path, 0x00, PATH_MAX); + if (strchr(mode, 'w')) { - log_error("malloc error!\n"); - return (NULL); + snprintf(path, PATH_MAX, "%s/%s", mnt_point, opts->device_pad_directory); + if (access(path, F_OK) != 0) + { + log_debug("Directory %s does not exist, creating one.\n", path); + if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) + { + log_debug("Unable to create directory %s: %s\n", path, + strerror(errno)); + return (NULL); + } + } + memset(path, 0x00, PATH_MAX); } - memset(path, 0x00, path_size); - snprintf(path, path_size, "%s/%s/%s.%s.pad", mnt_point, + snprintf(path, PATH_MAX, "%s/%s/%s.%s.pad", mnt_point, opts->device_pad_directory, user, opts->hostname); f = fopen(path, mode); - free(path); if (!f) { log_debug("Cannot open device file: %s\n", strerror(errno)); @@ -94,21 +100,12 @@ static FILE *pusb_pad_open_system(t_pusb_options *opts, const char *mode) { FILE *f; - char *path; - size_t path_size; + char path[PATH_MAX]; - path_size = strlen(opts->system_pad_directory) + 1 + - strlen(user) + 1 + strlen(opts->device.name) + strlen(".pad") + 1; - if (!(path = malloc(path_size))) - { - log_error("malloc error\n"); - return (NULL); - } - memset(path, 0x00, path_size); - snprintf(path, path_size, "%s/%s.%s.pad", opts->system_pad_directory, + memset(path, 0x00, PATH_MAX); + snprintf(path, PATH_MAX, "%s/%s.%s.pad", opts->system_pad_directory, user, opts->device.name); f = fopen(path, mode); - free(path); if (!f) { log_debug("Cannot open system file: %s\n", strerror(errno));