aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/actionhelper.c44
-rw-r--r--src/actions.c7
-rw-r--r--src/ckerrlog.h5
-rw-r--r--src/ckutil.c27
-rw-r--r--src/ckutil.h3
5 files changed, 71 insertions, 15 deletions
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 <unistd.h>
#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