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);
67 union dinode *curinode;
68 ino_t curinum, ocurrent;
73 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
78 * We suck in lots of fsck code, and just pick & choose the stuff we want.
80 * fsreadfd is set up to read from the file system, fswritefd to write to
84 main(int argc, char *argv[])
89 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
92 /* The -f option is left for historical
93 * reasons and has no meaning.
100 nflag++; /* "no" in fsck, readonly for us */
115 errx(1, "cannot set up file system `%s'", fsys);
118 printf("%s file system `%s'\nLast Mounted on %s\n",
119 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
122 sblock.fs_clean = 0; /* mark it dirty */
125 printf("*** FILE SYSTEM MARKED DIRTY\n");
126 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
127 printf("*** IF IT IS MOUNTED, RE-MOUNT WITH -u -o reload\n");
132 #define CMDFUNC(func) int func(int argc, char *argv[])
133 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
136 CMDFUNC(focus); /* focus on inode */
137 CMDFUNC(active); /* print active inode */
138 CMDFUNC(blocks); /* print blocks for active inode */
139 CMDFUNC(focusname); /* focus by name */
140 CMDFUNC(zapi); /* clear inode */
141 CMDFUNC(uplink); /* incr link */
142 CMDFUNC(downlink); /* decr link */
143 CMDFUNC(linkcount); /* set link count */
144 CMDFUNC(quit); /* quit */
145 CMDFUNC(findblk); /* find block */
146 CMDFUNC(ls); /* list directory */
147 CMDFUNC(rm); /* remove name */
148 CMDFUNC(ln); /* add name */
149 CMDFUNC(newtype); /* change type */
150 CMDFUNC(chmode); /* change mode */
151 CMDFUNC(chlen); /* change length */
152 CMDFUNC(chaflags); /* change flags */
153 CMDFUNC(chgen); /* change generation */
154 CMDFUNC(chowner); /* change owner */
155 CMDFUNC(chgroup); /* Change group */
156 CMDFUNC(back); /* pop back to last ino */
157 CMDFUNC(chbtime); /* Change btime */
158 CMDFUNC(chmtime); /* Change mtime */
159 CMDFUNC(chctime); /* Change ctime */
160 CMDFUNC(chatime); /* Change atime */
161 CMDFUNC(chinum); /* Change inode # of dirent */
162 CMDFUNC(chname); /* Change dirname of dirent */
163 CMDFUNC(chsize); /* Change size */
165 struct cmdtable cmds[] = {
166 { "help", "Print out help", 1, 1, FL_RO, helpfn },
167 { "?", "Print out help", 1, 1, FL_RO, helpfn },
168 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
169 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
170 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
171 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
172 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
173 { "active", "Print active inode", 1, 1, FL_RO, active },
174 { "print", "Print active inode", 1, 1, FL_RO, active },
175 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
176 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
177 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
178 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
179 { "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
180 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
181 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
182 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
183 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR | FL_ST, ln },
184 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
185 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
186 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
187 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
188 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
189 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
190 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
191 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
192 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
193 { "chsize", "Change size of current inode to SIZE", 2, 2, FL_WR, chsize },
194 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
195 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
196 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
197 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
198 { "quit", "Exit", 1, 1, FL_RO, quit },
199 { "q", "Exit", 1, 1, FL_RO, quit },
200 { "exit", "Exit", 1, 1, FL_RO, quit },
201 { NULL, 0, 0, 0, 0, NULL },
205 helpfn(int argc, char *argv[])
207 struct cmdtable *cmdtp;
209 printf("Commands are:\n%-10s %5s %5s %s\n",
210 "command", "min args", "max args", "what");
212 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
213 printf("%-10s %5u %5u %s\n",
214 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
221 static char pstring[64];
222 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
228 setcurinode(ino_t inum)
231 if (curip.i_number != 0)
233 ginode(inum, &curip);
234 curinode = curip.i_dp;
243 int cmd_argc, rval = 0, known;
244 #define scratch known
246 struct cmdtable *cmdp;
251 setcurinode(UFS_ROOTINO);
254 hist = history_init();
255 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
257 elptr = el_init("fsdb", stdin, stdout, stderr);
258 el_set(elptr, EL_EDITOR, "emacs");
259 el_set(elptr, EL_PROMPT, prompt);
260 el_set(elptr, EL_HIST, history, hist);
261 el_source(elptr, NULL);
263 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
265 printf("command `%s'\n", elline);
267 history(hist, &he, H_ENTER, elline);
269 line = strdup(elline);
270 cmd_argv = crack(line, &cmd_argc);
272 * el_parse returns -1 to signal that it's not been handled
275 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
279 for (cmdp = cmds; cmdp->cmd; cmdp++) {
280 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
281 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
282 warnx("`%s' requires write access", cmd_argv[0]),
284 else if (cmd_argc >= cmdp->minargc &&
285 cmd_argc <= cmdp->maxargc)
286 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
287 else if (cmd_argc >= cmdp->minargc &&
288 (cmdp->flags & FL_ST) == FL_ST) {
289 strcpy(line, elline);
290 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
291 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
293 rval = argcount(cmdp, cmd_argc, cmd_argv);
299 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
304 /* user typed "quit" */
309 warnx("rval was %d", rval);
317 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
318 if (inum < UFS_ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
319 printf("inode %ju out of range; range is [%ju,%ju]\n", \
320 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino);\
325 * Focus on given inode number
341 setcurinode(ocurrent);
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 CMDFUNCSTART(downlink)
392 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
393 printf("inode %ju link count now %d\n",
394 (uintmax_t)curinum, DIP(curinode, di_nlink));
399 const char *typename[] = {
421 scannames(struct inodesc *idesc)
423 struct direct *dirp = idesc->id_dirp;
425 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
426 slot++, diroff, dirp->d_ino, dirp->d_reclen,
427 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
428 diroff += dirp->d_reclen;
434 struct inodesc idesc;
435 checkactivedir(); /* let it go on anyway */
439 idesc.id_number = curinum;
440 idesc.id_func = scannames;
441 idesc.id_type = DATA;
442 idesc.id_fix = IGNORE;
443 ckinode(curinode, &idesc);
448 static int findblk_numtofind;
449 static int wantedblksize;
451 CMDFUNCSTART(findblk)
453 ino_t inum, inosused;
454 uint32_t *wantedblk32;
455 uint64_t *wantedblk64;
456 struct bufarea *cgbp;
460 wantedblksize = (argc - 1);
461 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
465 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
466 if (wantedblk64 == NULL)
468 for (i = 1; i < argc; i++)
469 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
471 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
472 if (wantedblk32 == NULL)
474 for (i = 1; i < argc; i++)
475 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
477 findblk_numtofind = wantedblksize;
479 * sblock.fs_ncg holds a number of cylinder groups.
480 * Iterate over all cylinder groups.
482 for (c = 0; c < sblock.fs_ncg; c++) {
484 * sblock.fs_ipg holds a number of inodes per cylinder group.
485 * Calculate a highest inode number for a given cylinder group.
487 inum = c * sblock.fs_ipg;
488 /* Read cylinder group. */
490 cgp = cgbp->b_un.b_cg;
492 * Get a highest used inode number for a given cylinder group.
493 * For UFS1 all inodes initialized at the newfs stage.
496 inosused = cgp->cg_initediblk;
498 inosused = sblock.fs_ipg;
500 for (; inosused > 0; inum++, inosused--) {
501 /* Skip magic inodes: 0, UFS_WINO, UFS_ROOTINO. */
502 if (inum < UFS_ROOTINO)
505 * Check if the block we are looking for is just an inode block.
507 * ino_to_fsba() - get block containing inode from its number.
508 * INOPB() - get a number of inodes in one disk block.
511 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
512 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
513 printf("block %llu: inode block (%ju-%ju)\n",
514 (unsigned long long)fsbtodb(&sblock,
515 ino_to_fsba(&sblock, inum)),
516 (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
517 (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
519 if (findblk_numtofind == 0)
522 /* Get on-disk inode aka dinode. */
524 /* Find IFLNK dinode with allocated data blocks. */
525 switch (DIP(curinode, di_mode) & IFMT) {
528 if (DIP(curinode, di_blocks) == 0)
533 uint64_t size = DIP(curinode, di_size);
534 if (size > 0 && size < sblock.fs_maxsymlinklen &&
535 DIP(curinode, di_blocks) == 0)
543 /* Look through direct data blocks. */
545 find_blks64(curinode->dp2.di_db, UFS_NDADDR, wantedblk64) :
546 find_blks32(curinode->dp1.di_db, UFS_NDADDR, wantedblk32))
548 for (i = 0; i < UFS_NIADDR; i++) {
550 * Does the block we are looking for belongs to the
554 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
555 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
556 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
557 curinode->dp1.di_ib[i]))
560 * Search through indirect, double and triple indirect
563 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
564 (curinode->dp1.di_ib[i] != 0))
566 find_indirblks64(curinode->dp2.di_ib[i], i,
568 find_indirblks32(curinode->dp1.di_ib[i], i,
575 setcurinode(ocurrent);
584 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
588 for (i = 0; i < wantedblksize; i++) {
589 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
598 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
602 for (i = 0; i < wantedblksize; i++) {
603 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
612 founddatablk(uint64_t blk)
615 printf("%llu: data block of inode %ju\n",
616 (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
618 if (findblk_numtofind == 0)
624 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
627 for (blk = 0; blk < size; blk++) {
630 if (compare_blk32(wantedblk, buf[blk])) {
631 if (founddatablk(buf[blk]))
639 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
641 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
642 uint32_t idblk[MAXNINDIR];
645 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
646 if (ind_level <= 0) {
647 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
651 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
652 if (compare_blk32(wantedblk, idblk[i])) {
653 if (founddatablk(idblk[i]))
657 if (find_indirblks32(idblk[i], ind_level, wantedblk))
666 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
669 for (blk = 0; blk < size; blk++) {
672 if (compare_blk64(wantedblk, buf[blk])) {
673 if (founddatablk(buf[blk]))
681 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
683 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
684 uint64_t idblk[MAXNINDIR];
687 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
688 if (ind_level <= 0) {
689 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
693 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
694 if (compare_blk64(wantedblk, idblk[i])) {
695 if (founddatablk(idblk[i]))
699 if (find_indirblks64(idblk[i], ind_level, wantedblk))
707 int findino(struct inodesc *idesc); /* from fsck */
708 static int dolookup(char *name);
713 struct inodesc idesc;
715 if (!checkactivedir())
717 idesc.id_number = curinum;
718 idesc.id_func = findino;
719 idesc.id_name = name;
720 idesc.id_type = DATA;
721 idesc.id_fix = IGNORE;
722 if (ckinode(curinode, &idesc) & FOUND) {
723 setcurinode(idesc.id_parent);
727 warnx("name `%s' not found in current inode directory", name);
732 CMDFUNCSTART(focusname)
741 if (argv[1][0] == '/') {
742 setcurinode(UFS_ROOTINO);
744 if (!checkactivedir())
747 for (p = argv[1]; p != NULL;) {
748 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
750 printf("component `%s': ", val);
752 if (!dolookup(val)) {
768 if (!checkactivedir())
770 rval = makeentry(curinum, inum, argv[2]);
772 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
774 printf("could not enter name? weird.\n");
782 if (!checkactivedir())
784 rval = changeino(curinum, argv[1], 0);
785 if (rval & ALTERED) {
786 printf("Name `%s' removed\n", argv[1]);
789 printf("could not remove name ('%s')? weird.\n", argv[1]);
794 long slotcount, desired;
797 chinumfunc(struct inodesc *idesc)
799 struct direct *dirp = idesc->id_dirp;
801 if (slotcount++ == desired) {
802 dirp->d_ino = idesc->id_parent;
803 return STOP|ALTERED|FOUND;
812 struct inodesc idesc;
815 if (!checkactivedir())
819 desired = strtol(argv[1], &cp, 0);
820 if (cp == argv[1] || *cp != '\0' || desired < 0) {
821 printf("invalid slot number `%s'\n", argv[1]);
825 idesc.id_number = curinum;
826 idesc.id_func = chinumfunc;
827 idesc.id_fix = IGNORE;
828 idesc.id_type = DATA;
829 idesc.id_parent = inum; /* XXX convenient hiding place */
831 if (ckinode(curinode, &idesc) & FOUND)
834 warnx("no %sth slot in current directory", argv[1]);
840 chnamefunc(struct inodesc *idesc)
842 struct direct *dirp = idesc->id_dirp;
843 struct direct testdir;
845 if (slotcount++ == desired) {
847 testdir.d_namlen = strlen(idesc->id_name);
848 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
849 dirp->d_namlen = testdir.d_namlen;
850 strcpy(dirp->d_name, idesc->id_name);
851 return STOP|ALTERED|FOUND;
853 return STOP|FOUND; /* won't fit, so give up */
862 struct inodesc idesc;
865 if (!checkactivedir())
868 desired = strtoul(argv[1], &cp, 0);
869 if (cp == argv[1] || *cp != '\0') {
870 printf("invalid slot number `%s'\n", argv[1]);
874 idesc.id_number = curinum;
875 idesc.id_func = chnamefunc;
876 idesc.id_fix = IGNORE;
877 idesc.id_type = DATA;
878 idesc.id_name = argv[2];
880 rval = ckinode(curinode, &idesc);
881 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
883 else if (rval & FOUND) {
884 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
887 warnx("no %sth slot in current directory", argv[1]);
893 const char *typename;
902 CMDFUNCSTART(newtype)
909 type = DIP(curinode, di_mode) & IFMT;
910 for (tp = typenamemap;
911 tp < &typenamemap[nitems(typenamemap)];
913 if (!strcmp(argv[1], tp->typename)) {
914 printf("setting type to %s\n", tp->typename);
919 if (tp == &typenamemap[nitems(typenamemap)]) {
920 warnx("type `%s' not known", argv[1]);
921 warnx("try one of `file', `dir', `socket', `fifo'");
924 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
925 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
940 len = strtol(argv[1], &cp, 0);
941 if (cp == argv[1] || *cp != '\0' || len < 0) {
942 warnx("bad length `%s'", argv[1]);
946 DIP_SET(curinode, di_size, len);
961 modebits = strtol(argv[1], &cp, 8);
962 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
963 warnx("bad modebits `%s'", argv[1]);
967 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
968 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
974 CMDFUNCSTART(chaflags)
983 flags = strtoul(argv[1], &cp, 0);
984 if (cp == argv[1] || *cp != '\0' ) {
985 warnx("bad flags `%s'", argv[1]);
989 if (flags > UINT_MAX) {
990 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
993 DIP_SET(curinode, di_flags, flags);
1008 gen = strtol(argv[1], &cp, 0);
1009 if (cp == argv[1] || *cp != '\0' ) {
1010 warnx("bad gen `%s'", argv[1]);
1014 if (gen > INT_MAX || gen < INT_MIN) {
1015 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1018 DIP_SET(curinode, di_gen, gen);
1024 CMDFUNCSTART(chsize)
1033 size = strtoll(argv[1], &cp, 0);
1034 if (cp == argv[1] || *cp != '\0') {
1035 warnx("bad size `%s'", argv[1]);
1040 warnx("size set to negative (%jd)\n", (intmax_t)size);
1043 DIP_SET(curinode, di_size, size);
1049 CMDFUNCSTART(linkcount)
1058 lcnt = strtol(argv[1], &cp, 0);
1059 if (cp == argv[1] || *cp != '\0' ) {
1060 warnx("bad link count `%s'", argv[1]);
1063 if (lcnt > USHRT_MAX || lcnt < 0) {
1064 warnx("max link count is %d\n", USHRT_MAX);
1068 DIP_SET(curinode, di_nlink, lcnt);
1074 CMDFUNCSTART(chowner)
1084 uid = strtoul(argv[1], &cp, 0);
1085 if (cp == argv[1] || *cp != '\0' ) {
1086 /* try looking up name */
1087 if ((pwd = getpwnam(argv[1]))) {
1090 warnx("bad uid `%s'", argv[1]);
1095 DIP_SET(curinode, di_uid, uid);
1101 CMDFUNCSTART(chgroup)
1111 gid = strtoul(argv[1], &cp, 0);
1112 if (cp == argv[1] || *cp != '\0' ) {
1113 if ((grp = getgrnam(argv[1]))) {
1116 warnx("bad gid `%s'", argv[1]);
1121 DIP_SET(curinode, di_gid, gid);
1128 dotime(char *name, time_t *secp, int32_t *nsecp)
1133 p = strchr(name, '.');
1136 nsec = strtoul(++p, &val, 0);
1137 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1138 warnx("invalid nanoseconds");
1143 if (strlen(name) != 14) {
1145 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1150 for (p = name; *p; p++)
1151 if (*p < '0' || *p > '9')
1155 #define VAL() ((*p++) - '0')
1157 t.tm_year = VAL() + t.tm_year * 10;
1158 t.tm_year = VAL() + t.tm_year * 10;
1159 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1161 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1163 t.tm_mday = VAL() + t.tm_mday * 10;
1165 t.tm_hour = VAL() + t.tm_hour * 10;
1167 t.tm_min = VAL() + t.tm_min * 10;
1169 t.tm_sec = VAL() + t.tm_sec * 10;
1174 warnx("date/time out of range");
1180 CMDFUNCSTART(chbtime)
1185 if (dotime(argv[1], &secs, &nsecs))
1187 if (sblock.fs_magic == FS_UFS1_MAGIC)
1189 curinode->dp2.di_birthtime = _time_to_time64(secs);
1190 curinode->dp2.di_birthnsec = nsecs;
1196 CMDFUNCSTART(chmtime)
1201 if (dotime(argv[1], &secs, &nsecs))
1203 if (sblock.fs_magic == FS_UFS1_MAGIC)
1204 curinode->dp1.di_mtime = _time_to_time32(secs);
1206 curinode->dp2.di_mtime = _time_to_time64(secs);
1207 DIP_SET(curinode, di_mtimensec, nsecs);
1213 CMDFUNCSTART(chatime)
1218 if (dotime(argv[1], &secs, &nsecs))
1220 if (sblock.fs_magic == FS_UFS1_MAGIC)
1221 curinode->dp1.di_atime = _time_to_time32(secs);
1223 curinode->dp2.di_atime = _time_to_time64(secs);
1224 DIP_SET(curinode, di_atimensec, nsecs);
1230 CMDFUNCSTART(chctime)
1235 if (dotime(argv[1], &secs, &nsecs))
1237 if (sblock.fs_magic == FS_UFS1_MAGIC)
1238 curinode->dp1.di_ctime = _time_to_time32(secs);
1240 curinode->dp2.di_ctime = _time_to_time64(secs);
1241 DIP_SET(curinode, di_ctimensec, nsecs);