xref: /openbsd-src/sbin/fsdb/fsdbutil.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: fsdbutil.c,v 1.15 2013/04/24 13:46:29 deraadt Exp $	*/
2 /*	$NetBSD: fsdbutil.c,v 1.5 1996/09/28 19:30:37 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by John T. Kohl.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/mount.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <grp.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50 
51 #include "fsdb.h"
52 #include "fsck.h"
53 
54 char **
55 crack(char *line, int *argc)
56 {
57 	static char *argv[8];
58 	int i;
59 	char *p, *val;
60 
61 	for (p = line, i = 0; p != NULL && i < 8; i++) {
62 		while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
63 			/**/;
64 		if (val)
65 			argv[i] = val;
66 		else
67 			break;
68 	}
69 	*argc = i;
70 	return argv;
71 }
72 
73 int
74 argcount(struct cmdtable *cmdp, int argc, char *argv[])
75 {
76 	if (cmdp->minargc == cmdp->maxargc)
77 		warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
78 	else
79 		warnx("command `%s' takes from %u to %u arguments",
80 		    cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
81 	warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
82 	return 1;
83 }
84 
85 void
86 printstat(const char *cp, ino_t inum, union dinode *dp)
87 {
88 	struct group *grp;
89 	struct passwd *pw;
90 	time_t t;
91 	char *p;
92 
93 	printf("%s: ", cp);
94 	switch (DIP(dp, di_mode) & IFMT) {
95 	case IFDIR:
96 		puts("directory");
97 		break;
98 	case IFREG:
99 		puts("regular file");
100 		break;
101 	case IFBLK:
102 		printf("block special (%d,%d)",
103 		    (int)major(DIP(dp, di_rdev)), (int)minor(DIP(dp, di_rdev)));
104 		break;
105 	case IFCHR:
106 		printf("character special (%d,%d)",
107 		    (int)major(DIP(dp, di_rdev)), (int)minor(DIP(dp, di_rdev)));
108 		break;
109 	case IFLNK:
110 		fputs("symlink",stdout);
111 		if (DIP(dp, di_size) > 0 &&
112 		    DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
113 		    DIP(dp, di_blocks) == 0) {
114 			char *p = sblock.fs_magic == FS_UFS1_MAGIC ?
115 			    (char *)dp->dp1.di_shortlink :
116 			    (char *)dp->dp2.di_shortlink;
117 			printf(" to `%.*s'\n", (int)DIP(dp, di_size), p);
118 		} else
119 			putchar('\n');
120 		break;
121 	case IFSOCK:
122 		puts("socket");
123 		break;
124 	case IFIFO:
125 		puts("fifo");
126 		break;
127 	}
128 
129 	printf("I=%llu MODE=%o SIZE=%llu", (unsigned long long)inum,
130 	    DIP(dp, di_mode), DIP(dp, di_size));
131 	t = DIP(dp, di_mtime);
132 	p = ctime(&t);
133 	printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
134 	    DIP(dp, di_mtimensec));
135 	t = DIP(dp, di_ctime);
136 	p = ctime(&t);
137 	printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
138 	    DIP(dp, di_ctimensec));
139 	t = DIP(dp, di_atime);
140 	p = ctime(&t);
141 	printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
142 	    DIP(dp, di_atimensec));
143 
144 	if ((pw = getpwuid(DIP(dp, di_uid))))
145 		printf("OWNER=%s ", pw->pw_name);
146 	else
147 		printf("OWNUID=%u ", DIP(dp, di_uid));
148 	if ((grp = getgrgid(DIP(dp, di_gid))))
149 		printf("GRP=%s ", grp->gr_name);
150 	else
151 		printf("GID=%u ", DIP(dp, di_gid));
152 
153 	printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", DIP(dp, di_nlink),
154 	    DIP(dp, di_flags), (unsigned)DIP(dp, di_blocks), DIP(dp, di_gen));
155 }
156 
157 int
158 checkactive(void)
159 {
160 	if (!curinode) {
161 		warnx("no current inode");
162 		return 0;
163 	}
164 	return 1;
165 }
166 
167 int
168 checkactivedir(void)
169 {
170 	if (!curinode) {
171 		warnx("no current inode");
172 		return 0;
173 	}
174 	if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
175 		warnx("inode %llu not a directory",
176 		    (unsigned long long)curinum);
177 		return 0;
178 	}
179 	return 1;
180 }
181 
182 int
183 printactive(void)
184 {
185 	if (!checkactive())
186 		return 1;
187 	switch (DIP(curinode, di_mode) & IFMT) {
188 	case IFDIR:
189 	case IFREG:
190 	case IFBLK:
191 	case IFCHR:
192 	case IFLNK:
193 	case IFSOCK:
194 	case IFIFO:
195 		printstat("current inode", curinum, curinode);
196 		break;
197 	case 0:
198 		printf("current inode %llu: unallocated inode\n",
199 		    (unsigned long long)curinum);
200 		break;
201 	default:
202 		printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n",
203 		    (unsigned long long)curinum, DIP(curinode, di_mode) & IFMT,
204 		    DIP(curinode, di_mode));
205 		break;
206 	}
207 	return 0;
208 }
209