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.
32 static const char rcsid[] =
36 #include <sys/param.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
52 static void usage(void) __dead2;
58 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
66 * We suck in lots of fsck code, and just pick & choose the stuff we want.
68 * fsreadfd is set up to read from the file system, fswritefd to write to
72 main(int argc, char *argv[])
77 while (-1 != (ch = getopt(argc, argv, "fdr"))) {
80 /* The -f option is left for historical
81 * reasons and has no meaning.
88 nflag++; /* "no" in fsck, readonly for us */
103 errx(1, "cannot set up file system `%s'", fsys);
104 printf("%s file system `%s'\nLast Mounted on %s\n",
105 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
108 sblock.fs_clean = 0; /* mark it dirty */
111 printf("*** FILE SYSTEM MARKED DIRTY\n");
112 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
113 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
118 #define CMDFUNC(func) int func(int argc, char *argv[])
119 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
122 CMDFUNC(focus); /* focus on inode */
123 CMDFUNC(active); /* print active inode */
124 CMDFUNC(blocks); /* print blocks for active inode */
125 CMDFUNC(focusname); /* focus by name */
126 CMDFUNC(zapi); /* clear inode */
127 CMDFUNC(uplink); /* incr link */
128 CMDFUNC(downlink); /* decr link */
129 CMDFUNC(linkcount); /* set link count */
130 CMDFUNC(quit); /* quit */
131 CMDFUNC(ls); /* list directory */
132 CMDFUNC(rm); /* remove name */
133 CMDFUNC(ln); /* add name */
134 CMDFUNC(newtype); /* change type */
135 CMDFUNC(chmode); /* change mode */
136 CMDFUNC(chlen); /* change length */
137 CMDFUNC(chaflags); /* change flags */
138 CMDFUNC(chgen); /* change generation */
139 CMDFUNC(chowner); /* change owner */
140 CMDFUNC(chgroup); /* Change group */
141 CMDFUNC(back); /* pop back to last ino */
142 CMDFUNC(chmtime); /* Change mtime */
143 CMDFUNC(chctime); /* Change ctime */
144 CMDFUNC(chatime); /* Change atime */
145 CMDFUNC(chinum); /* Change inode # of dirent */
146 CMDFUNC(chname); /* Change dirname of dirent */
148 struct cmdtable cmds[] = {
149 { "help", "Print out help", 1, 1, FL_RO, helpfn },
150 { "?", "Print out help", 1, 1, FL_RO, helpfn },
151 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
152 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
153 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
154 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
155 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
156 { "active", "Print active inode", 1, 1, FL_RO, active },
157 { "print", "Print active inode", 1, 1, FL_RO, active },
158 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
159 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
160 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
161 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
162 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
163 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
164 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
165 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln },
166 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
167 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname },
168 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
169 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
170 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
171 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
172 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
173 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
174 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
175 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
176 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
177 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
178 { "quit", "Exit", 1, 1, FL_RO, quit },
179 { "q", "Exit", 1, 1, FL_RO, quit },
180 { "exit", "Exit", 1, 1, FL_RO, quit },
185 helpfn(int argc, char *argv[])
187 struct cmdtable *cmdtp;
189 printf("Commands are:\n%-10s %5s %5s %s\n",
190 "command", "min argc", "max argc", "what");
192 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
193 printf("%-10s %5u %5u %s\n",
194 cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
201 static char pstring[64];
202 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
212 int cmd_argc, rval = 0, known;
213 #define scratch known
215 struct cmdtable *cmdp;
220 curinode = ginode(ROOTINO);
224 hist = history_init();
225 history(hist, &he, H_EVENT, 100); /* 100 elt history buffer */
227 elptr = el_init("fsdb", stdin, stdout, stderr);
228 el_set(elptr, EL_EDITOR, "emacs");
229 el_set(elptr, EL_PROMPT, prompt);
230 el_set(elptr, EL_HIST, history, hist);
231 el_source(elptr, NULL);
233 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
235 printf("command `%s'\n", elline);
237 history(hist, &he, H_ENTER, elline);
239 line = strdup(elline);
240 cmd_argv = crack(line, &cmd_argc);
242 * el_parse returns -1 to signal that it's not been handled
245 if (el_parse(elptr, cmd_argc, cmd_argv) != -1)
249 for (cmdp = cmds; cmdp->cmd; cmdp++) {
250 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
251 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
252 warnx("`%s' requires write access", cmd_argv[0]),
254 else if (cmd_argc >= cmdp->minargc &&
255 cmd_argc <= cmdp->maxargc)
256 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
257 else if (cmd_argc >= cmdp->minargc) {
258 strcpy(line, elline);
259 cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
260 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
262 rval = argcount(cmdp, cmd_argc, cmd_argv);
268 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
273 /* user typed "quit" */
276 warnx("rval was %d", rval);
283 struct dinode *curinode;
284 ino_t curinum, ocurrent;
286 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
287 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
288 printf("inode %d out of range; range is [%d,%d]\n", \
289 inum, ROOTINO, maxino); \
294 * Focus on given inode number
302 curinode = ginode(inum);
312 curinode = ginode(curinum);
327 if (curinode) /* re-set after potential change */
328 curinode = ginode(curinum);
353 printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink);
358 CMDFUNCSTART(downlink)
362 printf("inode %d link count now %d\n", curinum, --curinode->di_nlink);
367 const char *typename[] = {
388 scannames(struct inodesc *idesc)
390 struct direct *dirp = idesc->id_dirp;
392 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
393 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
394 dirp->d_namlen, dirp->d_name);
400 struct inodesc idesc;
401 checkactivedir(); /* let it go on anyway */
404 idesc.id_number = curinum;
405 idesc.id_func = scannames;
406 idesc.id_type = DATA;
407 idesc.id_fix = IGNORE;
408 ckinode(curinode, &idesc);
409 curinode = ginode(curinum);
414 int findino(struct inodesc *idesc); /* from fsck */
415 static int dolookup(char *name);
420 struct inodesc idesc;
422 if (!checkactivedir())
424 idesc.id_number = curinum;
425 idesc.id_func = findino;
426 idesc.id_name = name;
427 idesc.id_type = DATA;
428 idesc.id_fix = IGNORE;
429 if (ckinode(curinode, &idesc) & FOUND) {
430 curinum = idesc.id_parent;
431 curinode = ginode(curinum);
435 warnx("name `%s' not found in current inode directory", name);
440 CMDFUNCSTART(focusname)
449 if (argv[1][0] == '/') {
451 curinode = ginode(ROOTINO);
453 if (!checkactivedir())
456 for (p = argv[1]; p != NULL;) {
457 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
459 printf("component `%s': ", val);
461 if (!dolookup(val)) {
462 curinode = ginode(curinum);
478 if (!checkactivedir())
480 rval = makeentry(curinum, inum, argv[2]);
482 printf("Ino %d entered as `%s'\n", inum, argv[2]);
484 printf("could not enter name? weird.\n");
485 curinode = ginode(curinum);
493 if (!checkactivedir())
495 rval = changeino(curinum, argv[1], 0);
496 if (rval & ALTERED) {
497 printf("Name `%s' removed\n", argv[1]);
500 printf("could not remove name ('%s')? weird.\n", argv[1]);
505 long slotcount, desired;
508 chinumfunc(struct inodesc *idesc)
510 struct direct *dirp = idesc->id_dirp;
512 if (slotcount++ == desired) {
513 dirp->d_ino = idesc->id_parent;
514 return STOP|ALTERED|FOUND;
523 struct inodesc idesc;
526 if (!checkactivedir())
530 desired = strtol(argv[1], &cp, 0);
531 if (cp == argv[1] || *cp != '\0' || desired < 0) {
532 printf("invalid slot number `%s'\n", argv[1]);
536 idesc.id_number = curinum;
537 idesc.id_func = chinumfunc;
538 idesc.id_fix = IGNORE;
539 idesc.id_type = DATA;
540 idesc.id_parent = inum; /* XXX convenient hiding place */
542 if (ckinode(curinode, &idesc) & FOUND)
545 warnx("no %sth slot in current directory", argv[1]);
551 chnamefunc(struct inodesc *idesc)
553 struct direct *dirp = idesc->id_dirp;
554 struct direct testdir;
556 if (slotcount++ == desired) {
558 testdir.d_namlen = strlen(idesc->id_name);
559 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
560 dirp->d_namlen = testdir.d_namlen;
561 strcpy(dirp->d_name, idesc->id_name);
562 return STOP|ALTERED|FOUND;
564 return STOP|FOUND; /* won't fit, so give up */
573 struct inodesc idesc;
576 if (!checkactivedir())
579 desired = strtoul(argv[1], &cp, 0);
580 if (cp == argv[1] || *cp != '\0') {
581 printf("invalid slot number `%s'\n", argv[1]);
585 idesc.id_number = curinum;
586 idesc.id_func = chnamefunc;
587 idesc.id_fix = IGNORE;
588 idesc.id_type = DATA;
589 idesc.id_name = argv[2];
591 rval = ckinode(curinode, &idesc);
592 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
594 else if (rval & FOUND) {
595 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
598 warnx("no %sth slot in current directory", argv[1]);
604 const char *typename;
613 CMDFUNCSTART(newtype)
620 type = curinode->di_mode & IFMT;
621 for (tp = typenamemap;
622 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
624 if (!strcmp(argv[1], tp->typename)) {
625 printf("setting type to %s\n", tp->typename);
630 if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
631 warnx("type `%s' not known", argv[1]);
632 warnx("try one of `file', `dir', `socket', `fifo'");
635 curinode->di_mode &= ~IFMT;
636 curinode->di_mode |= type;
651 len = strtol(argv[1], &cp, 0);
652 if (cp == argv[1] || *cp != '\0' || len < 0) {
653 warnx("bad length `%s'", argv[1]);
657 curinode->di_size = len;
672 modebits = strtol(argv[1], &cp, 8);
673 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
674 warnx("bad modebits `%s'", argv[1]);
678 curinode->di_mode &= ~07777;
679 curinode->di_mode |= modebits;
685 CMDFUNCSTART(chaflags)
694 flags = strtoul(argv[1], &cp, 0);
695 if (cp == argv[1] || *cp != '\0' ) {
696 warnx("bad flags `%s'", argv[1]);
700 if (flags > UINT_MAX) {
701 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
704 curinode->di_flags = flags;
719 gen = strtol(argv[1], &cp, 0);
720 if (cp == argv[1] || *cp != '\0' ) {
721 warnx("bad gen `%s'", argv[1]);
725 if (gen > INT_MAX || gen < INT_MIN) {
726 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
729 curinode->di_gen = gen;
735 CMDFUNCSTART(linkcount)
744 lcnt = strtol(argv[1], &cp, 0);
745 if (cp == argv[1] || *cp != '\0' ) {
746 warnx("bad link count `%s'", argv[1]);
749 if (lcnt > USHRT_MAX || lcnt < 0) {
750 warnx("max link count is %d\n", USHRT_MAX);
754 curinode->di_nlink = lcnt;
760 CMDFUNCSTART(chowner)
770 uid = strtoul(argv[1], &cp, 0);
771 if (cp == argv[1] || *cp != '\0' ) {
772 /* try looking up name */
773 if ((pwd = getpwnam(argv[1]))) {
776 warnx("bad uid `%s'", argv[1]);
781 curinode->di_uid = uid;
787 CMDFUNCSTART(chgroup)
797 gid = strtoul(argv[1], &cp, 0);
798 if (cp == argv[1] || *cp != '\0' ) {
799 if ((grp = getgrnam(argv[1]))) {
802 warnx("bad gid `%s'", argv[1]);
807 curinode->di_gid = gid;
814 dotime(char *name, struct timespec *rts)
820 p = strchr(name, '.');
823 nsec = strtoul(++p, &val, 0);
824 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
825 warnx("invalid nanoseconds");
830 if (strlen(name) != 14) {
832 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
836 for (p = name; *p; p++)
837 if (*p < '0' || *p > '9')
841 #define VAL() ((*p++) - '0')
843 t.tm_year = VAL() + t.tm_year * 10;
844 t.tm_year = VAL() + t.tm_year * 10;
845 t.tm_year = VAL() + t.tm_year * 10 - 1900;
847 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
849 t.tm_mday = VAL() + t.tm_mday * 10;
851 t.tm_hour = VAL() + t.tm_hour * 10;
853 t.tm_min = VAL() + t.tm_min * 10;
855 t.tm_sec = VAL() + t.tm_sec * 10;
860 warnx("date/time out of range");
868 CMDFUNCSTART(chmtime)
870 if (dotime(argv[1], &curinode->di_ctime))
877 CMDFUNCSTART(chatime)
879 if (dotime(argv[1], &curinode->di_ctime))
886 CMDFUNCSTART(chctime)
888 if (dotime(argv[1], &curinode->di_ctime))