sbin/hammer: Add /* not reached */ for usage() variants
[dragonfly.git] / sbin / hammer / cmd_pfs.c
blob65fc34799b632d581bc6cff9063f3d62f3605b46
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 * If path is a symlink, return strdup'd path.
66 * If it's a directory via symlink, strip trailing /
67 * from strdup'd path and return the symlink.
69 static char*
70 getlink(const char *path)
72 int i;
73 char *linkpath;
74 struct stat st;
76 if (lstat(path, &st))
77 return(NULL);
78 linkpath = strdup(path);
80 if (S_ISDIR(st.st_mode)) {
81 i = strlen(linkpath) - 1;
82 while (i > 0 && linkpath[i] == '/')
83 linkpath[i--] = 0;
84 lstat(linkpath, &st);
86 if (S_ISLNK(st.st_mode))
87 return(linkpath);
89 free(linkpath);
90 return(NULL);
94 * Calculate the PFS id given a path to a file/directory or
95 * a @@%llx:%d softlink.
97 int
98 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
100 int fd;
102 clrpfs(pfs, NULL, -1);
105 * Extract the PFS id.
106 * dirname(path) is supposed to be a directory in root PFS.
108 if (scanpfsid(pfs, path) == 0)
109 path = dirname(path); /* strips trailing / first if any */
112 * Open the path regardless of scanpfsid() result, since some
113 * commands can take a regular file/directory (e.g. pfs-status).
115 fd = open(path, O_RDONLY);
116 if (fd < 0) {
117 err(1, "Failed to open %s", path);
118 /* not reached */
122 * If pfs.pfs_id has been set to non -1, the file descriptor fd
123 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
124 * doesn't depend on inode attributes if it's set to a valid id.
126 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
127 err(1, "Cannot access %s", path);
128 /* not reached */
131 return(fd);
135 * Extract the PFS id from path.
137 static int
138 scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
140 char *linkpath;
141 char buf[64];
142 uintmax_t dummy_tid;
143 struct stat st;
145 if (stat(path, &st))
146 ; /* possibly slave PFS */
147 else if (S_ISDIR(st.st_mode))
148 ; /* possibly master or slave PFS */
149 else
150 return(-1); /* neither */
152 linkpath = getlink(path);
153 if (linkpath) {
155 * Read the symlink assuming it's a link to PFS.
157 bzero(buf, sizeof(buf));
158 if (readlink(linkpath, buf, sizeof(buf) - 1) < 0) {
159 free(linkpath);
160 return(-1);
162 free(linkpath);
163 path = buf;
167 * The symlink created by pfs-master|slave is just a symlink.
168 * One could happen to remove a symlink and relink PFS as
169 * # ln -s ./@@-1:00001 ./link
170 * which results PFS having something extra before @@.
171 * One could also directly use the PFS and results the same.
172 * Get rid of it before we extract the PFS id.
174 if (strchr(path, '/')) {
175 path = basename(path); /* strips trailing / first if any */
176 if (path == NULL) {
177 err(1, "basename");
178 /* not reached */
183 * Test and extract the PFS id from the link.
184 * "@@%jx:%d" covers both "@@-1:%05d" format for master PFS
185 * and "@@0x%016jx:%05d" format for slave PFS.
187 if (sscanf(path, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
188 assert(pfs->pfs_id > 0);
189 return(0);
192 return(-1);
195 void
196 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
198 if (fd >= 0)
199 close(fd);
200 if (pfs->ondisk) {
201 free(pfs->ondisk);
202 pfs->ondisk = NULL;
206 static void
207 print_pfs_status(char *path)
209 struct hammer_ioc_pseudofs_rw pfs;
210 int fd;
212 fd = getpfs(&pfs, path);
213 printf("%s\t", path);
214 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
215 printf("Invalid PFS path %s\n", path);
216 } else {
217 printf("PFS#%d {\n", pfs.pfs_id);
218 dump_pfsd(pfs.ondisk, fd);
219 printf("}\n");
221 if (fd >= 0)
222 close(fd);
223 if (pfs.ondisk)
224 free(pfs.ondisk);
225 relpfs(fd, &pfs);
228 void
229 hammer_cmd_pseudofs_status(char **av, int ac)
231 int i;
233 if (ac == 0) {
234 char buf[2] = "."; /* can't be readonly string */
235 print_pfs_status(buf);
236 return;
239 for (i = 0; i < ac; ++i)
240 print_pfs_status(av[i]);
243 void
244 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
246 struct hammer_ioc_pseudofs_rw pfs;
247 struct hammer_pseudofs_data pfsd;
248 struct stat st;
249 const char *path;
250 char *dirpath;
251 char *linkpath;
252 int pfs_id;
253 int fd;
255 if (ac == 0) {
256 pseudofs_usage(1);
257 /* not reached */
259 path = av[0];
260 if (lstat(path, &st) == 0) {
261 errx(1, "Cannot create %s, file exists!", path);
262 /* not reached */
263 } else if (path[strlen(path) - 1] == '/') {
264 errx(1, "Invalid PFS path %s with trailing /", path);
265 /* not reached */
269 * Figure out the directory prefix, taking care of degenerate
270 * cases.
272 dirpath = dirname(path);
273 fd = open(dirpath, O_RDONLY);
274 if (fd < 0) {
275 err(1, "Cannot open directory %s", dirpath);
276 /* not reached */
280 * Avoid foot-shooting. Don't let the user create a PFS
281 * softlink via a PFS. PFS softlinks may only be accessed
282 * via the master filesystem. Checking it here ensures
283 * other PFS commands access PFS under the master filesystem.
285 clrpfs(&pfs, &pfsd, -1);
287 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs);
288 if (pfs.pfs_id != HAMMER_ROOT_PFSID) {
289 fprintf(stderr,
290 "You are attempting to access a PFS softlink "
291 "from a PFS. It may not represent the PFS\n"
292 "on the main filesystem mount that you "
293 "expect! You may only access PFS softlinks\n"
294 "via the main filesystem mount!\n");
295 exit(1);
298 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
299 clrpfs(&pfs, &pfsd, pfs_id);
300 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
301 if (errno != ENOENT) {
302 err(1, "Cannot create %s", path);
303 /* not reached */
305 break;
308 if (pfs_id == HAMMER_MAX_PFS) {
309 errx(1, "Cannot create %s, all PFSs in use", path);
310 /* not reached */
311 } else if (pfs_id == HAMMER_ROOT_PFSID) {
312 errx(1, "Fatal error: PFS#%d must exist", HAMMER_ROOT_PFSID);
313 /* not reached */
317 * Create the new PFS
319 printf("Creating PFS#%d\t", pfs_id);
320 clrpfs(&pfs, &pfsd, pfs_id);
321 init_pfsd(&pfsd, is_slave);
323 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
324 printf("failed: %s\n", strerror(errno));
325 } else {
326 /* special symlink, must be exactly 10 characters */
327 asprintf(&linkpath, "@@PFS%05d", pfs_id);
328 if (symlink(linkpath, path) < 0) {
329 printf("failed: cannot create symlink: %s\n",
330 strerror(errno));
331 } else {
332 printf("succeeded!\n");
333 hammer_cmd_pseudofs_update(av, ac);
336 free(dirpath);
337 close(fd);
340 void
341 hammer_cmd_pseudofs_destroy(char **av, int ac)
343 struct hammer_ioc_pseudofs_rw pfs;
344 char *linkpath;
345 int fd;
346 int i;
348 if (ac == 0) {
349 pseudofs_usage(1);
350 /* not reached */
352 fd = getpfs(&pfs, av[0]);
354 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
355 errx(1, "You cannot destroy PFS#%d", HAMMER_ROOT_PFSID);
356 /* not reached */
359 printf("You have requested that PFS#%d (%s) be destroyed\n",
360 pfs.pfs_id, pfs.ondisk->label);
361 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
362 printf("Do you really want to do this? [y/n] ");
363 fflush(stdout);
364 if (getyn() == 0) {
365 errx(1, "No action taken on PFS#%d", pfs.pfs_id);
366 /* not reached */
369 if (hammer_is_pfs_master(pfs.ondisk)) {
370 printf("This PFS is currently setup as a MASTER!\n");
371 printf("Are you absolutely sure you want to destroy it? [y/n] ");
372 fflush(stdout);
373 if (getyn() == 0) {
374 errx(1, "No action taken on PFS#%d", pfs.pfs_id);
375 /* not reached */
379 printf("Destroying PFS#%d (%s)", pfs.pfs_id, pfs.ondisk->label);
380 if (DebugOpt) {
381 printf("\n");
382 } else {
383 printf(" in");
384 for (i = 5; i; --i) {
385 printf(" %d", i);
386 fflush(stdout);
387 sleep(1);
389 printf(".. starting destruction pass\n");
393 * Remove the softlink on success.
395 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
396 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
397 linkpath = getlink(av[0]);
398 if (linkpath) {
399 if (remove(linkpath) < 0) {
400 err(1, "Unable to remove softlink %s", linkpath);
401 /* not reached */
403 free(linkpath);
405 } else {
406 printf("pfs-destroy of PFS#%d failed: %s\n",
407 pfs.pfs_id, strerror(errno));
409 relpfs(fd, &pfs);
412 void
413 hammer_cmd_pseudofs_upgrade(char **av, int ac)
415 struct hammer_ioc_pseudofs_rw pfs;
416 int fd;
418 if (ac == 0) {
419 pseudofs_usage(1);
420 /* not reached */
422 fd = getpfs(&pfs, av[0]);
424 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
425 errx(1, "You cannot upgrade PFS#%d"
426 " (It should already be a master)",
427 HAMMER_ROOT_PFSID);
428 /* not reached */
429 } else if (hammer_is_pfs_master(pfs.ondisk)) {
430 errx(1, "It is already a master");
431 /* not reached */
434 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
435 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
436 pfs.pfs_id, pfs.ondisk->label);
437 } else {
438 err(1, "pfs-upgrade of PFS#%d (%s) failed",
439 pfs.pfs_id, pfs.ondisk->label);
440 /* not reached */
442 relpfs(fd, &pfs);
445 void
446 hammer_cmd_pseudofs_downgrade(char **av, int ac)
448 struct hammer_ioc_pseudofs_rw pfs;
449 int fd;
451 if (ac == 0) {
452 pseudofs_usage(1);
453 /* not reached */
455 fd = getpfs(&pfs, av[0]);
457 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
458 errx(1, "You cannot downgrade PFS#%d", HAMMER_ROOT_PFSID);
459 /* not reached */
460 } else if (hammer_is_pfs_slave(pfs.ondisk)) {
461 errx(1, "It is already a slave");
462 /* not reached */
465 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
466 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
467 pfs.pfs_id, pfs.ondisk->label);
468 } else {
469 err(1, "pfs-downgrade of PFS#%d (%s) failed",
470 pfs.pfs_id, pfs.ondisk->label);
471 /* not reached */
473 relpfs(fd, &pfs);
476 void
477 hammer_cmd_pseudofs_update(char **av, int ac)
479 struct hammer_ioc_pseudofs_rw pfs;
480 int fd;
482 if (ac == 0) {
483 pseudofs_usage(1);
484 /* not reached */
486 fd = getpfs(&pfs, av[0]);
488 printf("%s\n", av[0]);
489 fflush(stdout);
491 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
492 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
493 if (hammer_is_pfs_slave(pfs.ondisk) &&
494 pfs.pfs_id == HAMMER_ROOT_PFSID) {
495 errx(1, "The real mount point cannot be made a PFS "
496 "slave, only PFS sub-directories can be made "
497 "slaves");
498 /* not reached */
500 pfs.bytes = sizeof(*pfs.ondisk);
501 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
502 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
503 dump_pfsd(pfs.ondisk, fd);
504 } else {
505 err(1, "Unable to retrieve PFS configuration "
506 "after successful update");
507 /* not reached */
509 } else {
510 err(1, "Unable to adjust PFS configuration");
511 /* not reached */
514 relpfs(fd, &pfs);
517 static void
518 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
520 uint32_t status;
522 bzero(pfsd, sizeof(*pfsd));
523 pfsd->sync_beg_tid = 1;
524 pfsd->sync_end_tid = 1;
525 pfsd->sync_beg_ts = 0;
526 pfsd->sync_end_ts = 0;
527 uuid_create(&pfsd->shared_uuid, &status);
528 uuid_create(&pfsd->unique_uuid, &status);
529 if (is_slave)
530 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
533 void
534 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
536 struct hammer_ioc_version version;
537 uint32_t status;
538 char *str = NULL;
540 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
541 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
542 uuid_to_string(&pfsd->shared_uuid, &str, &status);
543 printf(" shared-uuid=%s\n", str);
544 free(str);
545 uuid_to_string(&pfsd->unique_uuid, &str, &status);
546 printf(" unique-uuid=%s\n", str);
547 free(str);
548 printf(" label=\"%s\"\n", pfsd->label);
549 if (pfsd->snapshots[0])
550 printf(" snapshots=\"%s\"\n", pfsd->snapshots);
551 if (pfsd->prune_min < (60 * 60 * 24)) {
552 printf(" prune-min=%02d:%02d:%02d\n",
553 pfsd->prune_min / 60 / 60 % 24,
554 pfsd->prune_min / 60 % 60,
555 pfsd->prune_min % 60);
556 } else if (pfsd->prune_min % (60 * 60 * 24)) {
557 printf(" prune-min=%dd/%02d:%02d:%02d\n",
558 pfsd->prune_min / 60 / 60 / 24,
559 pfsd->prune_min / 60 / 60 % 24,
560 pfsd->prune_min / 60 % 60,
561 pfsd->prune_min % 60);
562 } else {
563 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
566 if (hammer_is_pfs_slave(pfsd))
567 printf(" operating as a SLAVE\n");
568 else
569 printf(" operating as a MASTER\n");
572 * Snapshots directory cannot be shown when there is no fd since
573 * hammer version can't be retrieved. mirror-dump passes -1 because
574 * its input came from mirror-read output thus no path is available
575 * to open(2).
577 if (fd >= 0 && pfsd->snapshots[0] == 0) {
578 bzero(&version, sizeof(version));
579 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
580 return;
581 HammerVersion = version.cur_version;
582 if (version.cur_version < 3) {
583 if (hammer_is_pfs_slave(pfsd)) {
584 printf(" snapshots directory not set for "
585 "slave\n");
586 } else {
587 printf(" snapshots directory for master "
588 "defaults to <pfs>/snapshots\n");
590 } else {
591 printf(" snapshots directory defaults to "
592 "/var/hammer/<pfs>\n");
597 static void
598 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
600 char *cmd;
601 char *ptr;
602 int len;
603 uint32_t status;
605 while (ac) {
606 cmd = *av;
607 if ((ptr = strchr(cmd, '=')) != NULL)
608 *ptr++ = 0;
611 * Basic assignment value test
613 if (ptr == NULL) {
614 errx(1, "option %s requires an assignment", cmd);
615 /* not reached */
618 status = uuid_s_ok;
619 if (strcmp(cmd, "sync-beg-tid") == 0) {
620 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
621 } else if (strcmp(cmd, "sync-end-tid") == 0) {
622 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
623 } else if (strcmp(cmd, "shared-uuid") == 0) {
624 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
625 } else if (strcmp(cmd, "unique-uuid") == 0) {
626 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
627 } else if (strcmp(cmd, "label") == 0) {
628 len = strlen(ptr);
629 if (ptr[0] == '"' && ptr[len-1] == '"') {
630 ptr[len-1] = 0;
631 ++ptr;
632 } else if (ptr[0] == '"') {
633 errx(1, "option %s: malformed string", cmd);
634 /* not reached */
636 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
637 } else if (strcmp(cmd, "snapshots") == 0) {
638 len = strlen(ptr);
639 if (ptr[0] != '/') {
640 fprintf(stderr,
641 "option %s: '%s' must be an "
642 "absolute path\n", cmd, ptr);
643 if (ptr[0] == 0) {
644 fprintf(stderr,
645 "use 'snapshots-clear' "
646 "to unset snapshots dir\n");
648 exit(1);
650 if (len >= (int)sizeof(pfsd->snapshots)) {
651 errx(1, "option %s: path too long, %d "
652 "character limit", cmd, len);
653 /* not reached */
655 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
656 "%s", ptr);
657 } else if (strcmp(cmd, "snapshots-clear") == 0) {
658 pfsd->snapshots[0] = 0;
659 } else if (strcmp(cmd, "prune-min") == 0) {
660 pfsd->prune_min = timetosecs(ptr);
661 if (pfsd->prune_min < 0) {
662 errx(1, "option %s: illegal time spec, "
663 "use Nd or [Nd/]hh[:mm[:ss]]", ptr);
664 /* not reached */
666 } else {
667 errx(1, "invalid option: %s", cmd);
668 /* not reached */
670 if (status != uuid_s_ok) {
671 errx(1, "option %s: error parsing uuid %s", cmd, ptr);
672 /* not reached */
674 --ac;
675 ++av;
679 static
680 void
681 pseudofs_usage(int code)
683 fprintf(stderr,
684 "hammer pfs-status <dirpath> ...\n"
685 "hammer pfs-master <dirpath> [options]\n"
686 "hammer pfs-slave <dirpath> [options]\n"
687 "hammer pfs-update <dirpath> [options]\n"
688 "hammer pfs-upgrade <dirpath>\n"
689 "hammer pfs-downgrade <dirpath>\n"
690 "hammer pfs-destroy <dirpath>\n"
691 "\n"
692 "options:\n"
693 " sync-beg-tid=0x16llx\n"
694 " sync-end-tid=0x16llx\n"
695 " shared-uuid=0x16llx\n"
696 " unique-uuid=0x16llx\n"
697 " label=\"string\"\n"
698 " snapshots=\"/path\"\n"
699 " snapshots-clear\n"
700 " prune-min=Nd\n"
701 " prune-min=[Nd/]hh[:mm[:ss]]\n"
703 exit(code);
707 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
709 * Return -1 if a parse error occurs.
710 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
712 static
714 timetosecs(char *str)
716 int days = 0;
717 int hrs = 0;
718 int mins = 0;
719 int secs = 0;
720 int n;
721 long long v;
722 char *ptr;
724 n = strtol(str, &ptr, 10);
725 if (n < 0)
726 return(-1);
727 if (*ptr == 'd') {
728 days = n;
729 ++ptr;
730 if (*ptr == '/')
731 n = strtol(ptr + 1, &ptr, 10);
732 else
733 n = 0;
735 if (n < 0)
736 return(-1);
737 hrs = n;
738 if (*ptr == ':') {
739 n = strtol(ptr + 1, &ptr, 10);
740 if (n < 0)
741 return(-1);
742 mins = n;
743 if (*ptr == ':') {
744 n = strtol(ptr + 1, &ptr, 10);
745 if (n < 0)
746 return(-1);
747 secs = n;
750 if (*ptr)
751 return(-1);
752 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;
753 if (v > 0x7FFFFFFF)
754 v = 0x7FFFFFFF;
755 return((int)v);