From 8702ce8bbd4d1435cc81fa8fcd7f5309d3c7b003 Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Tue, 18 Sep 2018 19:18:01 +0300 Subject: VC and secret dir are now stored with absolute path --- src/actionhelper.c | 2 +- src/ckutil.c | 6 +++++- src/ckutil.h | 6 ++++-- src/confparser.c | 12 +++++++----- unit/ck-test.c | 17 +++++++++++++++-- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/actionhelper.c b/src/actionhelper.c index 7d6aa8a..c4c1f34 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -65,5 +65,5 @@ edit_rc edit_get_config_or_suggestions(cklist *args, char *ret) { UNUSED(args); UNUSED(ret); - return ERC_ERR; + return ERC_ERR; } diff --git a/src/ckutil.c b/src/ckutil.c index 74a6b00..880696b 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -10,6 +10,7 @@ * -------------------------------------------------------------------------- */ #include #include +#include #include #include #include @@ -39,11 +40,14 @@ void util_replace_slash_with_uscore(char *s) { } } -int util_file_exists(const char* path) { +int util_file_exists(const char* path, char *absPath) { struct stat st = {0}; if (stat(path, &st) == -1) { return 0; } + if (absPath != NULL) { + realpath(path, absPath); + } return 1; } diff --git a/src/ckutil.h b/src/ckutil.h index 8f5eda8..56b9cd9 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -65,8 +65,10 @@ extern int str_is_empty(const char *s); /* Returns 1 if path is a directory, else returns 0. */ extern int util_is_dir(const char *path); -/* Returns 1 if file(or dir) exists, else returns 0. */ -extern int util_file_exists(const char *path); +/* Returns 1 if file(or dir) exists, else returns 0. + * Pass a char array in absPath to get the absolute path + * of the file it it exists. Pass NULL if no need. */ +extern int util_file_exists(const char *path, char *absPath); /* Returns 1 if file(or dir) is readable and writable, * else returns 0. */ diff --git a/src/confparser.c b/src/confparser.c index 5754dfa..948623c 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -139,17 +139,19 @@ void free_conf(Conf *conf) { } int init_create_config_file(UserOpt *opt) { - if (!util_file_exists(list_get_at(opt->args, 0))) { + char absVCdir[STR_L]; + if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) { printf("Version control directory: %s\ndoes not exist.\n", list_get_at(opt->args, 0)); return 1; } - if (!util_file_exists(list_get_at(opt->args, 1))) { + char absSRdir[STR_L]; + if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) { printf("Secret directory: %s\ndoes not exist.\n", list_get_at(opt->args, 1)); return 1; } - if (!util_file_exists(opt->confDir)) { + if (!util_file_exists(opt->confDir, NULL)) { util_mkdir(opt->confDir); } @@ -162,12 +164,12 @@ int init_create_config_file(UserOpt *opt) { char tmp[200]; strcpy(tmp, "version_control_dir = "); - strcat(tmp, list_get_at(opt->args, 0)); + strcat(tmp, absVCdir); strcat(tmp, "\n"); fputs(tmp, f); strcpy(tmp, "secret_dir = "); - strcat(tmp, list_get_at(opt->args, 1)); + strcat(tmp, absSRdir); strcat(tmp, "\n"); fputs(tmp, f); diff --git a/unit/ck-test.c b/unit/ck-test.c index 01156f1..9c18dfb 100644 --- a/unit/ck-test.c +++ b/unit/ck-test.c @@ -1,12 +1,25 @@ +#include +#include +#include + #include "cklist.h" -#include "stdio.h" #define CK_UNIT_TESTS \ X(ck_list_init, "Initialize ck list") \ X(ck_list_add, "Add elements to ck list") void ck_list_init() { - printf("Test1\n"); + cklist *ckl1 = list_make_new(); + cklist *ckl2 = list_make_and_add("first"); + list_add(ckl2, "second"); + list_add(ckl1, "first"); + list_add(ckl1, "second"); + assert(strcmp(list_get_at(ckl1, 0), + list_get_at(ckl2, 0)) == 0); + assert(strcmp(list_get_at(ckl1, 1), + list_get_at(ckl2, 1)) == 0); + list_free(ckl1); + list_free(ckl2); } void ck_list_add() { -- cgit v1.2.3