sbin/hammer: Change "PFS #" to "PFS#"
[dragonfly.git] / sbin / hammer / cmd_pfs.c
blob5d892259f5f0e9a4c27fa41379ebd73eee878c2d
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
34 * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $
37 #include <libgen.h>
39 #include "hammer.h"
41 static int scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path);
42 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
43 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
44 static void pseudofs_usage(int code);
45 static int timetosecs(char *str);
47 void
48 clrpfs(struct hammer_ioc_pseudofs_rw *pfs, hammer_pseudofs_data_t pfsd,
49 int pfs_id)
51 bzero(pfs, sizeof(*pfs));
53 if (pfsd)
54 pfs->ondisk = pfsd;
55 else
56 pfs->ondisk = malloc(sizeof(*pfs->ondisk));
57 bzero(pfs->ondisk, sizeof(*pfs->ondisk));
59 pfs->pfs_id = pfs_id;
60 pfs->bytes = sizeof(*pfs->ondisk);
61 pfs->version = HAMMER_IOC_PSEUDOFS_VERSION;
65 * Return a directory that contains path.
66 * A caller need to free the returned pointer.
67 * This behaves differently from dirname(3) with trailing /.
69 static char*
70 getdir(const char *path)
72 char *dirpath;
74 dirpath = strdup(path);
76 if (dirpath[strlen(dirpath) - 1] == '/') {
77 while (dirpath[strlen(dirpath) - 1] == '/')
78 dirpath[strlen(dirpath) - 1] = 0;
79 } else if (strrchr(dirpath, '/')) {
80 *strrchr(dirpath, '/') = 0;
81 } else {
82 free(dirpath);
83 dirpath = strdup(".");
86 if (strlen(dirpath) == 0) {
87 free(dirpath);
88 dirpath = strdup("/");
91 return(dirpath);
95 * Calculate the PFS id given a path to a file/directory or
96 * a @@%llx:%d softlink.
98 int
99 getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path)
101 int fd;
102 char *p;
104 clrpfs(pfs, NULL, -1);
107 * Trailing '/' must be removed so that upon pfs-destroy
108 * the symlink can be deleted without problems.
109 * Root directory (/) must be excluded from this.
111 p = path + (int)strlen(path) - 1;
112 assert(p >= path);
113 while (p != path && *p == '/')
114 *p-- = 0;
116 fd = scanpfsid(pfs, path);
117 if (fd < 0) {
119 * Once it comes here the hammer command may fail even if
120 * this function returns valid file descriptor, however this
121 * open still needs to be here as some PFS commands can take
122 * a regular file or directory path (e.g. hammer pfs-status).
124 fd = open(path, O_RDONLY);
125 if (fd < 0) {
126 fprintf(stderr, "Failed to open %s\n", path);
127 exit(1);
132 * If pfs.pfs_id has been set to non -1, the file descriptor fd
133 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
134 * doesn't depend on inode attributes if it's set to a valid id.
136 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
137 fprintf(stderr, "Cannot access PFS %s: %s\n",
138 path, strerror(errno));
139 exit(1);
141 return(fd);
145 * Extract the PFS id from path. Return a file descriptor of
146 * the parent directory which is expected to be located under
147 * the root filesystem (not another PFS).
149 static int
150 scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
152 int fd = -1;
153 const char *p;
154 char *dirpath;
155 char buf[64];
156 uintmax_t dummy_tid;
157 struct stat st;
159 if (stat(path, &st)) {
160 /* possibly slave PFS */
161 } else if (S_ISDIR(st.st_mode)) {
162 /* possibly master or slave PFS */
163 } else {
164 return(-1); /* neither */
168 * If the path is a link read the link.
170 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
171 bzero(buf, sizeof(buf));
172 if (readlink(path, buf, sizeof(buf) - 1) < 0)
173 return(-1);
174 p = buf;
175 } else {
176 p = path;
180 * The symlink created by pfs-master|slave is just a symlink.
181 * One could happen to remove a symlink and relink PFS as
182 * # ln -s ./@@-1:00001 ./link
183 * which results PFS having something extra before @@.
184 * One could also directly use the PFS and results the same.
185 * Get rid of it before we extract the PFS id.
187 if (strchr(p, '/')) {
188 p = basename(p);
189 if (p == NULL)
190 err(1, "basename");
194 * Test and extract the PFS id from the link, and then open
195 * and return fd of the parent directory of that link. fd is
196 * not so important as long as the right PFS id is extracted.
198 * "@@%jx:%d" convers both "@@-1:%05d" format for master PFS
199 * and "@@0x%016jx:%05d" format for slave PFS.
201 dirpath = getdir(path);
202 if (sscanf(p, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2)
203 fd = open(dirpath, O_RDONLY);
204 free(dirpath);
206 return(fd);
209 void
210 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
212 if (fd >= 0)
213 close(fd);
214 if (pfs->ondisk) {
215 free(pfs->ondisk);
216 pfs->ondisk = NULL;
220 static void
221 print_pfs_status(char *path)
223 struct hammer_ioc_pseudofs_rw pfs;
224 int fd;
226 fd = getpfs(&pfs, path);
227 printf("%s\t", path);
228 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
229 printf("Invalid PFS path %s\n", path);
230 } else {
231 printf("PFS#%d {\n", pfs.pfs_id);
232 dump_pfsd(pfs.ondisk, fd);
233 printf("}\n");
235 if (fd >= 0)
236 close(fd);
237 if (pfs.ondisk)
238 free(pfs.ondisk);
239 relpfs(fd, &pfs);
242 void
243 hammer_cmd_pseudofs_status(char **av, int ac)
245 int i;
247 if (ac == 0) {
248 char buf[2] = "."; /* can't be readonly string */
249 print_pfs_status(buf);
250 return;
253 for (i = 0; i < ac; ++i)
254 print_pfs_status(av[i]);
257 void
258 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
260 struct hammer_ioc_pseudofs_rw pfs;
261 struct hammer_pseudofs_data pfsd;
262 struct stat st;
263 const char *path;
264 char *dirpath;
265 char *linkpath;
266 int pfs_id;
267 int fd;
268 int error;
270 if (ac == 0)
271 pseudofs_usage(1);
272 path = av[0];
273 if (lstat(path, &st) == 0) {
274 fprintf(stderr, "Cannot create %s, file exists!\n", path);
275 exit(1);
279 * Figure out the directory prefix, taking care of degenerate
280 * cases.
282 dirpath = getdir(path);
283 fd = open(dirpath, O_RDONLY);
284 if (fd < 0) {
285 fprintf(stderr, "Cannot open directory %s\n", dirpath);
286 exit(1);
290 * Avoid foot-shooting. Don't let the user create a PFS
291 * softlink via a PFS. PFS softlinks may only be accessed
292 * via the master filesystem. Checking it here ensures
293 * other PFS commands access PFS under the master filesystem.
295 clrpfs(&pfs, &pfsd, -1);
297 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs);
298 if (pfs.pfs_id != HAMMER_ROOT_PFSID) {
299 fprintf(stderr,
300 "You are attempting to access a PFS softlink "
301 "from a PFS. It may not represent the PFS\n"
302 "on the main filesystem mount that you "
303 "expect! You may only access PFS softlinks\n"
304 "via the main filesystem mount!\n");
305 exit(1);
308 error = 0;
309 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
310 clrpfs(&pfs, &pfsd, pfs_id);
311 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
312 error = errno;
313 break;
316 if (pfs_id == HAMMER_MAX_PFS) {
317 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
318 exit(1);
319 } else if (pfs_id == HAMMER_ROOT_PFSID) {
320 fprintf(stderr, "Fatal error: PFS#%d must exist\n",
321 HAMMER_ROOT_PFSID);
322 exit(1);
325 if (error != ENOENT) {
326 fprintf(stderr, "Cannot create %s, got %s during scan\n",
327 path, strerror(error));
328 exit(1);
332 * Create the new PFS
334 printf("Creating PFS#%d\t", pfs_id);
335 clrpfs(&pfs, &pfsd, pfs_id);
336 init_pfsd(&pfsd, is_slave);
338 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
339 printf("failed: %s\n", strerror(errno));
340 } else {
341 /* special symlink, must be exactly 10 characters */
342 asprintf(&linkpath, "@@PFS%05d", pfs_id);
343 if (symlink(linkpath, path) < 0) {
344 printf("failed: cannot create symlink: %s\n",
345 strerror(errno));
346 } else {
347 printf("succeeded!\n");
348 hammer_cmd_pseudofs_update(av, ac);
351 free(dirpath);
352 close(fd);
355 void
356 hammer_cmd_pseudofs_destroy(char **av, int ac)
358 struct hammer_ioc_pseudofs_rw pfs;
359 struct stat st;
360 int fd;
361 int i;
363 if (ac == 0)
364 pseudofs_usage(1);
365 fd = getpfs(&pfs, av[0]);
367 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
368 fprintf(stderr, "You cannot destroy PFS#0\n");
369 exit(1);
371 printf("You have requested that PFS#%d (%s) be destroyed\n",
372 pfs.pfs_id, pfs.ondisk->label);
373 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
374 printf("Do you really want to do this? ");
375 fflush(stdout);
376 if (getyn() == 0) {
377 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
378 exit(1);
381 if (hammer_is_pfs_master(pfs.ondisk)) {
382 printf("This PFS is currently setup as a MASTER!\n");
383 printf("Are you absolutely sure you want to destroy it? ");
384 fflush(stdout);
385 if (getyn() == 0) {
386 fprintf(stderr, "No action taken on PFS#%d\n",
387 pfs.pfs_id);
388 exit(1);
392 printf("Destroying PFS#%d (%s)", pfs.pfs_id, pfs.ondisk->label);
393 if (DebugOpt) {
394 printf("\n");
395 } else {
396 printf(" in");
397 for (i = 5; i; --i) {
398 printf(" %d", i);
399 fflush(stdout);
400 sleep(1);
402 printf(".. starting destruction pass\n");
406 * Remove the softlink on success.
408 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
409 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
410 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
411 if (remove(av[0]) < 0) {
412 fprintf(stderr, "Unable to remove softlink: %s "
413 "(but the PFS has been destroyed)\n",
414 av[0]);
415 /* exit status 0 anyway */
418 } else {
419 printf("pfs-destroy of PFS#%d failed: %s\n",
420 pfs.pfs_id, strerror(errno));
422 relpfs(fd, &pfs);
425 void
426 hammer_cmd_pseudofs_upgrade(char **av, int ac)
428 struct hammer_ioc_pseudofs_rw pfs;
429 int fd;
431 if (ac == 0)
432 pseudofs_usage(1);
433 fd = getpfs(&pfs, av[0]);
435 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
436 fprintf(stderr, "You cannot upgrade PFS#0"
437 " (It should already be a master)\n");
438 exit(1);
439 } else if (hammer_is_pfs_master(pfs.ondisk)) {
440 printf("It is already a master\n");
441 exit(1);
444 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
445 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
446 pfs.pfs_id, pfs.ondisk->label);
447 } else {
448 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
449 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
451 relpfs(fd, &pfs);
454 void
455 hammer_cmd_pseudofs_downgrade(char **av, int ac)
457 struct hammer_ioc_pseudofs_rw pfs;
458 int fd;
460 if (ac == 0)
461 pseudofs_usage(1);
462 fd = getpfs(&pfs, av[0]);
464 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
465 fprintf(stderr, "You cannot downgrade PFS#0\n");
466 exit(1);
467 } else if (hammer_is_pfs_slave(pfs.ondisk)) {
468 printf("It is already a slave\n");
469 exit(1);
472 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
473 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
474 pfs.pfs_id, pfs.ondisk->label);
475 } else {
476 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
477 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
479 relpfs(fd, &pfs);
482 void
483 hammer_cmd_pseudofs_update(char **av, int ac)
485 struct hammer_ioc_pseudofs_rw pfs;
486 int fd;
488 if (ac == 0)
489 pseudofs_usage(1);
490 fd = getpfs(&pfs, av[0]);
492 printf("%s\n", av[0]);
493 fflush(stdout);
495 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
496 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
497 if (hammer_is_pfs_slave(pfs.ondisk) &&
498 pfs.pfs_id == HAMMER_ROOT_PFSID) {
499 printf("The real mount point cannot be made a PFS "
500 "slave, only PFS sub-directories can be made "
501 "slaves\n");
502 exit(1);
504 pfs.bytes = sizeof(*pfs.ondisk);
505 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
506 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
507 dump_pfsd(pfs.ondisk, fd);
508 } else {
509 printf("Unable to retrieve PFS configuration "
510 "after successful update: %s\n",
511 strerror(errno));
512 exit(1);
514 } else {
515 printf("Unable to adjust PFS configuration: %s\n",
516 strerror(errno));
517 exit(1);
520 relpfs(fd, &pfs);
523 static void
524 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
526 uint32_t status;
528 bzero(pfsd, sizeof(*pfsd));
529 pfsd->sync_beg_tid = 1;
530 pfsd->sync_end_tid = 1;
531 pfsd->sync_beg_ts = 0;
532 pfsd->sync_end_ts = 0;
533 uuid_create(&pfsd->shared_uuid, &status);
534 uuid_create(&pfsd->unique_uuid, &status);
535 if (is_slave)
536 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
539 void
540 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
542 struct hammer_ioc_version version;
543 uint32_t status;
544 char *str = NULL;
546 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
547 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
548 uuid_to_string(&pfsd->shared_uuid, &str, &status);
549 printf(" shared-uuid=%s\n", str);
550 free(str);
551 uuid_to_string(&pfsd->unique_uuid, &str, &status);
552 printf(" unique-uuid=%s\n", str);
553 free(str);
554 printf(" label=\"%s\"\n", pfsd->label);
555 if (pfsd->snapshots[0])
556 printf(" snapshots=\"%s\"\n", pfsd->snapshots);
557 if (pfsd->prune_min < (60 * 60 * 24)) {
558 printf(" prune-min=%02d:%02d:%02d\n",
559 pfsd->prune_min / 60 / 60 % 24,
560 pfsd->prune_min / 60 % 60,
561 pfsd->prune_min % 60);
562 } else if (pfsd->prune_min % (60 * 60 * 24)) {
563 printf(" prune-min=%dd/%02d:%02d:%02d\n",
564 pfsd->prune_min / 60 / 60 / 24,
565 pfsd->prune_min / 60 / 60 % 24,
566 pfsd->prune_min / 60 % 60,
567 pfsd->prune_min % 60);
568 } else {
569 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
572 if (hammer_is_pfs_slave(pfsd)) {
573 printf(" operating as a SLAVE\n");
574 } else {
575 printf(" operating as a MASTER\n");
579 * Snapshots directory cannot be shown when there is no fd since
580 * hammer version can't be retrieved. mirror-dump passes -1 because
581 * its input came from mirror-read output thus no path is available
582 * to open(2).
584 if (fd >= 0 && pfsd->snapshots[0] == 0) {
585 bzero(&version, sizeof(version));
586 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
587 return;
588 if (version.cur_version < 3) {
589 if (hammer_is_pfs_slave(pfsd)) {
590 printf(" snapshots directory not set for "
591 "slave\n");
592 } else {
593 printf(" snapshots directory for master "
594 "defaults to <pfs>/snapshots\n");
596 } else {
597 printf(" snapshots directory defaults to "
598 "/var/hammer/<pfs>\n");
603 static void
604 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
606 char *cmd;
607 char *ptr;
608 int len;
609 uint32_t status;
611 while (ac) {
612 cmd = *av;
613 if ((ptr = strchr(cmd, '=')) != NULL)
614 *ptr++ = 0;
617 * Basic assignment value test
619 if (ptr == NULL) {
620 fprintf(stderr,
621 "option %s requires an assignment\n",
622 cmd);
623 exit(1);
626 status = uuid_s_ok;
627 if (strcmp(cmd, "sync-beg-tid") == 0) {
628 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
629 } else if (strcmp(cmd, "sync-end-tid") == 0) {
630 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
631 } else if (strcmp(cmd, "shared-uuid") == 0) {
632 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
633 } else if (strcmp(cmd, "unique-uuid") == 0) {
634 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
635 } else if (strcmp(cmd, "label") == 0) {
636 len = strlen(ptr);
637 if (ptr[0] == '"' && ptr[len-1] == '"') {
638 ptr[len-1] = 0;
639 ++ptr;
640 } else if (ptr[0] == '"') {
641 fprintf(stderr,
642 "option %s: malformed string\n",
643 cmd);
644 exit(1);
646 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
647 } else if (strcmp(cmd, "snapshots") == 0) {
648 len = strlen(ptr);
649 if (ptr[0] != '/') {
650 fprintf(stderr,
651 "option %s: '%s' must be an "
652 "absolute path\n", cmd, ptr);
653 if (ptr[0] == 0) {
654 fprintf(stderr,
655 "use 'snapshots-clear' "
656 "to unset snapshots dir\n");
658 exit(1);
660 if (len >= (int)sizeof(pfsd->snapshots)) {
661 fprintf(stderr,
662 "option %s: path too long, %d "
663 "character limit\n", cmd, len);
664 exit(1);
666 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
667 "%s", ptr);
668 } else if (strcmp(cmd, "snapshots-clear") == 0) {
669 pfsd->snapshots[0] = 0;
670 } else if (strcmp(cmd, "prune-min") == 0) {
671 pfsd->prune_min = timetosecs(ptr);
672 if (pfsd->prune_min < 0) {
673 fprintf(stderr,
674 "option %s: illegal time spec, "
675 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
676 exit(1);
678 } else {
679 fprintf(stderr, "invalid option: %s\n", cmd);
680 exit(1);
682 if (status != uuid_s_ok) {
683 fprintf(stderr, "option %s: error parsing uuid %s\n",
684 cmd, ptr);
685 exit(1);
687 --ac;
688 ++av;
692 static
693 void
694 pseudofs_usage(int code)
696 fprintf(stderr,
697 "hammer pfs-status <dirpath> ...\n"
698 "hammer pfs-master <dirpath> [options]\n"
699 "hammer pfs-slave <dirpath> [options]\n"
700 "hammer pfs-update <dirpath> [options]\n"
701 "hammer pfs-upgrade <dirpath>\n"
702 "hammer pfs-downgrade <dirpath>\n"
703 "hammer pfs-destroy <dirpath>\n"
704 "\n"
705 "options:\n"
706 " sync-beg-tid=0x16llx\n"
707 " sync-end-tid=0x16llx\n"
708 " shared-uuid=0x16llx\n"
709 " unique-uuid=0x16llx\n"
710 " label=\"string\"\n"
711 " snapshots=\"/path\"\n"
712 " snapshots-clear\n"
713 " prune-min=Nd\n"
714 " prune-min=[Nd/]hh[:mm[:ss]]\n"
716 exit(code);
720 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
722 * Return -1 if a parse error occurs.
723 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
725 static
727 timetosecs(char *str)
729 int days = 0;
730 int hrs = 0;
731 int mins = 0;
732 int secs = 0;
733 int n;
734 long long v;
735 char *ptr;
737 n = strtol(str, &ptr, 10);
738 if (n < 0)
739 return(-1);
740 if (*ptr == 'd') {
741 days = n;
742 ++ptr;
743 if (*ptr == '/')
744 n = strtol(ptr + 1, &ptr, 10);
745 else
746 n = 0;
748 if (n < 0)
749 return(-1);
750 hrs = n;
751 if (*ptr == ':') {
752 n = strtol(ptr + 1, &ptr, 10);
753 if (n < 0)
754 return(-1);
755 mins = n;
756 if (*ptr == ':') {
757 n = strtol(ptr + 1, &ptr, 10);
758 if (n < 0)
759 return(-1);
760 secs = n;
763 if (*ptr)
764 return(-1);
765 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;
766 if (v > 0x7FFFFFFF)
767 v = 0x7FFFFFFF;
768 return((int)v);