1 /* $NetBSD: fsdbutil.c,v 1.21 2008/12/29 20:02:30 mlelstv Exp $ */
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: fsdbutil.c,v 1.21 2008/12/29 20:02:30 mlelstv Exp $");
37 #include <sys/types.h>
39 #include <sys/param.h>
41 #include <sys/mount.h>
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
59 crack(char *line
, int *argc
)
64 for (p
= line
, i
= 0; p
!= NULL
&& i
< 8; i
++) {
65 while ((val
= strsep(&p
, " \t\n")) != NULL
&& *val
== '\0')
77 argcount(struct cmdtable
*cmdp
, int argc
, char *argv
[])
79 if (cmdp
->minargc
== cmdp
->maxargc
)
80 warnx("command `%s' takes %u arguments", cmdp
->cmd
,
83 warnx("command `%s' takes from %u to %u arguments",
84 cmdp
->cmd
, cmdp
->minargc
- 1, cmdp
->maxargc
- 1);
86 warnx("usage: %s: %s", cmdp
->cmd
, cmdp
->helptxt
);
91 printstat(const char *cp
, ino_t inum
, union dinode
*dp
)
97 uint64_t size
, blocks
;
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
));
108 switch (mode
& IFMT
) {
113 puts("regular file");
116 printf("block special (%llu,%llu)",
117 (unsigned long long)major(rdev
),
118 (unsigned long long)minor(rdev
));
121 printf("character special (%llu,%llu)",
122 (unsigned long long)major(rdev
),
123 (unsigned long long)minor(rdev
));
126 fputs("symlink", stdout
);
127 if (size
> 0 && size
< (uint64_t)sblock
->fs_maxsymlinklen
&&
128 DIP(dp
, blocks
) == 0) {
129 p
= is_ufs2
? (char *)dp
->dp2
.di_db
:
130 (char *)dp
->dp1
.di_db
;
131 printf(" to `%.*s'\n", (int)size
, p
);
142 printf("I=%llu MODE=%o SIZE=%llu", (unsigned long long)inum
, mode
,
143 (unsigned long long)size
);
144 t
= is_ufs2
? iswap64(dp
->dp2
.di_mtime
) : iswap32(dp
->dp1
.di_mtime
);
146 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p
[4], &p
[20],
147 iswap32(DIP(dp
, mtimensec
)));
148 t
= is_ufs2
? iswap64(dp
->dp2
.di_ctime
) : iswap32(dp
->dp1
.di_ctime
);
150 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p
[4], &p
[20],
151 iswap32(DIP(dp
, ctimensec
)));
152 t
= is_ufs2
? iswap64(dp
->dp2
.di_atime
) : iswap32(dp
->dp1
.di_atime
);
154 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p
[4], &p
[20],
155 iswap32(DIP(dp
,atimensec
)));
157 if (!is_ufs2
&& sblock
->fs_old_inodefmt
< FS_44INODEFMT
)
158 uid
= iswap16(dp
->dp1
.di_ouid
);
160 uid
= iswap32(DIP(dp
, uid
));
161 if ((pw
= getpwuid(uid
)) != NULL
)
162 printf("OWNER=%s ", pw
->pw_name
);
164 printf("OWNUID=%u ", uid
);
165 if (!is_ufs2
&& sblock
->fs_old_inodefmt
< FS_44INODEFMT
)
166 gid
= iswap16(dp
->dp1
.di_ogid
);
168 gid
= iswap32(DIP(dp
, gid
));
169 if ((grp
= getgrgid(gid
)) != NULL
)
170 printf("GRP=%s ", grp
->gr_name
);
172 printf("GID=%u ", gid
);
174 printf("LINKCNT=%hd FLAGS=0x%x BLKCNT=0x%llx GEN=0x%x\n",
175 iswap16(DIP(dp
, nlink
)),
176 iswap32(DIP(dp
, flags
)), (unsigned long long)blocks
,
177 iswap32(DIP(dp
, gen
)));
184 warnx("no current inode");
194 warnx("no current inode");
197 if ((iswap16(DIP(curinode
, mode
)) & IFMT
) != IFDIR
) {
198 warnx("inode %llu not a directory",
199 (unsigned long long)curinum
);
212 mode
= iswap16(DIP(curinode
, mode
));
213 switch (mode
& IFMT
) {
221 printstat("current inode", curinum
, curinode
);
224 printf("current inode %llu: unallocated inode\n",
225 (unsigned long long)curinum
);
228 printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n",
229 (unsigned long long)curinum
, mode
& IFMT
, mode
);