fsdbutil.c revision 228603
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
31#ifndef lint
32static const char rcsid[] =
33  "$FreeBSD: head/sbin/fsdb/fsdbutil.c 228603 2011-12-16 23:09:31Z dim $";
34#endif /* not lint */
35
36#include <sys/param.h>
37#include <ctype.h>
38#include <err.h>
39#include <grp.h>
40#include <pwd.h>
41#include <stdint.h>
42#include <string.h>
43#include <time.h>
44#include <timeconv.h>
45
46#include <ufs/ufs/dinode.h>
47#include <ufs/ffs/fs.h>
48
49#include <sys/ioctl.h>
50
51#include "fsdb.h"
52#include "fsck.h"
53
54static int charsperline(void);
55static void printindir(ufs2_daddr_t blk, int level, char *bufp);
56static void printblocks(ino_t inum, union dinode *dp);
57
58char **
59crack(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
76char **
77recrack(char *line, int *argc, int argc_max)
78{
79    static char *argv[8];
80    int i;
81    char *p, *val;
82    for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
83	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
84	    /**/;
85	if (val)
86	    argv[i] = val;
87	else
88	    break;
89    }
90    argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
91    argv[i][strcspn(argv[i], "\n")] = '\0';
92    *argc = i + 1;
93    return argv;
94}
95
96int
97argcount(struct cmdtable *cmdp, int argc, char *argv[])
98{
99    if (cmdp->minargc == cmdp->maxargc)
100	warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
101	    cmdp->minargc-1, argc-1);
102    else
103	warnx("command `%s' takes from %u to %u arguments",
104	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
105
106    warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
107    return 1;
108}
109
110void
111printstat(const char *cp, ino_t inum, union dinode *dp)
112{
113    struct group *grp;
114    struct passwd *pw;
115    ufs2_daddr_t blocks;
116    int64_t gen;
117    char *p;
118    time_t t;
119
120    printf("%s: ", cp);
121    switch (DIP(dp, di_mode) & IFMT) {
122    case IFDIR:
123	puts("directory");
124	break;
125    case IFREG:
126	puts("regular file");
127	break;
128    case IFBLK:
129	printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
130	break;
131    case IFCHR:
132	printf("character special (%#jx)", DIP(dp, di_rdev));
133	break;
134    case IFLNK:
135	fputs("symlink",stdout);
136	if (DIP(dp, di_size) > 0 &&
137	    DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
138	    DIP(dp, di_blocks) == 0) {
139	    if (sblock.fs_magic == FS_UFS1_MAGIC)
140		p = (caddr_t)dp->dp1.di_db;
141	    else
142		p = (caddr_t)dp->dp2.di_db;
143	    printf(" to `%.*s'\n", (int) DIP(dp, di_size), p);
144	} else {
145	    putchar('\n');
146	}
147	break;
148    case IFSOCK:
149	puts("socket");
150	break;
151    case IFIFO:
152	puts("fifo");
153	break;
154    }
155    printf("I=%lu MODE=%o SIZE=%ju", (u_long)inum, DIP(dp, di_mode),
156	(uintmax_t)DIP(dp, di_size));
157    if (sblock.fs_magic != FS_UFS1_MAGIC) {
158	t = _time64_to_time(dp->dp2.di_birthtime);
159	p = ctime(&t);
160	printf("\n\tBTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
161	   dp->dp2.di_birthnsec);
162    }
163    if (sblock.fs_magic == FS_UFS1_MAGIC)
164	t = _time32_to_time(dp->dp1.di_mtime);
165    else
166	t = _time64_to_time(dp->dp2.di_mtime);
167    p = ctime(&t);
168    printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
169	   DIP(dp, di_mtimensec));
170    if (sblock.fs_magic == FS_UFS1_MAGIC)
171	t = _time32_to_time(dp->dp1.di_ctime);
172    else
173	t = _time64_to_time(dp->dp2.di_ctime);
174    p = ctime(&t);
175    printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
176	   DIP(dp, di_ctimensec));
177    if (sblock.fs_magic == FS_UFS1_MAGIC)
178	t = _time32_to_time(dp->dp1.di_atime);
179    else
180	t = _time64_to_time(dp->dp2.di_atime);
181    p = ctime(&t);
182    printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
183	   DIP(dp, di_atimensec));
184
185    if ((pw = getpwuid(DIP(dp, di_uid))))
186	printf("OWNER=%s ", pw->pw_name);
187    else
188	printf("OWNUID=%u ", DIP(dp, di_uid));
189    if ((grp = getgrgid(DIP(dp, di_gid))))
190	printf("GRP=%s ", grp->gr_name);
191    else
192	printf("GID=%u ", DIP(dp, di_gid));
193
194    blocks = DIP(dp, di_blocks);
195    gen = DIP(dp, di_gen);
196    printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%jx GEN=%jx\n",
197	   (short)DIP(dp, di_nlink), DIP(dp, di_flags), (intmax_t)blocks,
198	   (intmax_t)gen);
199}
200
201
202/*
203 * Determine the number of characters in a
204 * single line.
205 */
206
207static int
208charsperline(void)
209{
210	int columns;
211	char *cp;
212	struct winsize ws;
213
214	columns = 0;
215	if (ioctl(0, TIOCGWINSZ, &ws) != -1)
216		columns = ws.ws_col;
217	if (columns == 0 && (cp = getenv("COLUMNS")))
218		columns = atoi(cp);
219	if (columns == 0)
220		columns = 80;	/* last resort */
221	return (columns);
222}
223
224
225/*
226 * Recursively print a list of indirect blocks.
227 */
228static void
229printindir(ufs2_daddr_t blk, int level, char *bufp)
230{
231    struct bufarea buf, *bp;
232    char tempbuf[32];		/* enough to print an ufs2_daddr_t */
233    int i, j, cpl, charssofar;
234    ufs2_daddr_t blkno;
235
236    if (blk == 0)
237	return;
238    printf("%jd (%d) =>\n", (intmax_t)blk, level);
239    if (level == 0) {
240	/* for the final indirect level, don't use the cache */
241	bp = &buf;
242	bp->b_un.b_buf = bufp;
243	bp->b_prev = bp->b_next = bp;
244	initbarea(bp);
245
246	getblk(bp, blk, sblock.fs_bsize);
247    } else
248	bp = getdatablk(blk, sblock.fs_bsize);
249
250    cpl = charsperline();
251    for (i = charssofar = 0; i < NINDIR(&sblock); i++) {
252	if (sblock.fs_magic == FS_UFS1_MAGIC)
253		blkno = bp->b_un.b_indir1[i];
254	else
255		blkno = bp->b_un.b_indir2[i];
256	if (blkno == 0)
257	    continue;
258	j = sprintf(tempbuf, "%jd", (intmax_t)blkno);
259	if (level == 0) {
260	    charssofar += j;
261	    if (charssofar >= cpl - 2) {
262		putchar('\n');
263		charssofar = j;
264	    }
265	}
266	fputs(tempbuf, stdout);
267	if (level == 0) {
268	    printf(", ");
269	    charssofar += 2;
270	} else {
271	    printf(" =>\n");
272	    printindir(blkno, level - 1, bufp);
273	    printf("\n");
274	    charssofar = 0;
275	}
276    }
277    if (level == 0)
278	putchar('\n');
279    return;
280}
281
282
283/*
284 * Print the block pointers for one inode.
285 */
286static void
287printblocks(ino_t inum, union dinode *dp)
288{
289    char *bufp;
290    int i, nfrags;
291    long ndb, offset;
292    ufs2_daddr_t blkno;
293
294    printf("Blocks for inode %d:\n", inum);
295    printf("Direct blocks:\n");
296    ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
297    for (i = 0; i < NDADDR; i++) {
298	if (DIP(dp, di_db[i]) == 0) {
299	    putchar('\n');
300	    return;
301	}
302	if (i > 0)
303	    printf(", ");
304	blkno = DIP(dp, di_db[i]);
305	printf("%jd", (intmax_t)blkno);
306	if (--ndb == 0 && (offset = blkoff(&sblock, DIP(dp, di_size))) != 0) {
307	    nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
308	    printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
309	}
310    }
311    putchar('\n');
312    if (ndb == 0)
313	return;
314
315    bufp = malloc((unsigned int)sblock.fs_bsize);
316    if (bufp == 0)
317	errx(EEXIT, "cannot allocate indirect block buffer");
318    printf("Indirect blocks:\n");
319    for (i = 0; i < NIADDR; i++)
320	printindir(DIP(dp, di_ib[i]), i, bufp);
321    free(bufp);
322}
323
324
325int
326checkactive(void)
327{
328    if (!curinode) {
329	warnx("no current inode\n");
330	return 0;
331    }
332    return 1;
333}
334
335int
336checkactivedir(void)
337{
338    if (!curinode) {
339	warnx("no current inode\n");
340	return 0;
341    }
342    if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
343	warnx("inode %d not a directory", curinum);
344	return 0;
345    }
346    return 1;
347}
348
349int
350printactive(int doblocks)
351{
352    if (!checkactive())
353	return 1;
354    switch (DIP(curinode, di_mode) & IFMT) {
355    case IFDIR:
356    case IFREG:
357    case IFBLK:
358    case IFCHR:
359    case IFLNK:
360    case IFSOCK:
361    case IFIFO:
362	if (doblocks)
363	    printblocks(curinum, curinode);
364	else
365	    printstat("current inode", curinum, curinode);
366	break;
367    case 0:
368	printf("current inode %d: unallocated inode\n", curinum);
369	break;
370    default:
371	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
372	       curinum, DIP(curinode, di_mode) & IFMT, DIP(curinode, di_mode));
373	break;
374    }
375    return 0;
376}
377