diff --git a/src/configdir.c b/src/configdir.c index 4c6ae4d..9bc6744 100644 --- a/src/configdir.c +++ b/src/configdir.c @@ -29,6 +29,7 @@ #include #include +#include "toxic.h" #include "configdir.h" /* get the user's home directory */ @@ -38,9 +39,8 @@ void get_home_dir(char *home, int size) struct passwd *pwdbuf; const char *hmstr; char buf[NSS_BUFLEN_PASSWD]; - int rc; - rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); + int rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); if (rc == 0) { hmstr = pwd.pw_dir; @@ -102,30 +102,45 @@ char *get_user_config_dir(void) } /* - * Creates the config directory. + * Creates the config and chatlog directories. */ -int create_user_config_dir(char *path) +int create_user_config_dirs(char *path) { - int mkdir_err; - - mkdir_err = mkdir(path, 0700); struct stat buf; + int mkdir_err = mkdir(path, 0700); - if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { + if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) return -1; - } char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); + char *logpath = malloc(strlen(path) + strlen(LOGDIR) + 1); + + if (fullpath == NULL || logpath == NULL) + exit_toxic_err("failed in load_data_structures", FATALERR_MEMORY); + strcpy(fullpath, path); strcat(fullpath, CONFIGDIR); + strcpy(logpath, path); + strcat(logpath, LOGDIR); + mkdir_err = mkdir(fullpath, 0700); if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { free(fullpath); + free(logpath); return -1; } + mkdir_err = mkdir(logpath, 0700); + + if (mkdir_err && (errno != EEXIST || stat(logpath, &buf) || !S_ISDIR(buf.st_mode))) { + free(fullpath); + free(logpath); + return -1; + } + + free(logpath); free(fullpath); return 0; } diff --git a/src/configdir.h b/src/configdir.h index deded0e..aa430c5 100644 --- a/src/configdir.h +++ b/src/configdir.h @@ -28,6 +28,7 @@ #endif #define CONFIGDIR "/tox/" +#define LOGDIR "/tox/chatlogs/" #ifndef S_ISDIR #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) @@ -35,6 +36,6 @@ char *get_user_config_dir(void); void get_home_dir(char *home, int size); -int create_user_config_dir(char *path); +int create_user_config_dirs(char *path); #endif /* #define _configdir_h */ diff --git a/src/log.c b/src/log.c index 925f7ac..9828b70 100644 --- a/src/log.c +++ b/src/log.c @@ -43,7 +43,7 @@ void init_logging_session(char *name, const char *key, struct chatlog *log) name = UNKNOWN_NAME; char *user_config_dir = get_user_config_dir(); - int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name); + int path_len = strlen(user_config_dir) + strlen(LOGDIR) + strlen(name); /* use first 4 digits of key as log ident. If no key use a timestamp */ char ident[32]; @@ -66,7 +66,7 @@ void init_logging_session(char *name, const char *key, struct chatlog *log) } char log_path[MAX_STR_SIZE]; - snprintf(log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); + snprintf(log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, LOGDIR, name, ident); free(user_config_dir); diff --git a/src/toxic.c b/src/toxic.c index 930d5ac..e7d6aa3 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -571,7 +571,7 @@ static int init_data_files(void) return 0; char *user_config_dir = get_user_config_dir(); - int config_err = create_user_config_dir(user_config_dir); + int config_err = create_user_config_dirs(user_config_dir); if (DATA_FILE == NULL ) { if (config_err) {