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