xref: /freebsd/usr.sbin/fstyp/fstyp.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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 <err.h>
39 #include <errno.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	256
51 
52 typedef int (*fstyp_function)(FILE *, char *, size_t);
53 
54 static struct {
55 	const char	*name;
56 	fstyp_function	function;
57 	bool		unmountable;
58 } fstypes[] = {
59 	{ "cd9660", &fstyp_cd9660, false },
60 	{ "ext2fs", &fstyp_ext2fs, false },
61 	{ "geli", &fstyp_geli, true },
62 	{ "msdosfs", &fstyp_msdosfs, false },
63 	{ "ntfs", &fstyp_ntfs, false },
64 	{ "ufs", &fstyp_ufs, false },
65 #ifdef HAVE_ZFS
66 	{ "zfs", &fstyp_zfs, true },
67 #endif
68 	{ NULL, NULL, NULL }
69 };
70 
71 void *
72 read_buf(FILE *fp, off_t off, size_t len)
73 {
74 	int error;
75 	size_t nread;
76 	void *buf;
77 
78 	error = fseek(fp, off, SEEK_SET);
79 	if (error != 0) {
80 		warn("cannot seek to %jd", (uintmax_t)off);
81 		return (NULL);
82 	}
83 
84 	buf = malloc(len);
85 	if (buf == NULL) {
86 		warn("cannot malloc %zd bytes of memory", len);
87 		return (NULL);
88 	}
89 
90 	nread = fread(buf, len, 1, fp);
91 	if (nread != 1) {
92 		free(buf);
93 		if (feof(fp) == 0)
94 			warn("fread");
95 		return (NULL);
96 	}
97 
98 	return (buf);
99 }
100 
101 char *
102 checked_strdup(const char *s)
103 {
104 	char *c;
105 
106 	c = strdup(s);
107 	if (c == NULL)
108 		err(1, "strdup");
109 	return (c);
110 }
111 
112 void
113 rtrim(char *label, size_t size)
114 {
115 	ptrdiff_t i;
116 
117 	for (i = size - 1; i >= 0; i--) {
118 		if (label[i] == '\0')
119 			continue;
120 		else if (label[i] == ' ')
121 			label[i] = '\0';
122 		else
123 			break;
124 	}
125 }
126 
127 static void
128 usage(void)
129 {
130 
131 	fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n");
132 	exit(1);
133 }
134 
135 static void
136 type_check(const char *path, FILE *fp)
137 {
138 	int error, fd;
139 	off_t mediasize;
140 	struct stat sb;
141 
142 	fd = fileno(fp);
143 
144 	error = fstat(fd, &sb);
145 	if (error != 0)
146 		err(1, "%s: fstat", path);
147 
148 	if (S_ISREG(sb.st_mode))
149 		return;
150 
151 	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
152 	if (error != 0)
153 		errx(1, "%s: not a disk", path);
154 }
155 
156 int
157 main(int argc, char **argv)
158 {
159 	int ch, error, i, nbytes;
160 	bool ignore_type = false, show_label = false, show_unmountable = false;
161 	char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
162 	char *path;
163 	FILE *fp;
164 	fstyp_function fstyp_f;
165 
166 	while ((ch = getopt(argc, argv, "lsu")) != -1) {
167 		switch (ch) {
168 		case 'l':
169 			show_label = true;
170 			break;
171 		case 's':
172 			ignore_type = true;
173 			break;
174 		case 'u':
175 			show_unmountable = true;
176 			break;
177 		default:
178 			usage();
179 		}
180 	}
181 
182 	argc -= optind;
183 	argv += optind;
184 	if (argc != 1)
185 		usage();
186 
187 	path = argv[0];
188 
189 	fp = fopen(path, "r");
190 	if (fp == NULL)
191 		err(1, "%s", path);
192 
193 	error = cap_enter();
194 	if (error != 0 && errno != ENOSYS)
195 		err(1, "cap_enter");
196 
197 	if (ignore_type == false)
198 		type_check(path, fp);
199 
200 	memset(label, '\0', sizeof(label));
201 
202 	for (i = 0;; i++) {
203 		if (show_unmountable == false && fstypes[i].unmountable == true)
204 			continue;
205 		fstyp_f = fstypes[i].function;
206 		if (fstyp_f == NULL)
207 			break;
208 
209 		error = fstyp_f(fp, label, sizeof(label));
210 		if (error == 0)
211 			break;
212 	}
213 
214 	if (fstypes[i].name == NULL) {
215 		warnx("%s: filesystem not recognized", path);
216 		return (1);
217 	}
218 
219 	if (show_label && label[0] != '\0') {
220 		/*
221 		 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
222 		 *      encodes spaces.
223 		 */
224 		nbytes = strsnvis(strvised, sizeof(strvised), label,
225 		    VIS_GLOB | VIS_NL, "\"'$");
226 		if (nbytes == -1)
227 			err(1, "strsnvis");
228 
229 		printf("%s %s\n", fstypes[i].name, strvised);
230 	} else {
231 		printf("%s\n", fstypes[i].name);
232 	}
233 
234 	return (0);
235 }
236