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