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