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 $
31 * $DragonFly: src/sbin/fsdb/fsdb.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
34 #include <sys/param.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ffs/fs.h>
50 static void usage __P((void));
51 int cmdloop __P((void));
56 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
60 int returntosingle = 0;
64 * We suck in lots of fsck code, and just pick & choose the stuff we want.
66 * fsreadfd is set up to read from the file system, fswritefd to write to
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 */
102 errx(1, "cannot set up file system `%s'", fsys);
103 printf("%s file system `%s'\nLast Mounted on %s\n",
104 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
107 sblock.fs_clean = 0; /* mark it dirty */
110 printf("*** FILE SYSTEM MARKED DIRTY\n");
111 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
112 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
117 #define CMDFUNC(func) int func __P((int argc, char *argv[]))
118 #define CMDFUNCSTART(func) int func(argc, argv) \
123 CMDFUNC(focus); /* focus on inode */
124 CMDFUNC(active); /* print active inode */
125 CMDFUNC(blocks); /* print blocks for active inode */
126 CMDFUNC(focusname); /* focus by name */
127 CMDFUNC(zapi); /* clear inode */
128 CMDFUNC(uplink); /* incr link */
129 CMDFUNC(downlink); /* decr link */
130 CMDFUNC(linkcount); /* set link count */
131 CMDFUNC(quit); /* quit */
132 CMDFUNC(ls); /* list directory */
133 CMDFUNC(rm); /* remove name */
134 CMDFUNC(ln); /* add name */
135 CMDFUNC(newtype); /* change type */
136 CMDFUNC(chmode); /* change mode */
137 CMDFUNC(chlen); /* change length */
138 CMDFUNC(chaflags); /* change flags */
139 CMDFUNC(chgen); /* change generation */
140 CMDFUNC(chowner); /* change owner */
141 CMDFUNC(chgroup); /* Change group */
142 CMDFUNC(back); /* pop back to last ino */
143 CMDFUNC(chmtime); /* Change mtime */
144 CMDFUNC(chctime); /* Change ctime */
145 CMDFUNC(chatime); /* Change atime */
146 CMDFUNC(chinum); /* Change inode # of dirent */
147 CMDFUNC(chname); /* Change dirname of dirent */
149 struct cmdtable cmds[] = {
150 { "help", "Print out help", 1, 1, FL_RO, helpfn },
151 { "?", "Print out help", 1, 1, FL_RO, helpfn },
152 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
153 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
154 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
155 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
156 { "back", "Go to previous active inode", 1, 1, FL_RO, back },
157 { "active", "Print active inode", 1, 1, FL_RO, active },
158 { "print", "Print active inode", 1, 1, FL_RO, active },
159 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
160 { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
161 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
162 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
163 { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
164 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
165 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
166 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln },
167 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
168 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname },
169 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
170 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
171 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
172 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
173 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
174 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
175 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
176 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
177 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
178 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
179 { "quit", "Exit", 1, 1, FL_RO, quit },
180 { "q", "Exit", 1, 1, FL_RO, quit },
181 { "exit", "Exit", 1, 1, FL_RO, quit },
190 register struct cmdtable *cmdtp;
192 printf("Commands are:\n%-10s %5s %5s %s\n",
193 "command", "min argc", "max argc", "what");
195 for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
196 printf("%-10s %5u %5u %s\n",
197 cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
205 static char pstring[64];
206 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
216 int cmd_argc, rval = 0, known;
217 #define scratch known
219 struct cmdtable *cmdp;
223 curinode = ginode(ROOTINO);
227 hist = history_init();
228 history(hist, H_EVENT, 100); /* 100 elt history buffer */
230 elptr = el_init("fsdb", stdin, stdout);
231 el_set(elptr, EL_EDITOR, "emacs");
232 el_set(elptr, EL_PROMPT, prompt);
233 el_set(elptr, EL_HIST, history, hist);
234 el_source(elptr, NULL);
236 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
238 printf("command `%s'\n", elline);
240 history(hist, H_ENTER, elline);
242 line = strdup(elline);
243 cmd_argv = crack(line, &cmd_argc);
245 * el_parse returns -1 to signal that it's not been handled
248 if (el_parse(elptr, cmd_argc, cmd_argv) != -1)
252 for (cmdp = cmds; cmdp->cmd; cmdp++) {
253 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
254 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
255 warnx("`%s' requires write access", cmd_argv[0]),
257 else if (cmd_argc >= cmdp->minargc &&
258 cmd_argc <= cmdp->maxargc)
259 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
261 rval = argcount(cmdp, cmd_argc, cmd_argv);
267 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
274 warnx("rval was %d", rval);
281 struct dinode *curinode;
282 ino_t curinum, ocurrent;
284 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
285 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
286 printf("inode %d out of range; range is [%d,%d]\n", \
287 inum, ROOTINO, maxino); \
292 * Focus on given inode number
300 curinode = ginode(inum);
310 curinode = ginode(curinum);
325 if (curinode) /* re-set after potential change */
326 curinode = ginode(curinum);
351 printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink);
356 CMDFUNCSTART(downlink)
360 printf("inode %d link count now %d\n", curinum, --curinode->di_nlink);
365 const char *typename[] = {
387 struct inodesc *idesc;
389 register struct direct *dirp = idesc->id_dirp;
391 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
392 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
393 dirp->d_namlen, dirp->d_name);
399 struct inodesc idesc;
400 checkactivedir(); /* let it go on anyway */
403 idesc.id_number = curinum;
404 idesc.id_func = scannames;
405 idesc.id_type = DATA;
406 idesc.id_fix = IGNORE;
407 ckinode(curinode, &idesc);
408 curinode = ginode(curinum);
413 int findino __P((struct inodesc *idesc)); /* from fsck */
414 static int dolookup __P((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? weird.\n");
505 long slotcount, desired;
509 struct inodesc *idesc;
511 register struct direct *dirp = idesc->id_dirp;
513 if (slotcount++ == desired) {
514 dirp->d_ino = idesc->id_parent;
515 return STOP|ALTERED|FOUND;
524 struct inodesc idesc;
527 if (!checkactivedir())
531 desired = strtol(argv[1], &cp, 0);
532 if (cp == argv[1] || *cp != '\0' || desired < 0) {
533 printf("invalid slot number `%s'\n", argv[1]);
537 idesc.id_number = curinum;
538 idesc.id_func = chinumfunc;
539 idesc.id_fix = IGNORE;
540 idesc.id_type = DATA;
541 idesc.id_parent = inum; /* XXX convenient hiding place */
543 if (ckinode(curinode, &idesc) & FOUND)
546 warnx("no %sth slot in current directory", argv[1]);
553 struct inodesc *idesc;
555 register struct direct *dirp = idesc->id_dirp;
556 struct direct testdir;
558 if (slotcount++ == desired) {
560 testdir.d_namlen = strlen(idesc->id_name);
561 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
562 dirp->d_namlen = testdir.d_namlen;
563 strcpy(dirp->d_name, idesc->id_name);
564 return STOP|ALTERED|FOUND;
566 return STOP|FOUND; /* won't fit, so give up */
575 struct inodesc idesc;
578 if (!checkactivedir())
581 desired = strtoul(argv[1], &cp, 0);
582 if (cp == argv[1] || *cp != '\0') {
583 printf("invalid slot number `%s'\n", argv[1]);
587 idesc.id_number = curinum;
588 idesc.id_func = chnamefunc;
589 idesc.id_fix = IGNORE;
590 idesc.id_type = DATA;
591 idesc.id_name = argv[2];
593 rval = ckinode(curinode, &idesc);
594 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
596 else if (rval & FOUND) {
597 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
600 warnx("no %sth slot in current directory", argv[1]);
606 const char *typename;
615 CMDFUNCSTART(newtype)
622 type = curinode->di_mode & IFMT;
623 for (tp = typenamemap;
624 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
626 if (!strcmp(argv[1], tp->typename)) {
627 printf("setting type to %s\n", tp->typename);
632 if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
633 warnx("type `%s' not known", argv[1]);
634 warnx("try one of `file', `dir', `socket', `fifo'");
637 curinode->di_mode &= ~IFMT;
638 curinode->di_mode |= type;
653 len = strtol(argv[1], &cp, 0);
654 if (cp == argv[1] || *cp != '\0' || len < 0) {
655 warnx("bad length `%s'", argv[1]);
659 curinode->di_size = len;
674 modebits = strtol(argv[1], &cp, 8);
675 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
676 warnx("bad modebits `%s'", argv[1]);
680 curinode->di_mode &= ~07777;
681 curinode->di_mode |= modebits;
687 CMDFUNCSTART(chaflags)
696 flags = strtoul(argv[1], &cp, 0);
697 if (cp == argv[1] || *cp != '\0' ) {
698 warnx("bad flags `%s'", argv[1]);
702 if (flags > UINT_MAX) {
703 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
706 curinode->di_flags = flags;
721 gen = strtol(argv[1], &cp, 0);
722 if (cp == argv[1] || *cp != '\0' ) {
723 warnx("bad gen `%s'", argv[1]);
727 if (gen > INT_MAX || gen < INT_MIN) {
728 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
731 curinode->di_gen = gen;
737 CMDFUNCSTART(linkcount)
746 lcnt = strtol(argv[1], &cp, 0);
747 if (cp == argv[1] || *cp != '\0' ) {
748 warnx("bad link count `%s'", argv[1]);
751 if (lcnt > USHRT_MAX || lcnt < 0) {
752 warnx("max link count is %d\n", USHRT_MAX);
756 curinode->di_nlink = lcnt;
762 CMDFUNCSTART(chowner)
772 uid = strtoul(argv[1], &cp, 0);
773 if (cp == argv[1] || *cp != '\0' ) {
774 /* try looking up name */
775 if ((pwd = getpwnam(argv[1]))) {
778 warnx("bad uid `%s'", argv[1]);
783 curinode->di_uid = uid;
789 CMDFUNCSTART(chgroup)
799 gid = strtoul(argv[1], &cp, 0);
800 if (cp == argv[1] || *cp != '\0' ) {
801 if ((grp = getgrnam(argv[1]))) {
804 warnx("bad gid `%s'", argv[1]);
809 curinode->di_gid = gid;
818 struct timespec *rts;
824 p = strchr(name, '.');
827 nsec = strtoul(++p, &val, 0);
828 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
829 warnx("invalid nanoseconds");
834 if (strlen(name) != 14) {
836 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
840 for (p = name; *p; p++)
841 if (*p < '0' || *p > '9')
845 #define VAL() ((*p++) - '0')
847 t.tm_year = VAL() + t.tm_year * 10;
848 t.tm_year = VAL() + t.tm_year * 10;
849 t.tm_year = VAL() + t.tm_year * 10 - 1900;
851 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
853 t.tm_mday = VAL() + t.tm_mday * 10;
855 t.tm_hour = VAL() + t.tm_hour * 10;
857 t.tm_min = VAL() + t.tm_min * 10;
859 t.tm_sec = VAL() + t.tm_sec * 10;
864 warnx("date/time out of range");
872 CMDFUNCSTART(chmtime)
874 if (dotime(argv[1], &curinode->di_ctime))
881 CMDFUNCSTART(chatime)
883 if (dotime(argv[1], &curinode->di_ctime))
890 CMDFUNCSTART(chctime)
892 if (dotime(argv[1], &curinode->di_ctime))