fsdbutil.c revision 1.19 1 /* $NetBSD: fsdbutil.c,v 1.19 2008/07/08 07:53:08 simonb Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by John T. Kohl.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsdbutil.c,v 1.19 2008/07/08 07:53:08 simonb Exp $");
35 #endif /* not lint */
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/mount.h>
42 #include <fcntl.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <unistd.h>
50 #include <err.h>
51
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
54
55 #include "fsdb.h"
56 #include "fsck.h"
57
58 char **
59 crack(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
76 int
77 argcount(struct cmdtable *cmdp, int argc, char *argv[])
78 {
79 if (cmdp->minargc == cmdp->maxargc)
80 warnx("command `%s' takes %u arguments", cmdp->cmd,
81 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(const char *cp, ino_t inum, union dinode *dp)
92 {
93 struct group *grp;
94 struct passwd *pw;
95 time_t t;
96 char *p;
97 uint64_t size, blocks;
98 uint16_t mode;
99 uint32_t rdev;
100 uint32_t uid, gid;
101
102 size = iswap64(DIP(dp, size));
103 blocks = is_ufs2 ? iswap64(DIP(dp, blocks)) : iswap32(DIP(dp, blocks));
104 mode = iswap16(DIP(dp, mode));
105 rdev = iswap32(DIP(dp, rdev));
106
107 printf("%s: ", cp);
108 switch (mode & IFMT) {
109 case IFDIR:
110 puts("directory");
111 break;
112 case IFREG:
113 puts("regular file");
114 break;
115 case IFBLK:
116 printf("block special (%d,%d)", major(rdev), minor(rdev));
117 break;
118 case IFCHR:
119 printf("character special (%d,%d)", major(rdev), minor(rdev));
120 break;
121 case IFLNK:
122 fputs("symlink", stdout);
123 if (size > 0 && size < sblock->fs_maxsymlinklen &&
124 DIP(dp, blocks) == 0) {
125 p = is_ufs2 ? (char *)dp->dp2.di_db :
126 (char *)dp->dp1.di_db;
127 printf(" to `%.*s'\n", (int)size, p);
128 } else
129 putchar('\n');
130 break;
131 case IFSOCK:
132 puts("socket");
133 break;
134 case IFIFO:
135 puts("fifo");
136 break;
137 }
138 printf("I=%llu MODE=%o SIZE=%llu", (unsigned long long)inum, mode,
139 (unsigned long long)size);
140 t = is_ufs2 ? iswap64(dp->dp2.di_mtime) : iswap32(dp->dp1.di_mtime);
141 p = ctime(&t);
142 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
143 iswap32(DIP(dp, mtimensec)));
144 t = is_ufs2 ? iswap64(dp->dp2.di_ctime) : iswap32(dp->dp1.di_ctime);
145 p = ctime(&t);
146 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
147 iswap32(DIP(dp, ctimensec)));
148 t = is_ufs2 ? iswap64(dp->dp2.di_atime) : iswap32(dp->dp1.di_atime);
149 p = ctime(&t);
150 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
151 iswap32(DIP(dp,atimensec)));
152
153 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT)
154 uid = iswap16(dp->dp1.di_ouid);
155 else
156 uid = iswap32(DIP(dp, uid));
157 if ((pw = getpwuid(uid)) != NULL)
158 printf("OWNER=%s ", pw->pw_name);
159 else
160 printf("OWNUID=%u ", uid);
161 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT)
162 gid = iswap16(dp->dp1.di_ogid);
163 else
164 gid = iswap32(DIP(dp, gid));
165 if ((grp = getgrgid(gid)) != NULL)
166 printf("GRP=%s ", grp->gr_name);
167 else
168 printf("GID=%u ", gid);
169
170 printf("LINKCNT=%hd FLAGS=0x%x BLKCNT=0x%llx GEN=0x%x\n",
171 iswap16(DIP(dp, nlink)),
172 iswap32(DIP(dp, flags)), (unsigned long long)blocks,
173 iswap32(DIP(dp, gen)));
174 }
175
176 int
177 checkactive(void)
178 {
179 if (!curinode) {
180 warnx("no current inode");
181 return 0;
182 }
183 return 1;
184 }
185
186 int
187 checkactivedir(void)
188 {
189 if (!curinode) {
190 warnx("no current inode");
191 return 0;
192 }
193 if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) {
194 warnx("inode %llu not a directory",
195 (unsigned long long)curinum);
196 return 0;
197 }
198 return 1;
199 }
200
201 int
202 printactive(void)
203 {
204 uint16_t mode;
205
206 if (!checkactive())
207 return 1;
208 mode = iswap16(DIP(curinode, mode));
209 switch (mode & IFMT) {
210 case IFDIR:
211 case IFREG:
212 case IFBLK:
213 case IFCHR:
214 case IFLNK:
215 case IFSOCK:
216 case IFIFO:
217 printstat("current inode", curinum, curinode);
218 break;
219 case 0:
220 printf("current inode %llu: unallocated inode\n",
221 (unsigned long long)curinum);
222 break;
223 default:
224 printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n",
225 (unsigned long long)curinum, mode & IFMT, mode);
226 break;
227 }
228 return 0;
229 }
230