1 /* $NetBSD: fsdbutil.c,v 1.17 2007/03/10 01:16:17 hubertf 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.17 2007/03/10 01:16:17 hubertf 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 <fcntl.h> 50 #include <grp.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <unistd.h> 57 #include <err.h> 58 59 #include <ufs/ufs/dinode.h> 60 #include <ufs/ffs/fs.h> 61 62 #include "fsdb.h" 63 #include "fsck.h" 64 65 char ** 66 crack(char *line, int *argc) 67 { 68 static char *argv[8]; 69 int i; 70 char *p, *val; 71 for (p = line, i = 0; p != NULL && i < 8; i++) { 72 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0') 73 /**/ ; 74 if (val) 75 argv[i] = val; 76 else 77 break; 78 } 79 *argc = i; 80 return argv; 81 } 82 83 int 84 argcount(struct cmdtable *cmdp, int argc, char *argv[]) 85 { 86 if (cmdp->minargc == cmdp->maxargc) 87 warnx("command `%s' takes %u arguments", cmdp->cmd, 88 cmdp->minargc - 1); 89 else 90 warnx("command `%s' takes from %u to %u arguments", 91 cmdp->cmd, cmdp->minargc - 1, cmdp->maxargc - 1); 92 93 warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt); 94 return 1; 95 } 96 97 void 98 printstat(const char *cp, ino_t inum, union dinode *dp) 99 { 100 struct group *grp; 101 struct passwd *pw; 102 time_t t; 103 char *p; 104 uint64_t size, blocks; 105 uint16_t mode; 106 uint32_t rdev; 107 uint32_t uid, gid; 108 109 size = iswap64(DIP(dp, size)); 110 blocks = is_ufs2 ? iswap64(DIP(dp, blocks)) : iswap32(DIP(dp, blocks)); 111 mode = iswap16(DIP(dp, mode)); 112 rdev = iswap32(DIP(dp, rdev)); 113 114 printf("%s: ", cp); 115 switch (mode & IFMT) { 116 case IFDIR: 117 puts("directory"); 118 break; 119 case IFREG: 120 puts("regular file"); 121 break; 122 case IFBLK: 123 printf("block special (%d,%d)", major(rdev), minor(rdev)); 124 break; 125 case IFCHR: 126 printf("character special (%d,%d)", major(rdev), minor(rdev)); 127 break; 128 case IFLNK: 129 fputs("symlink", stdout); 130 if (size > 0 && size < sblock->fs_maxsymlinklen && 131 DIP(dp, blocks) == 0) { 132 p = is_ufs2 ? (char *)dp->dp2.di_db : 133 (char *)dp->dp1.di_db; 134 printf(" to `%.*s'\n", (int)size, p); 135 } else 136 putchar('\n'); 137 break; 138 case IFSOCK: 139 puts("socket"); 140 break; 141 case IFIFO: 142 puts("fifo"); 143 break; 144 } 145 printf("I=%llu MODE=%o SIZE=%llu", (unsigned long long)inum, mode, 146 (unsigned long long)size); 147 t = is_ufs2 ? iswap64(dp->dp2.di_mtime) : iswap32(dp->dp1.di_mtime); 148 p = ctime(&t); 149 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 150 iswap32(DIP(dp, mtimensec))); 151 t = is_ufs2 ? iswap64(dp->dp2.di_ctime) : iswap32(dp->dp1.di_ctime); 152 p = ctime(&t); 153 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 154 iswap32(DIP(dp, ctimensec))); 155 t = is_ufs2 ? iswap64(dp->dp2.di_atime) : iswap32(dp->dp1.di_atime); 156 p = ctime(&t); 157 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20], 158 iswap32(DIP(dp,atimensec))); 159 160 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT) 161 uid = iswap16(dp->dp1.di_ouid); 162 else 163 uid = iswap32(DIP(dp, uid)); 164 if ((pw = getpwuid(uid)) != NULL) 165 printf("OWNER=%s ", pw->pw_name); 166 else 167 printf("OWNUID=%u ", uid); 168 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT) 169 gid = iswap16(dp->dp1.di_ogid); 170 else 171 gid = iswap32(DIP(dp, gid)); 172 if ((grp = getgrgid(gid)) != NULL) 173 printf("GRP=%s ", grp->gr_name); 174 else 175 printf("GID=%u ", gid); 176 177 printf("LINKCNT=%hd FLAGS=0x%#x BLKCNT=0x%llx GEN=0x%x\n", 178 iswap16(DIP(dp, nlink)), 179 iswap32(DIP(dp, flags)), (unsigned long long)blocks, 180 iswap32(DIP(dp, gen))); 181 } 182 183 int 184 checkactive(void) 185 { 186 if (!curinode) { 187 warnx("no current inode"); 188 return 0; 189 } 190 return 1; 191 } 192 193 int 194 checkactivedir(void) 195 { 196 if (!curinode) { 197 warnx("no current inode"); 198 return 0; 199 } 200 if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) { 201 warnx("inode %llu not a directory", 202 (unsigned long long)curinum); 203 return 0; 204 } 205 return 1; 206 } 207 208 int 209 printactive(void) 210 { 211 uint16_t mode; 212 213 if (!checkactive()) 214 return 1; 215 mode = iswap16(DIP(curinode, mode)); 216 switch (mode & IFMT) { 217 case IFDIR: 218 case IFREG: 219 case IFBLK: 220 case IFCHR: 221 case IFLNK: 222 case IFSOCK: 223 case IFIFO: 224 printstat("current inode", curinum, curinode); 225 break; 226 case 0: 227 printf("current inode %llu: unallocated inode\n", 228 (unsigned long long)curinum); 229 break; 230 default: 231 printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n", 232 (unsigned long long)curinum, mode & IFMT, mode); 233 break; 234 } 235 return 0; 236 } 237