AMD64 - AUDIT RUN - Fix format strings, size_t, and other issues
[dragonfly.git] / sbin / hammer / cmd_pseudofs.c
1 /*
2  * Copyright (c) 2008 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/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $
35  */
36
37 #include "hammer.h"
38
39 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
40 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
41 static void dump_pfsd(hammer_pseudofs_data_t pfsd);
42 static void pseudofs_usage(int code);
43 static int getyn(void);
44 static int timetosecs(char *str);
45
46 /*
47  * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
48  * softlink.
49  */
50 int
51 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
52 {
53         uintmax_t dummy_tid;
54         struct stat st;
55         char *dirpath;
56         char buf[64];
57         int fd;
58         int n;
59
60         bzero(pfs, sizeof(*pfs));
61         pfs->ondisk = malloc(sizeof(*pfs->ondisk));
62         bzero(pfs->ondisk, sizeof(*pfs->ondisk));
63         pfs->bytes = sizeof(*pfs->ondisk);
64
65         /*
66          * Calculate the directory containing the softlink
67          */
68         dirpath = strdup(path);
69         if (strrchr(dirpath, '/')) {
70                 *strrchr(dirpath, '/') = 0;
71                 if (strlen(dirpath) == 0) {
72                         free(dirpath);
73                         dirpath = strdup("/");
74                 }
75         } else {
76                 free(dirpath);
77                 dirpath = strdup(".");
78         }
79
80         if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
81                 /*
82                  * Avoid foot-shooting.  Don't let the user access a PFS
83                  * softlink via a PFS.  PFS softlinks may only be accessed
84                  * via the master filesystem.
85                  */
86                 fd = open(dirpath, O_RDONLY);
87                 if (fd < 0)
88                         goto done;
89                 pfs->pfs_id = -1;
90                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
91                 if (pfs->pfs_id != 0) {
92                         fprintf(stderr,
93                                 "You are attempting to access a PFS softlink "
94                                 "from a PFS.  It may not represent the PFS\n"
95                                 "on the main filesystem mount that you "
96                                 "expect!  You may only access PFS softlinks\n"
97                                 "via the main filesystem mount!\n");
98                         exit(1);
99                 }
100                 close(fd);
101
102                 /*
103                  * Extract the PFS from the link.  HAMMER will automatically
104                  * convert @@PFS%05d links so if actually see one in that
105                  * form the target PFS may not exist or may be corrupt.  But
106                  * we can extract the PFS id anyway.
107                  */
108                 n = readlink(path, buf, sizeof(buf) - 1);
109                 if (n < 0)
110                         n = 0;
111                 buf[n] = 0;
112                 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
113                         fd = open(dirpath, O_RDONLY);
114                         goto done;
115                 }
116                 if (sscanf(buf, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
117                         fd = open(dirpath, O_RDONLY);
118                         goto done;
119                 }
120         }
121
122         /*
123          * Try to open the path and request the pfs_id that way.
124          */
125         fd = open(path, O_RDONLY);
126         if (fd >= 0) {
127                 pfs->pfs_id = -1;
128                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
129                 if (pfs->pfs_id == -1) {
130                         close(fd);
131                         fd = -1;
132                 }
133         }
134
135         /*
136          * Cleanup
137          */
138 done:
139         if (fd < 0) {
140                 fprintf(stderr, "Cannot access PFS %s: %s\n",
141                         path, strerror(errno));
142                 exit(1);
143         }
144         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
145                 fprintf(stderr, "Cannot access PFS %s: %s\n",
146                         path, strerror(errno));
147                 exit(1);
148         }
149         free(dirpath);
150         return(fd);
151 }
152
153 void
154 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
155 {
156         close(fd);
157         if (pfs->ondisk) {
158                 free(pfs->ondisk);
159                 pfs->ondisk = NULL;
160         }
161 }
162
163 void
164 hammer_cmd_pseudofs_status(char **av, int ac)
165 {
166         struct hammer_ioc_pseudofs_rw pfs;
167         int i;
168         int fd;
169
170         if (ac == 0)
171                 pseudofs_usage(1);
172
173         for (i = 0; i < ac; ++i) {
174                 printf("%s\t", av[i]);
175                 fd = getpfs(&pfs, av[i]);
176                 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
177                         printf("Not a HAMMER root\n");
178                 } else {
179                         printf("PFS #%d {\n", pfs.pfs_id);
180                         dump_pfsd(pfs.ondisk);
181                         printf("}\n");
182                 }
183                 if (fd >= 0)
184                         close(fd);
185                 if (pfs.ondisk)
186                         free(pfs.ondisk);
187         }
188 }
189
190 void
191 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
192 {
193         struct hammer_ioc_pseudofs_rw pfs;
194         struct hammer_pseudofs_data pfsd;
195         struct stat st;
196         const char *path;
197         char *dirpath;
198         char *linkpath;
199         int pfs_id;
200         int fd;
201         int error;
202
203         if (ac == 0)
204                 pseudofs_usage(1);
205         path = av[0];
206         if (lstat(path, &st) == 0) {
207                 fprintf(stderr, "Cannot create %s, file exists!\n", path);
208                 exit(1);
209         }
210
211         /*
212          * Figure out the directory prefix, taking care of degenerate
213          * cases.
214          */
215         dirpath = strdup(path);
216         if (strrchr(dirpath, '/') != NULL) {
217                 *strrchr(dirpath, '/') = 0;
218                 if (dirpath[0] == 0) {
219                         free(dirpath);
220                         dirpath = strdup("/");
221                 }
222         } else {
223                 free(dirpath);
224                 dirpath = strdup(".");
225         }
226         fd = open(dirpath, O_RDONLY);
227         if (fd < 0) {
228                 fprintf(stderr, "Cannot open directory %s\n", dirpath);
229                 exit(1);
230         }
231
232         error = 0;
233         for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
234                 bzero(&pfs, sizeof(pfs));
235                 bzero(&pfsd, sizeof(pfsd));
236                 pfs.pfs_id = pfs_id;
237                 pfs.ondisk = &pfsd;
238                 pfs.bytes = sizeof(pfsd);
239                 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
240                 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
241                         error = errno;
242                         break;
243                 }
244         }
245         if (pfs_id == HAMMER_MAX_PFS) {
246                 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
247                 exit(1);
248         }
249         if (error != ENOENT) {
250                 fprintf(stderr, "Cannot create %s, got %s during scan\n",
251                         path, strerror(error));
252                 exit(1);
253         }
254
255         /*
256          * Create the new PFS
257          */
258         printf("Creating PFS #%d\t", pfs_id);
259         bzero(&pfsd, sizeof(pfsd));
260         init_pfsd(&pfsd, is_slave);
261         pfs.pfs_id = pfs_id;
262         pfs.ondisk = &pfsd;
263         pfs.bytes = sizeof(pfsd);
264         pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
265
266         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
267                 printf("failed: %s\n", strerror(errno));
268         } else {
269                 /* special symlink, must be exactly 10 characters */
270                 asprintf(&linkpath, "@@PFS%05d", pfs_id);
271                 if (symlink(linkpath, path) < 0) {
272                         printf("failed: cannot create symlink: %s\n",
273                                 strerror(errno));
274                 } else {
275                         printf("succeeded!\n");
276                         hammer_cmd_pseudofs_update(av, ac);
277                 }
278         }
279         close(fd);
280 }
281
282 void
283 hammer_cmd_pseudofs_destroy(char **av, int ac)
284 {
285         struct hammer_ioc_pseudofs_rw pfs;
286         struct stat st;
287         int fd;
288         int i;
289
290         if (ac == 0)
291                 pseudofs_usage(1);
292         bzero(&pfs, sizeof(pfs));
293         fd = getpfs(&pfs, av[0]);
294
295         if (pfs.pfs_id == 0) {
296                 fprintf(stderr, "You cannot destroy PFS#0\n");
297                 exit(1);
298         }
299         printf("You have requested that PFS#%d (%s) be destroyed\n",
300                 pfs.pfs_id, pfs.ondisk->label);
301         printf("This will irrevocably destroy all data on this PFS!!!!!\n");
302         printf("Do you really want to do this? ");
303         fflush(stdout);
304         if (getyn() == 0) {
305                 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
306                 exit(1);
307         }
308
309         if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
310                 printf("This PFS is currently setup as a MASTER!\n");
311                 printf("Are you absolutely sure you want to destroy it? ");
312                 fflush(stdout);
313                 if (getyn() == 0) {
314                         fprintf(stderr, "No action taken on PFS#%d\n",
315                                 pfs.pfs_id);
316                         exit(1);
317                 }
318         }
319
320         printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
321         for (i = 5; i; --i) {
322                 printf(" %d", i);
323                 fflush(stdout);
324                 sleep(1);
325         }
326         printf(".. starting destruction pass\n");
327         fflush(stdout);
328
329         /*
330          * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
331          * RMR the PFS is basically destroyed even if someone ^C's it.
332          */
333         pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
334         pfs.ondisk->reserved01 = -1;
335         pfs.ondisk->sync_beg_tid = 1;
336         pfs.ondisk->sync_end_tid = 1;
337
338         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
339                 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
340                         strerror(errno));
341                 exit(1);
342         }
343
344         /*
345          * Ok, do it.  Remove the softlink on success.
346          */
347         if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
348                 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
349                 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
350                         if (remove(av[0]) < 0) {
351                                 fprintf(stderr, "Unable to remove softlink: %s "
352                                         "(but the PFS has been destroyed)\n",
353                                         av[0]);
354                                 /* exit status 0 anyway */
355                         }
356                 }
357         } else {
358                 printf("pfs-destroy of PFS#%d failed: %s\n",
359                         pfs.pfs_id, strerror(errno));
360         }
361 }
362
363 void
364 hammer_cmd_pseudofs_upgrade(char **av, int ac)
365 {
366         struct hammer_ioc_pseudofs_rw pfs;
367         int fd;
368
369         if (ac == 0)
370                 pseudofs_usage(1);
371         bzero(&pfs, sizeof(pfs));
372         fd = getpfs(&pfs, av[0]);
373
374         if (pfs.pfs_id == 0) {
375                 fprintf(stderr, "You cannot upgrade PFS#0"
376                                 " (It should already be a master)\n");
377                 exit(1);
378         }
379         if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
380                 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
381                         pfs.pfs_id, pfs.ondisk->label);
382         } else {
383                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
384                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
385         }
386 }
387
388 void
389 hammer_cmd_pseudofs_downgrade(char **av, int ac)
390 {
391         struct hammer_ioc_pseudofs_rw pfs;
392         int fd;
393
394         if (ac == 0)
395                 pseudofs_usage(1);
396         bzero(&pfs, sizeof(pfs));
397         fd = getpfs(&pfs, av[0]);
398
399         if (pfs.pfs_id == 0) {
400                 fprintf(stderr, "You cannot downgrade PFS#0\n");
401                 exit(1);
402         }
403
404         if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
405                 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
406                         pfs.pfs_id, pfs.ondisk->label);
407         } else {
408                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
409                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
410         }
411 }
412
413 void
414 hammer_cmd_pseudofs_update(char **av, int ac)
415 {
416         struct hammer_ioc_pseudofs_rw pfs;
417         int fd;
418
419         if (ac == 0)
420                 pseudofs_usage(1);
421         bzero(&pfs, sizeof(pfs));
422         fd = getpfs(&pfs, av[0]);
423
424         printf("%s\n", av[0]);
425         fflush(stdout);
426
427         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
428                 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
429                 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
430                     pfs.pfs_id == 0) {
431                         printf("The real mount point cannot be made a PFS "
432                                "slave, only PFS sub-directories can be made "
433                                "slaves\n");
434                         exit(1);
435                 }
436                 pfs.bytes = sizeof(*pfs.ondisk);
437                 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
438                         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
439                                 dump_pfsd(pfs.ondisk);
440                         } else {
441                                 printf("Unable to retrieve pfs configuration "
442                                         "after successful update: %s\n",
443                                         strerror(errno));
444                                 exit(1);
445                         }
446                 } else {
447                         printf("Unable to adjust pfs configuration: %s\n",
448                                 strerror(errno));
449                         exit(1);
450                 }
451         }
452 }
453
454 static void
455 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
456 {
457         uint32_t status;
458
459         pfsd->sync_beg_tid = 1;
460         pfsd->sync_end_tid = 1;
461         pfsd->sync_beg_ts = 0;
462         pfsd->sync_end_ts = 0;
463         uuid_create(&pfsd->shared_uuid, &status);
464         uuid_create(&pfsd->unique_uuid, &status);
465         if (is_slave)
466                 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
467 }
468
469 static
470 void
471 dump_pfsd(hammer_pseudofs_data_t pfsd)
472 {
473         u_int32_t status;
474         char *str = NULL;
475
476         printf("    sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
477         printf("    sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
478         uuid_to_string(&pfsd->shared_uuid, &str, &status);
479         printf("    shared-uuid=%s\n", str);
480         uuid_to_string(&pfsd->unique_uuid, &str, &status);
481         printf("    unique-uuid=%s\n", str);
482         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
483                 printf("    slave\n");
484         }
485         printf("    label=\"%s\"\n", pfsd->label);
486         if (pfsd->snapshots[0])
487                 printf("    snapshots=\"%s\"\n", pfsd->snapshots);
488         if (pfsd->prune_min < (60 * 60 * 24)) {
489                 printf("    prune-min=%02d:%02d:%02d\n",
490                         pfsd->prune_min / 60 / 60 % 24,
491                         pfsd->prune_min / 60 % 60,
492                         pfsd->prune_min % 60);
493         } else if (pfsd->prune_min % (60 * 60 * 24)) {
494                 printf("    prune-min=%dd/%02d:%02d:%02d\n",
495                         pfsd->prune_min / 60 / 60 / 24,
496                         pfsd->prune_min / 60 / 60 % 24,
497                         pfsd->prune_min / 60 % 60,
498                         pfsd->prune_min % 60);
499         } else {
500                 printf("    prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
501         }
502
503         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
504                 printf("    operating as a SLAVE\n");
505                 if (pfsd->snapshots[0] == 0)
506                         printf("    snapshots directory not set for slave\n");
507         } else {
508                 printf("    operating as a MASTER\n");
509                 if (pfsd->snapshots[0] == 0) {
510                         printf("    snapshots dir for master "
511                                "defaults to <fs>/snapshots\n");
512                 }
513         }
514 }
515
516 static void
517 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
518 {
519         char *cmd;
520         char *ptr;
521         int len;
522         uint32_t status;
523
524         while (ac) {
525                 cmd = *av;
526                 if ((ptr = strchr(cmd, '=')) != NULL)
527                         *ptr++ = 0;
528
529                 /*
530                  * Basic assignment value test
531                  */
532                 if (ptr == NULL) {
533                         fprintf(stderr,
534                                 "option %s requires an assignment\n",
535                                 cmd);
536                         exit(1);
537                 }
538
539                 status = uuid_s_ok;
540                 if (strcmp(cmd, "sync-beg-tid") == 0) {
541                         pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
542                 } else if (strcmp(cmd, "sync-end-tid") == 0) {
543                         pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
544                 } else if (strcmp(cmd, "shared-uuid") == 0) {
545                         uuid_from_string(ptr, &pfsd->shared_uuid, &status);
546                 } else if (strcmp(cmd, "unique-uuid") == 0) {
547                         uuid_from_string(ptr, &pfsd->unique_uuid, &status);
548                 } else if (strcmp(cmd, "label") == 0) {
549                         len = strlen(ptr);
550                         if (ptr[0] == '"' && ptr[len-1] == '"') {
551                                 ptr[len-1] = 0;
552                                 ++ptr;
553                         } else if (ptr[0] == '"') {
554                                 fprintf(stderr,
555                                         "option %s: malformed string\n",
556                                         cmd);
557                                 exit(1);
558                         }
559                         snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
560                 } else if (strcmp(cmd, "snapshots") == 0) {
561                         len = strlen(ptr);
562                         if (ptr[0] != '/') {
563                                 fprintf(stderr,
564                                         "option %s: '%s' must be an "
565                                         "absolute path\n", cmd, ptr);
566                                 if (ptr[0] == 0) {
567                                         fprintf(stderr, 
568                                                 "use 'snapshots-clear' "
569                                                 "to unset snapshots dir\n");
570                                 }
571                                 exit(1);
572                         }
573                         if (len >= (int)sizeof(pfsd->snapshots)) {
574                                 fprintf(stderr,
575                                         "option %s: path too long, %d "
576                                         "character limit\n", cmd, len);
577                                 exit(1);
578                         }
579                         snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
580                                  "%s", ptr);
581                 } else if (strcmp(cmd, "snapshots-clear") == 0) {
582                         pfsd->snapshots[0] = 0;
583                 } else if (strcmp(cmd, "prune-min") == 0) {
584                         pfsd->prune_min = timetosecs(ptr);
585                         if (pfsd->prune_min < 0) {
586                                 fprintf(stderr,
587                                         "option %s: illegal time spec, "
588                                         "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
589                                 exit(1);
590                         }
591                 } else {
592                         fprintf(stderr, "invalid option: %s\n", cmd);
593                         exit(1);
594                 }
595                 if (status != uuid_s_ok) {
596                         fprintf(stderr, "option %s: error parsing uuid %s\n",
597                                 cmd, ptr);
598                         exit(1);
599                 }
600                 --ac;
601                 ++av;
602         }
603 }
604
605 static
606 void
607 pseudofs_usage(int code)
608 {
609         fprintf(stderr, 
610                 "hammer pfs-status <dirpath> ...\n"
611                 "hammer pfs-master <dirpath> [options]\n"
612                 "hammer pfs-slave <dirpath> [options]\n"
613                 "hammer pfs-update <dirpath> [options]\n"
614                 "hammer pfs-upgrade <dirpath>\n"
615                 "hammer pfs-downgrade <dirpath>\n"
616                 "hammer pfs-destroy <dirpath>\n"
617                 "\n"
618                 "options:\n"
619                 "    sync-beg-tid=0x16llx\n"
620                 "    sync-end-tid=0x16llx\n"
621                 "    shared-uuid=0x16llx\n"
622                 "    unique-uuid=0x16llx\n"
623                 "    label=\"string\"\n"
624                 "    snapshots=\"/path\"\n"
625                 "    snapshots-clear\n"
626                 "    prune-min=[Nd/][hh[:mm[:ss]]]\n"
627         );
628         exit(code);
629 }
630
631 static
632 int
633 getyn(void)
634 {
635         char buf[256];
636         int len;
637
638         if (fgets(buf, sizeof(buf), stdin) == NULL)
639                 return(0);
640         len = strlen(buf);
641         while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
642                 --len;
643         buf[len] = 0;
644         if (strcmp(buf, "y") == 0 ||
645             strcmp(buf, "yes") == 0 ||
646             strcmp(buf, "Y") == 0 ||
647             strcmp(buf, "YES") == 0) {
648                 return(1);
649         }
650         return(0);
651 }
652
653 /*
654  * Convert time in the form [Nd/][hh[:mm[:ss]]] to seconds.
655  *
656  * Return -1 if a parse error occurs.
657  * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
658  */
659 static
660 int
661 timetosecs(char *str)
662 {
663         int days = 0;
664         int hrs = 0;
665         int mins = 0;
666         int secs = 0;
667         int n;
668         long long v;
669         char *ptr;
670
671         n = strtol(str, &ptr, 10);
672         if (n < 0)
673                 return(-1);
674         if (*ptr == 'd') {
675                 days = n;
676                 ++ptr;
677                 if (*ptr == '/')
678                     n = strtol(ptr + 1, &ptr, 10);
679                 else
680                     n = 0;
681         }
682         if (n < 0)
683                 return(-1);
684         hrs = n;
685         if (*ptr == ':') {
686                 n = strtol(ptr + 1, &ptr, 10);
687                 if (n < 0)
688                         return(-1);
689                 mins = n;
690                 if (*ptr == ':') {
691                         n = strtol(ptr + 1, &ptr, 10);
692                         if (n < 0)
693                                 return(-1);
694                         secs = n;
695                 }
696         }
697         if (*ptr)
698                 return(-1);
699         v = days * 24 * 60 * 60 + hrs *  60 * 60 + mins * 60 + secs;
700         if (v > 0x7FFFFFFF)
701                 v = 0x7FFFFFFF;
702         return((int)v);
703 }