HAMMER utilities: Features and sync with VFS.
[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.5 2008/01/24 02:16:47 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
45 int
46 main(int ac, char **av)
47 {
48         u_int64_t tid;
49         int ch;
50         int status;
51         char *blkdevs = NULL;
52
53         while ((ch = getopt(ac, av, "hf:r")) != -1) {
54                 switch(ch) {
55                 case 'h':
56                         usage(0);
57                         /* not reached */
58                 case 'r':
59                         RecurseOpt = 1;
60                         break;
61                 case 'f':
62                         blkdevs = optarg;
63                         break;
64                 default:
65                         usage(1);
66                         /* not reached */
67                 }
68         }
69         ac -= optind;
70         av += optind;
71         if (ac < 1) {
72                 usage(1);
73                 /* not reached */
74         }
75
76         if (strcmp(av[0], "now") == 0) {
77                 hammer_parsetime(&tid, "0s");
78                 printf("0x%08x\n", (int)(tid / 1000000000LL));
79                 exit(0);
80         }
81         if (strcmp(av[0], "stamp") == 0) {
82                 if (av[1] == NULL)
83                         usage(1);
84                 hammer_parsetime(&tid, av[1]);
85                 printf("0x%08x\n", (int)(tid / 1000000000LL));
86                 exit(0);
87         }
88         if (strcmp(av[0], "namekey") == 0) {
89                 int64_t key;
90
91                 if (av[1] == NULL)
92                         usage(1);
93                 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
94                 if (key == 0)
95                         key |= 0x100000000LL;
96                 printf("0x%016llx\n", key);
97                 exit(0);
98         }
99         if (strcmp(av[0], "namekey32") == 0) {
100                 int32_t key;
101
102                 if (av[1] == NULL)
103                         usage(1);
104                 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
105                 if (key == 0)
106                         ++key;
107                 printf("0x%08x\n", key);
108                 exit(0);
109         }
110
111         uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
112         if (status != uuid_s_ok) {
113                 errx(1, "uuids file does not have the DragonFly "
114                         "HAMMER filesystem type");
115         }
116
117         init_alist_templates();
118         if (strcmp(av[0], "show") == 0) {
119                 int32_t vol_no = -1;
120                 int32_t clu_no = -1;
121
122                 hammer_parsedevs(blkdevs);
123                 if (ac > 1)
124                         sscanf(av[1], "%d:%d", &vol_no, &clu_no);
125                 hammer_cmd_show(vol_no, clu_no, 0);
126                 exit(0);
127         }
128         usage(1);
129         /* not reached */
130         return(0);
131 }
132
133 /*
134  * Parse a timestamp for the mount point
135  *
136  * yyyymmddhhmmss
137  * -N[s/h/d/m/y]
138  */
139 static
140 void
141 hammer_parsetime(u_int64_t *tidp, const char *timestr)
142 {
143         struct tm tm;
144         time_t t;
145         int32_t n;
146         char c;
147         double seconds = 0;
148
149         t = time(NULL);
150
151         if (*timestr == 0)
152                 usage(1);
153
154         if (isalpha(timestr[strlen(timestr)-1])) {
155                 if (sscanf(timestr, "%d%c", &n, &c) != 2)
156                         usage(1);
157                 switch(c) {
158                 case 'Y':
159                         n *= 365;
160                         goto days;
161                 case 'M':
162                         n *= 30;
163                         /* fall through */
164                 case 'D':
165                 days:
166                         n *= 24;
167                         /* fall through */
168                 case 'h':
169                         n *= 60;
170                         /* fall through */
171                 case 'm':
172                         n *= 60;
173                         /* fall through */
174                 case 's':
175                         t -= n;
176                         break;
177                 default:
178                         usage(1);
179                 }
180         } else {
181                 localtime_r(&t, &tm);
182                 seconds = (double)tm.tm_sec;
183                 tm.tm_year += 1900;
184                 tm.tm_mon += 1;
185                 n = sscanf(timestr, "%4d%2d%2d:%2d%2d%lf",
186                            &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
187                            &tm.tm_hour, &tm.tm_min, &seconds);
188                 tm.tm_mon -= 1;
189                 tm.tm_year -= 1900;
190                 /* if [:hhmmss] is omitted, assume :000000.0 */
191                 if (n < 4)
192                         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
193                 else
194                         tm.tm_sec = (int)seconds;
195                 t = mktime(&tm);
196         }
197         *tidp = (u_int64_t)t * 1000000000 + 
198                 (seconds - (int)seconds) * 1000000000;
199 }
200
201 static
202 void
203 hammer_parsedevs(const char *blkdevs)
204 {
205         char *copy;
206         char *volname;
207
208         if (blkdevs == NULL) {
209                 errx(1, "A -f blkdevs specification is required "
210                         "for this command");
211         }
212
213         copy = strdup(blkdevs);
214         while ((volname = copy) != NULL) {
215                 if ((copy = strchr(copy, ':')) != NULL)
216                         *copy++ = 0;
217                 setup_volume(-1, volname, 0, O_RDONLY);
218         }
219 }
220
221 static
222 void
223 usage(int exit_code)
224 {
225         fprintf(stderr, 
226                 "hammer -h\n"
227                 "hammer now\n"
228                 "hammer stamp <time>\n"
229                 "hammer -f blkdevs [-r] show [vol_no[:clu_no]]\n"
230         );
231         fprintf(stderr, "    time: +n[s/m/h/D/M/Y]\n"
232                         "    time: yyyymmdd[:hhmmss]\n");
233         exit(exit_code);
234 }
235