1 /* $NetBSD: fsdbutil.c,v 1.23 2021/05/29 16:51:25 christos 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.23 2021/05/29 16:51:25 christos 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 (%llu,%llu)", 117 (unsigned long long)major(rdev), 118 (unsigned long long)minor(rdev)); 119 break; 120 case IFCHR: 121 printf("character special (%llu,%llu)", 122 (unsigned long long)major(rdev), 123 (unsigned long long)minor(rdev)); 124 break; 125 case IFLNK: 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); 132 } else 133 putchar('\n'); 134 break; 135 case IFSOCK: 136 puts("socket"); 137 break; 138 case IFIFO: 139 puts("fifo"); 140 break; 141 } 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); 145 p = ctime(&t); 146 printf("\n\t MTIME=%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); 149 p = ctime(&t); 150 printf("\n\t CTIME=%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); 153 p = ctime(&t); 154 printf("\n\t ATIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 155 iswap32(DIP(dp,atimensec))); 156 if (is_ufs2) { 157 t = iswap64(dp->dp2.di_birthtime); 158 p = ctime(&t); 159 printf("\n\tBIRTHTIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20], 160 iswap32(dp->dp2.di_birthnsec)); 161 } else { 162 printf("\n"); 163 } 164 165 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT) 166 uid = iswap16(dp->dp1.di_ouid); 167 else 168 uid = iswap32(DIP(dp, uid)); 169 if ((pw = getpwuid(uid)) != NULL) 170 printf("OWNER=%s ", pw->pw_name); 171 else 172 printf("OWNUID=%u ", uid); 173 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT) 174 gid = iswap16(dp->dp1.di_ogid); 175 else 176 gid = iswap32(DIP(dp, gid)); 177 if ((grp = getgrgid(gid)) != NULL) 178 printf("GRP=%s ", grp->gr_name); 179 else 180 printf("GID=%u ", gid); 181 182 printf("LINKCNT=%hd FLAGS=0x%x BLKCNT=0x%llx GEN=0x%x\n", 183 iswap16(DIP(dp, nlink)), 184 iswap32(DIP(dp, flags)), (unsigned long long)blocks, 185 iswap32(DIP(dp, gen))); 186 } 187 188 int 189 checkactive(void) 190 { 191 if (!curinode) { 192 warnx("no current inode"); 193 return 0; 194 } 195 return 1; 196 } 197 198 int 199 checkactivedir(void) 200 { 201 if (!curinode) { 202 warnx("no current inode"); 203 return 0; 204 } 205 if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) { 206 warnx("inode %llu not a directory", 207 (unsigned long long)curinum); 208 return 0; 209 } 210 return 1; 211 } 212 213 int 214 printactive(void) 215 { 216 uint16_t mode; 217 218 if (!checkactive()) 219 return 1; 220 mode = iswap16(DIP(curinode, mode)); 221 switch (mode & IFMT) { 222 case IFDIR: 223 case IFREG: 224 case IFBLK: 225 case IFCHR: 226 case IFLNK: 227 case IFSOCK: 228 case IFIFO: 229 printstat("current inode", curinum, curinode); 230 break; 231 case 0: 232 printf("current inode %llu: unallocated inode\n", 233 (unsigned long long)curinum); 234 break; 235 default: 236 printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n", 237 (unsigned long long)curinum, mode & IFMT, mode); 238 break; 239 } 240 return 0; 241 } 242