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>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ffs/fs.h>
53 static void usage(void) __dead2;
55 static int compare_blk32(uint32_t *wantedblk, uint32_t curblk);
56 static int compare_blk64(uint64_t *wantedblk, uint64_t curblk);
57 static int founddatablk(uint64_t blk);
58 static int find_blks32(uint32_t *buf, int size, uint32_t *blknum);
59 static int find_blks64(uint64_t *buf, int size, uint64_t *blknum);
60 static int find_indirblks32(uint32_t blk, int ind_level, uint32_t *blknum);
61 static int find_indirblks64(uint64_t blk, int ind_level, uint64_t *blknum);
66 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
74 * We suck in lots of fsck code, and just pick & choose the stuff we want.
76 * fsreadfd is set up to read from the file system, fswritefd to write to
80 main(int argc, char *argv[])
85 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
88 /* The -f option is left for historical
89 * reasons and has no meaning.
96 nflag++; /* "no" in fsck, readonly for us */
111 errx(1, "cannot set up file system `%s'", fsys);
112 printf("%s file system `%s'\nLast Mounted on %s\n",
113 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
116 sblock.fs_clean = 0; /* mark it dirty */
119 printf("*** FILE SYSTEM MARKED DIRTY\n");
120 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
121 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
126 #define CMDFUNC(func) int func(int argc, char *argv[])
127 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
130 CMDFUNC(focus); /* focus on inode */
131 CMDFUNC(active); /* print active inode */
132 CMDFUNC(blocks); /* print blocks for active inode */
133 CMDFUNC(focusname); /* focus by name */
134 CMDFUNC(zapi); /* clear inode */
135 CMDFUNC(uplink); /* incr link */
136 CMDFUNC(downlink); /* decr link */
137 CMDFUNC(linkcount); /* set link count */
138 CMDFUNC(quit); /* quit */
139 CMDFUNC(findblk); /* find block */
140 CMDFUNC(ls); /* list directory */
141 CMDFUNC(rm); /* remove name */
142 CMDFUNC(ln); /* add name */
143 CMDFUNC(newtype); /* change type */
144 CMDFUNC(chmode); /* change mode */
145 CMDFUNC(chlen); /* change length */
146 CMDFUNC(chaflags); /* change flags */
147 CMDFUNC(chgen); /* change generation */
148 CMDFUNC(chowner); /* change owner */
149 CMDFUNC(chgroup); /* Change group */
150 CMDFUNC(back); /* pop back to last ino */
151 CMDFUNC(chbtime); /* Change btime */
152 CMDFUNC(chmtime); /* Change mtime */
153 CMDFUNC(chctime); /* Change ctime */
154 CMDFUNC(chatime); /* Change atime */
155 CMDFUNC(chinum); /* Change inode # of dirent */
156 CMDFUNC(chname); /* Change dirname of dirent */
158 struct cmdtable cmds[] = {
159 { "help", "Print out help", 1, 1, FL_RO, helpfn },
160 { "?", "Print out help", 1, 1, FL_RO, helpfn },
161 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
162 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
163 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
164 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
165 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
166 { "active", "Print active inode", 1, 1, FL_RO, active },
167 { "print", "Print active inode", 1, 1, FL_RO, active },
168 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
169 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
170 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
171 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
172 { "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
173 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
174 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
175 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
176 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR | FL_ST, ln },
177 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
178 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
179 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
180 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
181 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
182 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
183 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
184 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
185 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
186 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
187 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
188 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
189 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
190 { "quit", "Exit", 1, 1, FL_RO, quit },
191 { "q", "Exit", 1, 1, FL_RO, quit },
192 { "exit", "Exit", 1, 1, FL_RO, quit },
193 { NULL, 0, 0, 0, 0, NULL },
197 helpfn(int argc, char *argv[])
199 struct cmdtable *cmdtp;
201 printf("Commands are:\n%-10s %5s %5s %s\n",
202 "command", "min args", "max args", "what");
204 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
205 printf("%-10s %5u %5u %s\n",
206 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
213 static char pstring[64];
214 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
224 int cmd_argc, rval = 0, known;
225 #define scratch known
227 struct cmdtable *cmdp;
232 curinode = ginode(ROOTINO);
236 hist = history_init();
237 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
239 elptr = el_init("fsdb", stdin, stdout, stderr);
240 el_set(elptr, EL_EDITOR, "emacs");
241 el_set(elptr, EL_PROMPT, prompt);
242 el_set(elptr, EL_HIST, history, hist);
243 el_source(elptr, NULL);
245 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
247 printf("command `%s'\n", elline);
249 history(hist, &he, H_ENTER, elline);
251 line = strdup(elline);
252 cmd_argv = crack(line, &cmd_argc);
254 * el_parse returns -1 to signal that it's not been handled
257 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
261 for (cmdp = cmds; cmdp->cmd; cmdp++) {
262 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
263 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
264 warnx("`%s' requires write access", cmd_argv[0]),
266 else if (cmd_argc >= cmdp->minargc &&
267 cmd_argc <= cmdp->maxargc)
268 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
269 else if (cmd_argc >= cmdp->minargc &&
270 (cmdp->flags & FL_ST) == FL_ST) {
271 strcpy(line, elline);
272 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
273 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
275 rval = argcount(cmdp, cmd_argc, cmd_argv);
281 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
286 /* user typed "quit" */
289 warnx("rval was %d", rval);
296 union dinode *curinode;
297 ino_t curinum, ocurrent;
299 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
300 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
301 printf("inode %d out of range; range is [%d,%d]\n", \
302 inum, ROOTINO, maxino); \
307 * Focus on given inode number
315 curinode = ginode(inum);
325 curinode = ginode(curinum);
340 if (curinode) /* re-set after potential change */
341 curinode = ginode(curinum);
366 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
367 printf("inode %d link count now %d\n", curinum, DIP(curinode, di_nlink));
372 CMDFUNCSTART(downlink)
376 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
377 printf("inode %d link count now %d\n", curinum, DIP(curinode, di_nlink));
382 const char *typename[] = {
404 scannames(struct inodesc *idesc)
406 struct direct *dirp = idesc->id_dirp;
408 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
409 slot++, diroff, dirp->d_ino, dirp->d_reclen,
410 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
411 diroff += dirp->d_reclen;
417 struct inodesc idesc;
418 checkactivedir(); /* let it go on anyway */
422 idesc.id_number = curinum;
423 idesc.id_func = scannames;
424 idesc.id_type = DATA;
425 idesc.id_fix = IGNORE;
426 ckinode(curinode, &idesc);
427 curinode = ginode(curinum);
432 static int findblk_numtofind;
433 static int wantedblksize;
435 CMDFUNCSTART(findblk)
437 ino_t inum, inosused;
438 uint32_t *wantedblk32;
439 uint64_t *wantedblk64;
440 struct cg *cgp = &cgrp;
443 wantedblksize = (argc - 1);
444 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
448 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
449 if (wantedblk64 == NULL)
451 for (i = 1; i < argc; i++)
452 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
454 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
455 if (wantedblk32 == NULL)
457 for (i = 1; i < argc; i++)
458 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
460 findblk_numtofind = wantedblksize;
462 * sblock.fs_ncg holds a number of cylinder groups.
463 * Iterate over all cylinder groups.
465 for (c = 0; c < sblock.fs_ncg; c++) {
467 * sblock.fs_ipg holds a number of inodes per cylinder group.
468 * Calculate a highest inode number for a given cylinder group.
470 inum = c * sblock.fs_ipg;
471 /* Read cylinder group. */
472 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
473 memcpy(cgp, cgblk.b_un.b_cg, sblock.fs_cgsize);
475 * Get a highest used inode number for a given cylinder group.
476 * For UFS1 all inodes initialized at the newfs stage.
479 inosused = cgp->cg_initediblk;
481 inosused = sblock.fs_ipg;
483 for (; inosused > 0; inum++, inosused--) {
484 /* Skip magic inodes: 0, WINO, ROOTINO. */
488 * Check if the block we are looking for is just an inode block.
490 * ino_to_fsba() - get block containing inode from its number.
491 * INOPB() - get a number of inodes in one disk block.
494 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
495 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
496 printf("block %llu: inode block (%d-%d)\n",
497 (unsigned long long)fsbtodb(&sblock,
498 ino_to_fsba(&sblock, inum)),
499 (inum / INOPB(&sblock)) * INOPB(&sblock),
500 (inum / INOPB(&sblock) + 1) * INOPB(&sblock));
502 if (findblk_numtofind == 0)
505 /* Get on-disk inode aka dinode. */
507 curinode = ginode(inum);
508 /* Find IFLNK dinode with allocated data blocks. */
509 switch (DIP(curinode, di_mode) & IFMT) {
512 if (DIP(curinode, di_blocks) == 0)
517 uint64_t size = DIP(curinode, di_size);
518 if (size > 0 && size < sblock.fs_maxsymlinklen &&
519 DIP(curinode, di_blocks) == 0)
527 /* Look through direct data blocks. */
529 find_blks64(curinode->dp2.di_db, NDADDR, wantedblk64) :
530 find_blks32(curinode->dp1.di_db, NDADDR, wantedblk32))
532 for (i = 0; i < NIADDR; i++) {
534 * Does the block we are looking for belongs to the
538 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
539 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
540 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
541 curinode->dp1.di_ib[i]))
544 * Search through indirect, double and triple indirect
547 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
548 (curinode->dp1.di_ib[i] != 0))
550 find_indirblks64(curinode->dp2.di_ib[i], i,
552 find_indirblks32(curinode->dp1.di_ib[i], i,
560 curinode = ginode(curinum);
565 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
569 for (i = 0; i < wantedblksize; i++) {
570 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
579 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
583 for (i = 0; i < wantedblksize; i++) {
584 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
593 founddatablk(uint64_t blk)
596 printf("%llu: data block of inode %d\n",
597 (unsigned long long)fsbtodb(&sblock, blk), curinum);
599 if (findblk_numtofind == 0)
605 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
608 for (blk = 0; blk < size; blk++) {
611 if (compare_blk32(wantedblk, buf[blk])) {
612 if (founddatablk(buf[blk]))
620 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
622 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
623 uint32_t idblk[MAXNINDIR];
626 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
627 if (ind_level <= 0) {
628 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
632 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
633 if (compare_blk32(wantedblk, idblk[i])) {
634 if (founddatablk(idblk[i]))
638 if (find_indirblks32(idblk[i], ind_level, wantedblk))
647 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
650 for (blk = 0; blk < size; blk++) {
653 if (compare_blk64(wantedblk, buf[blk])) {
654 if (founddatablk(buf[blk]))
662 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
664 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
665 uint64_t idblk[MAXNINDIR];
668 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
669 if (ind_level <= 0) {
670 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
674 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
675 if (compare_blk64(wantedblk, idblk[i])) {
676 if (founddatablk(idblk[i]))
680 if (find_indirblks64(idblk[i], ind_level, wantedblk))
688 int findino(struct inodesc *idesc); /* from fsck */
689 static int dolookup(char *name);
694 struct inodesc idesc;
696 if (!checkactivedir())
698 idesc.id_number = curinum;
699 idesc.id_func = findino;
700 idesc.id_name = name;
701 idesc.id_type = DATA;
702 idesc.id_fix = IGNORE;
703 if (ckinode(curinode, &idesc) & FOUND) {
704 curinum = idesc.id_parent;
705 curinode = ginode(curinum);
709 warnx("name `%s' not found in current inode directory", name);
714 CMDFUNCSTART(focusname)
723 if (argv[1][0] == '/') {
725 curinode = ginode(ROOTINO);
727 if (!checkactivedir())
730 for (p = argv[1]; p != NULL;) {
731 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
733 printf("component `%s': ", val);
735 if (!dolookup(val)) {
736 curinode = ginode(curinum);
752 if (!checkactivedir())
754 rval = makeentry(curinum, inum, argv[2]);
756 printf("Ino %d entered as `%s'\n", inum, argv[2]);
758 printf("could not enter name? weird.\n");
759 curinode = ginode(curinum);
767 if (!checkactivedir())
769 rval = changeino(curinum, argv[1], 0);
770 if (rval & ALTERED) {
771 printf("Name `%s' removed\n", argv[1]);
774 printf("could not remove name ('%s')? weird.\n", argv[1]);
779 long slotcount, desired;
782 chinumfunc(struct inodesc *idesc)
784 struct direct *dirp = idesc->id_dirp;
786 if (slotcount++ == desired) {
787 dirp->d_ino = idesc->id_parent;
788 return STOP|ALTERED|FOUND;
797 struct inodesc idesc;
800 if (!checkactivedir())
804 desired = strtol(argv[1], &cp, 0);
805 if (cp == argv[1] || *cp != '\0' || desired < 0) {
806 printf("invalid slot number `%s'\n", argv[1]);
810 idesc.id_number = curinum;
811 idesc.id_func = chinumfunc;
812 idesc.id_fix = IGNORE;
813 idesc.id_type = DATA;
814 idesc.id_parent = inum; /* XXX convenient hiding place */
816 if (ckinode(curinode, &idesc) & FOUND)
819 warnx("no %sth slot in current directory", argv[1]);
825 chnamefunc(struct inodesc *idesc)
827 struct direct *dirp = idesc->id_dirp;
828 struct direct testdir;
830 if (slotcount++ == desired) {
832 testdir.d_namlen = strlen(idesc->id_name);
833 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
834 dirp->d_namlen = testdir.d_namlen;
835 strcpy(dirp->d_name, idesc->id_name);
836 return STOP|ALTERED|FOUND;
838 return STOP|FOUND; /* won't fit, so give up */
847 struct inodesc idesc;
850 if (!checkactivedir())
853 desired = strtoul(argv[1], &cp, 0);
854 if (cp == argv[1] || *cp != '\0') {
855 printf("invalid slot number `%s'\n", argv[1]);
859 idesc.id_number = curinum;
860 idesc.id_func = chnamefunc;
861 idesc.id_fix = IGNORE;
862 idesc.id_type = DATA;
863 idesc.id_name = argv[2];
865 rval = ckinode(curinode, &idesc);
866 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
868 else if (rval & FOUND) {
869 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
872 warnx("no %sth slot in current directory", argv[1]);
878 const char *typename;
887 CMDFUNCSTART(newtype)
894 type = DIP(curinode, di_mode) & IFMT;
895 for (tp = typenamemap;
896 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
898 if (!strcmp(argv[1], tp->typename)) {
899 printf("setting type to %s\n", tp->typename);
904 if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
905 warnx("type `%s' not known", argv[1]);
906 warnx("try one of `file', `dir', `socket', `fifo'");
909 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
910 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
925 len = strtol(argv[1], &cp, 0);
926 if (cp == argv[1] || *cp != '\0' || len < 0) {
927 warnx("bad length `%s'", argv[1]);
931 DIP_SET(curinode, di_size, len);
946 modebits = strtol(argv[1], &cp, 8);
947 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
948 warnx("bad modebits `%s'", argv[1]);
952 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
953 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
959 CMDFUNCSTART(chaflags)
968 flags = strtoul(argv[1], &cp, 0);
969 if (cp == argv[1] || *cp != '\0' ) {
970 warnx("bad flags `%s'", argv[1]);
974 if (flags > UINT_MAX) {
975 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
978 DIP_SET(curinode, di_flags, flags);
993 gen = strtol(argv[1], &cp, 0);
994 if (cp == argv[1] || *cp != '\0' ) {
995 warnx("bad gen `%s'", argv[1]);
999 if (gen > INT_MAX || gen < INT_MIN) {
1000 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1003 DIP_SET(curinode, di_gen, gen);
1009 CMDFUNCSTART(linkcount)
1018 lcnt = strtol(argv[1], &cp, 0);
1019 if (cp == argv[1] || *cp != '\0' ) {
1020 warnx("bad link count `%s'", argv[1]);
1023 if (lcnt > USHRT_MAX || lcnt < 0) {
1024 warnx("max link count is %d\n", USHRT_MAX);
1028 DIP_SET(curinode, di_nlink, lcnt);
1034 CMDFUNCSTART(chowner)
1044 uid = strtoul(argv[1], &cp, 0);
1045 if (cp == argv[1] || *cp != '\0' ) {
1046 /* try looking up name */
1047 if ((pwd = getpwnam(argv[1]))) {
1050 warnx("bad uid `%s'", argv[1]);
1055 DIP_SET(curinode, di_uid, uid);
1061 CMDFUNCSTART(chgroup)
1071 gid = strtoul(argv[1], &cp, 0);
1072 if (cp == argv[1] || *cp != '\0' ) {
1073 if ((grp = getgrnam(argv[1]))) {
1076 warnx("bad gid `%s'", argv[1]);
1081 DIP_SET(curinode, di_gid, gid);
1088 dotime(char *name, time_t *secp, int32_t *nsecp)
1093 p = strchr(name, '.');
1096 nsec = strtoul(++p, &val, 0);
1097 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1098 warnx("invalid nanoseconds");
1103 if (strlen(name) != 14) {
1105 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1110 for (p = name; *p; p++)
1111 if (*p < '0' || *p > '9')
1115 #define VAL() ((*p++) - '0')
1117 t.tm_year = VAL() + t.tm_year * 10;
1118 t.tm_year = VAL() + t.tm_year * 10;
1119 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1121 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1123 t.tm_mday = VAL() + t.tm_mday * 10;
1125 t.tm_hour = VAL() + t.tm_hour * 10;
1127 t.tm_min = VAL() + t.tm_min * 10;
1129 t.tm_sec = VAL() + t.tm_sec * 10;
1134 warnx("date/time out of range");
1140 CMDFUNCSTART(chbtime)
1145 if (dotime(argv[1], &secs, &nsecs))
1147 if (sblock.fs_magic == FS_UFS1_MAGIC)
1149 curinode->dp2.di_birthtime = _time_to_time64(secs);
1150 curinode->dp2.di_birthnsec = nsecs;
1156 CMDFUNCSTART(chmtime)
1161 if (dotime(argv[1], &secs, &nsecs))
1163 if (sblock.fs_magic == FS_UFS1_MAGIC)
1164 curinode->dp1.di_mtime = _time_to_time32(secs);
1166 curinode->dp2.di_mtime = _time_to_time64(secs);
1167 DIP_SET(curinode, di_mtimensec, nsecs);
1173 CMDFUNCSTART(chatime)
1178 if (dotime(argv[1], &secs, &nsecs))
1180 if (sblock.fs_magic == FS_UFS1_MAGIC)
1181 curinode->dp1.di_atime = _time_to_time32(secs);
1183 curinode->dp2.di_atime = _time_to_time64(secs);
1184 DIP_SET(curinode, di_atimensec, nsecs);
1190 CMDFUNCSTART(chctime)
1195 if (dotime(argv[1], &secs, &nsecs))
1197 if (sblock.fs_magic == FS_UFS1_MAGIC)
1198 curinode->dp1.di_ctime = _time_to_time32(secs);
1200 curinode->dp2.di_ctime = _time_to_time64(secs);
1201 DIP_SET(curinode, di_ctimensec, nsecs);