Welcome to little lamb

Code » anopa » commit 5ffedc8

test: Add option --repeat

author Olivier Brunel
2015-03-30 17:36:14 UTC
committer Olivier Brunel
2015-04-04 12:47:36 UTC
parent 30a66895d65f78fa8477f8889159638737a439d9

test: Add option --repeat

doc/aa-test.pod +6 -1
src/utils/aa-test.c +25 -1

diff --git a/doc/aa-test.pod b/doc/aa-test.pod
index 409935f..db0a1e4 100644
--- a/doc/aa-test.pod
+++ b/doc/aa-test.pod
@@ -5,7 +5,7 @@ aa-test - Test file types
 =head1 SYNOPSIS
 
 B<aa-test> [B<-b> | B<-d> | B<-e> | B<-f> | B<-L> | B<-p> | B<-S> | B<-r> |
-B<-w> | B<-x>] I<FILE>
+B<-w> | B<-x>] [B<-R> [I<TIMES>]] I<FILE>
 
 =head1 OPTIONS
 
@@ -39,6 +39,11 @@ Test whether I<FILE> exists and is a symlink
 
 Test whether I<FILE> exists and is a named pipe (FIFO)
 
+=item B<-R, --repeat> [I<TIMES>]
+
+If I<FILE> doesn't exist, wait 1 second and try again, repeating the test up to
+I<TIMES> times. By default 0 is used, meaning repeat the test forever.
+
 =item B<-r, --read>
 
 Test whether I<FILE> exists and read permission is granted
diff --git a/src/utils/aa-test.c b/src/utils/aa-test.c
index 1156fa8..df22a4d 100644
--- a/src/utils/aa-test.c
+++ b/src/utils/aa-test.c
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <skalibs/uint.h>
 #include <skalibs/strerr2.h>
 #include <anopa/common.h>
 
@@ -40,6 +41,7 @@ dieusage (int rc)
             " -r, --read                    Test for read permission on FILE\n"
             " -w, --write                   Test for write permission on FILE\n"
             " -x, --execute                 Test for execute permission on FILE\n"
+            " -R, --repeat[=TIMES]          Repeat test every second up to TIMES times\n"
             "\n"
             " -h, --help                    Show this help screen and exit\n"
             " -V, --version                 Show version information and exit\n"
@@ -54,6 +56,7 @@ main (int argc, char * const argv[])
     uid_t euid;
     int mode;
     char test = 0;
+    int repeat = -1;
 
     for (;;)
     {
@@ -66,6 +69,7 @@ main (int argc, char * const argv[])
             { "symlink",            no_argument,        NULL,   'L' },
             { "pipe",               no_argument,        NULL,   'p' },
             { "read",               no_argument,        NULL,   'r' },
+            { "repeat",             optional_argument,  NULL,   'R' },
             { "socket",             no_argument,        NULL,   'S' },
             { "version",            no_argument,        NULL,   'V' },
             { "write",              no_argument,        NULL,   'w' },
@@ -74,7 +78,7 @@ main (int argc, char * const argv[])
         };
         int c;
 
-        c = getopt_long (argc, argv, "bdefhLprSVwx", longopts, NULL);
+        c = getopt_long (argc, argv, "bdefhLprR::SVwx", longopts, NULL);
         if (c == -1)
             break;
         switch (c)
@@ -95,6 +99,13 @@ main (int argc, char * const argv[])
             case 'h':
                 dieusage (0);
 
+            case 'R':
+                if (optarg && !uint0_scan (optarg, &repeat))
+                    strerr_diefu2sys (1, "set repeat counter to ", optarg);
+                else if (!optarg)
+                    repeat = 0;
+                break;
+
             case 'V':
                 aa_die_version ();
 
@@ -108,10 +119,23 @@ main (int argc, char * const argv[])
     if (argc != 1 || test == 0)
         dieusage (1);
 
+    if (repeat > 0)
+        ++repeat;
+
+again:
     if (lstat (argv[0], &st) < 0)
     {
         if (errno != ENOENT)
             strerr_diefu2sys (2, "stat ", argv[0]);
+        else if (repeat >= 0)
+        {
+            if (repeat > 1)
+                --repeat;
+            else if (repeat == 1)
+                return 3;
+            sleep (1);
+            goto again;
+        }
         else
             return 3;
     }