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;
63 * We suck in lots of fsck code, and just pick & choose the stuff we want.
65 * fsreadfd is set up to read from the file system, fswritefd to write to
69 main(int argc
, char **argv
)
74 while (-1 != (ch
= getopt(argc
, argv
, "fdr"))) {
77 /* The -f option is left for historical
78 * reasons and has no meaning.
85 nflag
++; /* "no" in fsck, readonly for us */
99 errx(1, "cannot set up file system `%s'", fsys
);
100 printf("%s file system `%s'\nLast Mounted on %s\n",
101 nflag
? "Examining": "Editing", fsys
, sblock
.fs_fsmnt
);
104 sblock
.fs_clean
= 0; /* mark it dirty */
107 printf("*** FILE SYSTEM MARKED DIRTY\n");
108 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
109 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
114 #define CMDFUNC(func) int func (int argc, char **argv)
115 #define CMDFUNCSTART(func) int func(int argc, char **argv)
118 CMDFUNC(focus
); /* focus on inode */
119 CMDFUNC(active
); /* print active inode */
120 CMDFUNC(blocks
); /* print blocks for active inode */
121 CMDFUNC(focusname
); /* focus by name */
122 CMDFUNC(zapi
); /* clear inode */
123 CMDFUNC(uplink
); /* incr link */
124 CMDFUNC(downlink
); /* decr link */
125 CMDFUNC(linkcount
); /* set link count */
126 CMDFUNC(quit
); /* quit */
127 CMDFUNC(ls
); /* list directory */
128 CMDFUNC(rm
); /* remove name */
129 CMDFUNC(ln
); /* add name */
130 CMDFUNC(newtype
); /* change type */
131 CMDFUNC(chmode
); /* change mode */
132 CMDFUNC(chlen
); /* change length */
133 CMDFUNC(chaflags
); /* change flags */
134 CMDFUNC(chgen
); /* change generation */
135 CMDFUNC(chowner
); /* change owner */
136 CMDFUNC(chgroup
); /* Change group */
137 CMDFUNC(back
); /* pop back to last ino */
138 CMDFUNC(chmtime
); /* Change mtime */
139 CMDFUNC(chctime
); /* Change ctime */
140 CMDFUNC(chatime
); /* Change atime */
141 CMDFUNC(chinum
); /* Change inode # of dirent */
142 CMDFUNC(chname
); /* Change dirname of dirent */
144 struct cmdtable cmds
[] = {
145 { "help", "Print out help", 1, 1, FL_RO
, helpfn
},
146 { "?", "Print out help", 1, 1, FL_RO
, helpfn
},
147 { "inode", "Set active inode to INUM", 2, 2, FL_RO
, focus
},
148 { "clri", "Clear inode INUM", 2, 2, FL_WR
, zapi
},
149 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO
, focusname
},
150 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO
, focusname
},
151 { "back", "Go to previous active inode", 1, 1, FL_RO
, back
},
152 { "active", "Print active inode", 1, 1, FL_RO
, active
},
153 { "print", "Print active inode", 1, 1, FL_RO
, active
},
154 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO
, blocks
},
155 { "uplink", "Increment link count", 1, 1, FL_WR
, uplink
},
156 { "downlink", "Decrement link count", 1, 1, FL_WR
, downlink
},
157 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR
, linkcount
},
158 { "ls", "List current inode as directory", 1, 1, FL_RO
, ls
},
159 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR
, rm
},
160 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR
, rm
},
161 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR
, ln
},
162 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR
, chinum
},
163 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR
, chname
},
164 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR
, newtype
},
165 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR
, chmode
},
166 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR
, chlen
},
167 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR
, chowner
},
168 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR
, chgroup
},
169 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR
, chaflags
},
170 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR
, chgen
},
171 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR
, chmtime
},
172 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR
, chctime
},
173 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR
, chatime
},
174 { "quit", "Exit", 1, 1, FL_RO
, quit
},
175 { "q", "Exit", 1, 1, FL_RO
, quit
},
176 { "exit", "Exit", 1, 1, FL_RO
, quit
},
181 helpfn(int argc
, char **argv
)
183 struct cmdtable
*cmdtp
;
185 printf("Commands are:\n%-10s %5s %5s %s\n",
186 "command", "min argc", "max argc", "what");
188 for (cmdtp
= cmds
; cmdtp
->cmd
; cmdtp
++)
189 printf("%-10s %5u %5u %s\n",
190 cmdtp
->cmd
, cmdtp
->minargc
, cmdtp
->maxargc
, cmdtp
->helptxt
);
197 static char pstring
[64];
198 snprintf(pstring
, sizeof(pstring
), "fsdb (inum: %ju)> ",
209 int cmd_argc
, rval
= 0, known
;
210 #define scratch known
212 struct cmdtable
*cmdp
;
217 curinode
= ginode(UFS_ROOTINO
);
218 curinum
= UFS_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 < UFS_ROOTINO || inum > maxino || \
280 cp == argv[ac] || *cp != '\0' ) { \
281 printf("inode %ju out of range; range is [%ju,%ju]\n", \
282 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino); \
287 * Focus on given inode number
295 curinode
= ginode(inum
);
305 curinode
= ginode(curinum
);
313 struct ufs1_dinode
*dp
;
320 if (curinode
) /* re-set after potential change */
321 curinode
= ginode(curinum
);
346 printf("inode %ju link count now %d\n", (uintmax_t)curinum
,
347 ++curinode
->di_nlink
);
352 CMDFUNCSTART(downlink
)
356 printf("inode %ju link count now %d\n", (uintmax_t)curinum
,
357 --curinode
->di_nlink
);
362 const char *typename
[] = {
383 scannames(struct inodesc
*idesc
)
385 struct direct
*dirp
= idesc
->id_dirp
;
387 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
388 slot
++, dirp
->d_ino
, dirp
->d_reclen
, typename
[dirp
->d_type
],
389 dirp
->d_namlen
, dirp
->d_name
);
395 struct inodesc idesc
;
396 checkactivedir(); /* let it go on anyway */
399 idesc
.id_number
= curinum
;
400 idesc
.id_func
= scannames
;
401 idesc
.id_type
= DATA
;
402 idesc
.id_fix
= IGNORE
;
403 ckinode(curinode
, &idesc
);
404 curinode
= ginode(curinum
);
409 int findino(struct inodesc
*idesc
); /* from fsck */
410 static int dolookup(char *name
);
415 struct inodesc idesc
;
417 if (!checkactivedir())
419 idesc
.id_number
= curinum
;
420 idesc
.id_func
= findino
;
421 idesc
.id_name
= name
;
422 idesc
.id_type
= DATA
;
423 idesc
.id_fix
= IGNORE
;
424 if (ckinode(curinode
, &idesc
) & FOUND
) {
425 curinum
= idesc
.id_parent
;
426 curinode
= ginode(curinum
);
430 warnx("name `%s' not found in current inode directory", name
);
435 CMDFUNCSTART(focusname
)
444 if (argv
[1][0] == '/') {
445 curinum
= UFS_ROOTINO
;
446 curinode
= ginode(UFS_ROOTINO
);
448 if (!checkactivedir())
451 for (p
= argv
[1]; p
!= NULL
;) {
452 while ((val
= strsep(&p
, "/")) != NULL
&& *val
== '\0');
454 printf("component `%s': ", val
);
456 if (!dolookup(val
)) {
457 curinode
= ginode(curinum
);
473 if (!checkactivedir())
475 rval
= makeentry(curinum
, inum
, argv
[2]);
477 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum
, argv
[2]);
479 printf("could not enter name? weird.\n");
480 curinode
= ginode(curinum
);
488 if (!checkactivedir())
490 rval
= changeino(curinum
, argv
[1], 0);
491 if (rval
& ALTERED
) {
492 printf("Name `%s' removed\n", argv
[1]);
495 printf("could not remove name? weird.\n");
500 long slotcount
, desired
;
503 chinumfunc(struct inodesc
*idesc
)
505 struct direct
*dirp
= idesc
->id_dirp
;
507 if (slotcount
++ == desired
) {
508 dirp
->d_ino
= idesc
->id_parent
;
509 return STOP
|ALTERED
|FOUND
;
518 struct inodesc idesc
;
521 if (!checkactivedir())
525 desired
= strtol(argv
[1], &cp
, 0);
526 if (cp
== argv
[1] || *cp
!= '\0' || desired
< 0) {
527 printf("invalid slot number `%s'\n", argv
[1]);
531 idesc
.id_number
= curinum
;
532 idesc
.id_func
= chinumfunc
;
533 idesc
.id_fix
= IGNORE
;
534 idesc
.id_type
= DATA
;
535 idesc
.id_parent
= inum
; /* XXX convenient hiding place */
537 if (ckinode(curinode
, &idesc
) & FOUND
)
540 warnx("no %sth slot in current directory", argv
[1]);
546 chnamefunc(struct inodesc
*idesc
)
548 struct direct
*dirp
= idesc
->id_dirp
;
549 struct direct testdir
;
551 if (slotcount
++ == desired
) {
553 testdir
.d_namlen
= strlen(idesc
->id_name
);
554 if (DIRSIZ(NEWDIRFMT
, &testdir
) <= dirp
->d_reclen
) {
555 dirp
->d_namlen
= testdir
.d_namlen
;
556 strcpy(dirp
->d_name
, idesc
->id_name
);
557 return STOP
|ALTERED
|FOUND
;
559 return STOP
|FOUND
; /* won't fit, so give up */
568 struct inodesc idesc
;
571 if (!checkactivedir())
574 desired
= strtoul(argv
[1], &cp
, 0);
575 if (cp
== argv
[1] || *cp
!= '\0') {
576 printf("invalid slot number `%s'\n", argv
[1]);
580 idesc
.id_number
= curinum
;
581 idesc
.id_func
= chnamefunc
;
582 idesc
.id_fix
= IGNORE
;
583 idesc
.id_type
= DATA
;
584 idesc
.id_name
= argv
[2];
586 rval
= ckinode(curinode
, &idesc
);
587 if ((rval
& (FOUND
|ALTERED
)) == (FOUND
|ALTERED
))
589 else if (rval
& FOUND
) {
590 warnx("new name `%s' does not fit in slot %s\n", argv
[2], argv
[1]);
593 warnx("no %sth slot in current directory", argv
[1]);
599 const char *typename
;
608 CMDFUNCSTART(newtype
)
615 type
= curinode
->di_mode
& IFMT
;
616 for (tp
= typenamemap
; tp
< &typenamemap
[NELEM(typenamemap
)]; tp
++) {
617 if (!strcmp(argv
[1], tp
->typename
)) {
618 printf("setting type to %s\n", tp
->typename
);
623 if (tp
== &typenamemap
[NELEM(typenamemap
)]) {
624 warnx("type `%s' not known", argv
[1]);
625 warnx("try one of `file', `dir', `socket', `fifo'");
628 curinode
->di_mode
&= ~IFMT
;
629 curinode
->di_mode
|= type
;
644 len
= strtol(argv
[1], &cp
, 0);
645 if (cp
== argv
[1] || *cp
!= '\0' || len
< 0) {
646 warnx("bad length `%s'", argv
[1]);
650 curinode
->di_size
= len
;
665 modebits
= strtol(argv
[1], &cp
, 8);
666 if (cp
== argv
[1] || *cp
!= '\0' || (modebits
& ~07777)) {
667 warnx("bad modebits `%s'", argv
[1]);
671 curinode
->di_mode
&= ~07777;
672 curinode
->di_mode
|= modebits
;
678 CMDFUNCSTART(chaflags
)
687 flags
= strtoul(argv
[1], &cp
, 0);
688 if (cp
== argv
[1] || *cp
!= '\0' ) {
689 warnx("bad flags `%s'", argv
[1]);
693 if (flags
> UINT_MAX
) {
694 warnx("flags set beyond 32-bit range of field (%lx)\n", flags
);
697 curinode
->di_flags
= flags
;
712 gen
= strtol(argv
[1], &cp
, 0);
713 if (cp
== argv
[1] || *cp
!= '\0' ) {
714 warnx("bad gen `%s'", argv
[1]);
718 if (gen
> INT_MAX
|| gen
< INT_MIN
) {
719 warnx("gen set beyond 32-bit range of field (%lx)\n", gen
);
722 curinode
->di_gen
= gen
;
728 CMDFUNCSTART(linkcount
)
737 lcnt
= strtol(argv
[1], &cp
, 0);
738 if (cp
== argv
[1] || *cp
!= '\0' ) {
739 warnx("bad link count `%s'", argv
[1]);
742 if (lcnt
> USHRT_MAX
|| lcnt
< 0) {
743 warnx("max link count is %d\n", USHRT_MAX
);
747 curinode
->di_nlink
= lcnt
;
753 CMDFUNCSTART(chowner
)
763 uid
= strtoul(argv
[1], &cp
, 0);
764 if (cp
== argv
[1] || *cp
!= '\0' ) {
765 /* try looking up name */
766 if ((pwd
= getpwnam(argv
[1]))) {
769 warnx("bad uid `%s'", argv
[1]);
774 curinode
->di_uid
= uid
;
780 CMDFUNCSTART(chgroup
)
790 gid
= strtoul(argv
[1], &cp
, 0);
791 if (cp
== argv
[1] || *cp
!= '\0' ) {
792 if ((grp
= getgrnam(argv
[1]))) {
795 warnx("bad gid `%s'", argv
[1]);
800 curinode
->di_gid
= gid
;
807 dotime(char *name
, int32_t *rts
)
813 p
= strchr(name
, '.');
816 nsec
= strtoul(++p
, &val
, 0);
817 if (val
== p
|| *val
!= '\0' || nsec
>= 1000000000 || nsec
< 0) {
818 warnx("invalid nanoseconds");
823 if (strlen(name
) != 14) {
825 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
829 for (p
= name
; *p
; p
++)
830 if (*p
< '0' || *p
> '9')
834 #define VAL() ((*p++) - '0')
836 t
.tm_year
= VAL() + t
.tm_year
* 10;
837 t
.tm_year
= VAL() + t
.tm_year
* 10;
838 t
.tm_year
= VAL() + t
.tm_year
* 10 - 1900;
840 t
.tm_mon
= VAL() + t
.tm_mon
* 10 - 1;
842 t
.tm_mday
= VAL() + t
.tm_mday
* 10;
844 t
.tm_hour
= VAL() + t
.tm_hour
* 10;
846 t
.tm_min
= VAL() + t
.tm_min
* 10;
848 t
.tm_sec
= VAL() + t
.tm_sec
* 10;
853 warnx("date/time out of range");
861 CMDFUNCSTART(chmtime
)
863 if (dotime(argv
[1], &curinode
->di_mtime
))
870 CMDFUNCSTART(chatime
)
872 if (dotime(argv
[1], &curinode
->di_atime
))
879 CMDFUNCSTART(chctime
)
881 if (dotime(argv
[1], &curinode
->di_ctime
))