Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sbin / mount_hammer / mount_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/mount_hammer/mount_hammer.c,v 1.4 2008/06/03 18:43:34 dillon Exp $
35  */
36
37 #include <sys/types.h>
38 #include <sys/diskslice.h>
39 #include <sys/diskmbr.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <vfs/hammer/hammer_mount.h>
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stdarg.h>
47 #include <stddef.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <uuid.h>
53 #include <err.h>
54 #include <assert.h>
55 #include <ctype.h>
56
57 #include "mntopts.h"
58
59 typedef const char **ary_ptr_t;
60
61 static void hammer_parsetime(u_int64_t *tidp, const char *timestr);
62 static void extract_volumes(ary_ptr_t *aryp, int *countp, char **av, int ac);
63
64 #define MOPT_UPDATE         { "update",     0, MNT_UPDATE, 0 }
65
66 #define MOPT_HAMMEROPTS         \
67         { "history", 1, HMNT_NOHISTORY, 1 }
68
69 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_HAMMEROPTS,
70                                  MOPT_UPDATE, MOPT_NULL };
71
72 static void usage(void);
73
74 int
75 main(int ac, char **av)
76 {
77         struct hammer_mount_info info;
78         struct vfsconf vfc;
79         int mount_flags = 0;
80         int error;
81         int ch;
82         int init_flags = 0;
83         char *mountpt;
84
85         bzero(&info, sizeof(info));
86         info.asof = 0;
87         mount_flags = 0;
88         info.hflags = 0;
89
90         while ((ch = getopt(ac, av, "o:T:u")) != -1) {
91                 switch(ch) {
92                 case 'T':
93                         hammer_parsetime(&info.asof, optarg);
94                         break;
95                 case 'o':
96                         getmntopts(optarg, mopts, &mount_flags, &info.hflags);
97                         break;
98                 case 'u':
99                         init_flags |= MNT_UPDATE;
100                         break;
101                 default:
102                         usage();
103                         /* not reached */
104                 }
105         }
106         ac -= optind;
107         av += optind;
108         mount_flags |= init_flags;
109
110         /*
111          * Only the mount point need be specified in update mode.
112          */
113         if (init_flags & MNT_UPDATE) {
114                 if (ac != 1) {
115                         usage();
116                         /* not reached */
117                 }
118                 mountpt = av[0];
119                 if (mount(vfc.vfc_name, mountpt, mount_flags, &info))
120                         err(1, NULL);
121                 exit(0);
122         }
123
124         if (ac < 2) {
125                 usage();
126                 /* not reached */
127         }
128
129         /*
130          * Mount arguments: vol [vol...] mountpt
131          */
132         extract_volumes(&info.volumes, &info.nvolumes, av, ac - 1);
133         mountpt = av[ac - 1];
134
135         /*
136          * Load the hammer module if necessary (this bit stolen from
137          * mount_null).
138          */
139         error = getvfsbyname("hammer", &vfc);
140         if (error && vfsisloadable("hammer")) {
141                 if (vfsload("hammer") != 0)
142                         err(1, "vfsload(hammer)");
143                 endvfsent();
144                 error = getvfsbyname("hammer", &vfc);
145         }
146         if (error)
147                 errx(1, "hammer filesystem is not available");
148
149         if (mount(vfc.vfc_name, mountpt, mount_flags, &info))
150                 err(1, NULL);
151         exit(0);
152 }
153
154 /*
155  * Parse a timestamp for the mount point
156  *
157  * yyyymmddhhmmss
158  * -N[s/h/d/m/y]
159  */
160 static
161 void
162 hammer_parsetime(u_int64_t *tidp, const char *timestr)
163 {
164         struct tm tm;
165         time_t t;
166         int32_t n;
167         char c;
168         double seconds = 0;
169
170         t = time(NULL);
171
172         if (*timestr == 0)
173                 usage();
174
175         if (isalpha(timestr[strlen(timestr)-1])) {
176                 if (sscanf(timestr, "%d%c", &n, &c) != 2)
177                         usage();
178                 switch(c) {
179                 case 'Y':
180                         n *= 365;
181                         goto days;
182                 case 'M':
183                         n *= 30;
184                         /* fall through */
185                 case 'D':
186                 days:
187                         n *= 24;
188                         /* fall through */
189                 case 'h':
190                         n *= 60;
191                         /* fall through */
192                 case 'm':
193                         n *= 60;
194                         /* fall through */
195                 case 's':
196                         t -= n;
197                         break;
198                 default:
199                         usage();
200                 }
201         } else {
202                 localtime_r(&t, &tm);
203                 seconds = (double)tm.tm_sec;
204                 tm.tm_year -= 1900;
205                 tm.tm_mon -= 1;
206                 n = sscanf(timestr, "%4d%2d%2d:%2d%2d%lf",
207                            &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
208                            &tm.tm_hour, &tm.tm_min, &seconds);
209                 tm.tm_mon += 1;
210                 tm.tm_year += 1900;
211                 tm.tm_sec = (int)seconds;
212                 t = mktime(&tm);
213         }
214         localtime_r(&t, &tm);
215         printf("mount_hammer as-of %s", asctime(&tm));
216         *tidp = (u_int64_t)t * 1000000000 + 
217                 (seconds - (int)seconds) * 1000000000;
218 }
219
220 /*
221  * Extract a volume list
222  */
223 static void
224 extract_volumes(ary_ptr_t *aryp, int *countp, char **av, int ac)
225 {
226         int idx = 0;
227         int arymax = 32;
228         const char **ary = malloc(sizeof(char *) * 32);
229         char *ptr;
230         char *next;
231
232         while (ac) {
233                 if (idx == arymax) {
234                         arymax += 32;
235                         ary = realloc(ary, sizeof(char *) * arymax);
236                 }
237                 if (strchr(*av, ':') == NULL) {
238                         ary[idx++] = *av;
239                 } else {
240                         next = strdup(*av);
241                         while ((ptr = next) != NULL) {
242                                 if (idx == arymax) {
243                                         arymax += 32;
244                                         ary = realloc(ary, sizeof(char *) *
245                                                       arymax);
246                                 }
247                                 if ((next = strchr(ptr, ':')) != NULL)
248                                         *next++ = 0;
249                                 ary[idx++] = ptr;
250                         }
251                 }
252                 --ac;
253                 ++av;
254                 
255         }
256         *aryp = ary;
257         *countp = idx;
258 }
259
260 static
261 void
262 usage(void)
263 {
264         fprintf(stderr, "mount_hammer [-T time] [-o options] "
265                         "volume [volume...] mount_pt");
266         fprintf(stderr, "mount_hammer -u [-o options] mount_pt");
267         fprintf(stderr, "    time: +n[s/m/h/D/M/Y]\n"
268                         "    time: yyyymmdd[:hhmmss]\n");
269         exit(1);
270 }