xref: /freebsd/sbin/fsdb/fsdbutil.c (revision eba230afba4932f02a1ca44efc797cf7499a5cb0)
139bb6d1eSPeter Wemm /*	$NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $	*/
239bb6d1eSPeter Wemm 
31de7b4b8SPedro F. Giffuni /*-
41de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
51de7b4b8SPedro F. Giffuni  *
639bb6d1eSPeter Wemm  *  Copyright (c) 1995 John T. Kohl
739bb6d1eSPeter Wemm  *  All rights reserved.
839bb6d1eSPeter Wemm  *
939bb6d1eSPeter Wemm  *  Redistribution and use in source and binary forms, with or without
1039bb6d1eSPeter Wemm  *  modification, are permitted provided that the following conditions
1139bb6d1eSPeter Wemm  *  are met:
1239bb6d1eSPeter Wemm  *  1. Redistributions of source code must retain the above copyright
1339bb6d1eSPeter Wemm  *     notice, this list of conditions and the following disclaimer.
1439bb6d1eSPeter Wemm  *  2. Redistributions in binary form must reproduce the above copyright
1539bb6d1eSPeter Wemm  *     notice, this list of conditions and the following disclaimer in the
1639bb6d1eSPeter Wemm  *     documentation and/or other materials provided with the distribution.
1739bb6d1eSPeter Wemm  *  3. The name of the author may not be used to endorse or promote products
1839bb6d1eSPeter Wemm  *     derived from this software without specific prior written permission.
1939bb6d1eSPeter Wemm  *
2039bb6d1eSPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
2139bb6d1eSPeter Wemm  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2239bb6d1eSPeter Wemm  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2339bb6d1eSPeter Wemm  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2439bb6d1eSPeter Wemm  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2539bb6d1eSPeter Wemm  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2639bb6d1eSPeter Wemm  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2739bb6d1eSPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2839bb6d1eSPeter Wemm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2939bb6d1eSPeter Wemm  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3039bb6d1eSPeter Wemm  * POSSIBILITY OF SUCH DAMAGE.
3139bb6d1eSPeter Wemm  */
3239bb6d1eSPeter Wemm 
339eb8ec95SAlfred Perlstein #include <sys/param.h>
3439bb6d1eSPeter Wemm #include <ctype.h>
350227048aSPhilippe Charnier #include <err.h>
3639bb6d1eSPeter Wemm #include <grp.h>
3739bb6d1eSPeter Wemm #include <pwd.h>
388b5e064dSJohan Karlsson #include <stdint.h>
3939bb6d1eSPeter Wemm #include <string.h>
400227048aSPhilippe Charnier #include <time.h>
418b5e064dSJohan Karlsson #include <timeconv.h>
4239bb6d1eSPeter Wemm 
4339bb6d1eSPeter Wemm #include <ufs/ufs/dinode.h>
449eb8ec95SAlfred Perlstein #include <ufs/ffs/fs.h>
4539bb6d1eSPeter Wemm 
46280a49ecSJoerg Wunsch #include <sys/ioctl.h>
47280a49ecSJoerg Wunsch 
4839bb6d1eSPeter Wemm #include "fsdb.h"
4939bb6d1eSPeter Wemm #include "fsck.h"
5039bb6d1eSPeter Wemm 
5168d7185bSKirk McKusick void prtblknos(struct fs *fs, union dinode *dp);
52280a49ecSJoerg Wunsch 
5339bb6d1eSPeter Wemm char **
crack(char * line,int * argc)540638cc1aSWarner Losh crack(char *line, int *argc)
5539bb6d1eSPeter Wemm {
5639bb6d1eSPeter Wemm     static char *argv[8];
5739bb6d1eSPeter Wemm     int i;
5839bb6d1eSPeter Wemm     char *p, *val;
5939bb6d1eSPeter Wemm     for (p = line, i = 0; p != NULL && i < 8; i++) {
6039bb6d1eSPeter Wemm 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
6139bb6d1eSPeter Wemm 	    /**/;
6239bb6d1eSPeter Wemm 	if (val)
6339bb6d1eSPeter Wemm 	    argv[i] = val;
6439bb6d1eSPeter Wemm 	else
6539bb6d1eSPeter Wemm 	    break;
6639bb6d1eSPeter Wemm     }
6739bb6d1eSPeter Wemm     *argc = i;
6839bb6d1eSPeter Wemm     return argv;
6939bb6d1eSPeter Wemm }
7039bb6d1eSPeter Wemm 
718660ce22SBrian Feldman char **
recrack(char * line,int * argc,int argc_max)720638cc1aSWarner Losh recrack(char *line, int *argc, int argc_max)
738660ce22SBrian Feldman {
748660ce22SBrian Feldman     static char *argv[8];
758660ce22SBrian Feldman     int i;
768660ce22SBrian Feldman     char *p, *val;
778660ce22SBrian Feldman     for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
788660ce22SBrian Feldman 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
798660ce22SBrian Feldman 	    /**/;
808660ce22SBrian Feldman 	if (val)
818660ce22SBrian Feldman 	    argv[i] = val;
828660ce22SBrian Feldman 	else
838660ce22SBrian Feldman 	    break;
848660ce22SBrian Feldman     }
858660ce22SBrian Feldman     argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
868660ce22SBrian Feldman     argv[i][strcspn(argv[i], "\n")] = '\0';
878660ce22SBrian Feldman     *argc = i + 1;
888660ce22SBrian Feldman     return argv;
898660ce22SBrian Feldman }
908660ce22SBrian Feldman 
9139bb6d1eSPeter Wemm int
argcount(struct cmdtable * cmdp,int argc,char * argv[])920638cc1aSWarner Losh argcount(struct cmdtable *cmdp, int argc, char *argv[])
9339bb6d1eSPeter Wemm {
9439bb6d1eSPeter Wemm     if (cmdp->minargc == cmdp->maxargc)
958660ce22SBrian Feldman 	warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
961bc50849SMaxim Konovalov 	    cmdp->minargc-1, argc-1);
9739bb6d1eSPeter Wemm     else
9839bb6d1eSPeter Wemm 	warnx("command `%s' takes from %u to %u arguments",
9939bb6d1eSPeter Wemm 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
10039bb6d1eSPeter Wemm 
10139bb6d1eSPeter Wemm     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
10239bb6d1eSPeter Wemm     return 1;
10339bb6d1eSPeter Wemm }
10439bb6d1eSPeter Wemm 
10539bb6d1eSPeter Wemm void
printstat(const char * cp,ino_t inum,union dinode * dp)1061c85e6a3SKirk McKusick printstat(const char *cp, ino_t inum, union dinode *dp)
10739bb6d1eSPeter Wemm {
10839bb6d1eSPeter Wemm     struct group *grp;
10939bb6d1eSPeter Wemm     struct passwd *pw;
1101c85e6a3SKirk McKusick     ufs2_daddr_t blocks;
1111c85e6a3SKirk McKusick     int64_t gen;
11239bb6d1eSPeter Wemm     char *p;
113962cf4d3SBruce Evans     time_t t;
11439bb6d1eSPeter Wemm 
11539bb6d1eSPeter Wemm     printf("%s: ", cp);
116d8ba45e2SEd Maste     switch (DIP(dp, di_mode) & IFMT) {
117d8ba45e2SEd Maste     case IFDIR:
11839bb6d1eSPeter Wemm 	puts("directory");
11939bb6d1eSPeter Wemm 	break;
120d8ba45e2SEd Maste     case IFREG:
12139bb6d1eSPeter Wemm 	puts("regular file");
12239bb6d1eSPeter Wemm 	break;
123d8ba45e2SEd Maste     case IFBLK:
1249f365aa1SEd Schouten 	printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
12539bb6d1eSPeter Wemm 	break;
126d8ba45e2SEd Maste     case IFCHR:
1279f365aa1SEd Schouten 	printf("character special (%#jx)", DIP(dp, di_rdev));
12839bb6d1eSPeter Wemm 	break;
129d8ba45e2SEd Maste     case IFLNK:
13039bb6d1eSPeter Wemm 	fputs("symlink",stdout);
1311c85e6a3SKirk McKusick 	if (DIP(dp, di_size) > 0 &&
1321c85e6a3SKirk McKusick 	    DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
1331c85e6a3SKirk McKusick 	    DIP(dp, di_blocks) == 0) {
134*5b13fa79SJessica Clarke 	    printf(" to `%.*s'\n", (int) DIP(dp, di_size),
135*5b13fa79SJessica Clarke 		DIP(dp, di_shortlink));
1361c85e6a3SKirk McKusick 	} else {
13739bb6d1eSPeter Wemm 	    putchar('\n');
1381c85e6a3SKirk McKusick 	}
13939bb6d1eSPeter Wemm 	break;
140d8ba45e2SEd Maste     case IFSOCK:
14139bb6d1eSPeter Wemm 	puts("socket");
14239bb6d1eSPeter Wemm 	break;
143d8ba45e2SEd Maste     case IFIFO:
14439bb6d1eSPeter Wemm 	puts("fifo");
14539bb6d1eSPeter Wemm 	break;
14639bb6d1eSPeter Wemm     }
147e25a029eSMatthew D Fleming     printf("I=%ju MODE=%o SIZE=%ju", (uintmax_t)inum, DIP(dp, di_mode),
1488b5e064dSJohan Karlsson 	(uintmax_t)DIP(dp, di_size));
14996dd6360SCeri Davies     if (sblock.fs_magic != FS_UFS1_MAGIC) {
15096dd6360SCeri Davies 	t = _time64_to_time(dp->dp2.di_birthtime);
15196dd6360SCeri Davies 	p = ctime(&t);
15296dd6360SCeri Davies 	printf("\n\tBTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
15396dd6360SCeri Davies 	   dp->dp2.di_birthnsec);
15496dd6360SCeri Davies     }
1551c85e6a3SKirk McKusick     if (sblock.fs_magic == FS_UFS1_MAGIC)
1561c85e6a3SKirk McKusick 	t = _time32_to_time(dp->dp1.di_mtime);
1571c85e6a3SKirk McKusick     else
1581c85e6a3SKirk McKusick 	t = _time64_to_time(dp->dp2.di_mtime);
159962cf4d3SBruce Evans     p = ctime(&t);
16039bb6d1eSPeter Wemm     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
1611c85e6a3SKirk McKusick 	   DIP(dp, di_mtimensec));
1621c85e6a3SKirk McKusick     if (sblock.fs_magic == FS_UFS1_MAGIC)
1631c85e6a3SKirk McKusick 	t = _time32_to_time(dp->dp1.di_ctime);
1641c85e6a3SKirk McKusick     else
1651c85e6a3SKirk McKusick 	t = _time64_to_time(dp->dp2.di_ctime);
166962cf4d3SBruce Evans     p = ctime(&t);
16739bb6d1eSPeter Wemm     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
1681c85e6a3SKirk McKusick 	   DIP(dp, di_ctimensec));
1691c85e6a3SKirk McKusick     if (sblock.fs_magic == FS_UFS1_MAGIC)
1701c85e6a3SKirk McKusick 	t = _time32_to_time(dp->dp1.di_atime);
1711c85e6a3SKirk McKusick     else
1721c85e6a3SKirk McKusick 	t = _time64_to_time(dp->dp2.di_atime);
173962cf4d3SBruce Evans     p = ctime(&t);
17439bb6d1eSPeter Wemm     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
1751c85e6a3SKirk McKusick 	   DIP(dp, di_atimensec));
17639bb6d1eSPeter Wemm 
1771c85e6a3SKirk McKusick     if ((pw = getpwuid(DIP(dp, di_uid))))
17839bb6d1eSPeter Wemm 	printf("OWNER=%s ", pw->pw_name);
17939bb6d1eSPeter Wemm     else
1801c85e6a3SKirk McKusick 	printf("OWNUID=%u ", DIP(dp, di_uid));
1811c85e6a3SKirk McKusick     if ((grp = getgrgid(DIP(dp, di_gid))))
18239bb6d1eSPeter Wemm 	printf("GRP=%s ", grp->gr_name);
18339bb6d1eSPeter Wemm     else
1841c85e6a3SKirk McKusick 	printf("GID=%u ", DIP(dp, di_gid));
18539bb6d1eSPeter Wemm 
1861c85e6a3SKirk McKusick     blocks = DIP(dp, di_blocks);
1871c85e6a3SKirk McKusick     gen = DIP(dp, di_gen);
18824c8f29cSDimitry Andric     printf("LINKCNT=%d FLAGS=%#x BLKCNT=%jx GEN=%jx\n", DIP(dp, di_nlink),
189cf2cbe34SDimitry Andric 	DIP(dp, di_flags), (intmax_t)blocks, (intmax_t)gen);
19039bb6d1eSPeter Wemm }
19139bb6d1eSPeter Wemm 
192280a49ecSJoerg Wunsch 
19339bb6d1eSPeter Wemm int
checkactive(void)1940638cc1aSWarner Losh checkactive(void)
19539bb6d1eSPeter Wemm {
19639bb6d1eSPeter Wemm     if (!curinode) {
19739bb6d1eSPeter Wemm 	warnx("no current inode\n");
19839bb6d1eSPeter Wemm 	return 0;
19939bb6d1eSPeter Wemm     }
20039bb6d1eSPeter Wemm     return 1;
20139bb6d1eSPeter Wemm }
20239bb6d1eSPeter Wemm 
20339bb6d1eSPeter Wemm int
checkactivedir(void)2040638cc1aSWarner Losh checkactivedir(void)
20539bb6d1eSPeter Wemm {
20639bb6d1eSPeter Wemm     if (!curinode) {
20739bb6d1eSPeter Wemm 	warnx("no current inode\n");
20839bb6d1eSPeter Wemm 	return 0;
20939bb6d1eSPeter Wemm     }
210d8ba45e2SEd Maste     if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
211e25a029eSMatthew D Fleming 	warnx("inode %ju not a directory", (uintmax_t)curinum);
21239bb6d1eSPeter Wemm 	return 0;
21339bb6d1eSPeter Wemm     }
21439bb6d1eSPeter Wemm     return 1;
21539bb6d1eSPeter Wemm }
21639bb6d1eSPeter Wemm 
21739bb6d1eSPeter Wemm int
printactive(int doblocks)2180638cc1aSWarner Losh printactive(int doblocks)
21939bb6d1eSPeter Wemm {
22039bb6d1eSPeter Wemm     if (!checkactive())
22139bb6d1eSPeter Wemm 	return 1;
222d8ba45e2SEd Maste     switch (DIP(curinode, di_mode) & IFMT) {
223d8ba45e2SEd Maste     case IFDIR:
224d8ba45e2SEd Maste     case IFREG:
225d8ba45e2SEd Maste     case IFBLK:
226d8ba45e2SEd Maste     case IFCHR:
227d8ba45e2SEd Maste     case IFLNK:
228d8ba45e2SEd Maste     case IFSOCK:
229d8ba45e2SEd Maste     case IFIFO:
230280a49ecSJoerg Wunsch 	if (doblocks)
23168d7185bSKirk McKusick 	    prtblknos(&sblock, curinode);
232280a49ecSJoerg Wunsch 	else
23339bb6d1eSPeter Wemm 	    printstat("current inode", curinum, curinode);
23439bb6d1eSPeter Wemm 	break;
23539bb6d1eSPeter Wemm     case 0:
236e25a029eSMatthew D Fleming 	printf("current inode %ju: unallocated inode\n", (uintmax_t)curinum);
23739bb6d1eSPeter Wemm 	break;
23839bb6d1eSPeter Wemm     default:
239e25a029eSMatthew D Fleming 	printf("current inode %ju: screwy itype 0%o (mode 0%o)?\n",
240d8ba45e2SEd Maste 	    (uintmax_t)curinum, DIP(curinode, di_mode) & IFMT,
241e25a029eSMatthew D Fleming 	    DIP(curinode, di_mode));
24239bb6d1eSPeter Wemm 	break;
24339bb6d1eSPeter Wemm     }
24439bb6d1eSPeter Wemm     return 0;
24539bb6d1eSPeter Wemm }
246