1 /* $NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 thorpej Exp $ */
4 * Copyright (c) 1995 John T. Kohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sbin/fsdb/fsdb.c,v 1.13.2.3 2002/03/20 13:39:02 joerg Exp $
33 #include <sys/param.h>
42 #include <vfs/ufs/dinode.h>
43 #include <vfs/ufs/dir.h>
44 #include <vfs/ufs/fs.h>
49 static void usage(void);
55 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
59 int returntosingle = 0;
62 * We suck in lots of fsck code, and just pick & choose the stuff we want.
64 * fsreadfd is set up to read from the file system, fswritefd to write to
68 main(int argc, char **argv)
73 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
76 /* The -f option is left for historical
77 * reasons and has no meaning.
84 nflag++; /* "no" in fsck, readonly for us */
98 errx(1, "cannot set up file system `%s'", fsys);
99 printf("%s file system `%s'\nLast Mounted on %s\n",
100 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
103 sblock.fs_clean = 0; /* mark it dirty */
106 printf("*** FILE SYSTEM MARKED DIRTY\n");
107 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
108 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
113 #define CMDFUNC(func) int func (int argc, char **argv)
114 #define CMDFUNCSTART(func) int func(int argc, char **argv)
117 CMDFUNC(focus); /* focus on inode */
118 CMDFUNC(active); /* print active inode */
119 CMDFUNC(blocks); /* print blocks for active inode */
120 CMDFUNC(focusname); /* focus by name */
121 CMDFUNC(zapi); /* clear inode */
122 CMDFUNC(uplink); /* incr link */
123 CMDFUNC(downlink); /* decr link */
124 CMDFUNC(linkcount); /* set link count */
125 CMDFUNC(quit); /* quit */
126 CMDFUNC(ls); /* list directory */
127 CMDFUNC(rm); /* remove name */
128 CMDFUNC(ln); /* add name */
129 CMDFUNC(newtype); /* change type */
130 CMDFUNC(chmode); /* change mode */
131 CMDFUNC(chlen); /* change length */
132 CMDFUNC(chaflags); /* change flags */
133 CMDFUNC(chgen); /* change generation */
134 CMDFUNC(chowner); /* change owner */
135 CMDFUNC(chgroup); /* Change group */
136 CMDFUNC(back); /* pop back to last ino */
137 CMDFUNC(chmtime); /* Change mtime */
138 CMDFUNC(chctime); /* Change ctime */
139 CMDFUNC(chatime); /* Change atime */
140 CMDFUNC(chinum); /* Change inode # of dirent */
141 CMDFUNC(chname); /* Change dirname of dirent */
143 struct cmdtable cmds[] = {
144 { "help", "Print out help", 1, 1, FL_RO, helpfn },
145 { "?", "Print out help", 1, 1, FL_RO, helpfn },
146 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
147 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
148 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
149 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
150 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
151 { "active", "Print active inode", 1, 1, FL_RO, active },
152 { "print", "Print active inode", 1, 1, FL_RO, active },
153 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
154 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
155 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
156 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
157 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
158 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
159 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
160 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln },
161 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
162 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname },
163 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
164 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
165 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
166 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
167 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
168 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
169 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
170 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
171 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
172 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
173 { "quit", "Exit", 1, 1, FL_RO, quit },
174 { "q", "Exit", 1, 1, FL_RO, quit },
175 { "exit", "Exit", 1, 1, FL_RO, quit },
180 helpfn(int argc, char **argv)
182 struct cmdtable *cmdtp;
184 printf("Commands are:\n%-10s %5s %5s %s\n",
185 "command", "min argc", "max argc", "what");
187 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
188 printf("%-10s %5u %5u %s\n",
189 cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
196 static char pstring[64];
197 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
208 int cmd_argc, rval = 0, known;
209 #define scratch known
211 struct cmdtable *cmdp;
216 curinode = ginode(UFS_ROOTINO);
217 curinum = UFS_ROOTINO;
220 hist = history_init();
221 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
223 elptr = el_init("fsdb", stdin, stdout, stderr);
224 el_set(elptr, EL_EDITOR, "emacs");
225 el_set(elptr, EL_PROMPT, prompt);
226 el_set(elptr, EL_HIST, history, hist);
227 el_source(elptr, NULL);
229 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
231 printf("command `%s'\n", elline);
233 history(hist, &he, H_ENTER, elline);
235 line = strdup(elline);
236 cmd_argv = crack(line, &cmd_argc);
238 * el_parse returns -1 to signal that it's not been handled
241 if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
245 for (cmdp = cmds; cmdp->cmd; cmdp++) {
246 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
247 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
248 warnx("`%s' requires write access", cmd_argv[0]),
250 else if (cmd_argc >= cmdp->minargc &&
251 cmd_argc <= cmdp->maxargc)
252 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
254 rval = argcount(cmdp, cmd_argc, cmd_argv);
260 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
267 warnx("rval was %d", rval);
274 struct ufs1_dinode *curinode;
275 ino_t curinum, ocurrent;
277 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
278 if (inum < UFS_ROOTINO || inum > maxino || \
279 cp == argv[ac] || *cp != '\0' ) { \
280 printf("inode %ju out of range; range is [%ju,%ju]\n", \
281 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino); \
286 * Focus on given inode number
294 curinode = ginode(inum);
304 curinode = ginode(curinum);
312 struct ufs1_dinode *dp;
319 if (curinode) /* re-set after potential change */
320 curinode = ginode(curinum);
345 printf("inode %ju link count now %d\n", (uintmax_t)curinum,
346 ++curinode->di_nlink);
351 CMDFUNCSTART(downlink)
355 printf("inode %ju link count now %d\n", (uintmax_t)curinum,
356 --curinode->di_nlink);
361 const char *typename[] = {
382 scannames(struct inodesc *idesc)
384 struct direct *dirp = idesc->id_dirp;
386 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
387 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
388 dirp->d_namlen, dirp->d_name);
394 struct inodesc idesc;
395 checkactivedir(); /* let it go on anyway */
398 idesc.id_number = curinum;
399 idesc.id_func = scannames;
400 idesc.id_type = DATA;
401 idesc.id_fix = IGNORE;
402 ckinode(curinode, &idesc);
403 curinode = ginode(curinum);
408 int findino(struct inodesc *idesc); /* from fsck */
409 static int dolookup(char *name);
414 struct inodesc idesc;
416 if (!checkactivedir())
418 idesc.id_number = curinum;
419 idesc.id_func = findino;
420 idesc.id_name = name;
421 idesc.id_type = DATA;
422 idesc.id_fix = IGNORE;
423 if (ckinode(curinode, &idesc) & FOUND) {
424 curinum = idesc.id_parent;
425 curinode = ginode(curinum);
429 warnx("name `%s' not found in current inode directory", name);
434 CMDFUNCSTART(focusname)
443 if (argv[1][0] == '/') {
444 curinum = UFS_ROOTINO;
445 curinode = ginode(UFS_ROOTINO);
447 if (!checkactivedir())
450 for (p = argv[1]; p != NULL;) {
451 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
453 printf("component `%s': ", val);
455 if (!dolookup(val)) {
456 curinode = ginode(curinum);
472 if (!checkactivedir())
474 rval = makeentry(curinum, inum, argv[2]);
476 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
478 printf("could not enter name? weird.\n");
479 curinode = ginode(curinum);
487 if (!checkactivedir())
489 rval = changeino(curinum, argv[1], 0);
490 if (rval & ALTERED) {
491 printf("Name `%s' removed\n", argv[1]);
494 printf("could not remove name? weird.\n");
499 long slotcount, desired;
502 chinumfunc(struct inodesc *idesc)
504 struct direct *dirp = idesc->id_dirp;
506 if (slotcount++ == desired) {
507 dirp->d_ino = idesc->id_parent;
508 return STOP|ALTERED|FOUND;
517 struct inodesc idesc;
520 if (!checkactivedir())
524 desired = strtol(argv[1], &cp, 0);
525 if (cp == argv[1] || *cp != '\0' || desired < 0) {
526 printf("invalid slot number `%s'\n", argv[1]);
530 idesc.id_number = curinum;
531 idesc.id_func = chinumfunc;
532 idesc.id_fix = IGNORE;
533 idesc.id_type = DATA;
534 idesc.id_parent = inum; /* XXX convenient hiding place */
536 if (ckinode(curinode, &idesc) & FOUND)
539 warnx("no %sth slot in current directory", argv[1]);
545 chnamefunc(struct inodesc *idesc)
547 struct direct *dirp = idesc->id_dirp;
548 struct direct testdir;
550 if (slotcount++ == desired) {
552 testdir.d_namlen = strlen(idesc->id_name);
553 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
554 dirp->d_namlen = testdir.d_namlen;
555 strcpy(dirp->d_name, idesc->id_name);
556 return STOP|ALTERED|FOUND;
558 return STOP|FOUND; /* won't fit, so give up */
567 struct inodesc idesc;
570 if (!checkactivedir())
573 desired = strtoul(argv[1], &cp, 0);
574 if (cp == argv[1] || *cp != '\0') {
575 printf("invalid slot number `%s'\n", argv[1]);
579 idesc.id_number = curinum;
580 idesc.id_func = chnamefunc;
581 idesc.id_fix = IGNORE;
582 idesc.id_type = DATA;
583 idesc.id_name = argv[2];
585 rval = ckinode(curinode, &idesc);
586 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
588 else if (rval & FOUND) {
589 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
592 warnx("no %sth slot in current directory", argv[1]);
598 const char *typename;
607 CMDFUNCSTART(newtype)
614 type = curinode->di_mode & IFMT;
615 for (tp = typenamemap; tp < &typenamemap[NELEM(typenamemap)]; tp++) {
616 if (!strcmp(argv[1], tp->typename)) {
617 printf("setting type to %s\n", tp->typename);
622 if (tp == &typenamemap[NELEM(typenamemap)]) {
623 warnx("type `%s' not known", argv[1]);
624 warnx("try one of `file', `dir', `socket', `fifo'");
627 curinode->di_mode &= ~IFMT;
628 curinode->di_mode |= type;
643 len = strtol(argv[1], &cp, 0);
644 if (cp == argv[1] || *cp != '\0' || len < 0) {
645 warnx("bad length `%s'", argv[1]);
649 curinode->di_size = len;
664 modebits = strtol(argv[1], &cp, 8);
665 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
666 warnx("bad modebits `%s'", argv[1]);
670 curinode->di_mode &= ~07777;
671 curinode->di_mode |= modebits;
677 CMDFUNCSTART(chaflags)
686 flags = strtoul(argv[1], &cp, 0);
687 if (cp == argv[1] || *cp != '\0' ) {
688 warnx("bad flags `%s'", argv[1]);
692 if (flags > UINT_MAX) {
693 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
696 curinode->di_flags = flags;
711 gen = strtol(argv[1], &cp, 0);
712 if (cp == argv[1] || *cp != '\0' ) {
713 warnx("bad gen `%s'", argv[1]);
717 if (gen > INT_MAX || gen < INT_MIN) {
718 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
721 curinode->di_gen = gen;
727 CMDFUNCSTART(linkcount)
736 lcnt = strtol(argv[1], &cp, 0);
737 if (cp == argv[1] || *cp != '\0' ) {
738 warnx("bad link count `%s'", argv[1]);
741 if (lcnt > USHRT_MAX || lcnt < 0) {
742 warnx("max link count is %d\n", USHRT_MAX);
746 curinode->di_nlink = lcnt;
752 CMDFUNCSTART(chowner)
762 uid = strtoul(argv[1], &cp, 0);
763 if (cp == argv[1] || *cp != '\0' ) {
764 /* try looking up name */
765 if ((pwd = getpwnam(argv[1]))) {
768 warnx("bad uid `%s'", argv[1]);
773 curinode->di_uid = uid;
779 CMDFUNCSTART(chgroup)
789 gid = strtoul(argv[1], &cp, 0);
790 if (cp == argv[1] || *cp != '\0' ) {
791 if ((grp = getgrnam(argv[1]))) {
794 warnx("bad gid `%s'", argv[1]);
799 curinode->di_gid = gid;
806 dotime(char *name, int32_t *rts)
812 p = strchr(name, '.');
815 nsec = strtoul(++p, &val, 0);
816 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
817 warnx("invalid nanoseconds");
822 if (strlen(name) != 14) {
824 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
828 for (p = name; *p; p++)
829 if (*p < '0' || *p > '9')
833 #define VAL() ((*p++) - '0')
835 t.tm_year = VAL() + t.tm_year * 10;
836 t.tm_year = VAL() + t.tm_year * 10;
837 t.tm_year = VAL() + t.tm_year * 10 - 1900;
839 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
841 t.tm_mday = VAL() + t.tm_mday * 10;
843 t.tm_hour = VAL() + t.tm_hour * 10;
845 t.tm_min = VAL() + t.tm_min * 10;
847 t.tm_sec = VAL() + t.tm_sec * 10;
852 warnx("date/time out of range");
860 CMDFUNCSTART(chmtime)
862 if (dotime(argv[1], &curinode->di_mtime))
869 CMDFUNCSTART(chatime)
871 if (dotime(argv[1], &curinode->di_atime))
878 CMDFUNCSTART(chctime)
880 if (dotime(argv[1], &curinode->di_ctime))