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