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.6 2003/11/01 17:15:59 drhodus Exp $
34 #include <sys/param.h>
43 #include <vfs/ufs/dinode.h>
44 #include <vfs/ufs/dir.h>
45 #include <vfs/ufs/fs.h>
50 static void usage(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
70 main(int argc
, char **argv
)
75 while (-1 != (ch
= getopt(argc
, argv
, "fdr"))) {
78 /* The -f option is left for historical
79 * reasons and has no meaning.
86 nflag
++; /* "no" in fsck, readonly for us */
100 errx(1, "cannot set up file system `%s'", fsys
);
101 printf("%s file system `%s'\nLast Mounted on %s\n",
102 nflag
? "Examining": "Editing", fsys
, sblock
.fs_fsmnt
);
105 sblock
.fs_clean
= 0; /* mark it dirty */
108 printf("*** FILE SYSTEM MARKED DIRTY\n");
109 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
110 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
115 #define CMDFUNC(func) int func (int argc, char **argv)
116 #define CMDFUNCSTART(func) int func(int argc, char **argv)
119 CMDFUNC(focus
); /* focus on inode */
120 CMDFUNC(active
); /* print active inode */
121 CMDFUNC(blocks
); /* print blocks for active inode */
122 CMDFUNC(focusname
); /* focus by name */
123 CMDFUNC(zapi
); /* clear inode */
124 CMDFUNC(uplink
); /* incr link */
125 CMDFUNC(downlink
); /* decr link */
126 CMDFUNC(linkcount
); /* set link count */
127 CMDFUNC(quit
); /* quit */
128 CMDFUNC(ls
); /* list directory */
129 CMDFUNC(rm
); /* remove name */
130 CMDFUNC(ln
); /* add name */
131 CMDFUNC(newtype
); /* change type */
132 CMDFUNC(chmode
); /* change mode */
133 CMDFUNC(chlen
); /* change length */
134 CMDFUNC(chaflags
); /* change flags */
135 CMDFUNC(chgen
); /* change generation */
136 CMDFUNC(chowner
); /* change owner */
137 CMDFUNC(chgroup
); /* Change group */
138 CMDFUNC(back
); /* pop back to last ino */
139 CMDFUNC(chmtime
); /* Change mtime */
140 CMDFUNC(chctime
); /* Change ctime */
141 CMDFUNC(chatime
); /* Change atime */
142 CMDFUNC(chinum
); /* Change inode # of dirent */
143 CMDFUNC(chname
); /* Change dirname of dirent */
145 struct cmdtable cmds
[] = {
146 { "help", "Print out help", 1, 1, FL_RO
, helpfn
},
147 { "?", "Print out help", 1, 1, FL_RO
, helpfn
},
148 { "inode", "Set active inode to INUM", 2, 2, FL_RO
, focus
},
149 { "clri", "Clear inode INUM", 2, 2, FL_WR
, zapi
},
150 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO
, focusname
},
151 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO
, focusname
},
152 { "back", "Go to previous active inode", 1, 1, FL_RO
, back
},
153 { "active", "Print active inode", 1, 1, FL_RO
, active
},
154 { "print", "Print active inode", 1, 1, FL_RO
, active
},
155 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO
, blocks
},
156 { "uplink", "Increment link count", 1, 1, FL_WR
, uplink
},
157 { "downlink", "Decrement link count", 1, 1, FL_WR
, downlink
},
158 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR
, linkcount
},
159 { "ls", "List current inode as directory", 1, 1, FL_RO
, ls
},
160 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR
, rm
},
161 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR
, rm
},
162 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR
, ln
},
163 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR
, chinum
},
164 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR
, chname
},
165 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR
, newtype
},
166 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR
, chmode
},
167 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR
, chlen
},
168 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR
, chowner
},
169 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR
, chgroup
},
170 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR
, chaflags
},
171 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR
, chgen
},
172 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR
, chmtime
},
173 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR
, chctime
},
174 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR
, chatime
},
175 { "quit", "Exit", 1, 1, FL_RO
, quit
},
176 { "q", "Exit", 1, 1, FL_RO
, quit
},
177 { "exit", "Exit", 1, 1, FL_RO
, quit
},
182 helpfn(int argc
, char **argv
)
184 register struct cmdtable
*cmdtp
;
186 printf("Commands are:\n%-10s %5s %5s %s\n",
187 "command", "min argc", "max argc", "what");
189 for (cmdtp
= cmds
; cmdtp
->cmd
; cmdtp
++)
190 printf("%-10s %5u %5u %s\n",
191 cmdtp
->cmd
, cmdtp
->minargc
, cmdtp
->maxargc
, cmdtp
->helptxt
);
198 static char pstring
[64];
199 snprintf(pstring
, sizeof(pstring
), "fsdb (inum: %d)> ", curinum
);
209 int cmd_argc
, rval
= 0, known
;
210 #define scratch known
212 struct cmdtable
*cmdp
;
216 curinode
= ginode(ROOTINO
);
220 hist
= history_init();
221 history(hist
, H_EVENT
, 100); /* 100 elt history buffer */
223 elptr
= el_init("fsdb", stdin
, stdout
);
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
, 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
, 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 dinode
*curinode
;
275 ino_t curinum
, ocurrent
;
277 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
278 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
279 printf("inode %d out of range; range is [%d,%d]\n", \
280 inum, ROOTINO, maxino); \
285 * Focus on given inode number
293 curinode
= ginode(inum
);
303 curinode
= ginode(curinum
);
318 if (curinode
) /* re-set after potential change */
319 curinode
= ginode(curinum
);
344 printf("inode %d link count now %d\n", curinum
, ++curinode
->di_nlink
);
349 CMDFUNCSTART(downlink
)
353 printf("inode %d link count now %d\n", curinum
, --curinode
->di_nlink
);
358 const char *typename
[] = {
379 scannames(struct inodesc
*idesc
)
381 register struct direct
*dirp
= idesc
->id_dirp
;
383 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
384 slot
++, dirp
->d_ino
, dirp
->d_reclen
, typename
[dirp
->d_type
],
385 dirp
->d_namlen
, dirp
->d_name
);
391 struct inodesc idesc
;
392 checkactivedir(); /* let it go on anyway */
395 idesc
.id_number
= curinum
;
396 idesc
.id_func
= scannames
;
397 idesc
.id_type
= DATA
;
398 idesc
.id_fix
= IGNORE
;
399 ckinode(curinode
, &idesc
);
400 curinode
= ginode(curinum
);
405 int findino(struct inodesc
*idesc
); /* from fsck */
406 static int dolookup(char *name
);
411 struct inodesc idesc
;
413 if (!checkactivedir())
415 idesc
.id_number
= curinum
;
416 idesc
.id_func
= findino
;
417 idesc
.id_name
= name
;
418 idesc
.id_type
= DATA
;
419 idesc
.id_fix
= IGNORE
;
420 if (ckinode(curinode
, &idesc
) & FOUND
) {
421 curinum
= idesc
.id_parent
;
422 curinode
= ginode(curinum
);
426 warnx("name `%s' not found in current inode directory", name
);
431 CMDFUNCSTART(focusname
)
440 if (argv
[1][0] == '/') {
442 curinode
= ginode(ROOTINO
);
444 if (!checkactivedir())
447 for (p
= argv
[1]; p
!= NULL
;) {
448 while ((val
= strsep(&p
, "/")) != NULL
&& *val
== '\0');
450 printf("component `%s': ", val
);
452 if (!dolookup(val
)) {
453 curinode
= ginode(curinum
);
469 if (!checkactivedir())
471 rval
= makeentry(curinum
, inum
, argv
[2]);
473 printf("Ino %d entered as `%s'\n", inum
, argv
[2]);
475 printf("could not enter name? weird.\n");
476 curinode
= ginode(curinum
);
484 if (!checkactivedir())
486 rval
= changeino(curinum
, argv
[1], 0);
487 if (rval
& ALTERED
) {
488 printf("Name `%s' removed\n", argv
[1]);
491 printf("could not remove name? weird.\n");
496 long slotcount
, desired
;
499 chinumfunc(struct inodesc
*idesc
)
501 register struct direct
*dirp
= idesc
->id_dirp
;
503 if (slotcount
++ == desired
) {
504 dirp
->d_ino
= idesc
->id_parent
;
505 return STOP
|ALTERED
|FOUND
;
514 struct inodesc idesc
;
517 if (!checkactivedir())
521 desired
= strtol(argv
[1], &cp
, 0);
522 if (cp
== argv
[1] || *cp
!= '\0' || desired
< 0) {
523 printf("invalid slot number `%s'\n", argv
[1]);
527 idesc
.id_number
= curinum
;
528 idesc
.id_func
= chinumfunc
;
529 idesc
.id_fix
= IGNORE
;
530 idesc
.id_type
= DATA
;
531 idesc
.id_parent
= inum
; /* XXX convenient hiding place */
533 if (ckinode(curinode
, &idesc
) & FOUND
)
536 warnx("no %sth slot in current directory", argv
[1]);
542 chnamefunc(struct inodesc
*idesc
)
544 register struct direct
*dirp
= idesc
->id_dirp
;
545 struct direct testdir
;
547 if (slotcount
++ == desired
) {
549 testdir
.d_namlen
= strlen(idesc
->id_name
);
550 if (DIRSIZ(NEWDIRFMT
, &testdir
) <= dirp
->d_reclen
) {
551 dirp
->d_namlen
= testdir
.d_namlen
;
552 strcpy(dirp
->d_name
, idesc
->id_name
);
553 return STOP
|ALTERED
|FOUND
;
555 return STOP
|FOUND
; /* won't fit, so give up */
564 struct inodesc idesc
;
567 if (!checkactivedir())
570 desired
= strtoul(argv
[1], &cp
, 0);
571 if (cp
== argv
[1] || *cp
!= '\0') {
572 printf("invalid slot number `%s'\n", argv
[1]);
576 idesc
.id_number
= curinum
;
577 idesc
.id_func
= chnamefunc
;
578 idesc
.id_fix
= IGNORE
;
579 idesc
.id_type
= DATA
;
580 idesc
.id_name
= argv
[2];
582 rval
= ckinode(curinode
, &idesc
);
583 if ((rval
& (FOUND
|ALTERED
)) == (FOUND
|ALTERED
))
585 else if (rval
& FOUND
) {
586 warnx("new name `%s' does not fit in slot %s\n", argv
[2], argv
[1]);
589 warnx("no %sth slot in current directory", argv
[1]);
595 const char *typename
;
604 CMDFUNCSTART(newtype
)
611 type
= curinode
->di_mode
& IFMT
;
612 for (tp
= typenamemap
;
613 tp
< &typenamemap
[sizeof(typenamemap
)/sizeof(*typenamemap
)];
615 if (!strcmp(argv
[1], tp
->typename
)) {
616 printf("setting type to %s\n", tp
->typename
);
621 if (tp
== &typenamemap
[sizeof(typenamemap
)/sizeof(*typenamemap
)]) {
622 warnx("type `%s' not known", argv
[1]);
623 warnx("try one of `file', `dir', `socket', `fifo'");
626 curinode
->di_mode
&= ~IFMT
;
627 curinode
->di_mode
|= type
;
642 len
= strtol(argv
[1], &cp
, 0);
643 if (cp
== argv
[1] || *cp
!= '\0' || len
< 0) {
644 warnx("bad length `%s'", argv
[1]);
648 curinode
->di_size
= len
;
663 modebits
= strtol(argv
[1], &cp
, 8);
664 if (cp
== argv
[1] || *cp
!= '\0' || (modebits
& ~07777)) {
665 warnx("bad modebits `%s'", argv
[1]);
669 curinode
->di_mode
&= ~07777;
670 curinode
->di_mode
|= modebits
;
676 CMDFUNCSTART(chaflags
)
685 flags
= strtoul(argv
[1], &cp
, 0);
686 if (cp
== argv
[1] || *cp
!= '\0' ) {
687 warnx("bad flags `%s'", argv
[1]);
691 if (flags
> UINT_MAX
) {
692 warnx("flags set beyond 32-bit range of field (%lx)\n", flags
);
695 curinode
->di_flags
= flags
;
710 gen
= strtol(argv
[1], &cp
, 0);
711 if (cp
== argv
[1] || *cp
!= '\0' ) {
712 warnx("bad gen `%s'", argv
[1]);
716 if (gen
> INT_MAX
|| gen
< INT_MIN
) {
717 warnx("gen set beyond 32-bit range of field (%lx)\n", gen
);
720 curinode
->di_gen
= gen
;
726 CMDFUNCSTART(linkcount
)
735 lcnt
= strtol(argv
[1], &cp
, 0);
736 if (cp
== argv
[1] || *cp
!= '\0' ) {
737 warnx("bad link count `%s'", argv
[1]);
740 if (lcnt
> USHRT_MAX
|| lcnt
< 0) {
741 warnx("max link count is %d\n", USHRT_MAX
);
745 curinode
->di_nlink
= lcnt
;
751 CMDFUNCSTART(chowner
)
761 uid
= strtoul(argv
[1], &cp
, 0);
762 if (cp
== argv
[1] || *cp
!= '\0' ) {
763 /* try looking up name */
764 if ((pwd
= getpwnam(argv
[1]))) {
767 warnx("bad uid `%s'", argv
[1]);
772 curinode
->di_uid
= uid
;
778 CMDFUNCSTART(chgroup
)
788 gid
= strtoul(argv
[1], &cp
, 0);
789 if (cp
== argv
[1] || *cp
!= '\0' ) {
790 if ((grp
= getgrnam(argv
[1]))) {
793 warnx("bad gid `%s'", argv
[1]);
798 curinode
->di_gid
= gid
;
805 dotime(char *name
, struct timespec
*rts
)
811 p
= strchr(name
, '.');
814 nsec
= strtoul(++p
, &val
, 0);
815 if (val
== p
|| *val
!= '\0' || nsec
>= 1000000000 || nsec
< 0) {
816 warnx("invalid nanoseconds");
821 if (strlen(name
) != 14) {
823 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
827 for (p
= name
; *p
; p
++)
828 if (*p
< '0' || *p
> '9')
832 #define VAL() ((*p++) - '0')
834 t
.tm_year
= VAL() + t
.tm_year
* 10;
835 t
.tm_year
= VAL() + t
.tm_year
* 10;
836 t
.tm_year
= VAL() + t
.tm_year
* 10 - 1900;
838 t
.tm_mon
= VAL() + t
.tm_mon
* 10 - 1;
840 t
.tm_mday
= VAL() + t
.tm_mday
* 10;
842 t
.tm_hour
= VAL() + t
.tm_hour
* 10;
844 t
.tm_min
= VAL() + t
.tm_min
* 10;
846 t
.tm_sec
= VAL() + t
.tm_sec
* 10;
851 warnx("date/time out of range");
859 CMDFUNCSTART(chmtime
)
861 if (dotime(argv
[1], &curinode
->di_mtime
))
868 CMDFUNCSTART(chatime
)
870 if (dotime(argv
[1], &curinode
->di_atime
))
877 CMDFUNCSTART(chctime
)
879 if (dotime(argv
[1], &curinode
->di_ctime
))