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