2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * fsmagic - magic based on filesystem info - directory, special files, etc.
35 FILE_RCSID("@(#)$File: fsmagic.c,v 1.75 2014/12/04 15:56:46 christos Exp $")
44 /* Since major is a function on SVR4, we cannot use `ifndef major'. */
46 # include <sys/mkdev.h>
49 #ifdef MAJOR_IN_SYSMACROS
50 # include <sys/sysmacros.h>
53 #ifdef major /* Might be defined in sys/types.h. */
57 # define WIN32_LEAN_AND_MEAN
62 # define major(dev) (((dev) >> 8) & 0xff)
63 # define minor(dev) ((dev) & 0xff)
68 bad_link(struct magic_set *ms, int err, char *buf)
70 int mime = ms->flags & MAGIC_MIME;
71 if ((mime & MAGIC_MIME_TYPE) &&
72 file_printf(ms, "inode/symlink")
76 if (ms->flags & MAGIC_ERROR) {
78 "broken symbolic link to %s", buf);
81 if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
88 handle_mime(struct magic_set *ms, int mime, const char *str)
90 if ((mime & MAGIC_MIME_TYPE)) {
91 if (file_printf(ms, "inode/%s", str) == -1)
93 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
97 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
103 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
106 int mime = ms->flags & MAGIC_MIME;
110 struct stat tstatbuf;
113 if (ms->flags & MAGIC_APPLE)
118 #define COMMA (did++ ? ", " : "")
120 * Fstat is cheaper but fails for files you don't have read perms on.
121 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
124 if ((ms->flags & MAGIC_SYMLINK) == 0)
128 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
132 HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
133 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
135 if (hFile != INVALID_HANDLE_VALUE) {
137 * Stat failed, but we can still open it - assume it's
138 * a block device, if nothing else.
141 sb->st_mode = S_IFBLK;
144 switch (GetFileType(hFile)) {
146 sb->st_mode |= S_IFCHR;
147 sb->st_mode &= ~S_IFREG;
150 sb->st_mode |= S_IFIFO;
151 sb->st_mode &= ~S_IFREG;
160 if (ms->flags & MAGIC_ERROR) {
161 file_error(ms, errno, "cannot stat `%s'", fn);
164 if (file_printf(ms, "cannot open `%s' (%s)",
165 fn, strerror(errno)) == -1)
173 if (sb->st_mode & S_ISUID)
174 if (file_printf(ms, "%ssetuid", COMMA) == -1)
178 if (sb->st_mode & S_ISGID)
179 if (file_printf(ms, "%ssetgid", COMMA) == -1)
183 if (sb->st_mode & S_ISVTX)
184 if (file_printf(ms, "%ssticky", COMMA) == -1)
189 switch (sb->st_mode & S_IFMT) {
192 if (handle_mime(ms, mime, "directory") == -1)
194 } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
200 * If -s has been specified, treat character special files
201 * like ordinary files. Otherwise, just report that they
202 * are block special files and go on to the next file.
204 if ((ms->flags & MAGIC_DEVICES) != 0) {
209 if (handle_mime(ms, mime, "chardevice") == -1)
212 #ifdef HAVE_STRUCT_STAT_ST_RDEV
214 if (file_printf(ms, "%scharacter special (%d/%d/%d)",
215 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
216 dv_subunit(sb->st_rdev)) == -1)
219 if (file_printf(ms, "%scharacter special (%ld/%ld)",
220 COMMA, (long)major(sb->st_rdev),
221 (long)minor(sb->st_rdev)) == -1)
225 if (file_printf(ms, "%scharacter special", COMMA) == -1)
234 * If -s has been specified, treat block special files
235 * like ordinary files. Otherwise, just report that they
236 * are block special files and go on to the next file.
238 if ((ms->flags & MAGIC_DEVICES) != 0) {
243 if (handle_mime(ms, mime, "blockdevice") == -1)
246 #ifdef HAVE_STRUCT_STAT_ST_RDEV
248 if (file_printf(ms, "%sblock special (%d/%d/%d)",
249 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
250 dv_subunit(sb->st_rdev)) == -1)
253 if (file_printf(ms, "%sblock special (%ld/%ld)",
254 COMMA, (long)major(sb->st_rdev),
255 (long)minor(sb->st_rdev)) == -1)
259 if (file_printf(ms, "%sblock special", COMMA) == -1)
265 /* TODO add code to handle V7 MUX and Blit MUX files */
268 if((ms->flags & MAGIC_DEVICES) != 0)
271 if (handle_mime(ms, mime, "fifo") == -1)
273 } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
280 if (handle_mime(ms, mime, "door") == -1)
282 } else if (file_printf(ms, "%sdoor", COMMA) == -1)
288 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
289 if (ms->flags & MAGIC_ERROR) {
290 file_error(ms, errno, "unreadable symlink `%s'",
295 if (handle_mime(ms, mime, "symlink") == -1)
297 } else if (file_printf(ms,
298 "%sunreadable symlink `%s' (%s)", COMMA, fn,
299 strerror(errno)) == -1)
303 buf[nch] = '\0'; /* readlink(2) does not do this */
305 /* If broken symlink, say so and quit early. */
307 if (stat(buf, &tstatbuf) < 0)
308 return bad_link(ms, errno, buf);
311 char buf2[BUFSIZ+BUFSIZ+4];
313 if ((tmp = strrchr(fn, '/')) == NULL) {
314 tmp = buf; /* in current directory anyway */
316 if (tmp - fn + 1 > BUFSIZ) {
317 if (ms->flags & MAGIC_ERROR) {
319 "path too long: `%s'", buf);
323 if (handle_mime(ms, mime,
324 "x-path-too-long") == -1)
326 } else if (file_printf(ms,
327 "%spath too long: `%s'", COMMA,
333 (void)strlcpy(buf2, fn, sizeof buf2);
334 buf2[tmp - fn + 1] = '\0';
335 /* plus (rel) link */
336 (void)strlcat(buf2, buf, sizeof buf2);
339 if (stat(tmp, &tstatbuf) < 0)
340 return bad_link(ms, errno, buf);
343 /* Otherwise, handle it. */
344 if ((ms->flags & MAGIC_SYMLINK) != 0) {
346 ms->flags &= MAGIC_SYMLINK;
347 p = magic_file(ms, buf);
348 ms->flags |= MAGIC_SYMLINK;
351 } else { /* just print what it points to */
353 if (handle_mime(ms, mime, "symlink") == -1)
355 } else if (file_printf(ms, "%ssymbolic link to %s",
365 if (handle_mime(ms, mime, "socket") == -1)
367 } else if (file_printf(ms, "%ssocket", COMMA) == -1)
374 * regular file, check next possibility
376 * If stat() tells us the file has zero length, report here that
377 * the file is empty, so we can skip all the work of opening and
379 * But if the -s option has been given, we skip this
380 * optimization, since on some systems, stat() reports zero
381 * size for raw disk partitions. (If the block special device
382 * really has zero length, the fact that it is empty will be
383 * detected and reported correctly when we read the file.)
385 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
387 if (handle_mime(ms, mime, "x-empty") == -1)
389 } else if (file_printf(ms, "%sempty", COMMA) == -1)
397 file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
402 if (!mime && did && ret == 0) {
403 if (file_printf(ms, " ") == -1)