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