HAMMER utilities: Add a verbose (-v) option.
[dragonfly.git] / sbin / hammer / hammer.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sbin/hammer/hammer.c,v 1.6 2008/01/25 05:53:41 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static void hammer_parsetime(u_int64_t *tidp, const char *timestr);
40 static void hammer_parsedevs(const char *blkdevs);
41 static void usage(int exit_code);
42
43 int RecurseOpt;
44 int VerboseOpt;
45
46 int
47 main(int ac, char **av)
48 {
49         u_int64_t tid;
50         int ch;
51         int status;
52         char *blkdevs = NULL;
53
54         while ((ch = getopt(ac, av, "hf:rv")) != -1) {
55                 switch(ch) {
56                 case 'h':
57                         usage(0);
58                         /* not reached */
59                 case 'r':
60                         RecurseOpt = 1;
61                         break;
62                 case 'f':
63                         blkdevs = optarg;
64                         break;
65                 case 'v':
66                         ++VerboseOpt;
67                         break;
68                 default:
69                         usage(1);
70                         /* not reached */
71                 }
72         }
73         ac -= optind;
74         av += optind;
75         if (ac < 1) {
76                 usage(1);
77                 /* not reached */
78         }
79
80         if (strcmp(av[0], "now") == 0) {
81                 hammer_parsetime(&tid, "0s");
82                 printf("0x%08x\n", (int)(tid / 1000000000LL));
83                 exit(0);
84         }
85         if (strcmp(av[0], "stamp") == 0) {
86                 if (av[1] == NULL)
87                         usage(1);
88                 hammer_parsetime(&tid, av[1]);
89                 printf("0x%08x\n", (int)(tid / 1000000000LL));
90                 exit(0);
91         }
92         if (strcmp(av[0], "namekey") == 0) {
93                 int64_t key;
94
95                 if (av[1] == NULL)
96                         usage(1);
97                 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
98                 if (key == 0)
99                         key |= 0x100000000LL;
100                 printf("0x%016llx\n", key);
101                 exit(0);
102         }
103         if (strcmp(av[0], "namekey32") == 0) {
104                 int32_t key;
105
106                 if (av[1] == NULL)
107                         usage(1);
108                 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
109                 if (key == 0)
110                         ++key;
111                 printf("0x%08x\n", key);
112                 exit(0);
113         }
114
115         uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
116         if (status != uuid_s_ok) {
117                 errx(1, "uuids file does not have the DragonFly "
118                         "HAMMER filesystem type");
119         }
120
121         init_alist_templates();
122         if (strcmp(av[0], "show") == 0) {
123                 int32_t vol_no = -1;
124                 int32_t clu_no = -1;
125
126                 hammer_parsedevs(blkdevs);
127                 if (ac > 1)
128                         sscanf(av[1], "%d:%d", &vol_no, &clu_no);
129                 hammer_cmd_show(vol_no, clu_no, 0);
130                 exit(0);
131         }
132         usage(1);
133         /* not reached */
134         return(0);
135 }
136
137 /*
138  * Parse a timestamp for the mount point
139  *
140  * yyyymmddhhmmss
141  * -N[s/h/d/m/y]
142  */
143 static
144 void
145 hammer_parsetime(u_int64_t *tidp, const char *timestr)
146 {
147         struct tm tm;
148         time_t t;
149         int32_t n;
150         char c;
151         double seconds = 0;
152
153         t = time(NULL);
154
155         if (*timestr == 0)
156                 usage(1);
157
158         if (isalpha(timestr[strlen(timestr)-1])) {
159                 if (sscanf(timestr, "%d%c", &n, &c) != 2)
160                         usage(1);
161                 switch(c) {
162                 case 'Y':
163                         n *= 365;
164                         goto days;
165                 case 'M':
166                         n *= 30;
167                         /* fall through */
168                 case 'D':
169                 days:
170                         n *= 24;
171                         /* fall through */
172                 case 'h':
173                         n *= 60;
174                         /* fall through */
175                 case 'm':
176                         n *= 60;
177                         /* fall through */
178                 case 's':
179                         t -= n;
180                         break;
181                 default:
182                         usage(1);
183                 }
184         } else {
185                 localtime_r(&t, &tm);
186                 seconds = (double)tm.tm_sec;
187                 tm.tm_year += 1900;
188                 tm.tm_mon += 1;
189                 n = sscanf(timestr, "%4d%2d%2d:%2d%2d%lf",
190                            &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
191                            &tm.tm_hour, &tm.tm_min, &seconds);
192                 tm.tm_mon -= 1;
193                 tm.tm_year -= 1900;
194                 /* if [:hhmmss] is omitted, assume :000000.0 */
195                 if (n < 4)
196                         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
197                 else
198                         tm.tm_sec = (int)seconds;
199                 t = mktime(&tm);
200         }
201         *tidp = (u_int64_t)t * 1000000000 + 
202                 (seconds - (int)seconds) * 1000000000;
203 }
204
205 static
206 void
207 hammer_parsedevs(const char *blkdevs)
208 {
209         char *copy;
210         char *volname;
211
212         if (blkdevs == NULL) {
213                 errx(1, "A -f blkdevs specification is required "
214                         "for this command");
215         }
216
217         copy = strdup(blkdevs);
218         while ((volname = copy) != NULL) {
219                 if ((copy = strchr(copy, ':')) != NULL)
220                         *copy++ = 0;
221                 setup_volume(-1, volname, 0, O_RDONLY);
222         }
223 }
224
225 static
226 void
227 usage(int exit_code)
228 {
229         fprintf(stderr, 
230                 "hammer -h\n"
231                 "hammer now\n"
232                 "hammer stamp <time>\n"
233                 "hammer -f blkdevs [-r] show [vol_no[:clu_no]]\n"
234         );
235         fprintf(stderr, "    time: +n[s/m/h/D/M/Y]\n"
236                         "    time: yyyymmdd[:hhmmss]\n");
237         exit(exit_code);
238 }
239