Wed Jan 1 12:47:19 2020 UTC ()
fstyp: Consider '@' syntax in device file path for HAMMER2

though devpath is unsupported in NetBSD atm.
taken-from: DragonFlyBSD


(tkusumi)
diff -r1.10 -r1.11 src/usr.sbin/fstyp/fstyp.c

cvs diff -r1.10 -r1.11 src/usr.sbin/fstyp/fstyp.c (expand / switch to unified diff)

--- src/usr.sbin/fstyp/fstyp.c 2020/01/01 09:17:27 1.10
+++ src/usr.sbin/fstyp/fstyp.c 2020/01/01 12:47:19 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: fstyp.c,v 1.10 2020/01/01 09:17:27 tkusumi Exp $ */ 1/* $NetBSD: fstyp.c,v 1.11 2020/01/01 12:47:19 tkusumi Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc. 4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * Copyright (c) 2016 The DragonFly Project 5 * Copyright (c) 2016 The DragonFly Project
6 * Copyright (c) 2014 The FreeBSD Foundation 6 * Copyright (c) 2014 The FreeBSD Foundation
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * This code is derived from software contributed to The NetBSD Foundation 9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>. 10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11 * 11 *
12 * This software was developed by Edward Tomasz Napierala under sponsorship 12 * This software was developed by Edward Tomasz Napierala under sponsorship
13 * from the FreeBSD Foundation. 13 * from the FreeBSD Foundation.
14 * 14 *
@@ -25,28 +25,29 @@ @@ -25,28 +25,29 @@
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 * 35 *
36 */ 36 */
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38__RCSID("$NetBSD: fstyp.c,v 1.10 2020/01/01 09:17:27 tkusumi Exp $"); 38__RCSID("$NetBSD: fstyp.c,v 1.11 2020/01/01 12:47:19 tkusumi Exp $");
39 39
 40#include <sys/param.h>
40#include <sys/disklabel.h> 41#include <sys/disklabel.h>
41#include <sys/dkio.h> 42#include <sys/dkio.h>
42#include <sys/ioctl.h> 43#include <sys/ioctl.h>
43#include <sys/stat.h> 44#include <sys/stat.h>
44#include <err.h> 45#include <err.h>
45#include <errno.h> 46#include <errno.h>
46#include <iconv.h> 47#include <iconv.h>
47#include <locale.h> 48#include <locale.h>
48#include <stdbool.h> 49#include <stdbool.h>
49#include <stddef.h> 50#include <stddef.h>
50#include <stdio.h> 51#include <stdio.h>
51#include <stdlib.h> 52#include <stdlib.h>
52#include <string.h> 53#include <string.h>
@@ -175,27 +176,29 @@ type_check(const char *path, FILE *fp) @@ -175,27 +176,29 @@ type_check(const char *path, FILE *fp)
175 return; 176 return;
176 177
177 error = ioctl(fd, DIOCGDINFO, &dl); 178 error = ioctl(fd, DIOCGDINFO, &dl);
178 if (error != 0) 179 if (error != 0)
179 errx(EXIT_FAILURE, "%s: not a disk", path); 180 errx(EXIT_FAILURE, "%s: not a disk", path);
180} 181}
181 182
182int 183int
183main(int argc, char **argv) 184main(int argc, char **argv)
184{ 185{
185 int ch, error, i, nbytes; 186 int ch, error, i, nbytes;
186 bool ignore_type = false, show_unmountable = false; 187 bool ignore_type = false, show_unmountable = false;
187 char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1]; 188 char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
188 char *path; 189 char fdpath[MAXPATHLEN];
 190 char *p;
 191 const char *path;
189 const char *name = NULL; 192 const char *name = NULL;
190 FILE *fp; 193 FILE *fp;
191 fstyp_function fstyp_f; 194 fstyp_function fstyp_f;
192 fsvtyp_function fsvtyp_f; 195 fsvtyp_function fsvtyp_f;
193 196
194 while ((ch = getopt(argc, argv, "lsu")) != -1) { 197 while ((ch = getopt(argc, argv, "lsu")) != -1) {
195 switch (ch) { 198 switch (ch) {
196 case 'l': 199 case 'l':
197 show_label = true; 200 show_label = true;
198 break; 201 break;
199 case 's': 202 case 's':
200 ignore_type = true; 203 ignore_type = true;
201 break; 204 break;
@@ -223,32 +226,48 @@ main(int argc, char **argv) @@ -223,32 +226,48 @@ main(int argc, char **argv)
223 iconv_t cd; 226 iconv_t cd;
224 227
225 if (fstypes[i].precache_encoding == NULL) 228 if (fstypes[i].precache_encoding == NULL)
226 continue; 229 continue;
227 cd = iconv_open("", fstypes[i].precache_encoding); 230 cd = iconv_open("", fstypes[i].precache_encoding);
228 if (cd == (iconv_t)-1) 231 if (cd == (iconv_t)-1)
229 err(1, "%s: iconv_open %s", fstypes[i].name, 232 err(1, "%s: iconv_open %s", fstypes[i].name,
230 fstypes[i].precache_encoding); 233 fstypes[i].precache_encoding);
231 /* Iconv keeps a small cache of unused encodings. */ 234 /* Iconv keeps a small cache of unused encodings. */
232 iconv_close(cd); 235 iconv_close(cd);
233 } 236 }
234 } 237 }
235 238
236 fp = fopen(path, "r"); 239 /*
237 if (fp == NULL) 240 * DragonFly: Filesystems may have syntax to decorate path.
238 goto fsvtyp; /* DragonFly */ 241 * Make a wild guess.
 242 * XXX devpath is unsupported in NetBSD, but at least parse '@' for fp.
 243 */
 244 strlcpy(fdpath, path, sizeof(fdpath));
 245 p = strchr(fdpath, '@');
 246 if (p)
 247 *p = '\0';
 248
 249 fp = fopen(fdpath, "r");
 250 if (fp == NULL) {
 251 if (strcmp(path, fdpath))
 252 fp = fopen(path, "r");
 253 if (fp == NULL)
 254 goto fsvtyp; /* DragonFly */
 255 else
 256 strlcpy(fdpath, path, sizeof(fdpath));
 257 }
239 258
240 if (ignore_type == false) 259 if (ignore_type == false)
241 type_check(path, fp); 260 type_check(fdpath, fp);
242 261
243 memset(label, '\0', sizeof(label)); 262 memset(label, '\0', sizeof(label));
244 263
245 for (i = 0;; i++) { 264 for (i = 0;; i++) {
246 if (!show_unmountable && fstypes[i].unmountable) 265 if (!show_unmountable && fstypes[i].unmountable)
247 continue; 266 continue;
248 fstyp_f = fstypes[i].function; 267 fstyp_f = fstypes[i].function;
249 if (fstyp_f == NULL) 268 if (fstyp_f == NULL)
250 break; 269 break;
251 270
252 error = fstyp_f(fp, label, sizeof(label)); 271 error = fstyp_f(fp, label, sizeof(label));
253 if (error == 0) { 272 if (error == 0) {
254 name = fstypes[i].name; 273 name = fstypes[i].name;