xref: /dragonfly/usr.sbin/fstyp/fstyp.c (revision 6a3cbbc2)
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 <iconv.h>
39 #include <locale.h>
40 #include <stdbool.h>
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <vis.h>
47 
48 #include "fstyp.h"
49 
50 #define	LABEL_LEN	512
51 
52 bool show_label = false;
53 
54 typedef int (*fstyp_function)(FILE *, char *, size_t, const char *);
55 typedef int (*fsvtyp_function)(const char *, char *, size_t);
56 
57 static struct {
58 	const char	*name;
59 	fstyp_function	function;
60 	bool		unmountable;
61 	const char	*precache_encoding;
62 } fstypes[] = {
63 	{ "apfs", &fstyp_apfs, true, NULL },
64 	{ "cd9660", &fstyp_cd9660, false, NULL },
65 	{ "exfat", &fstyp_exfat, false, EXFAT_ENC },
66 	{ "ext2fs", &fstyp_ext2fs, false, NULL },
67 	{ "hfs+", &fstyp_hfsp, false, NULL },
68 	{ "msdosfs", &fstyp_msdosfs, false, NULL },
69 	{ "ntfs", &fstyp_ntfs, false, NTFS_ENC },
70 	{ "ufs", &fstyp_ufs, false, NULL },
71 	{ "hammer", &fstyp_hammer, false, NULL },
72 	{ "hammer2", &fstyp_hammer2, false, NULL },
73 	{ NULL, NULL, NULL, NULL }
74 };
75 
76 static struct {
77 	const char	*name;
78 	fsvtyp_function	function;
79 	bool		unmountable;
80 	const char	*precache_encoding;
81 } fsvtypes[] = {
82 	{ "hammer", &fsvtyp_hammer, false, NULL }, /* Must be before partial */
83 	{ "hammer(partial)", &fsvtyp_hammer_partial, true, NULL },
84 	{ NULL, NULL, NULL, NULL }
85 };
86 
87 void *
88 read_buf(FILE *fp, off_t off, size_t len)
89 {
90 	int error;
91 	size_t nread;
92 	void *buf;
93 
94 	error = fseek(fp, off, SEEK_SET);
95 	if (error != 0) {
96 		warn("cannot seek to %jd", (uintmax_t)off);
97 		return (NULL);
98 	}
99 
100 	buf = malloc(len);
101 	if (buf == NULL) {
102 		warn("cannot malloc %zd bytes of memory", len);
103 		return (NULL);
104 	}
105 
106 	nread = fread(buf, len, 1, fp);
107 	if (nread != 1) {
108 		free(buf);
109 		if (feof(fp) == 0)
110 			warn("fread");
111 		return (NULL);
112 	}
113 
114 	return (buf);
115 }
116 
117 char *
118 checked_strdup(const char *s)
119 {
120 	char *c;
121 
122 	c = strdup(s);
123 	if (c == NULL)
124 		err(1, "strdup");
125 	return (c);
126 }
127 
128 void
129 rtrim(char *label, size_t size)
130 {
131 	ptrdiff_t i;
132 
133 	for (i = size - 1; i >= 0; i--) {
134 		if (label[i] == '\0')
135 			continue;
136 		else if (label[i] == ' ')
137 			label[i] = '\0';
138 		else
139 			break;
140 	}
141 }
142 
143 static void
144 usage(void)
145 {
146 
147 	fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n");
148 	exit(1);
149 }
150 
151 static void
152 type_check(const char *path, FILE *fp)
153 {
154 	int error, fd;
155 	struct stat sb;
156 	struct partinfo pinfo;
157 
158 	fd = fileno(fp);
159 
160 	error = fstat(fd, &sb);
161 	if (error != 0)
162 		err(1, "%s: fstat", path);
163 
164 	if (S_ISREG(sb.st_mode))
165 		return;
166 
167 	error = ioctl(fd, DIOCGPART, &pinfo);
168 	if (error != 0)
169 		errx(1, "%s: not a disk", path);
170 }
171 
172 int
173 main(int argc, char **argv)
174 {
175 	int ch, error, i, nbytes;
176 	bool ignore_type = false, show_unmountable = false;
177 	char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
178 	char fdpath[MAXPATHLEN];
179 	char *p;
180 	const char *path;
181 	const char *name = NULL;
182 	FILE *fp;
183 	fstyp_function fstyp_f;
184 	fsvtyp_function fsvtyp_f;
185 
186 	while ((ch = getopt(argc, argv, "lsu")) != -1) {
187 		switch (ch) {
188 		case 'l':
189 			show_label = true;
190 			break;
191 		case 's':
192 			ignore_type = true;
193 			break;
194 		case 'u':
195 			show_unmountable = true;
196 			break;
197 		default:
198 			usage();
199 		}
200 	}
201 
202 	argc -= optind;
203 	argv += optind;
204 	if (argc != 1)
205 		usage();
206 
207 	path = argv[0];
208 
209 	if (setlocale(LC_CTYPE, "") == NULL)
210 		err(1, "setlocale");
211 
212 	/*
213 	 * DragonFly: Filesystems may have syntax to decorate path.
214 	 * Make a wild guess.
215 	 */
216 	strlcpy(fdpath, path, sizeof(fdpath));
217 	p = strchr(fdpath, '@');
218 	if (p)
219 		*p = '\0';
220 
221 	fp = fopen(fdpath, "r");
222 	if (fp == NULL) {
223 		if (strcmp(path, fdpath))
224 			fp = fopen(path, "r");
225 		if (fp == NULL)
226 			goto fsvtyp; /* DragonFly */
227 		else
228 			strlcpy(fdpath, path, sizeof(fdpath));
229 	}
230 
231 	if (ignore_type == false)
232 		type_check(fdpath, fp);
233 
234 	memset(label, '\0', sizeof(label));
235 
236 	for (i = 0;; i++) {
237 		if (show_unmountable == false && fstypes[i].unmountable == true)
238 			continue;
239 		fstyp_f = fstypes[i].function;
240 		if (fstyp_f == NULL)
241 			break;
242 
243 		error = fstyp_f(fp, label, sizeof(label), path);
244 		if (error == 0) {
245 			name = fstypes[i].name;
246 			goto done;
247 		}
248 	}
249 fsvtyp:
250 	for (i = 0;; i++) {
251 		if (show_unmountable == false && fsvtypes[i].unmountable == true)
252 			continue;
253 		fsvtyp_f = fsvtypes[i].function;
254 		if (fsvtyp_f == NULL)
255 			break;
256 
257 		error = fsvtyp_f(path, label, sizeof(label));
258 		if (error == 0) {
259 			name = fsvtypes[i].name;
260 			goto done;
261 		}
262 	}
263 
264 	warnx("%s: filesystem not recognized", path);
265 	return (1);
266 done:
267 	if (show_label && label[0] != '\0') {
268 		/*
269 		 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
270 		 *      encodes spaces.
271 		 */
272 		nbytes = strsnvis(strvised, sizeof(strvised), label,
273 		    VIS_GLOB | VIS_NL, "\"'$");
274 		if (nbytes == -1)
275 			err(1, "strsnvis");
276 
277 		printf("%s %s\n", name, strvised);
278 	} else {
279 		printf("%s\n", name);
280 	}
281 
282 	return (0);
283 }
284