From 2922690b716540b7e3971ffbdf506148503c7788 Mon Sep 17 00:00:00 2001 From: gramanas Date: Sat, 27 Oct 2018 12:35:07 +0300 Subject: change own&grp when it should. version 0.8.1! --- CMakeLists.txt | 4 +- README.html | 209 +++++++++++++++++++++++++++++++++-------------------- README.org | 33 +++++++++ src/actionhelper.c | 44 ++++++++--- src/actions.c | 7 +- src/ckerrlog.h | 5 ++ src/ckutil.c | 27 +++++++ src/ckutil.h | 3 + 8 files changed, 237 insertions(+), 95 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4327f84..1ed42c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,8 @@ cmake_minimum_required (VERSION 3.5.6) project(ck C) # version set(ck_MAJOR_VERSION 0) -set(ck_MINOR_VERSION 7) -set(ck_PATCH_VERSION 14) +set(ck_MINOR_VERSION 8) +set(ck_PATCH_VERSION 1) # Feature test macros set(FEATURE_TEST_MACROS "-D_DEFAULT_SOURCE") diff --git a/README.html b/README.html index dc25609..d22b910 100644 --- a/README.html +++ b/README.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + @@ -232,40 +232,41 @@ for the JavaScript code in this tag.

Table of Contents

-
-

ck

-
+
+

ck

+

The Config Keeper

@@ -332,9 +333,9 @@ with the rest of us).

-
-

Technicalities

-
+
+

Technicalities

+

Upon adding a config to ck, it moves it to the specified folder and adds a symbolic link back where it came from (ln -s). @@ -347,9 +348,9 @@ majority should).

-
-

Download

-
+
+

Download

+

Go ahead and download ck and give it a try. It comes with a help sub-command that explains any inquires you might have. @@ -367,13 +368,13 @@ You can also read the manual down below.

-
-

build it

+
+

build it

-
-

requirements

-
+
+

requirements

+
  • cmake
  • sqlite3-dev
  • @@ -382,9 +383,9 @@ You can also read the manual down below.
-
-

make && install

-
+
+

make && install

+

Use -DCMAKE_INSTALL_PREFIX when running cmake to change the install path.

@@ -407,16 +408,16 @@ Use -DCMAKE_INSTALL_PREFIX when running cmake to change the install
-
-

for devs

-
+
+

for devs

+

Please be kind to each other.

-
-

CMake options

-
+
+

CMake options

+

cmake accepts the following options:

@@ -437,9 +438,9 @@ To use any one of them append it after the cmake command like so:
-
-

compiler

-
+
+

compiler

+

Pick your favorite

@@ -469,9 +470,9 @@ Pick your favorite
-
-

tests

-
+
+

tests

+

The testing "suite" is a bash script that runs regression and unit tests. Regression tests are under the tests/ directory @@ -480,9 +481,9 @@ under unit/ directory and test the code.

-
-

run tests

-
+
+

run tests

+

First make sure you build ck with the -DCK_TESTS=1 option. Then go to the build directory and type: @@ -494,9 +495,9 @@ go to the build directory and type:

-
-

test suite

-
+
+

test suite

+
$ ./test-ck -h
 ck test suite
@@ -514,8 +515,8 @@ flags:
 
-
-

manual

+
+

manual

ck's goal is to assist with the configuration file management. @@ -567,9 +568,9 @@ of flags one has to pass to ck.

-
-

ck configuration

-
+
+

ck configuration

+

ck uses sqlite to index the configuration files. The init action creates a .ck directory (under $HOME) @@ -607,13 +608,13 @@ $ ck -c /someplace/else ...

-
-

Actions

-
+
+

Actions

+
-
-

init

-
+
+

init

+

or i or -i

@@ -643,9 +644,9 @@ $ ck init /path_to/where_you_want/your_configs/to_be -

add

-
+
+

add

+

or a or -a

@@ -686,9 +687,9 @@ $ ck add program_name config_path [-s] [-p]
-
-

list

-
+
+

list

+

or ls or l or -l

@@ -751,9 +752,9 @@ $ ck -l ckconf
-
-

search

-
+
+

search

+

or grep or s or -s

@@ -802,9 +803,9 @@ $ for i ($(ck ls paths)) grep -E 'A|B' $
-
-

edit

-
+
+

edit

+

or e or -e

@@ -849,11 +850,63 @@ $ ck edit emacs accounts.el
+ +
+

restore

+
+

+or r or -r +

+ +

+Restore links. +

+ +

+Given a working ck instance (ckdb + ckrc + directories in ckrc with configs) +restore shall recreate the links from the config directories in ckrc +back to their corresponding position when added in ck. +

+ +

+It is useful for copying your configs to a new linux installation +or restoring deleted links. +

+ +

+It can either restore a specific program or all of them: +

+
+
# restore progName
+ck restore -p progName
+# restore all
+ck r all
+
+
+ +

+Note: +If ck tracks configs that are owned by root, simply running +`ck restore …` will fail due to permissions. To remedy this, ck will alter the +owner and group of a link to match the one in the ckrc directories. +Thus, running `sudo ck -c home/myuser.ck restore ..` will restore +the root user's links as it should and the user links will have +the user as the owner instead of the root. +

+ +

+ck checks that the configs exist and that the location for the link +is avaliable before making any links. However, in the even that symlink +fails for some other reason, the process will stop as is. Make sure you +take care of the already created links, if that's the case. +

+
+
-

Created: 2018-10-26 Fri 16:01

+

Created: 2018-10-27 Sat 12:38

Validate

diff --git a/README.org b/README.org index 7d5e4d7..3e1c20f 100644 --- a/README.org +++ b/README.org @@ -354,3 +354,36 @@ $ ck edit emacs # edit a specific emacs config, other than the primary $ ck edit emacs accounts.el #+END_SRC + +*** restore +or r or -r + +Restore links. + +Given a working ck instance (ckdb + ckrc + directories in ckrc with configs) +restore shall recreate the links from the config directories in ckrc +back to their corresponding position when added in ck. + +It is useful for copying your configs to a new linux installation +or restoring deleted links. + +It can either restore a specific program or all of them: +#+BEGIN_SRC sh + # restore progName + ck restore -p progName + # restore all + ck r all +#+END_SRC + +Note: +If ck tracks configs that are owned by root, simply running +`ck restore ...` will fail due to permissions. To remedy this, ck will alter the +owner and group of a link to match the one in the ckrc directories. +Thus, running `sudo ck -c /home/myuser/.ck restore ..` will restore +the root user's links as it should and the user links will have +the user as the owner instead of the root. + +ck checks that the configs exist and that the location for the link +is avaliable before making any links. However, in the even that symlink +fails for some other reason, the process will stop as is. Make sure you +take care of the already created links, if that's the case. diff --git a/src/actionhelper.c b/src/actionhelper.c index 0269470..6428ab0 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -266,19 +266,25 @@ int restore_make_links(cklist *from, cklist *to) { if (list_size(from) > 0 && list_size(to) > 0 && list_size(from) == list_size(to)) { - while (1) { - HELP("%s %d %d", list_get(to), util_file_exists(list_get(to), NULL), !util_is_file_link(list_get(to))); + do { if (util_file_exists(list_get(to), NULL) || !util_is_file_link(list_get(to))) { - sERR("File %s already exists. Terminating.", list_get(to)); + ERR("File %s already exists.", list_get(to)); + sERR("No links were created."); return -1; } + } while (list_next(to)); + list_rewind(to); + while (1) { if (util_symlink_file(list_get(from), list_get(to))) { - sERR("Failed: %s -> %s", list_get(from), list_get(to)); + ERR("FATAL could not link %s -> %s", list_get(from), list_get(to)); + sERR("Process stopping."); + return -1; + } + hLOG("Linking: %s -> %s", list_get(from), list_get(to)); + if (util_own_grp_copy(list_get(to), list_get(from))) { return -1; } - LOG("Linking: %s -> %s", list_get(from), list_get(to)); - HELP("Linking: %s -> %s", list_get(from), list_get(to)); if (!list_next(from)) { break; } @@ -348,11 +354,9 @@ void print_HELP_result(int err) { } void print_RESTORE_result(int err) { - if (!err) { - HELP("restore OK"); - return; + if (err == -1) { + sERR("Restore failed.") } - sERR("restore NOT OK"); } void print_INIT_help() { @@ -445,7 +449,25 @@ void print_HELP_help() { } void print_RESTORE_help() { - ckhelp("Restore help"); + ckhelp("Restore links.\n"); + ckhelp("Given a working ck instance (ckdb + ckrc + directories in ckrc with configs)"); + ckhelp("restore shall recreate the links from the config directories in ckrc"); + ckhelp("back to their corresponding position when added in ck.\n"); + ckhelp("It is useful for copying your configs to a new linux installation"); + ckhelp("or restoring deleted links.\n"); + ckhelp("It can either restore a specific program or all of them:"); + ckhelp(" `-p progName`: restores progName."); + ckhelp(" `all`: restores everything.\n"); + ckhelp("Note:\nIf ck tracks configs that are owned by root, simply running"); + ckhelp("`ck restore ...` will fail due to permissions. To remedy this, ck will alter the"); + ckhelp("owner and group of a link to match the one in the ckrc directories."); + ckhelp("Thus, running `sudo ck -c /home/myuser/.ck restore ..` will restore"); + ckhelp("the root user's links as it should and the user links will have"); + ckhelp("the user as the owner instead of the root.\n"); + ckhelp("ck checks that the configs exist and that the location for the link"); + ckhelp("is avaliable before making any links. However, in the even that symlink"); + ckhelp("fails for some other reason, the process will stop as is. Make sure you"); + ckhelp("take care of the already created links, if that's the case."); report_help(); } diff --git a/src/actions.c b/src/actions.c index 5220864..649ffb8 100644 --- a/src/actions.c +++ b/src/actions.c @@ -317,7 +317,7 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { if (list_next(opt->args)) { if (program_exists(&db, list_get(opt->args))) { if (restore_configs_exists(&db, conf, list_get(opt->args), from, to)) { - HELP("Making links for %s", list_get(opt->args)); + hLOG("Restoring links for %s...", list_get(opt->args)); } else { err_flag = 1; @@ -336,7 +336,7 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { else if (strcmp(list_get(opt->args), "all") == 0) { if (!list_next(opt->args)) { if (restore_all_exist(&db, conf, from, to)) { - HELP("Make all links"); + hLOG("Restoring all links..."); } else { err_flag = 1; @@ -353,7 +353,6 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { } close_DB(&db); if (!err_flag) { - HELP("LINKS"); int rc = restore_make_links(from, to); list_free(from); list_free(to); @@ -361,5 +360,5 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { } list_free(from); list_free(to); - return 1; + return -2; } diff --git a/src/ckerrlog.h b/src/ckerrlog.h index 9d71458..bf0c0a5 100644 --- a/src/ckerrlog.h +++ b/src/ckerrlog.h @@ -90,5 +90,10 @@ CK_STREAMS cklog_with_delim(" ", "[%s]", COMPONENT); \ cklog(__VA_ARGS__); +/* Print help message & log it */ +#define hLOG(...) \ + HELP(__VA_ARGS__); \ + LOG(__VA_ARGS__); + #define LOG_V(...) #endif /* CKERRLOG_H */ diff --git a/src/ckutil.c b/src/ckutil.c index 84eb43d..f292e8e 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -17,6 +17,9 @@ #include #include "ckutil.h" +#include "ckerrlog.h" + +ERRLOG(utility); int util_is_dir(const char *path) { if (!path) { @@ -153,3 +156,27 @@ int str_is_empty(const char *s) { } return 1; } + +int util_own_grp_copy(const char *dest, const char *original) { + if (!dest || !original) { + return -1; + } + struct stat destbuf, origbuf; + if (lstat(dest, &destbuf)) { + sERR("error stating %s", dest) + return -1; + } + if (stat(original, &origbuf)) { + sERR("error stating %s", original) + return -1; + } + if (destbuf.st_uid != origbuf.st_uid + || destbuf.st_gid != origbuf.st_gid) { + hLOG("Copying uid & gid: %s -> %s", original, dest); + if (lchown(dest, origbuf.st_uid, origbuf.st_gid)) { + sERR("Cannot change owner and group of %s", dest); + return -1; + } + } + return 0; +} diff --git a/src/ckutil.h b/src/ckutil.h index dde7665..d653116 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -90,4 +90,7 @@ extern int util_move_file(const char *path, const char* dest); /* Wrapper around symlink() */ extern int util_symlink_file(const char *path, const char* dest); + +/* Chnage owner and group of `new` and make it like `old`*/ +extern int util_own_grp_copy(const char *new, const char* old); #endif // CKUTIL_H -- cgit v1.2.3