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