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