summaryrefslogtreecommitdiff
path: root/misc/fstype.c
blob: 8b5f9aa2d9cdaaf056ec3bf2312026724ff3dc7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * fstype.c
 * 
 * Copyright (C) 2001 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/types.h>

#include "fsck.h"

struct fs_magic {
	const char 	*fs_name;
	int		offset;
	int		len;
	const char	*magic;
};

#define REISERFS_SUPER_MAGIC_STRING "ReIsErFs"
#define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
#define REISERFS_DISK_OFFSET_IN_BYTES ((64 * 1024) + 52)
/* the spot for the super in versions 3.5 - 3.5.10 (inclusive) */
#define REISERFS_OLD_DISK_OFFSET_IN_BYTES ((8 * 1024) + 52)

struct fs_magic type_array[] = {
	{ "ext2", 1024+56, 2, "\123\357" },
	{ "ext3", 1024+56, 2, "\123\357" },
	{ "reiserfs", REISERFS_DISK_OFFSET_IN_BYTES, 9,
		  REISER2FS_SUPER_MAGIC_STRING },
	{ "reiserfs", REISERFS_DISK_OFFSET_IN_BYTES, 8,
		  REISERFS_SUPER_MAGIC_STRING },
	{ "reiserfs", REISERFS_OLD_DISK_OFFSET_IN_BYTES, 9,
		  REISER2FS_SUPER_MAGIC_STRING },
	{ "reiserfs", REISERFS_OLD_DISK_OFFSET_IN_BYTES, 8,
		  REISERFS_SUPER_MAGIC_STRING },
	{ "minix", 1040, 2, "\177\023" },
	{ "minix", 1040, 2, "\217\023" },
	{ "minix", 1040, 2, "\150\044" },
	{ "minix", 1040, 2, "\170\044" },
	{ "xfs", 0, 4, "XFSB" },
	{ 0, 0, 0, 0 }
};

const char *identify_fs(const char *fs_name, const char *fs_types)
{
	char	buf[73728], *s;
	const char *t;
	struct fs_magic *p;
	int	fd;

	fd = open(fs_name, O_RDONLY);
	if (fd < 0)
		return NULL;
	if (lseek(fd, 0, SEEK_SET) < 0)
		return NULL;
	if (read(fd, buf, sizeof(buf)) != sizeof(buf))
		return NULL;
	close(fd);
	if (!fs_types || !strcmp(fs_types, "auto")) {
		for (p = type_array; p->fs_name; p++) {
			if (memcmp(p->magic, buf+p->offset, p->len) == 0)
				return p->fs_name;
		}
	} else {
		s = string_copy(fs_types);
		for (t = strtok(s, ","); t; t = strtok(NULL, ",")) {
			for (p = type_array; p->fs_name; p++) {
				if (strcmp(p->fs_name, t))
					continue;
				if (memcmp(p->magic, buf+p->offset,
					   p->len) == 0) {
					free(s);
					return p->fs_name;
				}
			}
		}
		free(s);
	}
	return NULL;
}

#ifdef TEST_PROGRAM
int main(int argc, char **argv)
{
	const char	*type;
	
	if (argc < 2 || argc > 3) {
		fprintf(stderr, "Usage: %s [type list] device\n", argv[0]);
		exit(1);
	}
	if (argc == 2) {
		type = identify_fs(argv[1], NULL);
		printf("%s is a %s filesystem\n", argv[1], type);
	} else {
		type = identify_fs(argv[2],argv[1]);
		printf("%s is a %s filesystem\n", argv[2], type);
	}
	return (0);
}
#endif