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");
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);
114 printf("%s file system `%s'\nLast Mounted on %s\n",
115 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
118 sblock.fs_clean = 0; /* mark it dirty */
121 printf("*** FILE SYSTEM MARKED DIRTY\n");
122 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
123 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
128 #define CMDFUNC(func) int func(int argc, char *argv[])
129 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
132 CMDFUNC(focus); /* focus on inode */
133 CMDFUNC(active); /* print active inode */
134 CMDFUNC(blocks); /* print blocks for active inode */
135 CMDFUNC(focusname); /* focus by name */
136 CMDFUNC(zapi); /* clear inode */
137 CMDFUNC(uplink); /* incr link */
138 CMDFUNC(downlink); /* decr link */
139 CMDFUNC(linkcount); /* set link count */
140 CMDFUNC(quit); /* quit */
141 CMDFUNC(findblk); /* find block */
142 CMDFUNC(ls); /* list directory */
143 CMDFUNC(rm); /* remove name */
144 CMDFUNC(ln); /* add name */
145 CMDFUNC(newtype); /* change type */
146 CMDFUNC(chmode); /* change mode */
147 CMDFUNC(chlen); /* change length */
148 CMDFUNC(chaflags); /* change flags */
149 CMDFUNC(chgen); /* change generation */
150 CMDFUNC(chowner); /* change owner */
151 CMDFUNC(chgroup); /* Change group */
152 CMDFUNC(back); /* pop back to last ino */
153 CMDFUNC(chbtime); /* Change btime */
154 CMDFUNC(chmtime); /* Change mtime */
155 CMDFUNC(chctime); /* Change ctime */
156 CMDFUNC(chatime); /* Change atime */
157 CMDFUNC(chinum); /* Change inode # of dirent */
158 CMDFUNC(chname); /* Change dirname of dirent */
159 CMDFUNC(chsize); /* Change size */
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 { "chsize", "Change size of current inode to SIZE", 2, 2, FL_WR, chsize },
190 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
191 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
192 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
193 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
194 { "quit", "Exit", 1, 1, FL_RO, quit },
195 { "q", "Exit", 1, 1, FL_RO, quit },
196 { "exit", "Exit", 1, 1, FL_RO, quit },
197 { NULL, 0, 0, 0, 0, NULL },
201 helpfn(int argc, char *argv[])
203 struct cmdtable *cmdtp;
205 printf("Commands are:\n%-10s %5s %5s %s\n",
206 "command", "min args", "max args", "what");
208 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
209 printf("%-10s %5u %5u %s\n",
210 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
217 static char pstring[64];
218 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
229 int cmd_argc, rval = 0, known;
230 #define scratch known
232 struct cmdtable *cmdp;
237 curinode = ginode(UFS_ROOTINO);
238 curinum = UFS_ROOTINO;
241 hist = history_init();
242 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
244 elptr = el_init("fsdb", stdin, stdout, stderr);
245 el_set(elptr, EL_EDITOR, "emacs");
246 el_set(elptr, EL_PROMPT, prompt);
247 el_set(elptr, EL_HIST, history, hist);
248 el_source(elptr, NULL);
250 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
252 printf("command `%s'\n", elline);
254 history(hist, &he, H_ENTER, elline);
256 line = strdup(elline);
257 cmd_argv = crack(line, &cmd_argc);
259 * el_parse returns -1 to signal that it's not been handled
262 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
266 for (cmdp = cmds; cmdp->cmd; cmdp++) {
267 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
268 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
269 warnx("`%s' requires write access", cmd_argv[0]),
271 else if (cmd_argc >= cmdp->minargc &&
272 cmd_argc <= cmdp->maxargc)
273 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
274 else if (cmd_argc >= cmdp->minargc &&
275 (cmdp->flags & FL_ST) == FL_ST) {
276 strcpy(line, elline);
277 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
278 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
280 rval = argcount(cmdp, cmd_argc, cmd_argv);
286 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
291 /* user typed "quit" */
294 warnx("rval was %d", rval);
301 union dinode *curinode;
302 ino_t curinum, ocurrent;
304 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
305 if (inum < UFS_ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
306 printf("inode %ju out of range; range is [%ju,%ju]\n", \
307 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino);\
312 * Focus on given inode number
320 curinode = ginode(inum);
330 curinode = ginode(curinum);
345 if (curinode) /* re-set after potential change */
346 curinode = ginode(curinum);
371 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
372 printf("inode %ju link count now %d\n",
373 (uintmax_t)curinum, DIP(curinode, di_nlink));
378 CMDFUNCSTART(downlink)
382 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
383 printf("inode %ju link count now %d\n",
384 (uintmax_t)curinum, DIP(curinode, di_nlink));
389 const char *typename[] = {
411 scannames(struct inodesc *idesc)
413 struct direct *dirp = idesc->id_dirp;
415 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
416 slot++, diroff, dirp->d_ino, dirp->d_reclen,
417 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
418 diroff += dirp->d_reclen;
424 struct inodesc idesc;
425 checkactivedir(); /* let it go on anyway */
429 idesc.id_number = curinum;
430 idesc.id_func = scannames;
431 idesc.id_type = DATA;
432 idesc.id_fix = IGNORE;
433 ckinode(curinode, &idesc);
434 curinode = ginode(curinum);
439 static int findblk_numtofind;
440 static int wantedblksize;
442 CMDFUNCSTART(findblk)
444 ino_t inum, inosused;
445 uint32_t *wantedblk32;
446 uint64_t *wantedblk64;
447 struct bufarea *cgbp;
451 wantedblksize = (argc - 1);
452 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
456 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
457 if (wantedblk64 == NULL)
459 for (i = 1; i < argc; i++)
460 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
462 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
463 if (wantedblk32 == NULL)
465 for (i = 1; i < argc; i++)
466 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
468 findblk_numtofind = wantedblksize;
470 * sblock.fs_ncg holds a number of cylinder groups.
471 * Iterate over all cylinder groups.
473 for (c = 0; c < sblock.fs_ncg; c++) {
475 * sblock.fs_ipg holds a number of inodes per cylinder group.
476 * Calculate a highest inode number for a given cylinder group.
478 inum = c * sblock.fs_ipg;
479 /* Read cylinder group. */
481 cgp = cgbp->b_un.b_cg;
483 * Get a highest used inode number for a given cylinder group.
484 * For UFS1 all inodes initialized at the newfs stage.
487 inosused = cgp->cg_initediblk;
489 inosused = sblock.fs_ipg;
491 for (; inosused > 0; inum++, inosused--) {
492 /* Skip magic inodes: 0, UFS_WINO, UFS_ROOTINO. */
493 if (inum < UFS_ROOTINO)
496 * Check if the block we are looking for is just an inode block.
498 * ino_to_fsba() - get block containing inode from its number.
499 * INOPB() - get a number of inodes in one disk block.
502 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
503 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
504 printf("block %llu: inode block (%ju-%ju)\n",
505 (unsigned long long)fsbtodb(&sblock,
506 ino_to_fsba(&sblock, inum)),
507 (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
508 (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
510 if (findblk_numtofind == 0)
513 /* Get on-disk inode aka dinode. */
515 curinode = ginode(inum);
516 /* Find IFLNK dinode with allocated data blocks. */
517 switch (DIP(curinode, di_mode) & IFMT) {
520 if (DIP(curinode, di_blocks) == 0)
525 uint64_t size = DIP(curinode, di_size);
526 if (size > 0 && size < sblock.fs_maxsymlinklen &&
527 DIP(curinode, di_blocks) == 0)
535 /* Look through direct data blocks. */
537 find_blks64(curinode->dp2.di_db, UFS_NDADDR, wantedblk64) :
538 find_blks32(curinode->dp1.di_db, UFS_NDADDR, wantedblk32))
540 for (i = 0; i < UFS_NIADDR; i++) {
542 * Does the block we are looking for belongs to the
546 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
547 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
548 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
549 curinode->dp1.di_ib[i]))
552 * Search through indirect, double and triple indirect
555 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
556 (curinode->dp1.di_ib[i] != 0))
558 find_indirblks64(curinode->dp2.di_ib[i], i,
560 find_indirblks32(curinode->dp1.di_ib[i], i,
568 curinode = ginode(curinum);
577 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
581 for (i = 0; i < wantedblksize; i++) {
582 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
591 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
595 for (i = 0; i < wantedblksize; i++) {
596 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
605 founddatablk(uint64_t blk)
608 printf("%llu: data block of inode %ju\n",
609 (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
611 if (findblk_numtofind == 0)
617 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
620 for (blk = 0; blk < size; blk++) {
623 if (compare_blk32(wantedblk, buf[blk])) {
624 if (founddatablk(buf[blk]))
632 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
634 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
635 uint32_t idblk[MAXNINDIR];
638 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
639 if (ind_level <= 0) {
640 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
644 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
645 if (compare_blk32(wantedblk, idblk[i])) {
646 if (founddatablk(idblk[i]))
650 if (find_indirblks32(idblk[i], ind_level, wantedblk))
659 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
662 for (blk = 0; blk < size; blk++) {
665 if (compare_blk64(wantedblk, buf[blk])) {
666 if (founddatablk(buf[blk]))
674 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
676 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
677 uint64_t idblk[MAXNINDIR];
680 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
681 if (ind_level <= 0) {
682 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
686 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
687 if (compare_blk64(wantedblk, idblk[i])) {
688 if (founddatablk(idblk[i]))
692 if (find_indirblks64(idblk[i], ind_level, wantedblk))
700 int findino(struct inodesc *idesc); /* from fsck */
701 static int dolookup(char *name);
706 struct inodesc idesc;
708 if (!checkactivedir())
710 idesc.id_number = curinum;
711 idesc.id_func = findino;
712 idesc.id_name = name;
713 idesc.id_type = DATA;
714 idesc.id_fix = IGNORE;
715 if (ckinode(curinode, &idesc) & FOUND) {
716 curinum = idesc.id_parent;
717 curinode = ginode(curinum);
721 warnx("name `%s' not found in current inode directory", name);
726 CMDFUNCSTART(focusname)
735 if (argv[1][0] == '/') {
736 curinum = UFS_ROOTINO;
737 curinode = ginode(UFS_ROOTINO);
739 if (!checkactivedir())
742 for (p = argv[1]; p != NULL;) {
743 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
745 printf("component `%s': ", val);
747 if (!dolookup(val)) {
748 curinode = ginode(curinum);
764 if (!checkactivedir())
766 rval = makeentry(curinum, inum, argv[2]);
768 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
770 printf("could not enter name? weird.\n");
771 curinode = ginode(curinum);
779 if (!checkactivedir())
781 rval = changeino(curinum, argv[1], 0);
782 if (rval & ALTERED) {
783 printf("Name `%s' removed\n", argv[1]);
786 printf("could not remove name ('%s')? weird.\n", argv[1]);
791 long slotcount, desired;
794 chinumfunc(struct inodesc *idesc)
796 struct direct *dirp = idesc->id_dirp;
798 if (slotcount++ == desired) {
799 dirp->d_ino = idesc->id_parent;
800 return STOP|ALTERED|FOUND;
809 struct inodesc idesc;
812 if (!checkactivedir())
816 desired = strtol(argv[1], &cp, 0);
817 if (cp == argv[1] || *cp != '\0' || desired < 0) {
818 printf("invalid slot number `%s'\n", argv[1]);
822 idesc.id_number = curinum;
823 idesc.id_func = chinumfunc;
824 idesc.id_fix = IGNORE;
825 idesc.id_type = DATA;
826 idesc.id_parent = inum; /* XXX convenient hiding place */
828 if (ckinode(curinode, &idesc) & FOUND)
831 warnx("no %sth slot in current directory", argv[1]);
837 chnamefunc(struct inodesc *idesc)
839 struct direct *dirp = idesc->id_dirp;
840 struct direct testdir;
842 if (slotcount++ == desired) {
844 testdir.d_namlen = strlen(idesc->id_name);
845 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
846 dirp->d_namlen = testdir.d_namlen;
847 strcpy(dirp->d_name, idesc->id_name);
848 return STOP|ALTERED|FOUND;
850 return STOP|FOUND; /* won't fit, so give up */
859 struct inodesc idesc;
862 if (!checkactivedir())
865 desired = strtoul(argv[1], &cp, 0);
866 if (cp == argv[1] || *cp != '\0') {
867 printf("invalid slot number `%s'\n", argv[1]);
871 idesc.id_number = curinum;
872 idesc.id_func = chnamefunc;
873 idesc.id_fix = IGNORE;
874 idesc.id_type = DATA;
875 idesc.id_name = argv[2];
877 rval = ckinode(curinode, &idesc);
878 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
880 else if (rval & FOUND) {
881 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
884 warnx("no %sth slot in current directory", argv[1]);
890 const char *typename;
899 CMDFUNCSTART(newtype)
906 type = DIP(curinode, di_mode) & IFMT;
907 for (tp = typenamemap;
908 tp < &typenamemap[nitems(typenamemap)];
910 if (!strcmp(argv[1], tp->typename)) {
911 printf("setting type to %s\n", tp->typename);
916 if (tp == &typenamemap[nitems(typenamemap)]) {
917 warnx("type `%s' not known", argv[1]);
918 warnx("try one of `file', `dir', `socket', `fifo'");
921 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
922 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
937 len = strtol(argv[1], &cp, 0);
938 if (cp == argv[1] || *cp != '\0' || len < 0) {
939 warnx("bad length `%s'", argv[1]);
943 DIP_SET(curinode, di_size, len);
958 modebits = strtol(argv[1], &cp, 8);
959 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
960 warnx("bad modebits `%s'", argv[1]);
964 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
965 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
971 CMDFUNCSTART(chaflags)
980 flags = strtoul(argv[1], &cp, 0);
981 if (cp == argv[1] || *cp != '\0' ) {
982 warnx("bad flags `%s'", argv[1]);
986 if (flags > UINT_MAX) {
987 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
990 DIP_SET(curinode, di_flags, flags);
1005 gen = strtol(argv[1], &cp, 0);
1006 if (cp == argv[1] || *cp != '\0' ) {
1007 warnx("bad gen `%s'", argv[1]);
1011 if (gen > INT_MAX || gen < INT_MIN) {
1012 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1015 DIP_SET(curinode, di_gen, gen);
1021 CMDFUNCSTART(chsize)
1030 size = strtoll(argv[1], &cp, 0);
1031 if (cp == argv[1] || *cp != '\0') {
1032 warnx("bad size `%s'", argv[1]);
1037 warnx("size set to negative (%jd)\n", (intmax_t)size);
1040 DIP_SET(curinode, di_size, size);
1046 CMDFUNCSTART(linkcount)
1055 lcnt = strtol(argv[1], &cp, 0);
1056 if (cp == argv[1] || *cp != '\0' ) {
1057 warnx("bad link count `%s'", argv[1]);
1060 if (lcnt > USHRT_MAX || lcnt < 0) {
1061 warnx("max link count is %d\n", USHRT_MAX);
1065 DIP_SET(curinode, di_nlink, lcnt);
1071 CMDFUNCSTART(chowner)
1081 uid = strtoul(argv[1], &cp, 0);
1082 if (cp == argv[1] || *cp != '\0' ) {
1083 /* try looking up name */
1084 if ((pwd = getpwnam(argv[1]))) {
1087 warnx("bad uid `%s'", argv[1]);
1092 DIP_SET(curinode, di_uid, uid);
1098 CMDFUNCSTART(chgroup)
1108 gid = strtoul(argv[1], &cp, 0);
1109 if (cp == argv[1] || *cp != '\0' ) {
1110 if ((grp = getgrnam(argv[1]))) {
1113 warnx("bad gid `%s'", argv[1]);
1118 DIP_SET(curinode, di_gid, gid);
1125 dotime(char *name, time_t *secp, int32_t *nsecp)
1130 p = strchr(name, '.');
1133 nsec = strtoul(++p, &val, 0);
1134 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1135 warnx("invalid nanoseconds");
1140 if (strlen(name) != 14) {
1142 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1147 for (p = name; *p; p++)
1148 if (*p < '0' || *p > '9')
1152 #define VAL() ((*p++) - '0')
1154 t.tm_year = VAL() + t.tm_year * 10;
1155 t.tm_year = VAL() + t.tm_year * 10;
1156 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1158 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1160 t.tm_mday = VAL() + t.tm_mday * 10;
1162 t.tm_hour = VAL() + t.tm_hour * 10;
1164 t.tm_min = VAL() + t.tm_min * 10;
1166 t.tm_sec = VAL() + t.tm_sec * 10;
1171 warnx("date/time out of range");
1177 CMDFUNCSTART(chbtime)
1182 if (dotime(argv[1], &secs, &nsecs))
1184 if (sblock.fs_magic == FS_UFS1_MAGIC)
1186 curinode->dp2.di_birthtime = _time_to_time64(secs);
1187 curinode->dp2.di_birthnsec = nsecs;
1193 CMDFUNCSTART(chmtime)
1198 if (dotime(argv[1], &secs, &nsecs))
1200 if (sblock.fs_magic == FS_UFS1_MAGIC)
1201 curinode->dp1.di_mtime = _time_to_time32(secs);
1203 curinode->dp2.di_mtime = _time_to_time64(secs);
1204 DIP_SET(curinode, di_mtimensec, nsecs);
1210 CMDFUNCSTART(chatime)
1215 if (dotime(argv[1], &secs, &nsecs))
1217 if (sblock.fs_magic == FS_UFS1_MAGIC)
1218 curinode->dp1.di_atime = _time_to_time32(secs);
1220 curinode->dp2.di_atime = _time_to_time64(secs);
1221 DIP_SET(curinode, di_atimensec, nsecs);
1227 CMDFUNCSTART(chctime)
1232 if (dotime(argv[1], &secs, &nsecs))
1234 if (sblock.fs_magic == FS_UFS1_MAGIC)
1235 curinode->dp1.di_ctime = _time_to_time32(secs);
1237 curinode->dp2.di_ctime = _time_to_time64(secs);
1238 DIP_SET(curinode, di_ctimensec, nsecs);