1 /* $NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 thorpej Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1995 John T. Kohl
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 static const char rcsid[] =
38 #include <sys/param.h>
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.h>
56 static void usage(void) __dead2;
58 static int compare_blk32(uint32_t *wantedblk, uint32_t curblk);
59 static int compare_blk64(uint64_t *wantedblk, uint64_t curblk);
60 static int founddatablk(uint64_t blk);
61 static int find_blks32(uint32_t *buf, int size, uint32_t *blknum);
62 static int find_blks64(uint64_t *buf, int size, uint64_t *blknum);
63 static int find_indirblks32(uint32_t blk, int ind_level, uint32_t *blknum);
64 static int find_indirblks64(uint64_t blk, int ind_level, uint64_t *blknum);
69 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
77 * We suck in lots of fsck code, and just pick & choose the stuff we want.
79 * fsreadfd is set up to read from the file system, fswritefd to write to
83 main(int argc, char *argv[])
88 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
91 /* The -f option is left for historical
92 * reasons and has no meaning.
99 nflag++; /* "no" in fsck, readonly for us */
114 errx(1, "cannot set up file system `%s'", fsys);
115 printf("%s file system `%s'\nLast Mounted on %s\n",
116 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
119 sblock.fs_clean = 0; /* mark it dirty */
122 printf("*** FILE SYSTEM MARKED DIRTY\n");
123 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
124 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
129 #define CMDFUNC(func) int func(int argc, char *argv[])
130 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
133 CMDFUNC(focus); /* focus on inode */
134 CMDFUNC(active); /* print active inode */
135 CMDFUNC(blocks); /* print blocks for active inode */
136 CMDFUNC(focusname); /* focus by name */
137 CMDFUNC(zapi); /* clear inode */
138 CMDFUNC(uplink); /* incr link */
139 CMDFUNC(downlink); /* decr link */
140 CMDFUNC(linkcount); /* set link count */
141 CMDFUNC(quit); /* quit */
142 CMDFUNC(findblk); /* find block */
143 CMDFUNC(ls); /* list directory */
144 CMDFUNC(rm); /* remove name */
145 CMDFUNC(ln); /* add name */
146 CMDFUNC(newtype); /* change type */
147 CMDFUNC(chmode); /* change mode */
148 CMDFUNC(chlen); /* change length */
149 CMDFUNC(chaflags); /* change flags */
150 CMDFUNC(chgen); /* change generation */
151 CMDFUNC(chowner); /* change owner */
152 CMDFUNC(chgroup); /* Change group */
153 CMDFUNC(back); /* pop back to last ino */
154 CMDFUNC(chbtime); /* Change btime */
155 CMDFUNC(chmtime); /* Change mtime */
156 CMDFUNC(chctime); /* Change ctime */
157 CMDFUNC(chatime); /* Change atime */
158 CMDFUNC(chinum); /* Change inode # of dirent */
159 CMDFUNC(chname); /* Change dirname of dirent */
161 struct cmdtable cmds[] = {
162 { "help", "Print out help", 1, 1, FL_RO, helpfn },
163 { "?", "Print out help", 1, 1, FL_RO, helpfn },
164 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
165 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
166 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
167 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
168 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
169 { "active", "Print active inode", 1, 1, FL_RO, active },
170 { "print", "Print active inode", 1, 1, FL_RO, active },
171 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
172 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
173 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
174 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
175 { "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
176 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
177 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
178 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
179 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR | FL_ST, ln },
180 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
181 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
182 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
183 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
184 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
185 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
186 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
187 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
188 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
189 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
190 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
191 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
192 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
193 { "quit", "Exit", 1, 1, FL_RO, quit },
194 { "q", "Exit", 1, 1, FL_RO, quit },
195 { "exit", "Exit", 1, 1, FL_RO, quit },
196 { NULL, 0, 0, 0, 0, NULL },
200 helpfn(int argc, char *argv[])
202 struct cmdtable *cmdtp;
204 printf("Commands are:\n%-10s %5s %5s %s\n",
205 "command", "min args", "max args", "what");
207 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
208 printf("%-10s %5u %5u %s\n",
209 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
216 static char pstring[64];
217 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
228 int cmd_argc, rval = 0, known;
229 #define scratch known
231 struct cmdtable *cmdp;
236 curinode = ginode(UFS_ROOTINO);
237 curinum = UFS_ROOTINO;
240 hist = history_init();
241 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
243 elptr = el_init("fsdb", stdin, stdout, stderr);
244 el_set(elptr, EL_EDITOR, "emacs");
245 el_set(elptr, EL_PROMPT, prompt);
246 el_set(elptr, EL_HIST, history, hist);
247 el_source(elptr, NULL);
249 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
251 printf("command `%s'\n", elline);
253 history(hist, &he, H_ENTER, elline);
255 line = strdup(elline);
256 cmd_argv = crack(line, &cmd_argc);
258 * el_parse returns -1 to signal that it's not been handled
261 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
265 for (cmdp = cmds; cmdp->cmd; cmdp++) {
266 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
267 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
268 warnx("`%s' requires write access", cmd_argv[0]),
270 else if (cmd_argc >= cmdp->minargc &&
271 cmd_argc <= cmdp->maxargc)
272 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
273 else if (cmd_argc >= cmdp->minargc &&
274 (cmdp->flags & FL_ST) == FL_ST) {
275 strcpy(line, elline);
276 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
277 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
279 rval = argcount(cmdp, cmd_argc, cmd_argv);
285 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
290 /* user typed "quit" */
293 warnx("rval was %d", rval);
300 union dinode *curinode;
301 ino_t curinum, ocurrent;
303 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
304 if (inum < UFS_ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
305 printf("inode %ju out of range; range is [%ju,%ju]\n", \
306 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino);\
311 * Focus on given inode number
319 curinode = ginode(inum);
329 curinode = ginode(curinum);
344 if (curinode) /* re-set after potential change */
345 curinode = ginode(curinum);
370 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
371 printf("inode %ju link count now %d\n",
372 (uintmax_t)curinum, DIP(curinode, di_nlink));
377 CMDFUNCSTART(downlink)
381 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
382 printf("inode %ju link count now %d\n",
383 (uintmax_t)curinum, DIP(curinode, di_nlink));
388 const char *typename[] = {
410 scannames(struct inodesc *idesc)
412 struct direct *dirp = idesc->id_dirp;
414 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
415 slot++, diroff, dirp->d_ino, dirp->d_reclen,
416 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
417 diroff += dirp->d_reclen;
423 struct inodesc idesc;
424 checkactivedir(); /* let it go on anyway */
428 idesc.id_number = curinum;
429 idesc.id_func = scannames;
430 idesc.id_type = DATA;
431 idesc.id_fix = IGNORE;
432 ckinode(curinode, &idesc);
433 curinode = ginode(curinum);
438 static int findblk_numtofind;
439 static int wantedblksize;
441 CMDFUNCSTART(findblk)
443 ino_t inum, inosused;
444 uint32_t *wantedblk32;
445 uint64_t *wantedblk64;
446 struct bufarea *cgbp;
450 wantedblksize = (argc - 1);
451 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
455 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
456 if (wantedblk64 == NULL)
458 for (i = 1; i < argc; i++)
459 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
461 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
462 if (wantedblk32 == NULL)
464 for (i = 1; i < argc; i++)
465 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
467 findblk_numtofind = wantedblksize;
469 * sblock.fs_ncg holds a number of cylinder groups.
470 * Iterate over all cylinder groups.
472 for (c = 0; c < sblock.fs_ncg; c++) {
474 * sblock.fs_ipg holds a number of inodes per cylinder group.
475 * Calculate a highest inode number for a given cylinder group.
477 inum = c * sblock.fs_ipg;
478 /* Read cylinder group. */
480 cgp = cgbp->b_un.b_cg;
482 * Get a highest used inode number for a given cylinder group.
483 * For UFS1 all inodes initialized at the newfs stage.
486 inosused = cgp->cg_initediblk;
488 inosused = sblock.fs_ipg;
490 for (; inosused > 0; inum++, inosused--) {
491 /* Skip magic inodes: 0, UFS_WINO, UFS_ROOTINO. */
492 if (inum < UFS_ROOTINO)
495 * Check if the block we are looking for is just an inode block.
497 * ino_to_fsba() - get block containing inode from its number.
498 * INOPB() - get a number of inodes in one disk block.
501 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
502 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
503 printf("block %llu: inode block (%ju-%ju)\n",
504 (unsigned long long)fsbtodb(&sblock,
505 ino_to_fsba(&sblock, inum)),
506 (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
507 (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
509 if (findblk_numtofind == 0)
512 /* Get on-disk inode aka dinode. */
514 curinode = ginode(inum);
515 /* Find IFLNK dinode with allocated data blocks. */
516 switch (DIP(curinode, di_mode) & IFMT) {
519 if (DIP(curinode, di_blocks) == 0)
524 uint64_t size = DIP(curinode, di_size);
525 if (size > 0 && size < sblock.fs_maxsymlinklen &&
526 DIP(curinode, di_blocks) == 0)
534 /* Look through direct data blocks. */
536 find_blks64(curinode->dp2.di_db, UFS_NDADDR, wantedblk64) :
537 find_blks32(curinode->dp1.di_db, UFS_NDADDR, wantedblk32))
539 for (i = 0; i < UFS_NIADDR; i++) {
541 * Does the block we are looking for belongs to the
545 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
546 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
547 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
548 curinode->dp1.di_ib[i]))
551 * Search through indirect, double and triple indirect
554 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
555 (curinode->dp1.di_ib[i] != 0))
557 find_indirblks64(curinode->dp2.di_ib[i], i,
559 find_indirblks32(curinode->dp1.di_ib[i], i,
567 curinode = ginode(curinum);
576 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
580 for (i = 0; i < wantedblksize; i++) {
581 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
590 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
594 for (i = 0; i < wantedblksize; i++) {
595 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
604 founddatablk(uint64_t blk)
607 printf("%llu: data block of inode %ju\n",
608 (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
610 if (findblk_numtofind == 0)
616 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
619 for (blk = 0; blk < size; blk++) {
622 if (compare_blk32(wantedblk, buf[blk])) {
623 if (founddatablk(buf[blk]))
631 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
633 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
634 uint32_t idblk[MAXNINDIR];
637 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
638 if (ind_level <= 0) {
639 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
643 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
644 if (compare_blk32(wantedblk, idblk[i])) {
645 if (founddatablk(idblk[i]))
649 if (find_indirblks32(idblk[i], ind_level, wantedblk))
658 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
661 for (blk = 0; blk < size; blk++) {
664 if (compare_blk64(wantedblk, buf[blk])) {
665 if (founddatablk(buf[blk]))
673 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
675 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
676 uint64_t idblk[MAXNINDIR];
679 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
680 if (ind_level <= 0) {
681 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
685 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
686 if (compare_blk64(wantedblk, idblk[i])) {
687 if (founddatablk(idblk[i]))
691 if (find_indirblks64(idblk[i], ind_level, wantedblk))
699 int findino(struct inodesc *idesc); /* from fsck */
700 static int dolookup(char *name);
705 struct inodesc idesc;
707 if (!checkactivedir())
709 idesc.id_number = curinum;
710 idesc.id_func = findino;
711 idesc.id_name = name;
712 idesc.id_type = DATA;
713 idesc.id_fix = IGNORE;
714 if (ckinode(curinode, &idesc) & FOUND) {
715 curinum = idesc.id_parent;
716 curinode = ginode(curinum);
720 warnx("name `%s' not found in current inode directory", name);
725 CMDFUNCSTART(focusname)
734 if (argv[1][0] == '/') {
735 curinum = UFS_ROOTINO;
736 curinode = ginode(UFS_ROOTINO);
738 if (!checkactivedir())
741 for (p = argv[1]; p != NULL;) {
742 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
744 printf("component `%s': ", val);
746 if (!dolookup(val)) {
747 curinode = ginode(curinum);
763 if (!checkactivedir())
765 rval = makeentry(curinum, inum, argv[2]);
767 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
769 printf("could not enter name? weird.\n");
770 curinode = ginode(curinum);
778 if (!checkactivedir())
780 rval = changeino(curinum, argv[1], 0);
781 if (rval & ALTERED) {
782 printf("Name `%s' removed\n", argv[1]);
785 printf("could not remove name ('%s')? weird.\n", argv[1]);
790 long slotcount, desired;
793 chinumfunc(struct inodesc *idesc)
795 struct direct *dirp = idesc->id_dirp;
797 if (slotcount++ == desired) {
798 dirp->d_ino = idesc->id_parent;
799 return STOP|ALTERED|FOUND;
808 struct inodesc idesc;
811 if (!checkactivedir())
815 desired = strtol(argv[1], &cp, 0);
816 if (cp == argv[1] || *cp != '\0' || desired < 0) {
817 printf("invalid slot number `%s'\n", argv[1]);
821 idesc.id_number = curinum;
822 idesc.id_func = chinumfunc;
823 idesc.id_fix = IGNORE;
824 idesc.id_type = DATA;
825 idesc.id_parent = inum; /* XXX convenient hiding place */
827 if (ckinode(curinode, &idesc) & FOUND)
830 warnx("no %sth slot in current directory", argv[1]);
836 chnamefunc(struct inodesc *idesc)
838 struct direct *dirp = idesc->id_dirp;
839 struct direct testdir;
841 if (slotcount++ == desired) {
843 testdir.d_namlen = strlen(idesc->id_name);
844 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
845 dirp->d_namlen = testdir.d_namlen;
846 strcpy(dirp->d_name, idesc->id_name);
847 return STOP|ALTERED|FOUND;
849 return STOP|FOUND; /* won't fit, so give up */
858 struct inodesc idesc;
861 if (!checkactivedir())
864 desired = strtoul(argv[1], &cp, 0);
865 if (cp == argv[1] || *cp != '\0') {
866 printf("invalid slot number `%s'\n", argv[1]);
870 idesc.id_number = curinum;
871 idesc.id_func = chnamefunc;
872 idesc.id_fix = IGNORE;
873 idesc.id_type = DATA;
874 idesc.id_name = argv[2];
876 rval = ckinode(curinode, &idesc);
877 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
879 else if (rval & FOUND) {
880 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
883 warnx("no %sth slot in current directory", argv[1]);
889 const char *typename;
898 CMDFUNCSTART(newtype)
905 type = DIP(curinode, di_mode) & IFMT;
906 for (tp = typenamemap;
907 tp < &typenamemap[nitems(typenamemap)];
909 if (!strcmp(argv[1], tp->typename)) {
910 printf("setting type to %s\n", tp->typename);
915 if (tp == &typenamemap[nitems(typenamemap)]) {
916 warnx("type `%s' not known", argv[1]);
917 warnx("try one of `file', `dir', `socket', `fifo'");
920 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
921 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
936 len = strtol(argv[1], &cp, 0);
937 if (cp == argv[1] || *cp != '\0' || len < 0) {
938 warnx("bad length `%s'", argv[1]);
942 DIP_SET(curinode, di_size, len);
957 modebits = strtol(argv[1], &cp, 8);
958 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
959 warnx("bad modebits `%s'", argv[1]);
963 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
964 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
970 CMDFUNCSTART(chaflags)
979 flags = strtoul(argv[1], &cp, 0);
980 if (cp == argv[1] || *cp != '\0' ) {
981 warnx("bad flags `%s'", argv[1]);
985 if (flags > UINT_MAX) {
986 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
989 DIP_SET(curinode, di_flags, flags);
1004 gen = strtol(argv[1], &cp, 0);
1005 if (cp == argv[1] || *cp != '\0' ) {
1006 warnx("bad gen `%s'", argv[1]);
1010 if (gen > INT_MAX || gen < INT_MIN) {
1011 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1014 DIP_SET(curinode, di_gen, gen);
1020 CMDFUNCSTART(linkcount)
1029 lcnt = strtol(argv[1], &cp, 0);
1030 if (cp == argv[1] || *cp != '\0' ) {
1031 warnx("bad link count `%s'", argv[1]);
1034 if (lcnt > USHRT_MAX || lcnt < 0) {
1035 warnx("max link count is %d\n", USHRT_MAX);
1039 DIP_SET(curinode, di_nlink, lcnt);
1045 CMDFUNCSTART(chowner)
1055 uid = strtoul(argv[1], &cp, 0);
1056 if (cp == argv[1] || *cp != '\0' ) {
1057 /* try looking up name */
1058 if ((pwd = getpwnam(argv[1]))) {
1061 warnx("bad uid `%s'", argv[1]);
1066 DIP_SET(curinode, di_uid, uid);
1072 CMDFUNCSTART(chgroup)
1082 gid = strtoul(argv[1], &cp, 0);
1083 if (cp == argv[1] || *cp != '\0' ) {
1084 if ((grp = getgrnam(argv[1]))) {
1087 warnx("bad gid `%s'", argv[1]);
1092 DIP_SET(curinode, di_gid, gid);
1099 dotime(char *name, time_t *secp, int32_t *nsecp)
1104 p = strchr(name, '.');
1107 nsec = strtoul(++p, &val, 0);
1108 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1109 warnx("invalid nanoseconds");
1114 if (strlen(name) != 14) {
1116 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1121 for (p = name; *p; p++)
1122 if (*p < '0' || *p > '9')
1126 #define VAL() ((*p++) - '0')
1128 t.tm_year = VAL() + t.tm_year * 10;
1129 t.tm_year = VAL() + t.tm_year * 10;
1130 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1132 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1134 t.tm_mday = VAL() + t.tm_mday * 10;
1136 t.tm_hour = VAL() + t.tm_hour * 10;
1138 t.tm_min = VAL() + t.tm_min * 10;
1140 t.tm_sec = VAL() + t.tm_sec * 10;
1145 warnx("date/time out of range");
1151 CMDFUNCSTART(chbtime)
1156 if (dotime(argv[1], &secs, &nsecs))
1158 if (sblock.fs_magic == FS_UFS1_MAGIC)
1160 curinode->dp2.di_birthtime = _time_to_time64(secs);
1161 curinode->dp2.di_birthnsec = nsecs;
1167 CMDFUNCSTART(chmtime)
1172 if (dotime(argv[1], &secs, &nsecs))
1174 if (sblock.fs_magic == FS_UFS1_MAGIC)
1175 curinode->dp1.di_mtime = _time_to_time32(secs);
1177 curinode->dp2.di_mtime = _time_to_time64(secs);
1178 DIP_SET(curinode, di_mtimensec, nsecs);
1184 CMDFUNCSTART(chatime)
1189 if (dotime(argv[1], &secs, &nsecs))
1191 if (sblock.fs_magic == FS_UFS1_MAGIC)
1192 curinode->dp1.di_atime = _time_to_time32(secs);
1194 curinode->dp2.di_atime = _time_to_time64(secs);
1195 DIP_SET(curinode, di_atimensec, nsecs);
1201 CMDFUNCSTART(chctime)
1206 if (dotime(argv[1], &secs, &nsecs))
1208 if (sblock.fs_magic == FS_UFS1_MAGIC)
1209 curinode->dp1.di_ctime = _time_to_time32(secs);
1211 curinode->dp2.di_ctime = _time_to_time64(secs);
1212 DIP_SET(curinode, di_ctimensec, nsecs);