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.
33 #include <sys/param.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
51 static void usage(void) __dead2;
53 static int compare_blk32(uint32_t *wantedblk, uint32_t curblk);
54 static int compare_blk64(uint64_t *wantedblk, uint64_t curblk);
55 static int founddatablk(uint64_t blk);
56 static int find_blks32(uint32_t *buf, int size, uint32_t *blknum);
57 static int find_blks64(uint64_t *buf, int size, uint64_t *blknum);
58 static int find_indirblks32(uint32_t blk, int ind_level, uint32_t *blknum);
59 static int find_indirblks64(uint64_t blk, int ind_level, uint64_t *blknum);
62 * Track modifications to the filesystem. Two types of changes are tracked.
63 * The first type of changes are those that are not critical to the integrity
64 * of the filesystem such as owner, group, time stamps, access mode, and
65 * generation number. The second type of changes are those that do affect
66 * the integrity of the filesystem including zeroing inodes, changing block
67 * pointers, directory entries, link counts, file lengths, file types and
70 * When quitting having made no changes or only changes to data that is not
71 * critical to filesystem integrity, the clean state of the filesystem is
72 * left unchanged. But if filesystem critical data are changed then fsdb
73 * will set the unclean flag which will require a full fsck to be run
74 * before the filesystem can be mounted.
76 static int fsnoncritmodified; /* filesystem non-critical modifications */
77 static int fscritmodified; /* filesystem integrity critical mods */
79 union dinode *curinode;
80 ino_t curinum, ocurrent;
85 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
90 * We suck in lots of fsck code, and just pick & choose the stuff we want.
92 * fsreadfd is set up to read from the file system, fswritefd to write to
96 main(int argc, char *argv[])
101 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
104 /* The -f option is left for historical
105 * reasons and has no meaning.
112 nflag++; /* "no" in fsck, readonly for us */
126 if (openfilesys(fsys) == 0 || readsb() == 0 || setup(fsys) == 0)
127 errx(1, "cannot set up file system `%s'", fsys);
130 printf("%s file system `%s'\nLast Mounted on %s\n",
131 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
134 if (fscritmodified != 0) {
135 sblock.fs_clean = 0; /* mark it dirty */
138 ckfini(fscritmodified ? 0 : sblock.fs_clean);
139 if (fscritmodified == 0)
141 printf("*** FILE SYSTEM MARKED DIRTY\n");
142 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
143 printf("*** IF IT IS MOUNTED, RE-MOUNT WITH -u -o reload\n");
148 #define CMDFUNC(func) int func(int argc, char *argv[])
149 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
152 CMDFUNC(focus); /* focus on inode */
153 CMDFUNC(active); /* print active inode */
154 CMDFUNC(blocks); /* print blocks for active inode */
155 CMDFUNC(focusname); /* focus by name */
156 CMDFUNC(zapi); /* clear inode */
157 CMDFUNC(uplink); /* incr link */
158 CMDFUNC(downlink); /* decr link */
159 CMDFUNC(linkcount); /* set link count */
160 CMDFUNC(quit); /* quit */
161 CMDFUNC(quitclean); /* quit with filesystem marked clean */
162 CMDFUNC(findblk); /* find block */
163 CMDFUNC(ls); /* list directory */
164 CMDFUNC(rm); /* remove name */
165 CMDFUNC(ln); /* add name */
166 CMDFUNC(newtype); /* change type */
167 CMDFUNC(chmode); /* change mode */
168 CMDFUNC(chlen); /* change length */
169 CMDFUNC(chaflags); /* change flags */
170 CMDFUNC(chgen); /* change generation */
171 CMDFUNC(chowner); /* change owner */
172 CMDFUNC(chgroup); /* Change group */
173 CMDFUNC(back); /* pop back to last ino */
174 CMDFUNC(chbtime); /* Change btime */
175 CMDFUNC(chmtime); /* Change mtime */
176 CMDFUNC(chctime); /* Change ctime */
177 CMDFUNC(chatime); /* Change atime */
178 CMDFUNC(chinum); /* Change inode # of dirent */
179 CMDFUNC(chname); /* Change dirname of dirent */
180 CMDFUNC(chsize); /* Change size */
181 CMDFUNC(chdb); /* Change direct block pointer */
183 struct cmdtable cmds[] = {
184 { "help", "Print out help", 1, 1, FL_RO, helpfn },
185 { "?", "Print out help", 1, 1, FL_RO, helpfn },
186 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
187 { "clri", "Clear inode INUM", 2, 2, FL_CWR, zapi },
188 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
189 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
190 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
191 { "active", "Print active inode", 1, 1, FL_RO, active },
192 { "print", "Print active inode", 1, 1, FL_RO, active },
193 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
194 { "uplink", "Increment link count", 1, 1, FL_CWR, uplink },
195 { "downlink", "Decrement link count", 1, 1, FL_CWR, downlink },
196 { "linkcount", "Set link count to COUNT", 2, 2, FL_CWR, linkcount },
197 { "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
198 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
199 { "rm", "Remove NAME from current inode directory", 2, 2, FL_CWR | FL_ST, rm },
200 { "del", "Remove NAME from current inode directory", 2, 2, FL_CWR | FL_ST, rm },
201 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_CWR | FL_ST, ln },
202 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_CWR, chinum },
203 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
204 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_CWR, newtype },
205 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
206 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
207 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
208 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_CWR, chaflags },
209 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
210 { "chsize", "Change size of current inode to SIZE", 2, 2, FL_CWR, chsize },
211 { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
212 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
213 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
214 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
215 { "chdb", "Change db pointer N of current inode to BLKNO", 3, 3, FL_CWR, chdb },
216 { "quitclean", "Exit with filesystem marked clean", 1, 1, FL_RO, quitclean },
217 { "quit", "Exit", 1, 1, FL_RO, quit },
218 { "q", "Exit", 1, 1, FL_RO, quit },
219 { "exit", "Exit", 1, 1, FL_RO, quit },
220 { NULL, 0, 0, 0, 0, NULL },
224 helpfn(int argc, char *argv[])
226 struct cmdtable *cmdtp;
228 printf("Commands are:\n%-10s %5s %5s %s\n",
229 "command", "min args", "max args", "what");
231 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
232 printf("%-10s %5u %5u %s\n",
233 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
240 static char pstring[64];
241 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
247 setcurinode(ino_t inum)
250 if (curip.i_number != 0)
252 ginode(inum, &curip);
253 curinode = curip.i_dp;
262 int cmd_argc, rval = 0, known;
263 #define scratch known
265 struct cmdtable *cmdp;
270 setcurinode(UFS_ROOTINO);
273 hist = history_init();
274 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
276 elptr = el_init("fsdb", stdin, stdout, stderr);
277 el_set(elptr, EL_EDITOR, "emacs");
278 el_set(elptr, EL_PROMPT, prompt);
279 el_set(elptr, EL_HIST, history, hist);
280 el_source(elptr, NULL);
282 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
284 printf("command `%s'\n", elline);
286 history(hist, &he, H_ENTER, elline);
288 line = strdup(elline);
289 cmd_argv = crack(line, &cmd_argc);
291 * el_parse returns -1 to signal that it's not been handled
294 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
298 for (cmdp = cmds; cmdp->cmd; cmdp++) {
299 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
300 if ((cmdp->flags & (FL_CWR | FL_WR)) != 0 && nflag)
301 warnx("`%s' requires write access", cmd_argv[0]),
303 else if (cmd_argc >= cmdp->minargc &&
304 cmd_argc <= cmdp->maxargc)
305 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
306 else if (cmd_argc >= cmdp->minargc &&
307 (cmdp->flags & FL_ST) == FL_ST) {
308 strcpy(line, elline);
309 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
310 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
312 rval = argcount(cmdp, cmd_argc, cmd_argv);
315 if ((cmdp->flags & FL_WR) != 0)
316 fsnoncritmodified = 1;
317 if ((cmdp->flags & FL_CWR) != 0)
324 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
329 /* user typed "quit" */
334 warnx("command failed, return value was %d", rval);
342 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
343 if (inum < UFS_ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
344 printf("inode %ju out of range; range is [%ju,%ju]\n", \
345 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino);\
350 * Focus on given inode number
366 setcurinode(ocurrent);
402 CMDFUNCSTART(quitclean)
404 if (fscritmodified) {
405 printf("Warning: modified filesystem marked clean\n");
416 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
417 printf("inode %ju link count now %d\n",
418 (uintmax_t)curinum, DIP(curinode, di_nlink));
423 CMDFUNCSTART(downlink)
427 DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
428 printf("inode %ju link count now %d\n",
429 (uintmax_t)curinum, DIP(curinode, di_nlink));
434 const char *typename[] = {
456 scannames(struct inodesc *idesc)
458 struct direct *dirp = idesc->id_dirp;
460 printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
461 slot++, diroff, dirp->d_ino, dirp->d_reclen,
462 typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
463 diroff += dirp->d_reclen;
469 struct inodesc idesc;
470 checkactivedir(); /* let it go on anyway */
474 idesc.id_number = curinum;
475 idesc.id_func = scannames;
476 idesc.id_type = DATA;
477 idesc.id_fix = IGNORE;
478 ckinode(curinode, &idesc);
483 static int findblk_numtofind;
484 static int wantedblksize;
486 CMDFUNCSTART(findblk)
488 ino_t inum, inosused;
489 uint32_t *wantedblk32;
490 uint64_t *wantedblk64;
491 struct bufarea *cgbp;
495 wantedblksize = (argc - 1);
496 is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
500 wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
501 if (wantedblk64 == NULL)
503 for (i = 1; i < argc; i++)
504 wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
506 wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
507 if (wantedblk32 == NULL)
509 for (i = 1; i < argc; i++)
510 wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
512 findblk_numtofind = wantedblksize;
514 * sblock.fs_ncg holds a number of cylinder groups.
515 * Iterate over all cylinder groups.
517 for (c = 0; c < sblock.fs_ncg; c++) {
519 * sblock.fs_ipg holds a number of inodes per cylinder group.
520 * Calculate a highest inode number for a given cylinder group.
522 inum = c * sblock.fs_ipg;
523 /* Read cylinder group. */
525 cgp = cgbp->b_un.b_cg;
527 * Get a highest used inode number for a given cylinder group.
528 * For UFS1 all inodes initialized at the newfs stage.
531 inosused = cgp->cg_initediblk;
533 inosused = sblock.fs_ipg;
535 for (; inosused > 0; inum++, inosused--) {
536 /* Skip magic inodes: 0, UFS_WINO, UFS_ROOTINO. */
537 if (inum < UFS_ROOTINO)
540 * Check if the block we are looking for is just an inode block.
542 * ino_to_fsba() - get block containing inode from its number.
543 * INOPB() - get a number of inodes in one disk block.
546 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
547 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
548 printf("block %llu: inode block (%ju-%ju)\n",
549 (unsigned long long)fsbtodb(&sblock,
550 ino_to_fsba(&sblock, inum)),
551 (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
552 (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
554 if (findblk_numtofind == 0)
557 /* Get on-disk inode aka dinode. */
559 /* Find IFLNK dinode with allocated data blocks. */
560 switch (DIP(curinode, di_mode) & IFMT) {
563 if (DIP(curinode, di_blocks) == 0)
568 uint64_t size = DIP(curinode, di_size);
569 if (size > 0 && size < sblock.fs_maxsymlinklen &&
570 DIP(curinode, di_blocks) == 0)
578 /* Look through direct data blocks. */
580 find_blks64(curinode->dp2.di_db, UFS_NDADDR, wantedblk64) :
581 find_blks32(curinode->dp1.di_db, UFS_NDADDR, wantedblk32))
583 for (i = 0; i < UFS_NIADDR; i++) {
585 * Does the block we are looking for belongs to the
589 compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
590 compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
591 if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
592 curinode->dp1.di_ib[i]))
595 * Search through indirect, double and triple indirect
598 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
599 (curinode->dp1.di_ib[i] != 0))
601 find_indirblks64(curinode->dp2.di_ib[i], i,
603 find_indirblks32(curinode->dp1.di_ib[i], i,
610 setcurinode(ocurrent);
619 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
623 for (i = 0; i < wantedblksize; i++) {
624 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
633 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
637 for (i = 0; i < wantedblksize; i++) {
638 if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
647 founddatablk(uint64_t blk)
650 printf("%llu: data block of inode %ju\n",
651 (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
653 if (findblk_numtofind == 0)
659 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
662 for (blk = 0; blk < size; blk++) {
665 if (compare_blk32(wantedblk, buf[blk])) {
666 if (founddatablk(buf[blk]))
674 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
676 #define MAXNINDIR (MAXBSIZE / sizeof(uint32_t))
677 uint32_t idblk[MAXNINDIR];
680 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
681 if (ind_level <= 0) {
682 if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
686 for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
687 if (compare_blk32(wantedblk, idblk[i])) {
688 if (founddatablk(idblk[i]))
692 if (find_indirblks32(idblk[i], ind_level, wantedblk))
701 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
704 for (blk = 0; blk < size; blk++) {
707 if (compare_blk64(wantedblk, buf[blk])) {
708 if (founddatablk(buf[blk]))
716 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
718 #define MAXNINDIR (MAXBSIZE / sizeof(uint64_t))
719 uint64_t idblk[MAXNINDIR];
722 blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
723 if (ind_level <= 0) {
724 if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
728 for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
729 if (compare_blk64(wantedblk, idblk[i])) {
730 if (founddatablk(idblk[i]))
734 if (find_indirblks64(idblk[i], ind_level, wantedblk))
742 int findino(struct inodesc *idesc); /* from fsck */
743 static int dolookup(char *name);
748 struct inodesc idesc;
750 if (!checkactivedir())
752 idesc.id_number = curinum;
753 idesc.id_func = findino;
754 idesc.id_name = name;
755 idesc.id_type = DATA;
756 idesc.id_fix = IGNORE;
757 if (ckinode(curinode, &idesc) & FOUND) {
758 setcurinode(idesc.id_parent);
762 warnx("name `%s' not found in current inode directory", name);
767 CMDFUNCSTART(focusname)
776 if (argv[1][0] == '/') {
777 setcurinode(UFS_ROOTINO);
779 if (!checkactivedir())
782 for (p = argv[1]; p != NULL;) {
783 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
785 printf("component `%s': ", val);
787 if (!dolookup(val)) {
803 if (!checkactivedir())
805 rval = makeentry(curinum, inum, argv[2]);
807 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
809 printf("could not enter name? weird.\n");
817 if (!checkactivedir())
819 rval = changeino(curinum, argv[1], 0, 0);
820 if (rval & ALTERED) {
821 printf("Name `%s' removed\n", argv[1]);
824 printf("could not remove name ('%s')? weird.\n", argv[1]);
829 long slotcount, desired;
832 chinumfunc(struct inodesc *idesc)
834 struct direct *dirp = idesc->id_dirp;
836 if (slotcount++ == desired) {
837 dirp->d_ino = idesc->id_parent;
838 return STOP|ALTERED|FOUND;
847 struct inodesc idesc;
850 if (!checkactivedir())
854 desired = strtol(argv[1], &cp, 0);
855 if (cp == argv[1] || *cp != '\0' || desired < 0) {
856 printf("invalid slot number `%s'\n", argv[1]);
860 idesc.id_number = curinum;
861 idesc.id_func = chinumfunc;
862 idesc.id_fix = IGNORE;
863 idesc.id_type = DATA;
864 idesc.id_parent = inum; /* XXX convenient hiding place */
866 if (ckinode(curinode, &idesc) & FOUND)
869 warnx("no %sth slot in current directory", argv[1]);
875 chnamefunc(struct inodesc *idesc)
877 struct direct *dirp = idesc->id_dirp;
878 struct direct testdir;
880 if (slotcount++ == desired) {
882 testdir.d_namlen = strlen(idesc->id_name);
883 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
884 dirp->d_namlen = testdir.d_namlen;
885 strcpy(dirp->d_name, idesc->id_name);
886 return STOP|ALTERED|FOUND;
888 return STOP|FOUND; /* won't fit, so give up */
897 struct inodesc idesc;
900 if (!checkactivedir())
903 desired = strtoul(argv[1], &cp, 0);
904 if (cp == argv[1] || *cp != '\0') {
905 printf("invalid slot number `%s'\n", argv[1]);
909 idesc.id_number = curinum;
910 idesc.id_func = chnamefunc;
911 idesc.id_fix = IGNORE;
912 idesc.id_type = DATA;
913 idesc.id_name = argv[2];
915 rval = ckinode(curinode, &idesc);
916 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
918 else if (rval & FOUND) {
919 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
922 warnx("no %sth slot in current directory", argv[1]);
928 const char *typename;
937 CMDFUNCSTART(newtype)
944 type = DIP(curinode, di_mode) & IFMT;
945 for (tp = typenamemap;
946 tp < &typenamemap[nitems(typenamemap)];
948 if (!strcmp(argv[1], tp->typename)) {
949 printf("setting type to %s\n", tp->typename);
954 if (tp == &typenamemap[nitems(typenamemap)]) {
955 warnx("type `%s' not known", argv[1]);
956 warnx("try one of `file', `dir', `socket', `fifo'");
959 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
960 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
974 modebits = strtol(argv[1], &cp, 8);
975 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
976 warnx("bad modebits `%s'", argv[1]);
980 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
981 DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
987 CMDFUNCSTART(chaflags)
995 flags = strtoul(argv[1], &cp, 0);
996 if (cp == argv[1] || *cp != '\0' ) {
997 warnx("bad flags `%s'", argv[1]);
1001 if (flags > UINT_MAX) {
1002 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
1005 DIP_SET(curinode, di_flags, flags);
1019 gen = strtol(argv[1], &cp, 0);
1020 if (cp == argv[1] || *cp != '\0' ) {
1021 warnx("bad gen `%s'", argv[1]);
1025 if (gen > UINT_MAX) {
1026 warnx("gen set beyond 32-bit range of field (0x%lx), max is 0x%x\n",
1030 DIP_SET(curinode, di_gen, gen);
1036 CMDFUNCSTART(chsize)
1044 size = strtoll(argv[1], &cp, 0);
1045 if (cp == argv[1] || *cp != '\0') {
1046 warnx("bad size `%s'", argv[1]);
1051 warnx("size set to negative (%jd)\n", (intmax_t)size);
1054 DIP_SET(curinode, di_size, size);
1069 idx = strtoull(argv[1], &cp, 0);
1070 if (cp == argv[1] || *cp != '\0') {
1071 warnx("bad pointer idx `%s'", argv[1]);
1074 bno = strtoll(argv[2], &cp, 0);
1075 if (cp == argv[2] || *cp != '\0') {
1076 warnx("bad block number `%s'", argv[2]);
1079 if (idx >= UFS_NDADDR) {
1080 warnx("pointer index %d is out of range", idx);
1084 DIP_SET(curinode, di_db[idx], bno);
1090 CMDFUNCSTART(linkcount)
1098 lcnt = strtol(argv[1], &cp, 0);
1099 if (cp == argv[1] || *cp != '\0' ) {
1100 warnx("bad link count `%s'", argv[1]);
1103 if (lcnt > USHRT_MAX || lcnt < 0) {
1104 warnx("max link count is %d\n", USHRT_MAX);
1108 DIP_SET(curinode, di_nlink, lcnt);
1114 CMDFUNCSTART(chowner)
1123 uid = strtoul(argv[1], &cp, 0);
1124 if (cp == argv[1] || *cp != '\0' ) {
1125 /* try looking up name */
1126 if ((pwd = getpwnam(argv[1]))) {
1129 warnx("bad uid `%s'", argv[1]);
1134 DIP_SET(curinode, di_uid, uid);
1140 CMDFUNCSTART(chgroup)
1149 gid = strtoul(argv[1], &cp, 0);
1150 if (cp == argv[1] || *cp != '\0' ) {
1151 if ((grp = getgrnam(argv[1]))) {
1154 warnx("bad gid `%s'", argv[1]);
1159 DIP_SET(curinode, di_gid, gid);
1166 dotime(char *name, time_t *secp, int32_t *nsecp)
1171 p = strchr(name, '.');
1174 nsec = strtoul(++p, &val, 0);
1175 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1176 warnx("invalid nanoseconds");
1181 if (strlen(name) != 14) {
1183 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1188 for (p = name; *p; p++)
1189 if (*p < '0' || *p > '9')
1193 #define VAL() ((*p++) - '0')
1195 t.tm_year = VAL() + t.tm_year * 10;
1196 t.tm_year = VAL() + t.tm_year * 10;
1197 t.tm_year = VAL() + t.tm_year * 10 - 1900;
1199 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1201 t.tm_mday = VAL() + t.tm_mday * 10;
1203 t.tm_hour = VAL() + t.tm_hour * 10;
1205 t.tm_min = VAL() + t.tm_min * 10;
1207 t.tm_sec = VAL() + t.tm_sec * 10;
1212 warnx("date/time out of range");
1218 CMDFUNCSTART(chbtime)
1223 if (dotime(argv[1], &secs, &nsecs))
1225 if (sblock.fs_magic == FS_UFS1_MAGIC)
1227 curinode->dp2.di_birthtime = _time_to_time64(secs);
1228 curinode->dp2.di_birthnsec = nsecs;
1234 CMDFUNCSTART(chmtime)
1239 if (dotime(argv[1], &secs, &nsecs))
1241 if (sblock.fs_magic == FS_UFS1_MAGIC)
1242 curinode->dp1.di_mtime = _time_to_time32(secs);
1244 curinode->dp2.di_mtime = _time_to_time64(secs);
1245 DIP_SET(curinode, di_mtimensec, nsecs);
1251 CMDFUNCSTART(chatime)
1256 if (dotime(argv[1], &secs, &nsecs))
1258 if (sblock.fs_magic == FS_UFS1_MAGIC)
1259 curinode->dp1.di_atime = _time_to_time32(secs);
1261 curinode->dp2.di_atime = _time_to_time64(secs);
1262 DIP_SET(curinode, di_atimensec, nsecs);
1268 CMDFUNCSTART(chctime)
1273 if (dotime(argv[1], &secs, &nsecs))
1275 if (sblock.fs_magic == FS_UFS1_MAGIC)
1276 curinode->dp1.di_ctime = _time_to_time32(secs);
1278 curinode->dp2.di_ctime = _time_to_time64(secs);
1279 DIP_SET(curinode, di_ctimensec, nsecs);