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);
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 */
157 CMDFUNC(chsize); /* Change size */
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 { "chsize", "Change size of current inode to SIZE", 2, 2, FL_WR, chsize },
188 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
189 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
190 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
191 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
192 { "quit", "Exit", 1, 1, FL_RO, quit },
193 { "q", "Exit", 1, 1, FL_RO, quit },
194 { "exit", "Exit", 1, 1, FL_RO, quit },
195 { NULL, 0, 0, 0, 0, NULL },
199 helpfn(int argc, char *argv[])
201 struct cmdtable *cmdtp;
203 printf("Commands are:\n%-10s %5s %5s %s\n",
204 "command", "min args", "max args", "what");
206 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
207 printf("%-10s %5u %5u %s\n",
208 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
215 static char pstring[64];
216 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
227 int cmd_argc, rval = 0, known;
228 #define scratch known
230 struct cmdtable *cmdp;
235 curinode = ginode(UFS_ROOTINO);
236 curinum = UFS_ROOTINO;
239 hist = history_init();
240 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
242 elptr = el_init("fsdb", stdin, stdout, stderr);
243 el_set(elptr, EL_EDITOR, "emacs");
244 el_set(elptr, EL_PROMPT, prompt);
245 el_set(elptr, EL_HIST, history, hist);
246 el_source(elptr, NULL);
248 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
250 printf("command `%s'\n", elline);
252 history(hist, &he, H_ENTER, elline);
254 line = strdup(elline);
255 cmd_argv = crack(line, &cmd_argc);
257 * el_parse returns -1 to signal that it's not been handled
260 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
264 for (cmdp = cmds; cmdp->cmd; cmdp++) {
265 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
266 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
267 warnx("`%s' requires write access", cmd_argv[0]),
269 else if (cmd_argc >= cmdp->minargc &&
270 cmd_argc <= cmdp->maxargc)
271 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
272 else if (cmd_argc >= cmdp->minargc &&
273 (cmdp->flags & FL_ST) == FL_ST) {
274 strcpy(line, elline);
275 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
276 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
278 rval = argcount(cmdp, cmd_argc, cmd_argv);
284 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
289 /* user typed "quit" */
292 warnx("rval was %d", rval);
299 union dinode *curinode;
300 ino_t curinum, ocurrent;
302 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
303 if (inum < UFS_ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
304 printf("inode %ju out of range; range is [%ju,%ju]\n", \
305 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino);\
310 * Focus on given inode number
318 curinode = ginode(inum);
328 curinode = ginode(curinum);
343 if (curinode) /* re-set after potential change */
344 curinode = ginode(curinum);
369 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
370 printf("inode %ju link count now %d\n",
371 (uintmax_t)curinum, DIP(curinode, di_nlink));
376 CMDFUNCSTART(downlink)
380 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
381 printf("inode %ju link count now %d\n",
382 (uintmax_t)curinum, DIP(curinode, di_nlink));
387 const char *typename[] = {
409 scannames(struct inodesc *idesc)
411 struct direct *dirp = idesc->id_dirp;
413 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
414 slot++, diroff, dirp->d_ino, dirp->d_reclen,
415 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
416 diroff += dirp->d_reclen;
422 struct inodesc idesc;
423 checkactivedir(); /* let it go on anyway */
427 idesc.id_number = curinum;
428 idesc.id_func = scannames;
429 idesc.id_type = DATA;
430 idesc.id_fix = IGNORE;
431 ckinode(curinode, &idesc);
432 curinode = ginode(curinum);
437 static int findblk_numtofind;
438 static int wantedblksize;
440 CMDFUNCSTART(findblk)
442 ino_t inum, inosused;
443 uint32_t *wantedblk32;
444 uint64_t *wantedblk64;
445 struct bufarea *cgbp;
449 wantedblksize = (argc - 1);
450 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
454 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
455 if (wantedblk64 == NULL)
457 for (i = 1; i < argc; i++)
458 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
460 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
461 if (wantedblk32 == NULL)
463 for (i = 1; i < argc; i++)
464 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
466 findblk_numtofind = wantedblksize;
468 * sblock.fs_ncg holds a number of cylinder groups.
469 * Iterate over all cylinder groups.
471 for (c = 0; c < sblock.fs_ncg; c++) {
473 * sblock.fs_ipg holds a number of inodes per cylinder group.
474 * Calculate a highest inode number for a given cylinder group.
476 inum = c * sblock.fs_ipg;
477 /* Read cylinder group. */
479 cgp = cgbp->b_un.b_cg;
481 * Get a highest used inode number for a given cylinder group.
482 * For UFS1 all inodes initialized at the newfs stage.
485 inosused = cgp->cg_initediblk;
487 inosused = sblock.fs_ipg;
489 for (; inosused > 0; inum++, inosused--) {
490 /* Skip magic inodes: 0, UFS_WINO, UFS_ROOTINO. */
491 if (inum < UFS_ROOTINO)
494 * Check if the block we are looking for is just an inode block.
496 * ino_to_fsba() - get block containing inode from its number.
497 * INOPB() - get a number of inodes in one disk block.
500 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
501 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
502 printf("block %llu: inode block (%ju-%ju)\n",
503 (unsigned long long)fsbtodb(&sblock,
504 ino_to_fsba(&sblock, inum)),
505 (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
506 (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
508 if (findblk_numtofind == 0)
511 /* Get on-disk inode aka dinode. */
513 curinode = ginode(inum);
514 /* Find IFLNK dinode with allocated data blocks. */
515 switch (DIP(curinode, di_mode) & IFMT) {
518 if (DIP(curinode, di_blocks) == 0)
523 uint64_t size = DIP(curinode, di_size);
524 if (size > 0 && size < sblock.fs_maxsymlinklen &&
525 DIP(curinode, di_blocks) == 0)
533 /* Look through direct data blocks. */
535 find_blks64(curinode->dp2.di_db, UFS_NDADDR, wantedblk64) :
536 find_blks32(curinode->dp1.di_db, UFS_NDADDR, wantedblk32))
538 for (i = 0; i < UFS_NIADDR; i++) {
540 * Does the block we are looking for belongs to the
544 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
545 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
546 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
547 curinode->dp1.di_ib[i]))
550 * Search through indirect, double and triple indirect
553 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
554 (curinode->dp1.di_ib[i] != 0))
556 find_indirblks64(curinode->dp2.di_ib[i], i,
558 find_indirblks32(curinode->dp1.di_ib[i], i,
566 curinode = ginode(curinum);
575 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
579 for (i = 0; i < wantedblksize; i++) {
580 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
589 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
593 for (i = 0; i < wantedblksize; i++) {
594 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
603 founddatablk(uint64_t blk)
606 printf("%llu: data block of inode %ju\n",
607 (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
609 if (findblk_numtofind == 0)
615 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
618 for (blk = 0; blk < size; blk++) {
621 if (compare_blk32(wantedblk, buf[blk])) {
622 if (founddatablk(buf[blk]))
630 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
632 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
633 uint32_t idblk[MAXNINDIR];
636 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
637 if (ind_level <= 0) {
638 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
642 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
643 if (compare_blk32(wantedblk, idblk[i])) {
644 if (founddatablk(idblk[i]))
648 if (find_indirblks32(idblk[i], ind_level, wantedblk))
657 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
660 for (blk = 0; blk < size; blk++) {
663 if (compare_blk64(wantedblk, buf[blk])) {
664 if (founddatablk(buf[blk]))
672 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
674 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
675 uint64_t idblk[MAXNINDIR];
678 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
679 if (ind_level <= 0) {
680 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
684 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
685 if (compare_blk64(wantedblk, idblk[i])) {
686 if (founddatablk(idblk[i]))
690 if (find_indirblks64(idblk[i], ind_level, wantedblk))
698 int findino(struct inodesc *idesc); /* from fsck */
699 static int dolookup(char *name);
704 struct inodesc idesc;
706 if (!checkactivedir())
708 idesc.id_number = curinum;
709 idesc.id_func = findino;
710 idesc.id_name = name;
711 idesc.id_type = DATA;
712 idesc.id_fix = IGNORE;
713 if (ckinode(curinode, &idesc) & FOUND) {
714 curinum = idesc.id_parent;
715 curinode = ginode(curinum);
719 warnx("name `%s' not found in current inode directory", name);
724 CMDFUNCSTART(focusname)
733 if (argv[1][0] == '/') {
734 curinum = UFS_ROOTINO;
735 curinode = ginode(UFS_ROOTINO);
737 if (!checkactivedir())
740 for (p = argv[1]; p != NULL;) {
741 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
743 printf("component `%s': ", val);
745 if (!dolookup(val)) {
746 curinode = ginode(curinum);
762 if (!checkactivedir())
764 rval = makeentry(curinum, inum, argv[2]);
766 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
768 printf("could not enter name? weird.\n");
769 curinode = ginode(curinum);
777 if (!checkactivedir())
779 rval = changeino(curinum, argv[1], 0);
780 if (rval & ALTERED) {
781 printf("Name `%s' removed\n", argv[1]);
784 printf("could not remove name ('%s')? weird.\n", argv[1]);
789 long slotcount, desired;
792 chinumfunc(struct inodesc *idesc)
794 struct direct *dirp = idesc->id_dirp;
796 if (slotcount++ == desired) {
797 dirp->d_ino = idesc->id_parent;
798 return STOP|ALTERED|FOUND;
807 struct inodesc idesc;
810 if (!checkactivedir())
814 desired = strtol(argv[1], &cp, 0);
815 if (cp == argv[1] || *cp != '\0' || desired < 0) {
816 printf("invalid slot number `%s'\n", argv[1]);
820 idesc.id_number = curinum;
821 idesc.id_func = chinumfunc;
822 idesc.id_fix = IGNORE;
823 idesc.id_type = DATA;
824 idesc.id_parent = inum; /* XXX convenient hiding place */
826 if (ckinode(curinode, &idesc) & FOUND)
829 warnx("no %sth slot in current directory", argv[1]);
835 chnamefunc(struct inodesc *idesc)
837 struct direct *dirp = idesc->id_dirp;
838 struct direct testdir;
840 if (slotcount++ == desired) {
842 testdir.d_namlen = strlen(idesc->id_name);
843 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
844 dirp->d_namlen = testdir.d_namlen;
845 strcpy(dirp->d_name, idesc->id_name);
846 return STOP|ALTERED|FOUND;
848 return STOP|FOUND; /* won't fit, so give up */
857 struct inodesc idesc;
860 if (!checkactivedir())
863 desired = strtoul(argv[1], &cp, 0);
864 if (cp == argv[1] || *cp != '\0') {
865 printf("invalid slot number `%s'\n", argv[1]);
869 idesc.id_number = curinum;
870 idesc.id_func = chnamefunc;
871 idesc.id_fix = IGNORE;
872 idesc.id_type = DATA;
873 idesc.id_name = argv[2];
875 rval = ckinode(curinode, &idesc);
876 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
878 else if (rval & FOUND) {
879 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
882 warnx("no %sth slot in current directory", argv[1]);
888 const char *typename;
897 CMDFUNCSTART(newtype)
904 type = DIP(curinode, di_mode) & IFMT;
905 for (tp = typenamemap;
906 tp < &typenamemap[nitems(typenamemap)];
908 if (!strcmp(argv[1], tp->typename)) {
909 printf("setting type to %s\n", tp->typename);
914 if (tp == &typenamemap[nitems(typenamemap)]) {
915 warnx("type `%s' not known", argv[1]);
916 warnx("try one of `file', `dir', `socket', `fifo'");
919 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
920 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
935 len = strtol(argv[1], &cp, 0);
936 if (cp == argv[1] || *cp != '\0' || len < 0) {
937 warnx("bad length `%s'", argv[1]);
941 DIP_SET(curinode, di_size, len);
956 modebits = strtol(argv[1], &cp, 8);
957 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
958 warnx("bad modebits `%s'", argv[1]);
962 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
963 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
969 CMDFUNCSTART(chaflags)
978 flags = strtoul(argv[1], &cp, 0);
979 if (cp == argv[1] || *cp != '\0' ) {
980 warnx("bad flags `%s'", argv[1]);
984 if (flags > UINT_MAX) {
985 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
988 DIP_SET(curinode, di_flags, flags);
1003 gen = strtol(argv[1], &cp, 0);
1004 if (cp == argv[1] || *cp != '\0' ) {
1005 warnx("bad gen `%s'", argv[1]);
1009 if (gen > INT_MAX || gen < INT_MIN) {
1010 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1013 DIP_SET(curinode, di_gen, gen);
1019 CMDFUNCSTART(chsize)
1028 size = strtoll(argv[1], &cp, 0);
1029 if (cp == argv[1] || *cp != '\0') {
1030 warnx("bad size `%s'", argv[1]);
1035 warnx("size set to negative (%jd)\n", (intmax_t)size);
1038 DIP_SET(curinode, di_size, size);
1044 CMDFUNCSTART(linkcount)
1053 lcnt = strtol(argv[1], &cp, 0);
1054 if (cp == argv[1] || *cp != '\0' ) {
1055 warnx("bad link count `%s'", argv[1]);
1058 if (lcnt > USHRT_MAX || lcnt < 0) {
1059 warnx("max link count is %d\n", USHRT_MAX);
1063 DIP_SET(curinode, di_nlink, lcnt);
1069 CMDFUNCSTART(chowner)
1079 uid = strtoul(argv[1], &cp, 0);
1080 if (cp == argv[1] || *cp != '\0' ) {
1081 /* try looking up name */
1082 if ((pwd = getpwnam(argv[1]))) {
1085 warnx("bad uid `%s'", argv[1]);
1090 DIP_SET(curinode, di_uid, uid);
1096 CMDFUNCSTART(chgroup)
1106 gid = strtoul(argv[1], &cp, 0);
1107 if (cp == argv[1] || *cp != '\0' ) {
1108 if ((grp = getgrnam(argv[1]))) {
1111 warnx("bad gid `%s'", argv[1]);
1116 DIP_SET(curinode, di_gid, gid);
1123 dotime(char *name, time_t *secp, int32_t *nsecp)
1128 p = strchr(name, '.');
1131 nsec = strtoul(++p, &val, 0);
1132 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1133 warnx("invalid nanoseconds");
1138 if (strlen(name) != 14) {
1140 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1145 for (p = name; *p; p++)
1146 if (*p < '0' || *p > '9')
1150 #define VAL() ((*p++) - '0')
1152 t.tm_year = VAL() + t.tm_year * 10;
1153 t.tm_year = VAL() + t.tm_year * 10;
1154 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1156 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1158 t.tm_mday = VAL() + t.tm_mday * 10;
1160 t.tm_hour = VAL() + t.tm_hour * 10;
1162 t.tm_min = VAL() + t.tm_min * 10;
1164 t.tm_sec = VAL() + t.tm_sec * 10;
1169 warnx("date/time out of range");
1175 CMDFUNCSTART(chbtime)
1180 if (dotime(argv[1], &secs, &nsecs))
1182 if (sblock.fs_magic == FS_UFS1_MAGIC)
1184 curinode->dp2.di_birthtime = _time_to_time64(secs);
1185 curinode->dp2.di_birthnsec = nsecs;
1191 CMDFUNCSTART(chmtime)
1196 if (dotime(argv[1], &secs, &nsecs))
1198 if (sblock.fs_magic == FS_UFS1_MAGIC)
1199 curinode->dp1.di_mtime = _time_to_time32(secs);
1201 curinode->dp2.di_mtime = _time_to_time64(secs);
1202 DIP_SET(curinode, di_mtimensec, nsecs);
1208 CMDFUNCSTART(chatime)
1213 if (dotime(argv[1], &secs, &nsecs))
1215 if (sblock.fs_magic == FS_UFS1_MAGIC)
1216 curinode->dp1.di_atime = _time_to_time32(secs);
1218 curinode->dp2.di_atime = _time_to_time64(secs);
1219 DIP_SET(curinode, di_atimensec, nsecs);
1225 CMDFUNCSTART(chctime)
1230 if (dotime(argv[1], &secs, &nsecs))
1232 if (sblock.fs_magic == FS_UFS1_MAGIC)
1233 curinode->dp1.di_ctime = _time_to_time32(secs);
1235 curinode->dp2.di_ctime = _time_to_time64(secs);
1236 DIP_SET(curinode, di_ctimensec, nsecs);