diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/actionhelper.c | 40 | ||||
-rw-r--r-- | src/actions.c | 4 | ||||
-rw-r--r-- | src/ck.c | 5 | ||||
-rw-r--r-- | src/ckerrlog.c | 86 | ||||
-rw-r--r-- | src/ckerrlog.h | 40 | ||||
-rw-r--r-- | src/ckutil.c | 13 | ||||
-rw-r--r-- | src/ckutil.h | 3 | ||||
-rw-r--r-- | src/confparser.c | 12 | ||||
-rw-r--r-- | src/confparser.h | 5 | ||||
-rw-r--r-- | src/dblayer.c | 2 |
11 files changed, 185 insertions, 27 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1228e82..c672bcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ set(ckLib_src ${SRC_DIR}/dbhelper.c ${SRC_DIR}/ckutil.c ${SRC_DIR}/cklist.c + ${SRC_DIR}/ckerrlog.c ) set(ckLib_hdr @@ -66,6 +67,7 @@ set(ckLib_hdr ${SRC_DIR}/ckutil.h ${SRC_DIR}/cklist.h ${SRC_DIR}/ckinfo.h + ${SRC_DIR}/ckerrlog.h ) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RES_DIR}/cmake) diff --git a/src/actionhelper.c b/src/actionhelper.c index 667b898..883229f 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -12,6 +12,8 @@ * * * -------------------------------------------------------------------------- */ +#include <libgen.h> + #include "actionhelper.h" char add_err[STR_M] = ""; @@ -26,9 +28,6 @@ int add_err_message(char *err) { return 0; } -int copy_config(char *path); - - void link_config(const AddOpt *opt, const char* newPath) { printf("Linking %s -> %s\n", newPath, opt->confPath); if (util_symlink_file(newPath, opt->confPath) != 0) { @@ -36,16 +35,22 @@ void link_config(const AddOpt *opt, const char* newPath) { } } -void move_config(const AddOpt *opt, const Conf *conf, char *ret) { - char newName[STR_M]; - str_make_ck_config_name(newName, opt->confPath, opt->progName); +int move_config(const AddOpt *opt, char *progDir, char *ret) { char newPath[STR_L]; - str_join_dirname_with_basename(newPath, opt->secret ? conf->SCRT_dir : conf->VC_dir, newName); - printf("Moving %s -> %s\n", opt->confPath, newPath); + char *tmp = strdup(opt->confPath); + str_join_dirname_with_basename(newPath, progDir, basename(tmp)); + free(tmp); + if (util_file_exists(newPath, NULL)) { + strcpy(add_err, "File already exists");\ + return -1; + } strcpy(ret, newPath); + printf("Moving %s -> %s\n", opt->confPath, newPath); if (util_move_file(opt->confPath, newPath) != 0) { strcpy(add_err, "Could not move file."); + return -1; } + return 0; } AddOpt add_make_options(cklist *args) { @@ -59,7 +64,8 @@ AddOpt add_make_options(cklist *args) { }; list_next(args); - if (!util_is_file_rw(list_get(args))) { + if (!util_is_file_rw(list_get(args)) + || !util_is_file_link(list_get(args))) { addOpt.err = ADD_ERR_WRONG_CONFIG; return addOpt; } @@ -117,10 +123,20 @@ void add_print_opts(AddOpt *opt) { } } -void add_make_link(const AddOpt *opt, - const Conf *conf) { +void get_or_make_program_dir(const AddOpt *opt, const Conf *conf, char *ret) { + char tmp[STR_L] = ""; + str_join_dirname_with_basename(tmp, opt->secret ? conf->scrt_dir : conf->vc_dir, opt->progName); + if (!util_file_exists(tmp, NULL)) { + util_mkdir(tmp); + } + strcpy(ret, tmp); +} + +void add_make_link(const AddOpt *opt, const Conf *conf) { + char progDir[STR_L]; + get_or_make_program_dir(opt, conf, progDir); char newPath[STR_L]; - move_config(opt, conf, newPath); + move_config(opt, progDir, newPath); if (add_err_message(NULL)) { return; } diff --git a/src/actions.c b/src/actions.c index 59343ab..186bf9d 100644 --- a/src/actions.c +++ b/src/actions.c @@ -48,7 +48,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { case ADD_NO_ERR: break; case ADD_ERR_WRONG_CONFIG: - PRINT_ERR("The config file specified doesn't exist."); + PRINT_ERR("The config file specified doesn't exist or is a link."); goto error; case ADD_ERR_WRONG_FLAGS: PRINT_ERR("Flags are: -s for secret and -p for primary."); @@ -114,7 +114,7 @@ int run_EDIT(UserOpt *opt, Conf *conf) { char confName[STR_M]; int secret = 0; if (edit_get_prime_config_from_program(&db, list_get(opt->args), confName, &secret) == 1) { - str_join_dirname_with_basename(confPath, secret ? conf->SCRT_dir : conf->VC_dir, confName); + str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName); printf("%s\n", confPath); } else { PRINT_ERR("No primary config"); @@ -32,10 +32,12 @@ #include "dblayer.h" #include "cklist.h" #include "ckutil.h" +#include "ckerrlog.h" int main(int argc, char *argv[]) { + initialize_errlog(); UserOpt opt; - Conf conf = {.VC_dir = NULL, .SCRT_dir = NULL}; + Conf conf = {.vc_dir = NULL, .scrt_dir = NULL}; /* get user opt */ switch(parse_action(argc, argv, &opt)) { case APR_HELP: @@ -76,5 +78,6 @@ int main(int argc, char *argv[]) { error: free_user_opt(&opt); free_conf(&conf); + report_errlog(); return 0; } diff --git a/src/ckerrlog.c b/src/ckerrlog.c new file mode 100644 index 0000000..1be1c08 --- /dev/null +++ b/src/ckerrlog.c @@ -0,0 +1,86 @@ +/* ckerrlog.c - Error report and logging for ck ------------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * -------------------------------------------------------------------------- */ +#include <time.h> + +#include "ckerrlog.h" +#include "ckutil.h" + +#define COMPONENT "ckerrlog" + +static char *err; +static char *log; +static int loglvl; +static char buf[STR_M]; + +char * get_time() { + time_t rawtime; + struct tm * timeinfo; + time (&rawtime); + timeinfo = localtime (&rawtime); + strftime (buf,80,"[%c]",timeinfo); + return buf; +} + +void initialize_errlog() { + err = NULL; + log = NULL; + loglvl = 0; + cklog("%s Log session started", get_time()); +} + +void report_errlog() { + if (err) + printf("Errors:\n%s", err); + free(err); + if (log) + printf("Logs:\n%s", log); + free(log); +} + +#define X(stream) \ + void add_ ## stream ## _with_delim(char *delim, char *txt, \ + va_list args) { \ + char tmp[STR_L]; \ + vsprintf(tmp, txt, args); \ + if (stream) { \ + stream = (char *)realloc(stream, strlen(stream) + \ + strlen(tmp) + strlen(delim) + 1); \ + strcat(stream, tmp); \ + strcat(stream, delim); \ + return; \ + } \ + stream = (char *)malloc(strlen(tmp) + strlen(delim) + 1); \ + strcpy(stream, tmp); \ + strcat(stream, delim); \ + } +CK_STREAMS +#undef X + +void ckerr(char *txt, ...) { + va_list args; + va_start(args, txt); + add_err_with_delim("\n", txt, args); + va_end(args); +} + +void cklog(char *txt, ...) { + va_list args; + va_start(args, txt); + add_log_with_delim("\n", txt, args); + va_end(args); +} + +void ckerr_add_component(char *txt, ...) { + va_list args; + va_start(args, txt); + add_err_with_delim(" ", txt, args); + va_end(args); +} diff --git a/src/ckerrlog.h b/src/ckerrlog.h new file mode 100644 index 0000000..b254d4b --- /dev/null +++ b/src/ckerrlog.h @@ -0,0 +1,40 @@ +/* ckerrlog.h - Error report and logging for ck ------------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * -------------------------------------------------------------------------- */ +#ifndef CKERRLOG_H +#define CKERRLOG_H + +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <stdio.h> + +#define CK_STREAMS \ + X(err) \ + X(log) + +void ckerr_add_component(char *txt, ...); + +#define ERR(...) \ + ckerr_add_component("-[%s]", COMPONENT); \ + ckerr(__VA_ARGS__); + +typedef struct st_ErrLog ErrLog; +struct st_ErrLog { + char *err; + char *log; +}; + +extern void initialize_errlog(); +extern void report_errlog(); +extern void ckerr(char *err, ...); +extern void cklog(char *log, ...); + +#endif /* CKERRLOG_H */ diff --git a/src/ckutil.c b/src/ckutil.c index 880696b..a477b36 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -45,19 +45,28 @@ int util_file_exists(const char* path, char *absPath) { if (stat(path, &st) == -1) { return 0; } - if (absPath != NULL) { + if (absPath) { realpath(path, absPath); } return 1; } -int util_is_file_rw(const char* path) { +int util_is_file_rw(const char *path) { if (access(path, R_OK | W_OK) == 0) { return 1; } return 0; } +int util_is_file_link(const char *path) { + struct stat buf; + lstat(path, &buf); + if (S_ISLNK(buf.st_mode)) { + return 0; + } + return 1; +} + void util_mkdir(const char *name) { mkdir(name, 0755); } diff --git a/src/ckutil.h b/src/ckutil.h index 56b9cd9..a16a5de 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -74,6 +74,9 @@ extern int util_file_exists(const char *path, char *absPath); * else returns 0. */ extern int util_is_file_rw(const char *path); +/* Returns 1 if file is link 0 otherwise */ +extern int util_is_file_link(const char *path); + extern void util_replace_slash_with_uscore(char *s); /* Wrapper around mkdir with 0775 permissions */ diff --git a/src/confparser.c b/src/confparser.c index ea9bb90..7f61037 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -19,8 +19,8 @@ const char * const CONFIG_NAME = "/ckrc"; void conf_values_initialize(Conf *c) { - c->SCRT_dir = NULL; - c->VC_dir = NULL; + c->scrt_dir = NULL; + c->vc_dir = NULL; } int remove_newline(char buff[]) { @@ -128,11 +128,11 @@ int config_file_parse(Conf *conf, UserOpt *opt) { } void free_conf(Conf *conf) { - if (conf->VC_dir) { - free(conf->VC_dir); + if (conf->vc_dir) { + free(conf->vc_dir); } - if (conf->SCRT_dir) { - free(conf->SCRT_dir); + if (conf->scrt_dir) { + free(conf->scrt_dir); } } diff --git a/src/confparser.h b/src/confparser.h index a07972b..9587318 100644 --- a/src/confparser.h +++ b/src/confparser.h @@ -19,9 +19,8 @@ #include "actionparser.h" #define CONFIG_VARIABLES_TABLE \ - X(VC_dir, " version_control_dir = %s ", "Version Control directory") \ - X(SCRT_dir, " secret_dir = %s " , "Secret directory") - + X(vc_dir, " version_control_dir = %s ", "Version Control directory") \ + X(scrt_dir, " secret_dir = %s " , "Secret directory") typedef enum ConfingVariables ConfVar; enum ConfingVariables { diff --git a/src/dblayer.c b/src/dblayer.c index 41e8bfe..8ec062c 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -351,7 +351,7 @@ int add_get_or_insert_program_to_db(DB *db, const char *name) { return -1; } if (pid == -1) { - return insert_to_program_table(db,name); + return insert_to_program_table(db, name); } return pid; } |