xref: /dragonfly/sbin/fsdb/fsdbutil.c (revision 3d33658b)
1 /*	$NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $	*/
2 
3 /*
4  *  Copyright (c) 1995 John T. Kohl
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
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.
17  *
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.
29  *
30  * $FreeBSD: src/sbin/fsdb/fsdbutil.c,v 1.9.2.2 2002/03/20 13:39:02 joerg Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <ctype.h>
35 #include <err.h>
36 #include <grp.h>
37 #include <pwd.h>
38 #include <string.h>
39 #include <time.h>
40 
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/ufs/fs.h>
43 
44 #include <sys/ioctl.h>
45 
46 #include "fsdb.h"
47 #include "fsck.h"
48 
49 static int charsperline(void);
50 static int printindir(ufs_daddr_t blk, int level, char *bufp);
51 static void printblocks(ino_t inum, struct ufs1_dinode *dp);
52 
53 char **
54 crack(char *line, int *argc)
55 {
56     static char *argv[8];
57     int i;
58     char *p, *val;
59     for (p = line, i = 0; p != NULL && i < 8; i++) {
60 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
61 	    /**/;
62 	if (val)
63 	    argv[i] = val;
64 	else
65 	    break;
66     }
67     *argc = i;
68     return argv;
69 }
70 
71 int
72 argcount(struct cmdtable *cmdp, int argc, char **argv)
73 {
74     if (cmdp->minargc == cmdp->maxargc)
75 	warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
76     else
77 	warnx("command `%s' takes from %u to %u arguments",
78 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
79 
80     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
81     return 1;
82 }
83 
84 void
85 printstat(const char *cp, ino_t inum, struct ufs1_dinode *dp)
86 {
87     struct group *grp;
88     struct passwd *pw;
89     char *p;
90     time_t t;
91 
92     printf("%s: ", cp);
93     switch (dp->di_mode & IFMT) {
94     case IFDIR:
95 	puts("directory");
96 	break;
97     case IFREG:
98 	puts("regular file");
99 	break;
100     case IFBLK:
101 	printf("block special (%d,%d)",
102 	       major(dp->di_rdev), minor(dp->di_rdev));
103 	break;
104     case IFCHR:
105 	printf("character special (%d,%d)",
106 	       major(dp->di_rdev), minor(dp->di_rdev));
107 	break;
108     case IFLNK:
109 	fputs("symlink",stdout);
110 	if (dp->di_size > 0 && dp->di_size < UFS1_MAXSYMLINKLEN &&
111 	    dp->di_blocks == 0)
112 	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
113 	else
114 		putchar('\n');
115 	break;
116     case IFSOCK:
117 	puts("socket");
118 	break;
119     case IFIFO:
120 	puts("fifo");
121 	break;
122     }
123     printf("I=%ju MODE=%o SIZE=%ju", (uintmax_t)inum, dp->di_mode,
124 	(uintmax_t)dp->di_size);
125     t = dp->di_mtime;
126     p = ctime(&t);
127     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
128 	   dp->di_mtimensec);
129     t = dp->di_ctime;
130     p = ctime(&t);
131     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
132 	   dp->di_ctimensec);
133     t = dp->di_atime;
134     p = ctime(&t);
135     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
136 	   dp->di_atimensec);
137 
138     if ((pw = getpwuid(dp->di_uid)))
139 	printf("OWNER=%s ", pw->pw_name);
140     else
141 	printf("OWNUID=%u ", dp->di_uid);
142     if ((grp = getgrgid(dp->di_gid)))
143 	printf("GRP=%s ", grp->gr_name);
144     else
145 	printf("GID=%u ", dp->di_gid);
146 
147     printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
148 	   dp->di_blocks, dp->di_gen);
149 }
150 
151 
152 /*
153  * Determine the number of characters in a
154  * single line.
155  */
156 
157 static int
158 charsperline(void)
159 {
160 	int columns;
161 	char *cp;
162 	struct winsize ws;
163 
164 	columns = 0;
165 	if (ioctl(0, TIOCGWINSZ, &ws) != -1)
166 		columns = ws.ws_col;
167 	if (columns == 0 && (cp = getenv("COLUMNS")))
168 		columns = atoi(cp);
169 	if (columns == 0)
170 		columns = 80;	/* last resort */
171 	return (columns);
172 }
173 
174 
175 /*
176  * Recursively print a list of indirect blocks.
177  */
178 static int
179 printindir(ufs_daddr_t blk, int level, char *bufp)
180 {
181     struct bufarea buf, *bp;
182     char tempbuf[32];		/* enough to print an ufs_daddr_t */
183     int i, j, cpl, charssofar;
184     ufs_daddr_t blkno;
185 
186     if (level == 0) {
187 	/* for the final indirect level, don't use the cache */
188 	bp = &buf;
189 	bp->b_un.b_buf = bufp;
190 	bp->b_prev = bp->b_next = bp;
191 	initbarea(bp);
192 
193 	getblk(bp, blk, sblock.fs_bsize);
194     } else
195 	bp = getdatablk(blk, sblock.fs_bsize);
196 
197     cpl = charsperline();
198     for (i = charssofar = 0; i < NINDIR(&sblock); i++) {
199 	blkno = bp->b_un.b_indir[i];
200 	if (blkno == 0) {
201 	    if (level == 0)
202 		putchar('\n');
203 	    return 0;
204 	}
205 	j = sprintf(tempbuf, "%d", blkno);
206 	if (level == 0) {
207 	    charssofar += j;
208 	    if (charssofar >= cpl - 2) {
209 		putchar('\n');
210 		charssofar = j;
211 	    }
212 	}
213 	fputs(tempbuf, stdout);
214 	if (level == 0) {
215 	    printf(", ");
216 	    charssofar += 2;
217 	} else {
218 	    printf(" =>\n");
219 	    if (printindir(blkno, level - 1, bufp) == 0)
220 		return 0;
221 	}
222     }
223     if (level == 0)
224 	putchar('\n');
225     return 1;
226 }
227 
228 
229 /*
230  * Print the block pointers for one inode.
231  */
232 static void
233 printblocks(ino_t inum, struct ufs1_dinode *dp)
234 {
235     char *bufp;
236     int i, nfrags;
237     long ndb, offset;
238 
239     printf("Blocks for inode %ju:\n", (uintmax_t)inum);
240     printf("Direct blocks:\n");
241     ndb = howmany(dp->di_size, sblock.fs_bsize);
242     for (i = 0; i < UFS_NDADDR; i++) {
243 	if (dp->di_db[i] == 0) {
244 	    putchar('\n');
245 	    return;
246 	}
247 	if (i > 0)
248 	    printf(", ");
249 	printf("%d", dp->di_db[i]);
250 	if (--ndb == 0 && (offset = blkoff(&sblock, dp->di_size)) != 0) {
251 	    nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
252 	    printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
253 	}
254     }
255     putchar('\n');
256     if (dp->di_ib[0] == 0)
257 	return;
258 
259     bufp = malloc((unsigned int)sblock.fs_bsize);
260     if (bufp == NULL)
261 	errx(EEXIT, "cannot allocate indirect block buffer");
262     printf("Indirect blocks:\n");
263     for (i = 0; i < UFS_NIADDR; i++)
264 	if (printindir(dp->di_ib[i], i, bufp) == 0)
265 	    break;
266     free(bufp);
267 }
268 
269 
270 int
271 checkactive(void)
272 {
273     if (!curinode) {
274 	warnx("no current inode\n");
275 	return 0;
276     }
277     return 1;
278 }
279 
280 int
281 checkactivedir(void)
282 {
283     if (!curinode) {
284 	warnx("no current inode\n");
285 	return 0;
286     }
287     if ((curinode->di_mode & IFMT) != IFDIR) {
288 	warnx("inode %ju not a directory", (uintmax_t)curinum);
289 	return 0;
290     }
291     return 1;
292 }
293 
294 int
295 printactive(int doblocks)
296 {
297     if (!checkactive())
298 	return 1;
299     switch (curinode->di_mode & IFMT) {
300     case IFDIR:
301     case IFREG:
302     case IFBLK:
303     case IFCHR:
304     case IFLNK:
305     case IFSOCK:
306     case IFIFO:
307 	if (doblocks)
308 	    printblocks(curinum, curinode);
309 	else
310 	    printstat("current inode", curinum, curinode);
311 	break;
312     case 0:
313 	printf("current inode %ju: unallocated inode\n", (uintmax_t)curinum);
314 	break;
315     default:
316 	printf("current inode %ju: screwy itype 0%o (mode 0%o)?\n",
317 	       (uintmax_t)curinum, curinode->di_mode & IFMT, curinode->di_mode);
318 	break;
319     }
320     return 0;
321 }
322