xref: /netbsd-src/sbin/fsdb/fsdbutil.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: fsdbutil.c,v 1.13 2003/04/02 10:39:29 fvdl 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.13 2003/04/02 10:39:29 fvdl 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(line, argc)
68 	char   *line;
69 	int    *argc;
70 {
71 	static char *argv[8];
72 	int     i;
73 	char   *p, *val;
74 	for (p = line, i = 0; p != NULL && i < 8; i++) {
75 		while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
76 			 /**/ ;
77 		if (val)
78 			argv[i] = val;
79 		else
80 			break;
81 	}
82 	*argc = i;
83 	return argv;
84 }
85 
86 int
87 argcount(cmdp, argc, argv)
88 	struct cmdtable *cmdp;
89 	int     argc;
90 	char   *argv[];
91 {
92 	if (cmdp->minargc == cmdp->maxargc)
93 		warnx("command `%s' takes %u arguments", cmdp->cmd,
94 		    cmdp->minargc - 1);
95 	else
96 		warnx("command `%s' takes from %u to %u arguments",
97 		    cmdp->cmd, cmdp->minargc - 1, cmdp->maxargc - 1);
98 
99 	warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
100 	return 1;
101 }
102 
103 void
104 printstat(cp, inum, dp)
105 	const char *cp;
106 	ino_t   inum;
107 	union dinode *dp;
108 {
109 	struct group *grp;
110 	struct passwd *pw;
111 	time_t  t;
112 	char   *p;
113 	uint64_t size, blocks;
114 	uint16_t mode;
115 	uint32_t rdev;
116 
117 	size = iswap64(DIP(dp, size));
118 	blocks = is_ufs2 ? iswap64(DIP(dp, blocks)) : iswap32(DIP(dp, blocks));
119 	mode = iswap16(DIP(dp, mode));
120 	rdev = iswap32(DIP(dp, rdev));
121 
122 	printf("%s: ", cp);
123 	switch (mode & IFMT) {
124 	case IFDIR:
125 		puts("directory");
126 		break;
127 	case IFREG:
128 		puts("regular file");
129 		break;
130 	case IFBLK:
131 		printf("block special (%d,%d)", major(rdev), minor(rdev));
132 		break;
133 	case IFCHR:
134 		printf("character special (%d,%d)", major(rdev), minor(rdev));
135 		break;
136 	case IFLNK:
137 		fputs("symlink", stdout);
138 		if (size > 0 && size < sblock->fs_maxsymlinklen &&
139 		    DIP(dp, blocks) == 0) {
140 			p = is_ufs2 ? (char *)dp->dp2.di_db :
141 			    (char *)dp->dp1.di_db;
142 			printf(" to `%.*s'\n", (int)size, p);
143 		} else
144 			putchar('\n');
145 		break;
146 	case IFSOCK:
147 		puts("socket");
148 		break;
149 	case IFIFO:
150 		puts("fifo");
151 		break;
152 	}
153 	printf("I=%u MODE=%o SIZE=%llu", inum, mode,
154 	    (unsigned long long)size);
155 	t = is_ufs2 ? iswap64(dp->dp2.di_mtime) : iswap32(dp->dp1.di_mtime);
156 	p = ctime(&t);
157 	printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
158 	    iswap32(DIP(dp, mtimensec)));
159 	t = is_ufs2 ? iswap64(dp->dp2.di_ctime) : iswap32(dp->dp1.di_ctime);
160 	p = ctime(&t);
161 	printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
162 	    iswap32(DIP(dp, ctimensec)));
163 	t = is_ufs2 ? iswap64(dp->dp2.di_atime) : iswap32(dp->dp1.di_atime);
164 	p = ctime(&t);
165 	printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
166 	    iswap32(DIP(dp,atimensec)));
167 
168 	if ((pw = getpwuid(iswap32(DIP(dp, uid)))) != NULL)
169 		printf("OWNER=%s ", pw->pw_name);
170 	else
171 		printf("OWNUID=%u ", iswap32(DIP(dp, uid)));
172 	if ((grp = getgrgid(iswap32(DIP(dp, gid)))) != NULL)
173 		printf("GRP=%s ", grp->gr_name);
174 	else
175 		printf("GID=%u ", iswap32(DIP(dp, gid)));
176 
177 	printf("LINKCNT=%hd FLAGS=0x%#x BLKCNT=0x%llx GEN=0x%x\n",
178 		iswap16(DIP(dp, nlink)),
179 	    iswap32(DIP(dp, flags)), (unsigned long long)blocks,
180 		iswap32(DIP(dp, gen)));
181 }
182 
183 int
184 checkactive()
185 {
186 	if (!curinode) {
187 		warnx("no current inode");
188 		return 0;
189 	}
190 	return 1;
191 }
192 
193 int
194 checkactivedir()
195 {
196 	if (!curinode) {
197 		warnx("no current inode");
198 		return 0;
199 	}
200 	if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) {
201 		warnx("inode %d not a directory", curinum);
202 		return 0;
203 	}
204 	return 1;
205 }
206 
207 int
208 printactive()
209 {
210 	uint16_t mode;
211 
212 	if (!checkactive())
213 		return 1;
214 	mode = iswap16(DIP(curinode, mode));
215 	switch (mode & IFMT) {
216 	case IFDIR:
217 	case IFREG:
218 	case IFBLK:
219 	case IFCHR:
220 	case IFLNK:
221 	case IFSOCK:
222 	case IFIFO:
223 		printstat("current inode", curinum, curinode);
224 		break;
225 	case 0:
226 		printf("current inode %d: unallocated inode\n", curinum);
227 		break;
228 	default:
229 		printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
230 		    curinum, mode & IFMT, mode);
231 		break;
232 	}
233 	return 0;
234 }
235