Home | History | Annotate | Line # | Download | only in fsdb
      1 /*	$NetBSD: fsdbutil.c,v 1.24 2022/11/17 06:40:38 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by John T. Kohl.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: fsdbutil.c,v 1.24 2022/11/17 06:40:38 chs Exp $");
     35 #endif /* not lint */
     36 
     37 #include <sys/types.h>
     38 #include <sys/stat.h>
     39 #include <sys/param.h>
     40 #include <sys/time.h>
     41 #include <sys/mount.h>
     42 #include <fcntl.h>
     43 #include <grp.h>
     44 #include <pwd.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <time.h>
     49 #include <unistd.h>
     50 #include <err.h>
     51 
     52 #include <ufs/ufs/dinode.h>
     53 #include <ufs/ffs/fs.h>
     54 
     55 #include "fsdb.h"
     56 #include "fsck.h"
     57 
     58 char  **
     59 crack(char *line, int *argc)
     60 {
     61 	static char *argv[8];
     62 	int     i;
     63 	char   *p, *val;
     64 	for (p = line, i = 0; p != NULL && i < 8; i++) {
     65 		while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
     66 			 /**/ ;
     67 		if (val)
     68 			argv[i] = val;
     69 		else
     70 			break;
     71 	}
     72 	*argc = i;
     73 	return argv;
     74 }
     75 
     76 int
     77 argcount(struct cmdtable *cmdp, int argc, char *argv[])
     78 {
     79 	if (cmdp->minargc == cmdp->maxargc)
     80 		warnx("command `%s' takes %u arguments", cmdp->cmd,
     81 		    cmdp->minargc - 1);
     82 	else
     83 		warnx("command `%s' takes from %u to %u arguments",
     84 		    cmdp->cmd, cmdp->minargc - 1, cmdp->maxargc - 1);
     85 
     86 	warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
     87 	return 1;
     88 }
     89 
     90 void
     91 printstat(const char *cp, ino_t inum, union dinode *dp)
     92 {
     93 	struct group *grp;
     94 	struct passwd *pw;
     95 	time_t  t;
     96 	char   *p;
     97 	uint64_t size, blocks;
     98 	uint32_t extsize;
     99 	uint16_t mode;
    100 	uint32_t rdev;
    101 	uint32_t uid, gid;
    102 
    103 	size = iswap64(DIP(dp, size));
    104 	extsize = is_ufs2 ? iswap32(dp->dp2.di_extsize) : 0;
    105 	blocks = is_ufs2 ? iswap64(DIP(dp, blocks)) : iswap32(DIP(dp, blocks));
    106 	mode = iswap16(DIP(dp, mode));
    107 	rdev = iswap32(DIP(dp, rdev));
    108 
    109 	printf("%s: ", cp);
    110 	switch (mode & IFMT) {
    111 	case IFDIR:
    112 		puts("directory");
    113 		break;
    114 	case IFREG:
    115 		puts("regular file");
    116 		break;
    117 	case IFBLK:
    118 		printf("block special (%llu,%llu)",
    119 		    (unsigned long long)major(rdev),
    120 		    (unsigned long long)minor(rdev));
    121 		break;
    122 	case IFCHR:
    123 		printf("character special (%llu,%llu)",
    124 		    (unsigned long long)major(rdev),
    125 		    (unsigned long long)minor(rdev));
    126 		break;
    127 	case IFLNK:
    128 		fputs("symlink", stdout);
    129 		if (size > 0 && size < (uint64_t)sblock->fs_maxsymlinklen &&
    130 		    DIP(dp, blocks) == 0) {
    131 			p = is_ufs2 ? (char *)dp->dp2.di_db :
    132 			    (char *)dp->dp1.di_db;
    133 			printf(" to `%.*s'\n", (int)size, p);
    134 		} else
    135 			putchar('\n');
    136 		break;
    137 	case IFSOCK:
    138 		puts("socket");
    139 		break;
    140 	case IFIFO:
    141 		puts("fifo");
    142 		break;
    143 	}
    144 	printf("I=%llu MODE=%o SIZE=%llu EXTSIZE=%u", (unsigned long long)inum,
    145 	    mode, (unsigned long long)size, extsize);
    146 	t = is_ufs2 ? iswap64(dp->dp2.di_mtime) : iswap32(dp->dp1.di_mtime);
    147 	p = ctime(&t);
    148 	printf("\n\t    MTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
    149 	    iswap32(DIP(dp, mtimensec)));
    150 	t = is_ufs2 ? iswap64(dp->dp2.di_ctime) : iswap32(dp->dp1.di_ctime);
    151 	p = ctime(&t);
    152 	printf("\n\t    CTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
    153 	    iswap32(DIP(dp, ctimensec)));
    154 	t = is_ufs2 ? iswap64(dp->dp2.di_atime) : iswap32(dp->dp1.di_atime);
    155 	p = ctime(&t);
    156 	printf("\n\t    ATIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
    157 	    iswap32(DIP(dp,atimensec)));
    158 	if (is_ufs2) {
    159 		t = iswap64(dp->dp2.di_birthtime);
    160 		p = ctime(&t);
    161 		printf("\n\tBIRTHTIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
    162 		    iswap32(dp->dp2.di_birthnsec));
    163 	} else {
    164 		printf("\n");
    165 	}
    166 
    167 	if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT)
    168 		uid = iswap16(dp->dp1.di_ouid);
    169 	else
    170 		uid = iswap32(DIP(dp, uid));
    171 	if ((pw = getpwuid(uid)) != NULL)
    172 		printf("OWNER=%s ", pw->pw_name);
    173 	else
    174 		printf("OWNUID=%u ", uid);
    175 	if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT)
    176 		gid = iswap16(dp->dp1.di_ogid);
    177 	else
    178 		gid = iswap32(DIP(dp, gid));
    179 	if ((grp = getgrgid(gid)) != NULL)
    180 		printf("GRP=%s ", grp->gr_name);
    181 	else
    182 		printf("GID=%u ", gid);
    183 
    184 	printf("LINKCNT=%hd FLAGS=0x%x BLKCNT=0x%llx GEN=0x%x\n",
    185 		iswap16(DIP(dp, nlink)),
    186 	    iswap32(DIP(dp, flags)), (unsigned long long)blocks,
    187 		iswap32(DIP(dp, gen)));
    188 }
    189 
    190 int
    191 checkactive(void)
    192 {
    193 	if (!curinode) {
    194 		warnx("no current inode");
    195 		return 0;
    196 	}
    197 	return 1;
    198 }
    199 
    200 int
    201 checkactivedir(void)
    202 {
    203 	if (!curinode) {
    204 		warnx("no current inode");
    205 		return 0;
    206 	}
    207 	if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) {
    208 		warnx("inode %llu not a directory",
    209 		    (unsigned long long)curinum);
    210 		return 0;
    211 	}
    212 	return 1;
    213 }
    214 
    215 int
    216 printactive(void)
    217 {
    218 	uint16_t mode;
    219 
    220 	if (!checkactive())
    221 		return 1;
    222 	mode = iswap16(DIP(curinode, mode));
    223 	switch (mode & IFMT) {
    224 	case IFDIR:
    225 	case IFREG:
    226 	case IFBLK:
    227 	case IFCHR:
    228 	case IFLNK:
    229 	case IFSOCK:
    230 	case IFIFO:
    231 		printstat("current inode", curinum, curinode);
    232 		break;
    233 	case 0:
    234 		printf("current inode %llu: unallocated inode\n",
    235 		    (unsigned long long)curinum);
    236 		break;
    237 	default:
    238 		printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n",
    239 		    (unsigned long long)curinum, mode & IFMT, mode);
    240 		break;
    241 	}
    242 	return 0;
    243 }
    244