]> arthur.barton.de Git - netatalk.git/blobdiff - bin/ad/ad_ls.c
Merge remote-tracking branch 'origin/develop' into spotlight
[netatalk.git] / bin / ad / ad_ls.c
index 02c4fc663a79799f219e0cc894b88e1e1799e1c3..b5947e17dd0b48c3fa7d12e477128989372fe38d 100644 (file)
@@ -33,7 +33,6 @@
 
 #include <atalk/adouble.h>
 #include <atalk/cnid.h>
-#include <atalk/volinfo.h>
 #include "ad.h"
 
 #define ADv2_DIRNAME ".AppleDouble"
@@ -41,6 +40,8 @@
 #define DIR_DOT_OR_DOTDOT(a) \
         ((strcmp(a, ".") == 0) || (strcmp(a, "..") == 0))
 
+static volatile sig_atomic_t alarmed;
+
 /* ls options */
 static int ls_a;
 static int ls_l;
@@ -70,6 +71,44 @@ static char *labels[] = {
     "ora"
 };
 
+/*
+  SIGNAL handling:
+  catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
+*/
+
+static void sig_handler(int signo)
+{
+    alarmed = 1;
+    return;
+}
+
+static void set_signal(void)
+{
+    struct sigaction sv;
+
+    sv.sa_handler = sig_handler;
+    sv.sa_flags = SA_RESTART;
+    sigemptyset(&sv.sa_mask);
+    if (sigaction(SIGTERM, &sv, NULL) < 0)
+        ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
+
+    if (sigaction(SIGINT, &sv, NULL) < 0)
+        ERROR("error in sigaction(SIGINT): %s", strerror(errno));
+
+    memset(&sv, 0, sizeof(struct sigaction));
+    sv.sa_handler = SIG_IGN;
+    sigemptyset(&sv.sa_mask);
+
+    if (sigaction(SIGABRT, &sv, NULL) < 0)
+        ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
+
+    if (sigaction(SIGHUP, &sv, NULL) < 0)
+        ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
+
+    if (sigaction(SIGQUIT, &sv, NULL) < 0)
+        ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
+}
+
 /*
   Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
   Returns pointer to name or NULL.
@@ -188,10 +227,10 @@ static void print_flags(char *path, afpvol_t *vol, const struct stat *st)
     if (S_ISDIR(st->st_mode))
         adflags = ADFLAGS_DIR;
 
-    if (vol->volinfo.v_path == NULL)
+    if (vol->vol == NULL || vol->vol->v_path == NULL)
         return;
 
-    ad_init(&ad, vol->volinfo.v_adouble, vol->volinfo.v_ad_options);
+    ad_init(&ad, vol->vol);
 
     if ( ad_metadata(path, adflags, &ad) < 0 )
         return;
@@ -341,7 +380,7 @@ static void print_flags(char *path, afpvol_t *vol, const struct stat *st)
     else
         printf(" !ADVOL_CACHE ");
 
-    ad_close_metadata(&ad);
+    ad_close(&ad, ADFLAGS_HF);
 }
 
 #define TYPE(b) ((st->st_mode & (S_IFMT)) == (b))
@@ -475,6 +514,11 @@ static int ad_ls_r(char *path, afpvol_t *vol)
     /* First run: print everything */
     dirempty = 1;
     while ((ep = readdir (dp))) {
+        if (alarmed) {
+            ret = -1;
+            goto exit;
+        }
+
         /* Check if its "." or ".." */
         if (DIR_DOT_OR_DOTDOT(ep->d_name))
             continue;
@@ -510,6 +554,11 @@ static int ad_ls_r(char *path, afpvol_t *vol)
     if (ls_R) {
         rewinddir(dp);
         while ((ep = readdir (dp))) {
+            if (alarmed) {
+                ret = -1;
+                goto exit;
+            }
+
             /* Check if its "." or ".." */
             if (DIR_DOT_OR_DOTDOT(ep->d_name))
                 continue;
@@ -545,7 +594,7 @@ exit:
     return ret;
 }
 
-int ad_ls(int argc, char **argv)
+int ad_ls(int argc, char **argv, AFPObj *obj)
 {
     int c, firstarg;
     afpvol_t vol;
@@ -577,8 +626,11 @@ int ad_ls(int argc, char **argv)
 
     }
 
+    set_signal();
+    cnid_init();
+
     if ((argc - optind) == 0) {
-        openvol(".", &vol);
+        openvol(obj, ".", &vol);
         ad_ls_r(".", &vol);
         closevol(&vol);
     }
@@ -598,7 +650,7 @@ int ad_ls(int argc, char **argv)
             first = 1;
             recursion = 0;
 
-            openvol(argv[optind], &vol);
+            openvol(obj, argv[optind], &vol);
             ad_ls_r(argv[optind], &vol);
             closevol(&vol);
         next:
@@ -620,7 +672,7 @@ int ad_ls(int argc, char **argv)
             first = 1;
             recursion = 0;
 
-            openvol(argv[optind], &vol);
+            openvol(obj, argv[optind], &vol);
             ad_ls_r(argv[optind], &vol);
             closevol(&vol);