diff options
Diffstat (limited to 'src/actionhelper.c')
-rw-r--r-- | src/actionhelper.c | 40 |
1 files changed, 28 insertions, 12 deletions
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; } |