1 /* $NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 thorpej Exp $ */
4 * Copyright (c) 1995 John T. Kohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 static const char rcsid[] =
36 #include <sys/param.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ufs/dir.h>
49 #include <ufs/ffs/fs.h>
54 static void usage(void) __dead2;
56 static int compare_blk32(uint32_t *wantedblk, uint32_t curblk);
57 static int compare_blk64(uint64_t *wantedblk, uint64_t curblk);
58 static int founddatablk(uint64_t blk);
59 static int find_blks32(uint32_t *buf, int size, uint32_t *blknum);
60 static int find_blks64(uint64_t *buf, int size, uint64_t *blknum);
61 static int find_indirblks32(uint32_t blk, int ind_level, uint32_t *blknum);
62 static int find_indirblks64(uint64_t blk, int ind_level, uint64_t *blknum);
67 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
75 * We suck in lots of fsck code, and just pick & choose the stuff we want.
77 * fsreadfd is set up to read from the file system, fswritefd to write to
81 main(int argc, char *argv[])
86 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
89 /* The -f option is left for historical
90 * reasons and has no meaning.
97 nflag++; /* "no" in fsck, readonly for us */
112 errx(1, "cannot set up file system `%s'", fsys);
113 printf("%s file system `%s'\nLast Mounted on %s\n",
114 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
117 sblock.fs_clean = 0; /* mark it dirty */
120 printf("*** FILE SYSTEM MARKED DIRTY\n");
121 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
122 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
127 #define CMDFUNC(func) int func(int argc, char *argv[])
128 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
131 CMDFUNC(focus); /* focus on inode */
132 CMDFUNC(active); /* print active inode */
133 CMDFUNC(blocks); /* print blocks for active inode */
134 CMDFUNC(focusname); /* focus by name */
135 CMDFUNC(zapi); /* clear inode */
136 CMDFUNC(uplink); /* incr link */
137 CMDFUNC(downlink); /* decr link */
138 CMDFUNC(linkcount); /* set link count */
139 CMDFUNC(quit); /* quit */
140 CMDFUNC(findblk); /* find block */
141 CMDFUNC(ls); /* list directory */
142 CMDFUNC(rm); /* remove name */
143 CMDFUNC(ln); /* add name */
144 CMDFUNC(newtype); /* change type */
145 CMDFUNC(chmode); /* change mode */
146 CMDFUNC(chlen); /* change length */
147 CMDFUNC(chaflags); /* change flags */
148 CMDFUNC(chgen); /* change generation */
149 CMDFUNC(chowner); /* change owner */
150 CMDFUNC(chgroup); /* Change group */
151 CMDFUNC(back); /* pop back to last ino */
152 CMDFUNC(chbtime); /* Change btime */
153 CMDFUNC(chmtime); /* Change mtime */
154 CMDFUNC(chctime); /* Change ctime */
155 CMDFUNC(chatime); /* Change atime */
156 CMDFUNC(chinum); /* Change inode # of dirent */
157 CMDFUNC(chname); /* Change dirname of dirent */
159 struct cmdtable cmds[] = {
160 { "help", "Print out help", 1, 1, FL_RO, helpfn },
161 { "?", "Print out help", 1, 1, FL_RO, helpfn },
162 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
163 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
164 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
165 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
166 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
167 { "active", "Print active inode", 1, 1, FL_RO, active },
168 { "print", "Print active inode", 1, 1, FL_RO, active },
169 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
170 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
171 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
172 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
173 { "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
174 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
175 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
176 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
177 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR | FL_ST, ln },
178 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
179 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
180 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
181 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
182 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
183 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
184 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
185 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
186 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
187 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
188 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
189 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
190 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
191 { "quit", "Exit", 1, 1, FL_RO, quit },
192 { "q", "Exit", 1, 1, FL_RO, quit },
193 { "exit", "Exit", 1, 1, FL_RO, quit },
194 { NULL, 0, 0, 0, 0, NULL },
198 helpfn(int argc, char *argv[])
200 struct cmdtable *cmdtp;
202 printf("Commands are:\n%-10s %5s %5s %s\n",
203 "command", "min args", "max args", "what");
205 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
206 printf("%-10s %5u %5u %s\n",
207 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
214 static char pstring[64];
215 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
226 int cmd_argc, rval = 0, known;
227 #define scratch known
229 struct cmdtable *cmdp;
234 curinode = ginode(ROOTINO);
238 hist = history_init();
239 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
241 elptr = el_init("fsdb", stdin, stdout, stderr);
242 el_set(elptr, EL_EDITOR, "emacs");
243 el_set(elptr, EL_PROMPT, prompt);
244 el_set(elptr, EL_HIST, history, hist);
245 el_source(elptr, NULL);
247 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
249 printf("command `%s'\n", elline);
251 history(hist, &he, H_ENTER, elline);
253 line = strdup(elline);
254 cmd_argv = crack(line, &cmd_argc);
256 * el_parse returns -1 to signal that it's not been handled
259 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
263 for (cmdp = cmds; cmdp->cmd; cmdp++) {
264 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
265 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
266 warnx("`%s' requires write access", cmd_argv[0]),
268 else if (cmd_argc >= cmdp->minargc &&
269 cmd_argc <= cmdp->maxargc)
270 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
271 else if (cmd_argc >= cmdp->minargc &&
272 (cmdp->flags & FL_ST) == FL_ST) {
273 strcpy(line, elline);
274 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
275 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
277 rval = argcount(cmdp, cmd_argc, cmd_argv);
283 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
288 /* user typed "quit" */
291 warnx("rval was %d", rval);
298 union dinode *curinode;
299 ino_t curinum, ocurrent;
301 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
302 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
303 printf("inode %ju out of range; range is [%ju,%ju]\n", \
304 (uintmax_t)inum, (uintmax_t)ROOTINO, (uintmax_t)maxino); \
309 * Focus on given inode number
317 curinode = ginode(inum);
327 curinode = ginode(curinum);
342 if (curinode) /* re-set after potential change */
343 curinode = ginode(curinum);
368 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
369 printf("inode %ju link count now %d\n",
370 (uintmax_t)curinum, DIP(curinode, di_nlink));
375 CMDFUNCSTART(downlink)
379 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
380 printf("inode %ju link count now %d\n",
381 (uintmax_t)curinum, DIP(curinode, di_nlink));
386 const char *typename[] = {
408 scannames(struct inodesc *idesc)
410 struct direct *dirp = idesc->id_dirp;
412 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
413 slot++, diroff, dirp->d_ino, dirp->d_reclen,
414 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
415 diroff += dirp->d_reclen;
421 struct inodesc idesc;
422 checkactivedir(); /* let it go on anyway */
426 idesc.id_number = curinum;
427 idesc.id_func = scannames;
428 idesc.id_type = DATA;
429 idesc.id_fix = IGNORE;
430 ckinode(curinode, &idesc);
431 curinode = ginode(curinum);
436 static int findblk_numtofind;
437 static int wantedblksize;
439 CMDFUNCSTART(findblk)
441 ino_t inum, inosused;
442 uint32_t *wantedblk32;
443 uint64_t *wantedblk64;
444 struct bufarea *cgbp;
448 wantedblksize = (argc - 1);
449 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
453 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
454 if (wantedblk64 == NULL)
456 for (i = 1; i < argc; i++)
457 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
459 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
460 if (wantedblk32 == NULL)
462 for (i = 1; i < argc; i++)
463 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
465 findblk_numtofind = wantedblksize;
467 * sblock.fs_ncg holds a number of cylinder groups.
468 * Iterate over all cylinder groups.
470 for (c = 0; c < sblock.fs_ncg; c++) {
472 * sblock.fs_ipg holds a number of inodes per cylinder group.
473 * Calculate a highest inode number for a given cylinder group.
475 inum = c * sblock.fs_ipg;
476 /* Read cylinder group. */
478 cgp = cgbp->b_un.b_cg;
480 * Get a highest used inode number for a given cylinder group.
481 * For UFS1 all inodes initialized at the newfs stage.
484 inosused = cgp->cg_initediblk;
486 inosused = sblock.fs_ipg;
488 for (; inosused > 0; inum++, inosused--) {
489 /* Skip magic inodes: 0, WINO, ROOTINO. */
493 * Check if the block we are looking for is just an inode block.
495 * ino_to_fsba() - get block containing inode from its number.
496 * INOPB() - get a number of inodes in one disk block.
499 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
500 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
501 printf("block %llu: inode block (%ju-%ju)\n",
502 (unsigned long long)fsbtodb(&sblock,
503 ino_to_fsba(&sblock, inum)),
504 (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
505 (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
507 if (findblk_numtofind == 0)
510 /* Get on-disk inode aka dinode. */
512 curinode = ginode(inum);
513 /* Find IFLNK dinode with allocated data blocks. */
514 switch (DIP(curinode, di_mode) & IFMT) {
517 if (DIP(curinode, di_blocks) == 0)
522 uint64_t size = DIP(curinode, di_size);
523 if (size > 0 && size < sblock.fs_maxsymlinklen &&
524 DIP(curinode, di_blocks) == 0)
532 /* Look through direct data blocks. */
534 find_blks64(curinode->dp2.di_db, NDADDR, wantedblk64) :
535 find_blks32(curinode->dp1.di_db, NDADDR, wantedblk32))
537 for (i = 0; i < NIADDR; i++) {
539 * Does the block we are looking for belongs to the
543 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
544 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
545 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
546 curinode->dp1.di_ib[i]))
549 * Search through indirect, double and triple indirect
552 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
553 (curinode->dp1.di_ib[i] != 0))
555 find_indirblks64(curinode->dp2.di_ib[i], i,
557 find_indirblks32(curinode->dp1.di_ib[i], i,
565 curinode = ginode(curinum);
570 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
574 for (i = 0; i < wantedblksize; i++) {
575 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
584 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
588 for (i = 0; i < wantedblksize; i++) {
589 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
598 founddatablk(uint64_t blk)
601 printf("%llu: data block of inode %ju\n",
602 (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
604 if (findblk_numtofind == 0)
610 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
613 for (blk = 0; blk < size; blk++) {
616 if (compare_blk32(wantedblk, buf[blk])) {
617 if (founddatablk(buf[blk]))
625 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
627 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
628 uint32_t idblk[MAXNINDIR];
631 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
632 if (ind_level <= 0) {
633 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
637 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
638 if (compare_blk32(wantedblk, idblk[i])) {
639 if (founddatablk(idblk[i]))
643 if (find_indirblks32(idblk[i], ind_level, wantedblk))
652 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
655 for (blk = 0; blk < size; blk++) {
658 if (compare_blk64(wantedblk, buf[blk])) {
659 if (founddatablk(buf[blk]))
667 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
669 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
670 uint64_t idblk[MAXNINDIR];
673 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
674 if (ind_level <= 0) {
675 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
679 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
680 if (compare_blk64(wantedblk, idblk[i])) {
681 if (founddatablk(idblk[i]))
685 if (find_indirblks64(idblk[i], ind_level, wantedblk))
693 int findino(struct inodesc *idesc); /* from fsck */
694 static int dolookup(char *name);
699 struct inodesc idesc;
701 if (!checkactivedir())
703 idesc.id_number = curinum;
704 idesc.id_func = findino;
705 idesc.id_name = name;
706 idesc.id_type = DATA;
707 idesc.id_fix = IGNORE;
708 if (ckinode(curinode, &idesc) & FOUND) {
709 curinum = idesc.id_parent;
710 curinode = ginode(curinum);
714 warnx("name `%s' not found in current inode directory", name);
719 CMDFUNCSTART(focusname)
728 if (argv[1][0] == '/') {
730 curinode = ginode(ROOTINO);
732 if (!checkactivedir())
735 for (p = argv[1]; p != NULL;) {
736 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
738 printf("component `%s': ", val);
740 if (!dolookup(val)) {
741 curinode = ginode(curinum);
757 if (!checkactivedir())
759 rval = makeentry(curinum, inum, argv[2]);
761 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
763 printf("could not enter name? weird.\n");
764 curinode = ginode(curinum);
772 if (!checkactivedir())
774 rval = changeino(curinum, argv[1], 0);
775 if (rval & ALTERED) {
776 printf("Name `%s' removed\n", argv[1]);
779 printf("could not remove name ('%s')? weird.\n", argv[1]);
784 long slotcount, desired;
787 chinumfunc(struct inodesc *idesc)
789 struct direct *dirp = idesc->id_dirp;
791 if (slotcount++ == desired) {
792 dirp->d_ino = idesc->id_parent;
793 return STOP|ALTERED|FOUND;
802 struct inodesc idesc;
805 if (!checkactivedir())
809 desired = strtol(argv[1], &cp, 0);
810 if (cp == argv[1] || *cp != '\0' || desired < 0) {
811 printf("invalid slot number `%s'\n", argv[1]);
815 idesc.id_number = curinum;
816 idesc.id_func = chinumfunc;
817 idesc.id_fix = IGNORE;
818 idesc.id_type = DATA;
819 idesc.id_parent = inum; /* XXX convenient hiding place */
821 if (ckinode(curinode, &idesc) & FOUND)
824 warnx("no %sth slot in current directory", argv[1]);
830 chnamefunc(struct inodesc *idesc)
832 struct direct *dirp = idesc->id_dirp;
833 struct direct testdir;
835 if (slotcount++ == desired) {
837 testdir.d_namlen = strlen(idesc->id_name);
838 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
839 dirp->d_namlen = testdir.d_namlen;
840 strcpy(dirp->d_name, idesc->id_name);
841 return STOP|ALTERED|FOUND;
843 return STOP|FOUND; /* won't fit, so give up */
852 struct inodesc idesc;
855 if (!checkactivedir())
858 desired = strtoul(argv[1], &cp, 0);
859 if (cp == argv[1] || *cp != '\0') {
860 printf("invalid slot number `%s'\n", argv[1]);
864 idesc.id_number = curinum;
865 idesc.id_func = chnamefunc;
866 idesc.id_fix = IGNORE;
867 idesc.id_type = DATA;
868 idesc.id_name = argv[2];
870 rval = ckinode(curinode, &idesc);
871 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
873 else if (rval & FOUND) {
874 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
877 warnx("no %sth slot in current directory", argv[1]);
883 const char *typename;
892 CMDFUNCSTART(newtype)
899 type = DIP(curinode, di_mode) & IFMT;
900 for (tp = typenamemap;
901 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
903 if (!strcmp(argv[1], tp->typename)) {
904 printf("setting type to %s\n", tp->typename);
909 if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
910 warnx("type `%s' not known", argv[1]);
911 warnx("try one of `file', `dir', `socket', `fifo'");
914 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
915 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
930 len = strtol(argv[1], &cp, 0);
931 if (cp == argv[1] || *cp != '\0' || len < 0) {
932 warnx("bad length `%s'", argv[1]);
936 DIP_SET(curinode, di_size, len);
951 modebits = strtol(argv[1], &cp, 8);
952 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
953 warnx("bad modebits `%s'", argv[1]);
957 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
958 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
964 CMDFUNCSTART(chaflags)
973 flags = strtoul(argv[1], &cp, 0);
974 if (cp == argv[1] || *cp != '\0' ) {
975 warnx("bad flags `%s'", argv[1]);
979 if (flags > UINT_MAX) {
980 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
983 DIP_SET(curinode, di_flags, flags);
998 gen = strtol(argv[1], &cp, 0);
999 if (cp == argv[1] || *cp != '\0' ) {
1000 warnx("bad gen `%s'", argv[1]);
1004 if (gen > INT_MAX || gen < INT_MIN) {
1005 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1008 DIP_SET(curinode, di_gen, gen);
1014 CMDFUNCSTART(linkcount)
1023 lcnt = strtol(argv[1], &cp, 0);
1024 if (cp == argv[1] || *cp != '\0' ) {
1025 warnx("bad link count `%s'", argv[1]);
1028 if (lcnt > USHRT_MAX || lcnt < 0) {
1029 warnx("max link count is %d\n", USHRT_MAX);
1033 DIP_SET(curinode, di_nlink, lcnt);
1039 CMDFUNCSTART(chowner)
1049 uid = strtoul(argv[1], &cp, 0);
1050 if (cp == argv[1] || *cp != '\0' ) {
1051 /* try looking up name */
1052 if ((pwd = getpwnam(argv[1]))) {
1055 warnx("bad uid `%s'", argv[1]);
1060 DIP_SET(curinode, di_uid, uid);
1066 CMDFUNCSTART(chgroup)
1076 gid = strtoul(argv[1], &cp, 0);
1077 if (cp == argv[1] || *cp != '\0' ) {
1078 if ((grp = getgrnam(argv[1]))) {
1081 warnx("bad gid `%s'", argv[1]);
1086 DIP_SET(curinode, di_gid, gid);
1093 dotime(char *name, time_t *secp, int32_t *nsecp)
1098 p = strchr(name, '.');
1101 nsec = strtoul(++p, &val, 0);
1102 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1103 warnx("invalid nanoseconds");
1108 if (strlen(name) != 14) {
1110 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1115 for (p = name; *p; p++)
1116 if (*p < '0' || *p > '9')
1120 #define VAL() ((*p++) - '0')
1122 t.tm_year = VAL() + t.tm_year * 10;
1123 t.tm_year = VAL() + t.tm_year * 10;
1124 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1126 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1128 t.tm_mday = VAL() + t.tm_mday * 10;
1130 t.tm_hour = VAL() + t.tm_hour * 10;
1132 t.tm_min = VAL() + t.tm_min * 10;
1134 t.tm_sec = VAL() + t.tm_sec * 10;
1139 warnx("date/time out of range");
1145 CMDFUNCSTART(chbtime)
1150 if (dotime(argv[1], &secs, &nsecs))
1152 if (sblock.fs_magic == FS_UFS1_MAGIC)
1154 curinode->dp2.di_birthtime = _time_to_time64(secs);
1155 curinode->dp2.di_birthnsec = nsecs;
1161 CMDFUNCSTART(chmtime)
1166 if (dotime(argv[1], &secs, &nsecs))
1168 if (sblock.fs_magic == FS_UFS1_MAGIC)
1169 curinode->dp1.di_mtime = _time_to_time32(secs);
1171 curinode->dp2.di_mtime = _time_to_time64(secs);
1172 DIP_SET(curinode, di_mtimensec, nsecs);
1178 CMDFUNCSTART(chatime)
1183 if (dotime(argv[1], &secs, &nsecs))
1185 if (sblock.fs_magic == FS_UFS1_MAGIC)
1186 curinode->dp1.di_atime = _time_to_time32(secs);
1188 curinode->dp2.di_atime = _time_to_time64(secs);
1189 DIP_SET(curinode, di_atimensec, nsecs);
1195 CMDFUNCSTART(chctime)
1200 if (dotime(argv[1], &secs, &nsecs))
1202 if (sblock.fs_magic == FS_UFS1_MAGIC)
1203 curinode->dp1.di_ctime = _time_to_time32(secs);
1205 curinode->dp2.di_ctime = _time_to_time64(secs);
1206 DIP_SET(curinode, di_ctimensec, nsecs);