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.9 2006/04/03 01:58:49 dillon 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 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
;
217 curinode
= ginode(ROOTINO
);
221 hist
= history_init();
222 history(hist
, &he
, H_SETSIZE
, 100); /* 100 elt history buffer */
224 elptr
= el_init("fsdb", stdin
, stdout
, stderr
);
225 el_set(elptr
, EL_EDITOR
, "emacs");
226 el_set(elptr
, EL_PROMPT
, prompt
);
227 el_set(elptr
, EL_HIST
, history
, hist
);
228 el_source(elptr
, NULL
);
230 while ((elline
= el_gets(elptr
, &scratch
)) != NULL
&& scratch
!= 0) {
232 printf("command `%s'\n", elline
);
234 history(hist
, &he
, H_ENTER
, elline
);
236 line
= strdup(elline
);
237 cmd_argv
= crack(line
, &cmd_argc
);
239 * el_parse returns -1 to signal that it's not been handled
242 if (el_parse(elptr
, cmd_argc
, (const char **)cmd_argv
) != -1)
246 for (cmdp
= cmds
; cmdp
->cmd
; cmdp
++) {
247 if (!strcmp(cmdp
->cmd
, cmd_argv
[0])) {
248 if ((cmdp
->flags
& FL_WR
) == FL_WR
&& nflag
)
249 warnx("`%s' requires write access", cmd_argv
[0]),
251 else if (cmd_argc
>= cmdp
->minargc
&&
252 cmd_argc
<= cmdp
->maxargc
)
253 rval
= (*cmdp
->handler
)(cmd_argc
, cmd_argv
);
255 rval
= argcount(cmdp
, cmd_argc
, cmd_argv
);
261 warnx("unknown command `%s'", cmd_argv
[0]), rval
= 1;
268 warnx("rval was %d", rval
);
275 struct ufs1_dinode
*curinode
;
276 ino_t curinum
, ocurrent
;
278 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
279 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
280 printf("inode %d out of range; range is [%d,%d]\n", \
281 inum, ROOTINO, 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 %d link count now %d\n", curinum
, ++curinode
->di_nlink
);
350 CMDFUNCSTART(downlink
)
354 printf("inode %d link count now %d\n", curinum
, --curinode
->di_nlink
);
359 const char *typename
[] = {
380 scannames(struct inodesc
*idesc
)
382 struct direct
*dirp
= idesc
->id_dirp
;
384 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
385 slot
++, dirp
->d_ino
, dirp
->d_reclen
, typename
[dirp
->d_type
],
386 dirp
->d_namlen
, dirp
->d_name
);
392 struct inodesc idesc
;
393 checkactivedir(); /* let it go on anyway */
396 idesc
.id_number
= curinum
;
397 idesc
.id_func
= scannames
;
398 idesc
.id_type
= DATA
;
399 idesc
.id_fix
= IGNORE
;
400 ckinode(curinode
, &idesc
);
401 curinode
= ginode(curinum
);
406 int findino(struct inodesc
*idesc
); /* from fsck */
407 static int dolookup(char *name
);
412 struct inodesc idesc
;
414 if (!checkactivedir())
416 idesc
.id_number
= curinum
;
417 idesc
.id_func
= findino
;
418 idesc
.id_name
= name
;
419 idesc
.id_type
= DATA
;
420 idesc
.id_fix
= IGNORE
;
421 if (ckinode(curinode
, &idesc
) & FOUND
) {
422 curinum
= idesc
.id_parent
;
423 curinode
= ginode(curinum
);
427 warnx("name `%s' not found in current inode directory", name
);
432 CMDFUNCSTART(focusname
)
441 if (argv
[1][0] == '/') {
443 curinode
= ginode(ROOTINO
);
445 if (!checkactivedir())
448 for (p
= argv
[1]; p
!= NULL
;) {
449 while ((val
= strsep(&p
, "/")) != NULL
&& *val
== '\0');
451 printf("component `%s': ", val
);
453 if (!dolookup(val
)) {
454 curinode
= ginode(curinum
);
470 if (!checkactivedir())
472 rval
= makeentry(curinum
, inum
, argv
[2]);
474 printf("Ino %d entered as `%s'\n", inum
, argv
[2]);
476 printf("could not enter name? weird.\n");
477 curinode
= ginode(curinum
);
485 if (!checkactivedir())
487 rval
= changeino(curinum
, argv
[1], 0);
488 if (rval
& ALTERED
) {
489 printf("Name `%s' removed\n", argv
[1]);
492 printf("could not remove name? weird.\n");
497 long slotcount
, desired
;
500 chinumfunc(struct inodesc
*idesc
)
502 struct direct
*dirp
= idesc
->id_dirp
;
504 if (slotcount
++ == desired
) {
505 dirp
->d_ino
= idesc
->id_parent
;
506 return STOP
|ALTERED
|FOUND
;
515 struct inodesc idesc
;
518 if (!checkactivedir())
522 desired
= strtol(argv
[1], &cp
, 0);
523 if (cp
== argv
[1] || *cp
!= '\0' || desired
< 0) {
524 printf("invalid slot number `%s'\n", argv
[1]);
528 idesc
.id_number
= curinum
;
529 idesc
.id_func
= chinumfunc
;
530 idesc
.id_fix
= IGNORE
;
531 idesc
.id_type
= DATA
;
532 idesc
.id_parent
= inum
; /* XXX convenient hiding place */
534 if (ckinode(curinode
, &idesc
) & FOUND
)
537 warnx("no %sth slot in current directory", argv
[1]);
543 chnamefunc(struct inodesc
*idesc
)
545 struct direct
*dirp
= idesc
->id_dirp
;
546 struct direct testdir
;
548 if (slotcount
++ == desired
) {
550 testdir
.d_namlen
= strlen(idesc
->id_name
);
551 if (DIRSIZ(NEWDIRFMT
, &testdir
) <= dirp
->d_reclen
) {
552 dirp
->d_namlen
= testdir
.d_namlen
;
553 strcpy(dirp
->d_name
, idesc
->id_name
);
554 return STOP
|ALTERED
|FOUND
;
556 return STOP
|FOUND
; /* won't fit, so give up */
565 struct inodesc idesc
;
568 if (!checkactivedir())
571 desired
= strtoul(argv
[1], &cp
, 0);
572 if (cp
== argv
[1] || *cp
!= '\0') {
573 printf("invalid slot number `%s'\n", argv
[1]);
577 idesc
.id_number
= curinum
;
578 idesc
.id_func
= chnamefunc
;
579 idesc
.id_fix
= IGNORE
;
580 idesc
.id_type
= DATA
;
581 idesc
.id_name
= argv
[2];
583 rval
= ckinode(curinode
, &idesc
);
584 if ((rval
& (FOUND
|ALTERED
)) == (FOUND
|ALTERED
))
586 else if (rval
& FOUND
) {
587 warnx("new name `%s' does not fit in slot %s\n", argv
[2], argv
[1]);
590 warnx("no %sth slot in current directory", argv
[1]);
596 const char *typename
;
605 CMDFUNCSTART(newtype
)
612 type
= curinode
->di_mode
& IFMT
;
613 for (tp
= typenamemap
;
614 tp
< &typenamemap
[sizeof(typenamemap
)/sizeof(*typenamemap
)];
616 if (!strcmp(argv
[1], tp
->typename
)) {
617 printf("setting type to %s\n", tp
->typename
);
622 if (tp
== &typenamemap
[sizeof(typenamemap
)/sizeof(*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
))