]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_find.c
It works
[netatalk.git] / bin / ad / ad_find.c
1 /* 
2    Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <time.h>
33
34 #include <atalk/adouble.h>
35 #include <atalk/cnid.h>
36 #include <atalk/cnid_dbd_private.h>
37 #include <atalk/volinfo.h>
38 #include "ad.h"
39
40 static volatile sig_atomic_t alarmed;
41
42 /*
43   SIGNAL handling:
44   catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
45 */
46
47 static void sig_handler(int signo)
48 {
49     alarmed = 1;
50     return;
51 }
52
53 static void set_signal(void)
54 {
55     struct sigaction sv;
56
57     sv.sa_handler = sig_handler;
58     sv.sa_flags = SA_RESTART;
59     sigemptyset(&sv.sa_mask);
60     if (sigaction(SIGTERM, &sv, NULL) < 0)
61         ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
62
63     if (sigaction(SIGINT, &sv, NULL) < 0)
64         ERROR("error in sigaction(SIGINT): %s", strerror(errno));
65
66     memset(&sv, 0, sizeof(struct sigaction));
67     sv.sa_handler = SIG_IGN;
68     sigemptyset(&sv.sa_mask);
69
70     if (sigaction(SIGABRT, &sv, NULL) < 0)
71         ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
72
73     if (sigaction(SIGHUP, &sv, NULL) < 0)
74         ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
75
76     if (sigaction(SIGQUIT, &sv, NULL) < 0)
77         ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
78 }
79
80 static void usage_find(void)
81 {
82     printf(
83         "Usage: ad find VOLUME_PATH NAME\n"
84         );
85 }
86
87 int ad_find(int argc, char **argv)
88 {
89     int c;
90     afpvol_t vol;
91
92     printf("argc: %d, argv0: %s\n", argc, argv[0]);
93
94     if (argc != 4) {
95         usage_find();
96         exit(1);
97     }
98
99     set_signal();
100     cnid_init();
101
102     SLOG("opening volume: %s", argv[2]);
103     if (openvol(argv[2], &vol) != 0)
104         ERROR("Cant open volume \"%s\"", argv[2]);
105
106     SLOG("searching: %s", argv[3]);
107
108     int count;
109     char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)];
110     if ((count = cnid_find(vol.volume.v_cdb, argv[3], strlen(argv[3]), resbuf, sizeof(resbuf))) < 1) {
111         SLOG("No results");
112     } else {
113         SLOG("%d matches", count);
114         cnid_t cnid;
115         char *bufp = resbuf;
116         while (count--) {
117             memcpy(&cnid, bufp, sizeof(cnid_t));
118             bufp += sizeof(cnid_t);
119             SLOG("Got CNID: %u", ntohl(cnid));
120         }
121     }
122
123     closevol(&vol);
124
125     return 0;
126 }