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