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