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