]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsdb/fsdbutil.c
MFV r329799, r329800:
[FreeBSD/FreeBSD.git] / sbin / fsdb / fsdbutil.c
1 /*      $NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $     */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  *  Copyright (c) 1995 John T. Kohl
7  *  All rights reserved.
8  * 
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. The name of the author may not be used to endorse or promote products
18  *     derived from this software without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <grp.h>
42 #include <pwd.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <time.h>
46 #include <timeconv.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50
51 #include <sys/ioctl.h>
52
53 #include "fsdb.h"
54 #include "fsck.h"
55
56 static int charsperline(void);
57 static void printindir(ufs2_daddr_t blk, int level, char *bufp);
58 static void printblocks(ino_t inum, union dinode *dp);
59
60 char **
61 crack(char *line, int *argc)
62 {
63     static char *argv[8];
64     int i;
65     char *p, *val;
66     for (p = line, i = 0; p != NULL && i < 8; i++) {
67         while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
68             /**/;
69         if (val)
70             argv[i] = val;
71         else
72             break;
73     }
74     *argc = i;
75     return argv;
76 }
77
78 char **
79 recrack(char *line, int *argc, int argc_max)
80 {
81     static char *argv[8];
82     int i;
83     char *p, *val;
84     for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
85         while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
86             /**/;
87         if (val)
88             argv[i] = val;
89         else
90             break;
91     }
92     argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
93     argv[i][strcspn(argv[i], "\n")] = '\0';
94     *argc = i + 1;
95     return argv;
96 }
97
98 int
99 argcount(struct cmdtable *cmdp, int argc, char *argv[])
100 {
101     if (cmdp->minargc == cmdp->maxargc)
102         warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
103             cmdp->minargc-1, argc-1);
104     else
105         warnx("command `%s' takes from %u to %u arguments",
106               cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
107             
108     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
109     return 1;
110 }
111
112 void
113 printstat(const char *cp, ino_t inum, union dinode *dp)
114 {
115     struct group *grp;
116     struct passwd *pw;
117     ufs2_daddr_t blocks;
118     int64_t gen;
119     char *p;
120     time_t t;
121
122     printf("%s: ", cp);
123     switch (DIP(dp, di_mode) & IFMT) {
124     case IFDIR:
125         puts("directory");
126         break;
127     case IFREG:
128         puts("regular file");
129         break;
130     case IFBLK:
131         printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
132         break;
133     case IFCHR:
134         printf("character special (%#jx)", DIP(dp, di_rdev));
135         break;
136     case IFLNK:
137         fputs("symlink",stdout);
138         if (DIP(dp, di_size) > 0 &&
139             DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
140             DIP(dp, di_blocks) == 0) {
141             if (sblock.fs_magic == FS_UFS1_MAGIC)
142                 p = (caddr_t)dp->dp1.di_db;
143             else
144                 p = (caddr_t)dp->dp2.di_db;
145             printf(" to `%.*s'\n", (int) DIP(dp, di_size), p);
146         } else {
147             putchar('\n');
148         }
149         break;
150     case IFSOCK:
151         puts("socket");
152         break;
153     case IFIFO:
154         puts("fifo");
155         break;
156     }
157     printf("I=%ju MODE=%o SIZE=%ju", (uintmax_t)inum, DIP(dp, di_mode),
158         (uintmax_t)DIP(dp, di_size));
159     if (sblock.fs_magic != FS_UFS1_MAGIC) {
160         t = _time64_to_time(dp->dp2.di_birthtime);
161         p = ctime(&t);
162         printf("\n\tBTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
163            dp->dp2.di_birthnsec);
164     }
165     if (sblock.fs_magic == FS_UFS1_MAGIC)
166         t = _time32_to_time(dp->dp1.di_mtime);
167     else
168         t = _time64_to_time(dp->dp2.di_mtime);
169     p = ctime(&t);
170     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
171            DIP(dp, di_mtimensec));
172     if (sblock.fs_magic == FS_UFS1_MAGIC)
173         t = _time32_to_time(dp->dp1.di_ctime);
174     else
175         t = _time64_to_time(dp->dp2.di_ctime);
176     p = ctime(&t);
177     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
178            DIP(dp, di_ctimensec));
179     if (sblock.fs_magic == FS_UFS1_MAGIC)
180         t = _time32_to_time(dp->dp1.di_atime);
181     else
182         t = _time64_to_time(dp->dp2.di_atime);
183     p = ctime(&t);
184     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
185            DIP(dp, di_atimensec));
186
187     if ((pw = getpwuid(DIP(dp, di_uid))))
188         printf("OWNER=%s ", pw->pw_name);
189     else
190         printf("OWNUID=%u ", DIP(dp, di_uid));
191     if ((grp = getgrgid(DIP(dp, di_gid))))
192         printf("GRP=%s ", grp->gr_name);
193     else
194         printf("GID=%u ", DIP(dp, di_gid));
195
196     blocks = DIP(dp, di_blocks);
197     gen = DIP(dp, di_gen);
198     printf("LINKCNT=%d FLAGS=%#x BLKCNT=%jx GEN=%jx\n", DIP(dp, di_nlink),
199         DIP(dp, di_flags), (intmax_t)blocks, (intmax_t)gen);
200 }
201
202
203 /*
204  * Determine the number of characters in a
205  * single line.
206  */
207
208 static int
209 charsperline(void)
210 {
211         int columns;
212         char *cp;
213         struct winsize ws;
214
215         columns = 0;
216         if (ioctl(0, TIOCGWINSZ, &ws) != -1)
217                 columns = ws.ws_col;
218         if (columns == 0 && (cp = getenv("COLUMNS")))
219                 columns = atoi(cp);
220         if (columns == 0)
221                 columns = 80;   /* last resort */
222         return (columns);
223 }
224
225
226 /*
227  * Recursively print a list of indirect blocks.
228  */
229 static void
230 printindir(ufs2_daddr_t blk, int level, char *bufp)
231 {
232     struct bufarea buf, *bp;
233     char tempbuf[32];           /* enough to print an ufs2_daddr_t */
234     int i, j, cpl, charssofar;
235     ufs2_daddr_t blkno;
236
237     if (blk == 0)
238         return;
239     printf("%jd (%d) =>\n", (intmax_t)blk, level);
240     if (level == 0) {
241         /* for the final indirect level, don't use the cache */
242         bp = &buf;
243         bp->b_un.b_buf = bufp;
244         initbarea(bp, BT_UNKNOWN);
245
246         getblk(bp, blk, sblock.fs_bsize);
247     } else
248         bp = getdatablk(blk, sblock.fs_bsize, BT_UNKNOWN);
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  */
286 static void
287 printblocks(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 %ju:\n", (uintmax_t)inum);
295     printf("Direct blocks:\n");
296     ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
297     for (i = 0; i < UFS_NDADDR && i < ndb; i++) {
298         if (i > 0)
299             printf(", ");
300         blkno = DIP(dp, di_db[i]);
301         printf("%jd", (intmax_t)blkno);
302     }
303     if (ndb <= UFS_NDADDR) {
304         offset = blkoff(&sblock, DIP(dp, di_size));
305         if (offset != 0) {
306             nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
307             printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
308         }
309     }
310     putchar('\n');
311     if (ndb <= UFS_NDADDR)
312         return;
313
314     bufp = malloc((unsigned int)sblock.fs_bsize);
315     if (bufp == NULL)
316         errx(EEXIT, "cannot allocate indirect block buffer");
317     printf("Indirect blocks:\n");
318     for (i = 0; i < UFS_NIADDR; i++)
319         printindir(DIP(dp, di_ib[i]), i, bufp);
320     free(bufp);
321 }
322
323
324 int
325 checkactive(void)
326 {
327     if (!curinode) {
328         warnx("no current inode\n");
329         return 0;
330     }
331     return 1;
332 }
333
334 int
335 checkactivedir(void)
336 {
337     if (!curinode) {
338         warnx("no current inode\n");
339         return 0;
340     }
341     if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
342         warnx("inode %ju not a directory", (uintmax_t)curinum);
343         return 0;
344     }
345     return 1;
346 }
347
348 int
349 printactive(int doblocks)
350 {
351     if (!checkactive())
352         return 1;
353     switch (DIP(curinode, di_mode) & IFMT) {
354     case IFDIR:
355     case IFREG:
356     case IFBLK:
357     case IFCHR:
358     case IFLNK:
359     case IFSOCK:
360     case IFIFO:
361         if (doblocks)
362             printblocks(curinum, curinode);
363         else
364             printstat("current inode", curinum, curinode);
365         break;
366     case 0:
367         printf("current inode %ju: unallocated inode\n", (uintmax_t)curinum);
368         break;
369     default:
370         printf("current inode %ju: screwy itype 0%o (mode 0%o)?\n",
371             (uintmax_t)curinum, DIP(curinode, di_mode) & IFMT,
372             DIP(curinode, di_mode));
373         break;
374     }
375     return 0;
376 }