Welcome to little lamb

Code » anopa » commit ef3bb9d

status/enable/start/stop: Fix --listdir w/ relative path...

author Olivier Brunel
2018-01-27 17:29:21 UTC
committer Olivier Brunel
2018-01-28 14:47:32 UTC
parent 868445e07d7127275c77ef54ef92016aa63fe98f

status/enable/start/stop: Fix --listdir w/ relative path...

Also applies to --{reset-,}source & --set-* for enable

src/anopa/aa-enable.c +50 -6
src/anopa/aa-start.c +15 -9
src/anopa/aa-status.c +19 -13
src/anopa/aa-stop.c +15 -9

diff --git a/src/anopa/aa-enable.c b/src/anopa/aa-enable.c
index 3d73e3b..362e7c6 100644
--- a/src/anopa/aa-enable.c
+++ b/src/anopa/aa-enable.c
@@ -33,6 +33,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <skalibs/bytestr.h>
+#include <skalibs/djbunix.h>
 #include <skalibs/stralloc.h>
 #include <skalibs/genalloc.h>
 #include <skalibs/direntry.h>
@@ -204,6 +205,18 @@ it_list (direntry *d, void *data)
 static int
 copy_file (const char *src, const char *dst)
 {
+    size_t len = sa_pl.len;
+    int ok = 1;
+
+    if (*src != '/')
+    {
+        if (!stralloc_cats (&sa_pl, "/"))
+            aa_strerr_diefu1sys (RC_FATAL_MEMORY, "stralloc_cats");
+        if (!stralloc_cats (&sa_pl, src))
+            aa_strerr_diefu1sys (RC_FATAL_MEMORY, "stralloc_cats");
+        src = sa_pl.s;
+    }
+
     if (aa_copy_file (src, dst, 0744, AA_CP_OVERWRITE) < 0)
     {
         int e = errno;
@@ -216,10 +229,13 @@ copy_file (const char *src, const char *dst)
         aa_bs (AA_ERR, strerror (e));
         aa_end_err ();
 
-        return 0;
+        ok = 0;
     }
 
-    return 1;
+    if (src == sa_pl.s)
+        sa_pl.len = len;
+
+    return ok;
 }
 
 static void
@@ -307,6 +323,8 @@ main (int argc, char * const argv[])
                 break;
 
             case 'c':
+                if (*optarg != '/' && sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 set_crash = optarg;
                 break;
 
@@ -315,6 +333,8 @@ main (int argc, char * const argv[])
                 break;
 
             case 'f':
+                if (*optarg != '/' && sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 set_finish = optarg;
                 break;
 
@@ -322,6 +342,8 @@ main (int argc, char * const argv[])
                 dieusage (RC_OK);
 
             case 'i':
+                if (*optarg != '/' && sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 set_init = optarg;
                 break;
 
@@ -331,6 +353,10 @@ main (int argc, char * const argv[])
 
             case 'l':
                 unslash (optarg);
+                /* if relative path (starts with '.') and we don't have cwd yet,
+                 * get it now -- i.e. before init_repo() */
+                if (*optarg == '.' && sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 path_list = optarg;
                 break;
 
@@ -343,10 +369,14 @@ main (int argc, char * const argv[])
                 break;
 
             case 'P':
+                if (*optarg != '/' && sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 set_post_stop = optarg;
                 break;
 
             case 'p':
+                if (*optarg != '/' && sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 set_post_start = optarg;
                 break;
 
@@ -365,6 +395,14 @@ main (int argc, char * const argv[])
 
             case 's':
                 unslash (optarg);
+                if (*optarg != '/')
+                {
+                    if (sa_pl.len == 0 && sagetcwd (&sa_pl) < 0)
+                        aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
+                    if (!stralloc_cats (&aa_sa_sources, sa_pl.s)
+                            || !stralloc_cats (&aa_sa_sources, "/"))
+                        aa_strerr_diefu1sys (RC_FATAL_MEMORY, "stralloc_catb");
+                }
                 if (!stralloc_catb (&aa_sa_sources, optarg, strlen (optarg) + 1))
                     aa_strerr_diefu1sys (RC_FATAL_MEMORY, "stralloc_catb");
                 break;
@@ -415,15 +453,21 @@ main (int argc, char * const argv[])
      * the one with (potentially) a config dir */
     if (path_list)
     {
-        if (*path_list != '/' && *path_list != '.')
+        size_t len = sa_pl.len;
+
+        /* relative: cwd already there, just add a slash */
+        if (*path_list == '.')
+            stralloc_cats (&sa_pl, "/");
+        /* neither relative nor absolute: prefix w/ default listdir path */
+        else if (*path_list != '/')
             stralloc_cats (&sa_pl, LISTDIR_PREFIX);
         stralloc_catb (&sa_pl, path_list, strlen (path_list) + 1);
         r = aa_scan_dir (&sa_pl, 0, it_list, NULL);
         if (r < 0)
             /* -r == ERR_IO, since it_list always returns 0 */
-            aa_strerr_diefu3sys (RC_FATAL_IO, "read list directory ",
-                    (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list,
-                    (*path_list != '/' && *path_list != '.') ? path_list : "");
+            aa_strerr_diefu2sys (RC_FATAL_IO, "read list directory ", sa_pl.s);
+
+        sa_pl.len = len;
     }
 
     for (i = 0; i < argc; ++i)
diff --git a/src/anopa/aa-start.c b/src/anopa/aa-start.c
index 4a494b7..c1bf18c 100644
--- a/src/anopa/aa-start.c
+++ b/src/anopa/aa-start.c
@@ -244,6 +244,7 @@ int
 main (int argc, char * const argv[])
 {
     PROG = "aa-start";
+    stralloc sacwd = STRALLOC_ZERO;
     const char *path_repo = aa_get_repodir ();
     const char *path_list = NULL;
     int i;
@@ -280,6 +281,10 @@ main (int argc, char * const argv[])
 
             case 'l':
                 unslash (optarg);
+                /* if relative path (starts with '.') and we don't have cwd yet,
+                 * get it now -- i.e. before init_repo() */
+                if (*optarg == '.' && sacwd.len == 0 && sagetcwd (&sacwd) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 path_list = optarg;
                 break;
 
@@ -333,20 +338,21 @@ main (int argc, char * const argv[])
 
     if (path_list)
     {
-        stralloc sa = STRALLOC_ZERO;
         int r;
 
-        if (*path_list != '/' && *path_list != '.')
-            stralloc_cats (&sa, LISTDIR_PREFIX);
-        stralloc_catb (&sa, path_list, strlen (path_list) + 1);
-        r = aa_scan_dir (&sa, 1, it_start, NULL);
-        stralloc_free (&sa);
+        /* relative: cwd already there, just add a slash */
+        if (*path_list == '.')
+            stralloc_cats (&sacwd, "/");
+        /* neither relative nor absolute: prefix w/ default listdir path */
+        else if (*path_list != '/')
+            stralloc_cats (&sacwd, LISTDIR_PREFIX);
+        stralloc_catb (&sacwd, path_list, strlen (path_list) + 1);
+        r = aa_scan_dir (&sacwd, 1, it_start, NULL);
         if (r < 0)
-            aa_strerr_diefu3sys (RC_FATAL_IO, "read list directory ",
-                    (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list,
-                    (*path_list != '/' && *path_list != '.') ? path_list : "");
+            aa_strerr_diefu2sys (RC_FATAL_IO, "read list directory ", sacwd.s);
     }
 
+    stralloc_free (&sacwd);
     tain_now_g ();
 
     for (i = 0; i < argc; ++i)
diff --git a/src/anopa/aa-status.c b/src/anopa/aa-status.c
index acb575e..395a6b5 100644
--- a/src/anopa/aa-status.c
+++ b/src/anopa/aa-status.c
@@ -823,6 +823,7 @@ int
 main (int argc, char * const argv[])
 {
     PROG = "aa-status";
+    stralloc sacwd = STRALLOC_ZERO;
     const char *path_repo = aa_get_repodir ();
     const char *path_list = NULL;
     struct config cfg = { 0, };
@@ -873,6 +874,10 @@ main (int argc, char * const argv[])
 
             case 'l':
                 unslash (optarg);
+                /* if relative path (starts with '.') and we don't have cwd yet,
+                 * get it now -- i.e. before init_repo() */
+                if (*optarg == '.' && sacwd.len == 0 && sagetcwd (&sacwd) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 path_list = optarg;
                 break;
 
@@ -949,27 +954,26 @@ main (int argc, char * const argv[])
 
     if (all)
     {
-        stralloc sa = STRALLOC_ZERO;
-
-        stralloc_catb (&sa, ".", 2);
-        r = aa_scan_dir (&sa, 0, it_all, &cfg);
+        sacwd.len = 0;
+        stralloc_cats (&sacwd, ".");
+        r = aa_scan_dir (&sacwd, 0, it_all, &cfg);
         if (r < 0)
             aa_strerr_diefu2sys (RC_FATAL_IO, "scan repo directory ", path_repo);
     }
     else if (path_list)
     {
-        stralloc sa = STRALLOC_ZERO;
         int r;
 
-        if (*path_list != '/' && *path_list != '.')
-            stralloc_cats (&sa, LISTDIR_PREFIX);
-        stralloc_catb (&sa, path_list, strlen (path_list) + 1);
-        r = aa_scan_dir (&sa, 1, it_listdir, &cfg);
-        stralloc_free (&sa);
+        /* relative: cwd already there, just add a slash */
+        if (*path_list == '.')
+            stralloc_cats (&sacwd, "/");
+        /* neither relative nor absolute: prefix w/ default listdir path */
+        else if (*path_list != '/')
+            stralloc_cats (&sacwd, LISTDIR_PREFIX);
+        stralloc_catb (&sacwd, path_list, strlen (path_list) + 1);
+        r = aa_scan_dir (&sacwd, 1, it_listdir, &cfg);
         if (r < 0)
-            aa_strerr_diefu3sys (RC_FATAL_IO, "read list directory ",
-                    (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list,
-                    (*path_list != '/' && *path_list != '.') ? path_list : "");
+            aa_strerr_diefu2sys (RC_FATAL_IO, "read list directory ", sacwd.s);
     }
     else
         for (int i = 0; i < argc; ++i)
@@ -981,6 +985,8 @@ main (int argc, char * const argv[])
             else
                 load_service (argv[i], &cfg);
 
+    stralloc_free (&sacwd);
+
     if (sort_fn)
         qsort (genalloc_s(struct serv, &ga_serv), genalloc_len (struct serv, &ga_serv),
                 sizeof (struct serv), sort_fn);
diff --git a/src/anopa/aa-stop.c b/src/anopa/aa-stop.c
index ede40ea..d96ff8a 100644
--- a/src/anopa/aa-stop.c
+++ b/src/anopa/aa-stop.c
@@ -310,6 +310,7 @@ int
 main (int argc, char * const argv[])
 {
     PROG = "aa-stop";
+    stralloc sacwd = STRALLOC_ZERO;
     const char *path_repo = aa_get_repodir ();
     const char *path_list = NULL;
     int all = 0;
@@ -359,6 +360,10 @@ main (int argc, char * const argv[])
 
             case 'l':
                 unslash (optarg);
+                /* if relative path (starts with '.') and we don't have cwd yet,
+                 * get it now -- i.e. before init_repo() */
+                if (*optarg == '.' && sacwd.len == 0 && sagetcwd (&sacwd) < 0)
+                    aa_strerr_diefu1sys (RC_FATAL_IO, "get current working directory");
                 path_list = optarg;
                 break;
 
@@ -468,18 +473,18 @@ main (int argc, char * const argv[])
     }
     else if (path_list)
     {
-        stralloc sa = STRALLOC_ZERO;
         int r;
 
-        if (*path_list != '/' && *path_list != '.')
-            stralloc_cats (&sa, LISTDIR_PREFIX);
-        stralloc_catb (&sa, path_list, strlen (path_list) + 1);
-        r = aa_scan_dir (&sa, 1, it_stop, NULL);
-        stralloc_free (&sa);
+        /* relative: cwd already there, just add a slash */
+        if (*path_list == '.')
+            stralloc_cats (&sacwd, "/");
+        /* neither relative nor absolute: prefix w/ default listdir path */
+        else if (*path_list != '/')
+            stralloc_cats (&sacwd, LISTDIR_PREFIX);
+        stralloc_catb (&sacwd, path_list, strlen (path_list) + 1);
+        r = aa_scan_dir (&sacwd, 1, it_stop, NULL);
         if (r < 0)
-            aa_strerr_diefu3sys (RC_FATAL_IO, "read list directory ",
-                    (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list,
-                    (*path_list != '/' && *path_list != '.') ? path_list : "");
+            aa_strerr_diefu2sys (RC_FATAL_IO, "read list directory ", sacwd.s);
     }
     else
         for (i = 0; i < argc; ++i)
@@ -491,6 +496,7 @@ main (int argc, char * const argv[])
             else
                 add_service (argv[i], NULL);
 
+    stralloc_free (&sacwd);
     tain_now_g ();
 
     mainloop (mode, scan_cb);