]> arthur.barton.de Git - netatalk.git/blobdiff - bin/ad/ad_find.c
New MySQL CNID backend
[netatalk.git] / bin / ad / ad_find.c
index bd426c856002e0f7b018b59f49e1f45fbc964ed8..093414a1d5e104f5e224c3697dac1e7ef01a9ce8 100644 (file)
 #include <limits.h>
 #include <string.h>
 #include <errno.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <ctype.h>
-#include <pwd.h>
-#include <grp.h>
-#include <time.h>
 
 #include <atalk/adouble.h>
 #include <atalk/cnid.h>
-#include <atalk/cnid_dbd_private.h>
-#include <atalk/volinfo.h>
+#include <atalk/cnid_bdb_private.h>
 #include <atalk/bstrlib.h>
 #include <atalk/bstradd.h>
 #include <atalk/directory.h>
+#include <atalk/util.h>
+#include <atalk/unicode.h>
 #include "ad.h"
 
 static volatile sig_atomic_t alarmed;
@@ -83,16 +78,32 @@ static void set_signal(void)
 static void usage_find(void)
 {
     printf(
-        "Usage: ad find VOLUME_PATH NAME\n"
+        "Usage: ad find [-v VOLUME_PATH] NAME\n"
         );
 }
 
-int ad_find(int argc, char **argv)
+int ad_find(int argc, char **argv, AFPObj *obj)
 {
-    int c;
+    int c, ret;
     afpvol_t vol;
+    const char *srchvol = getcwdpath();
+
+    while ((c = getopt(argc-1, &argv[1], ":v:")) != -1) {
+        switch(c) {
+        case 'v':
+            srchvol = strdup(optarg);
+            break;
+        case ':':
+        case '?':
+            usage_find();
+            return -1;
+            break;
+        }
+
+    }
+    optind++;
 
-    if (argc != 4) {
+    if ((argc - optind) != 1) {
         usage_find();
         exit(1);
     }
@@ -100,14 +111,32 @@ int ad_find(int argc, char **argv)
     set_signal();
     cnid_init();
 
-    if (openvol(argv[2], &vol) != 0)
-        ERROR("Cant open volume \"%s\"", argv[2]);
+    if (openvol(obj, srchvol, &vol) != 0)
+        ERROR("Cant open volume \"%s\"", srchvol);
+
+    uint16_t flags = CONV_TOLOWER;
+    char namebuf[MAXPATHLEN + 1];
+    if (convert_charset(vol.vol->v_volcharset,
+                        vol.vol->v_volcharset,
+                        vol.vol->v_maccharset,
+                        argv[optind],
+                        strlen(argv[optind]),
+                        namebuf,
+                        MAXPATHLEN,
+                        &flags) == (size_t)-1) {
+        ERROR("conversion error");
+    }
 
     int count;
     char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)];
-    if ((count = cnid_find(vol.volume.v_cdb, argv[3], strlen(argv[3]), resbuf, sizeof(resbuf))) < 1) {
-        SLOG("No results");
+    if ((count = cnid_find(vol.vol->v_cdb,
+                           namebuf,
+                           strlen(namebuf),
+                           resbuf,
+                           sizeof(resbuf))) < 1) {
+        ret = 1;
     } else {
+        ret = 0;
         cnid_t cnid;
         char *bufp = resbuf;
         bstring sep = bfromcstr("/");
@@ -116,7 +145,7 @@ int ad_find(int argc, char **argv)
             bufp += sizeof(cnid_t);
 
             bstring path = NULL;
-            bstring volpath = bfromcstr(vol.volinfo.v_path);
+            bstring volpath = bfromcstr(vol.vol->v_path);
             BSTRING_STRIP_SLASH(volpath);
             char buffer[12 + MAXPATHLEN + 1];
             int buflen = 12 + MAXPATHLEN + 1;
@@ -125,15 +154,17 @@ int ad_find(int argc, char **argv)
             struct bstrList *pathlist = bstrListCreateMin(32);
 
             while (did != DIRDID_ROOT) {
-                if ((name = cnid_resolve(vol.volume.v_cdb, &did, buffer, buflen)) == NULL)
-                    ERROR("Can't resolve CNID: %u", ntohl(did));
+                if ((name = cnid_resolve(vol.vol->v_cdb, &did, buffer, buflen)) == NULL)
+                    goto next;
                 bstrListPush(pathlist, bfromcstr(name));
             }
             bstrListPush(pathlist, volpath);
             path = bjoinInv(pathlist, sep);
-            bstrListDestroy(pathlist);
             
             printf("%s\n", cfrombstr(path));
+
+        next:
+            bstrListDestroy(pathlist);
             bdestroy(path);
         }
         bdestroy(sep);
@@ -141,5 +172,5 @@ int ad_find(int argc, char **argv)
 
     closevol(&vol);
 
-    return 0;
+    return ret;
 }