xref: /dragonfly/usr.sbin/fstyp/fstyp.c (revision e5a92d33)
1 /*-
2  * Copyright (c) 2016 The DragonFly Project
3  * Copyright (c) 2014 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/param.h>
33 #include <sys/diskslice.h>
34 #include <sys/ioctl.h>
35 #include <sys/stat.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <stdbool.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <vis.h>
45 
46 #include "fstyp.h"
47 
48 #define	LABEL_LEN	256
49 
50 typedef int (*fstyp_function)(FILE *, char *, size_t, const char *);
51 typedef int (*fsvtyp_function)(const char *, char *, size_t);
52 
53 static struct {
54 	const char	*name;
55 	fstyp_function	function;
56 	bool		unmountable;
57 } fstypes[] = {
58 	{ "cd9660", &fstyp_cd9660, false },
59 	{ "exfat", &fstyp_exfat, false },
60 	{ "ext2fs", &fstyp_ext2fs, false },
61 	{ "msdosfs", &fstyp_msdosfs, false },
62 	{ "ntfs", &fstyp_ntfs, false },
63 	{ "ufs", &fstyp_ufs, false },
64 	{ "hammer", &fstyp_hammer, false },
65 	{ "hammer2", &fstyp_hammer2, false },
66 	{ NULL, NULL, NULL }
67 };
68 
69 static struct {
70 	const char	*name;
71 	fsvtyp_function	function;
72 	bool		unmountable;
73 } fsvtypes[] = {
74 	{ "hammer", &fsvtyp_hammer, false }, /* Must be before partial */
75 	{ "hammer(partial)", &fsvtyp_hammer_partial, true },
76 	// XXX hammer2 not supported yet
77 	{ NULL, NULL, NULL }
78 };
79 
80 void *
81 read_buf(FILE *fp, off_t off, size_t len)
82 {
83 	int error;
84 	size_t nread;
85 	void *buf;
86 
87 	error = fseek(fp, off, SEEK_SET);
88 	if (error != 0) {
89 		warn("cannot seek to %jd", (uintmax_t)off);
90 		return (NULL);
91 	}
92 
93 	buf = malloc(len);
94 	if (buf == NULL) {
95 		warn("cannot malloc %zd bytes of memory", len);
96 		return (NULL);
97 	}
98 
99 	nread = fread(buf, len, 1, fp);
100 	if (nread != 1) {
101 		free(buf);
102 		if (feof(fp) == 0)
103 			warn("fread");
104 		return (NULL);
105 	}
106 
107 	return (buf);
108 }
109 
110 char *
111 checked_strdup(const char *s)
112 {
113 	char *c;
114 
115 	c = strdup(s);
116 	if (c == NULL)
117 		err(1, "strdup");
118 	return (c);
119 }
120 
121 void
122 rtrim(char *label, size_t size)
123 {
124 	ptrdiff_t i;
125 
126 	for (i = size - 1; i >= 0; i--) {
127 		if (label[i] == '\0')
128 			continue;
129 		else if (label[i] == ' ')
130 			label[i] = '\0';
131 		else
132 			break;
133 	}
134 }
135 
136 static void
137 usage(void)
138 {
139 
140 	fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n");
141 	exit(1);
142 }
143 
144 static void
145 type_check(const char *path, FILE *fp)
146 {
147 	int error, fd;
148 	struct stat sb;
149 	struct partinfo pinfo;
150 
151 	fd = fileno(fp);
152 
153 	error = fstat(fd, &sb);
154 	if (error != 0)
155 		err(1, "%s: fstat", path);
156 
157 	if (S_ISREG(sb.st_mode))
158 		return;
159 
160 	error = ioctl(fd, DIOCGPART, &pinfo);
161 	if (error != 0)
162 		errx(1, "%s: not a disk", path);
163 }
164 
165 int
166 main(int argc, char **argv)
167 {
168 	int ch, error, i, nbytes;
169 	bool ignore_type = false, show_label = false, show_unmountable = false;
170 	char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
171 	char fdpath[MAXPATHLEN];
172 	char *p;
173 	const char *path;
174 	const char *name = NULL;
175 	FILE *fp;
176 	fstyp_function fstyp_f;
177 	fsvtyp_function fsvtyp_f;
178 
179 	while ((ch = getopt(argc, argv, "lsu")) != -1) {
180 		switch (ch) {
181 		case 'l':
182 			show_label = true;
183 			break;
184 		case 's':
185 			ignore_type = true;
186 			break;
187 		case 'u':
188 			show_unmountable = true;
189 			break;
190 		default:
191 			usage();
192 		}
193 	}
194 
195 	argc -= optind;
196 	argv += optind;
197 	if (argc != 1)
198 		usage();
199 
200 	path = argv[0];
201 
202 	/*
203 	 * DragonFly: Filesystems may have syntax to decorate path.
204 	 * Make a wild guess.
205 	 */
206 	strlcpy(fdpath, path, sizeof(fdpath));
207 	p = strchr(fdpath, '@');
208 	if (p)
209 		*p = '\0';
210 
211 	fp = fopen(fdpath, "r");
212 	if (fp == NULL) {
213 		if (strcmp(path, fdpath))
214 			fp = fopen(path, "r");
215 		if (fp == NULL)
216 			goto fsvtyp; /* DragonFly */
217 		else
218 			strlcpy(fdpath, path, sizeof(fdpath));
219 	}
220 
221 	if (ignore_type == false)
222 		type_check(fdpath, fp);
223 
224 	memset(label, '\0', sizeof(label));
225 
226 	for (i = 0;; i++) {
227 		if (show_unmountable == false && fstypes[i].unmountable == true)
228 			continue;
229 		fstyp_f = fstypes[i].function;
230 		if (fstyp_f == NULL)
231 			break;
232 
233 		error = fstyp_f(fp, label, sizeof(label), path);
234 		if (error == 0) {
235 			name = fstypes[i].name;
236 			goto done;
237 		}
238 	}
239 fsvtyp:
240 	for (i = 0;; i++) {
241 		if (show_unmountable == false && fsvtypes[i].unmountable == true)
242 			continue;
243 		fsvtyp_f = fsvtypes[i].function;
244 		if (fsvtyp_f == NULL)
245 			break;
246 
247 		error = fsvtyp_f(path, label, sizeof(label));
248 		if (error == 0) {
249 			name = fsvtypes[i].name;
250 			goto done;
251 		}
252 	}
253 
254 	warnx("%s: filesystem not recognized", path);
255 	return (1);
256 done:
257 	if (show_label && label[0] != '\0') {
258 		/*
259 		 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
260 		 *      encodes spaces.
261 		 */
262 		nbytes = strsnvis(strvised, sizeof(strvised), label,
263 		    VIS_GLOB | VIS_NL, "\"'$");
264 		if (nbytes == -1)
265 			err(1, "strsnvis");
266 
267 		printf("%s %s\n", name, strvised);
268 	} else {
269 		printf("%s\n", name);
270 	}
271 
272 	return (0);
273 }
274