From d015a5e66ac9b92bba4eb5020e7bf2384f65ed25 Mon Sep 17 00:00:00 2001 From: gramanas Date: Tue, 13 Nov 2018 01:29:29 +0200 Subject: Homedir finished --- src/actionhelper.c | 2 +- src/actions.c | 11 +++++------ src/ckutil.c | 16 ++++++++-------- src/ckutil.h | 4 ++-- src/dblayer.c | 50 +++++++++++++++++++++++++++++++++++++------------- src/dblayer.h | 8 ++++---- unit/ck-test.c | 8 ++++---- 7 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/actionhelper.c b/src/actionhelper.c index 1d2a518..c0dff0f 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -151,7 +151,7 @@ void edit_print_suggested_configs(DB *db, const char *pName) { strcat(name, pName); strcat(name, ":"); cklist *paths = list_make_and_add(name); - get_program_paths(db, paths, pName, 1, 0); + get_program_paths(db, paths, pName, 1, 0, NULL); list_print(paths); list_free(paths); } diff --git a/src/actions.c b/src/actions.c index 36e3a42..990b371 100644 --- a/src/actions.c +++ b/src/actions.c @@ -56,7 +56,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { } add_print_opts(&addOpt); /* Try adding the new config to the DB */ - if (add_transaction_try(&db, &addOpt)) { + if (add_transaction_try(&db, &addOpt, conf->home_dir)) { goto error; } add_make_link(&addOpt, conf); @@ -190,13 +190,13 @@ int run_LIST(UserOpt *opt, Conf *conf) { char tmp[STR_L] = ""; switch(listOpt._lt) { case LT_PATH: - list_get_paths(&db, the_list, listOpt.bName, listOpt.attr); + list_get_paths(&db, the_list, listOpt.bName, listOpt.attr, conf->home_dir); break; case LT_PROGRAM: list_get_programs(&db, the_list); break; case LT_TREE: - list_get_path_program_tree(&db, the_list, listOpt.bName, listOpt.attr); + list_get_path_program_tree(&db, the_list, listOpt.bName, listOpt.attr, conf->home_dir); list_print(the_list); goto close; case LT_CKCONF: @@ -218,7 +218,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { ERR("Program %s doesn't exist in the database.", listOpt.pName); goto error; } - get_program_paths(&db, the_list, listOpt.pName, listOpt.bName, listOpt.attr); + get_program_paths(&db, the_list, listOpt.pName, listOpt.bName, listOpt.attr, conf->home_dir); break; } switch(listOpt._lst) { @@ -242,7 +242,6 @@ int run_LIST(UserOpt *opt, Conf *conf) { } int run_SEARCH(UserOpt *opt, Conf *conf) { - UNUSED(conf); if (system("which grep > /dev/null 2>&1") != 0) { ERR("No grep avaliable. Please make sure you have grep installed."); return -1; @@ -256,7 +255,7 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { } cklist *paths = list_make_new(); - list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/); + list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/, conf->home_dir); close_DB(&db); if (list_size(paths) && list_size(opt->args)) { do { diff --git a/src/ckutil.c b/src/ckutil.c index 0895677..8ae7e41 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -143,9 +143,9 @@ int str_is_empty(const char *s) { return 1; } -int swap_tilde_with_home(char *ret, const char *s, const char *home) { +char * swap_tilde_with_home(char *ret, const char *s, const char *home) { if (!ret || !s || !home) { - return -1; + return NULL; } ulong i; ulong homelen = strlen(home); @@ -154,22 +154,22 @@ int swap_tilde_with_home(char *ret, const char *s, const char *home) { for (i = 1; i < strlen(s); i++) { ret[homelen - 1 + i] = s[i]; } - return 0; + return ret; } - return -1; + return NULL; } -int swap_home_with_tilde(char *ret, const char *s, const char *home) { +char * swap_home_with_tilde(char *ret, const char *s, const char *home) { if (!ret || !s || !home) { - return -1; + return NULL; } ulong i; ulong homelen = strlen(home); for (i = 0; i < strlen(home); i++) { if (s[i] != home[i]) { - return -1; + return NULL; } } /* s starts with $HOME */ @@ -177,7 +177,7 @@ int swap_home_with_tilde(char *ret, const char *s, const char *home) { for (i = homelen; i < strlen(s); i++) { ret[i + 1 - homelen] = s[i]; } - return 0; + return ret; } int util_own_grp_copy(const char *dest, const char *original) { diff --git a/src/ckutil.h b/src/ckutil.h index b9d94db..1a8c162 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -59,8 +59,8 @@ extern void str_join_dirname_with_basename(char *ret, const char *path, * else returns 0. */ extern int str_is_empty(const char *s); -extern int swap_tilde_with_home(char *ret, const char *s, const char *home); -extern int swap_home_with_tilde(char *ret, const char *s, const char *home); +extern char *swap_tilde_with_home(char *ret, const char *s, const char *home); +extern char *swap_home_with_tilde(char *ret, const char *s, const char *home); /*********************/ /* utility functions */ diff --git a/src/dblayer.c b/src/dblayer.c index 08b1e4c..f263b86 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -331,8 +331,12 @@ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) { return 0; } -int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, const int secret, const int prime) { - int cid = get_config_id(db, path); +int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, const int secret, const int prime, const char *home) { + char tpath[STR_L]; + if (!swap_home_with_tilde(tpath, path, home)) { + strcpy(tpath, path); + } + int cid = get_config_id(db, tpath); if (cid == -2) { db->error = SQL_ERR_SQLITE; return -1; @@ -343,7 +347,7 @@ int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, cons db->error = SQL_ERR_PRIMARY_REDEFINITION; return -1; } - return insert_to_config_table(db, path, secret, prime); + return insert_to_config_table(db, tpath, secret, prime); } /* If it exist it means the user has inserted the same path twice */ @@ -365,7 +369,7 @@ int add_get_or_insert_program_to_db(DB *db, const char *name) { int add_basename_exists(DB *db, const char *pName, const char *path) { cklist *baseNames = list_make_new(); - get_program_paths(db, baseNames, pName, 1 /*basename */, 0); + get_program_paths(db, baseNames, pName, 1 /*basename */, 0, NULL); char *tmp = strdup(path); int rc = list_exists(baseNames, basename(tmp)); free(tmp); @@ -373,7 +377,7 @@ int add_basename_exists(DB *db, const char *pName, const char *path) { return rc; } -int add_transaction_try(DB *db, const AddOpt * const opt) { +int add_transaction_try(DB *db, const AddOpt * const opt, const char *home) { __BEGIN_TRANSACTION__ int pid = add_get_or_insert_program_to_db(db, opt->progName); if (db->error == SQL_ERR_SQLITE) { @@ -384,7 +388,7 @@ int add_transaction_try(DB *db, const AddOpt * const opt) { ERR("Cannot have two configs with the same basename, for the same program."); return 1; } - int cid = add_get_or_insert_config_to_db(db, pid, opt->confPath, opt->secret, opt->prime); + int cid = add_get_or_insert_config_to_db(db, pid, opt->confPath, opt->secret, opt->prime, home); if (db->error == SQL_ERR_SQLITE) { PRINT_ERR("Could not insert config to db.\n"); return 1; @@ -473,11 +477,13 @@ int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int } } free(tmp); + break; } else { + /* Since we are here, it means there is only 1 config for the selected program */ + flag = 0; str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); strcpy(ret, confName); - flag = 0; if (sec) { *sec = sqlite3_column_int(stmt, 1); } @@ -490,7 +496,7 @@ int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int return -1; } -int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int attr) { +int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int attr, const char *home) { int pid = get_program_id(db, pName); /* error */ if (pid == -2) { @@ -527,7 +533,13 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att strcat(entry, basename(tmp)); } else { - strcat(entry, tmp); + char tpath[STR_L] = ""; + if (swap_tilde_with_home(tpath, tmp, home)) { + strcat(entry, tpath); + } + else { + strcat(entry, tmp); + } } if (attr) { /* primary */ @@ -552,7 +564,7 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att return -1; } -int list_get_paths(DB *db, cklist *ckl, int bName, int attr) { +int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home) { sqlite3_stmt *stmt; int rc; @@ -571,7 +583,13 @@ int list_get_paths(DB *db, cklist *ckl, int bName, int attr) { strcat(path, basename(tmp)); } else { - strcat(path, tmp); + char tpath[STR_L] = ""; + if (swap_tilde_with_home(tpath, tmp, home)) { + strcat(path, tpath); + } + else { + strcat(path, tmp); + } } free(tmp); if (attr) { @@ -615,7 +633,7 @@ int list_get_programs(DB *db, cklist *ckl) { return 1; } -int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr) { +int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home) { sqlite3_stmt *stmt; int rc; @@ -662,7 +680,13 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr) { strcat(treePath, basename(tmp)); } else { - strcat(treePath, tmp); + char tpath[STR_L] = ""; + if (swap_tilde_with_home(tpath, tmp, home)) { + strcat(treePath, tpath); + } + else { + strcat(treePath, tmp); + } } free(tmp); if (attr) { diff --git a/src/dblayer.h b/src/dblayer.h index 20ab4b2..b826327 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -43,7 +43,7 @@ extern DB open_DB(const UserOpt *opt); extern void close_DB(DB *db); extern int program_exists(DB *db, const char *pName); -extern int get_program_paths(DB *db, cklist *ckl, const char* pName, int bName, int attr); +extern int get_program_paths(DB *db, cklist *ckl, const char* pName, int bName, int attr, const char *home); /********/ /* init */ @@ -58,7 +58,7 @@ extern DB init_make_DB(const UserOpt *opt); /*******/ /* Returns 1 in error, 0 otherwise */ -extern int add_transaction_try(DB *db, const AddOpt * const opt); +extern int add_transaction_try(DB *db, const AddOpt * const opt, const char *home); /********/ /* edit */ @@ -72,9 +72,9 @@ extern int edit_get_config(DB *db, const char *pName, char *ret, const char *cNa /* list */ /********/ -extern int list_get_paths(DB *db, cklist *ckl, int bName, int attr); +extern int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home); extern int list_get_programs(DB *db, cklist *ckl); -extern int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr); +extern int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home); /*******/ /* del */ diff --git a/unit/ck-test.c b/unit/ck-test.c index 8d8ee08..36b23d8 100644 --- a/unit/ck-test.c +++ b/unit/ck-test.c @@ -49,12 +49,12 @@ void ck_str_utils_test() { /* getenv("HOME") -> /home/user */ char home[STR_S] = "/home/user"; char testpath[STR_S] = "/home/user/file"; - assert(swap_home_with_tilde(str, testpath, home) == 0); + assert(swap_home_with_tilde(str, testpath, home)); assert(strcmp(str, "~/file") == 0); - assert(swap_tilde_with_home(str2, str, home) == 0); + assert(swap_tilde_with_home(str2, str, home)); assert(strcmp(str2, testpath) == 0); - assert(swap_home_with_tilde(str, "/not/starting/with/home", home)); - assert(swap_tilde_with_home(str2, "/not/starting/with/tilde", home)); + assert(swap_home_with_tilde(str, "/not/starting/with/home", home) == NULL); + assert(swap_tilde_with_home(str2, "/not/starting/with/tilde", home) == NULL); } int main() { -- cgit v1.2.3