1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino * Copyright (c) 2010 The DragonFly Project. All rights reserved.
3*86d7f5d3SJohn Marino *
4*86d7f5d3SJohn Marino * This code is derived from software contributed to The DragonFly Project
5*86d7f5d3SJohn Marino * by Ákos Kovács <akoskovacs@gmx.com>
6*86d7f5d3SJohn Marino *
7*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
8*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
9*86d7f5d3SJohn Marino * are met:
10*86d7f5d3SJohn Marino *
11*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
12*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
13*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
14*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in
15*86d7f5d3SJohn Marino * the documentation and/or other materials provided with the
16*86d7f5d3SJohn Marino * distribution.
17*86d7f5d3SJohn Marino * 3. Neither the name of The DragonFly Project nor the names of its
18*86d7f5d3SJohn Marino * contributors may be used to endorse or promote products derived
19*86d7f5d3SJohn Marino * from this software without specific, prior written permission.
20*86d7f5d3SJohn Marino *
21*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*86d7f5d3SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*86d7f5d3SJohn Marino * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*86d7f5d3SJohn Marino * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*86d7f5d3SJohn Marino * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*86d7f5d3SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*86d7f5d3SJohn Marino * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*86d7f5d3SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*86d7f5d3SJohn Marino * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*86d7f5d3SJohn Marino * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*86d7f5d3SJohn Marino * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*86d7f5d3SJohn Marino * SUCH DAMAGE.
33*86d7f5d3SJohn Marino */
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino #include <sys/stat.h>
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino #include <errno.h>
38*86d7f5d3SJohn Marino
39*86d7f5d3SJohn Marino #include "libfsid.h"
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino static struct fs_type fs_types[] = {
42*86d7f5d3SJohn Marino { "HAMMER", hammer_probe, hammer_volname },
43*86d7f5d3SJohn Marino { "UFS", ufs_probe, ufs_volname },
44*86d7f5d3SJohn Marino { "CD9660", cd9660_probe, cd9660_volname },
45*86d7f5d3SJohn Marino { "EXT2", ext2_probe, ext2_volname },
46*86d7f5d3SJohn Marino { "MSDOSFS",msdosfs_probe, msdosfs_volname },
47*86d7f5d3SJohn Marino { NULL, NULL, NULL }
48*86d7f5d3SJohn Marino };
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino const char *
fsid_fsname(fsid_t id)51*86d7f5d3SJohn Marino fsid_fsname(fsid_t id)
52*86d7f5d3SJohn Marino {
53*86d7f5d3SJohn Marino if (id < 1)
54*86d7f5d3SJohn Marino return 0;
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino return fs_types[id-1].fs_name;
57*86d7f5d3SJohn Marino }
58*86d7f5d3SJohn Marino
59*86d7f5d3SJohn Marino int
fsid_fs_count(void)60*86d7f5d3SJohn Marino fsid_fs_count(void)
61*86d7f5d3SJohn Marino {
62*86d7f5d3SJohn Marino int count;
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino for (count = 0; fs_types[count].fs_name != NULL; count++)
65*86d7f5d3SJohn Marino ; /* nothing */
66*86d7f5d3SJohn Marino
67*86d7f5d3SJohn Marino return count;
68*86d7f5d3SJohn Marino }
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino fsid_t
fsid_probe(const char * dev,const char * fs_type)71*86d7f5d3SJohn Marino fsid_probe(const char *dev, const char *fs_type)
72*86d7f5d3SJohn Marino {
73*86d7f5d3SJohn Marino int i;
74*86d7f5d3SJohn Marino
75*86d7f5d3SJohn Marino if (dev == NULL || fs_type == NULL)
76*86d7f5d3SJohn Marino return FSID_UNKNOWN;
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino for (i = 0; fs_types[i].fs_name != NULL; i++) {
79*86d7f5d3SJohn Marino if ((strcmp(fs_type, fs_types[i].fs_name)) == 0)
80*86d7f5d3SJohn Marino return fs_types[i].fs_probe(dev);
81*86d7f5d3SJohn Marino }
82*86d7f5d3SJohn Marino return FSID_UNKNOWN;
83*86d7f5d3SJohn Marino }
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino fsid_t
fsid_probe_all(const char * dev)86*86d7f5d3SJohn Marino fsid_probe_all(const char *dev)
87*86d7f5d3SJohn Marino {
88*86d7f5d3SJohn Marino int i;
89*86d7f5d3SJohn Marino fsid_t ret;
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino if (dev == NULL)
92*86d7f5d3SJohn Marino return FSID_UNKNOWN;
93*86d7f5d3SJohn Marino
94*86d7f5d3SJohn Marino for (i = 0; fs_types[i].fs_name != NULL; i++) {
95*86d7f5d3SJohn Marino if ((ret = fs_types[i].fs_probe(dev)) != FSID_UNKNOWN)
96*86d7f5d3SJohn Marino return ret;
97*86d7f5d3SJohn Marino }
98*86d7f5d3SJohn Marino return FSID_UNKNOWN;
99*86d7f5d3SJohn Marino }
100*86d7f5d3SJohn Marino
101*86d7f5d3SJohn Marino char *
fsid_volname(const char * dev,const char * fs_type)102*86d7f5d3SJohn Marino fsid_volname(const char *dev, const char *fs_type)
103*86d7f5d3SJohn Marino {
104*86d7f5d3SJohn Marino int i;
105*86d7f5d3SJohn Marino
106*86d7f5d3SJohn Marino if (dev == NULL || fs_type == NULL)
107*86d7f5d3SJohn Marino return NULL;
108*86d7f5d3SJohn Marino
109*86d7f5d3SJohn Marino for (i = 0; fs_types[i].fs_name != NULL; i++) {
110*86d7f5d3SJohn Marino if ((strcmp(fs_type, fs_types[i].fs_name)) == 0) {
111*86d7f5d3SJohn Marino return fs_types[i].fs_volname(dev);
112*86d7f5d3SJohn Marino }
113*86d7f5d3SJohn Marino }
114*86d7f5d3SJohn Marino return NULL;
115*86d7f5d3SJohn Marino }
116*86d7f5d3SJohn Marino
117*86d7f5d3SJohn Marino char *
fsid_volname_all(const char * dev)118*86d7f5d3SJohn Marino fsid_volname_all(const char *dev)
119*86d7f5d3SJohn Marino {
120*86d7f5d3SJohn Marino int fs_id;
121*86d7f5d3SJohn Marino
122*86d7f5d3SJohn Marino if (dev == NULL)
123*86d7f5d3SJohn Marino return NULL;
124*86d7f5d3SJohn Marino
125*86d7f5d3SJohn Marino if ((fs_id = fsid_probe_all(dev)) != 0)
126*86d7f5d3SJohn Marino return fs_types[fs_id - 1].fs_volname(dev);
127*86d7f5d3SJohn Marino else
128*86d7f5d3SJohn Marino return NULL;
129*86d7f5d3SJohn Marino }
130*86d7f5d3SJohn Marino
131*86d7f5d3SJohn Marino int
fsid_dev_read(const char * dev,off_t off,size_t len,char * buf)132*86d7f5d3SJohn Marino fsid_dev_read(const char *dev, off_t off, size_t len, char *buf)
133*86d7f5d3SJohn Marino {
134*86d7f5d3SJohn Marino int fd;
135*86d7f5d3SJohn Marino
136*86d7f5d3SJohn Marino if ((fd = open(dev, O_RDONLY)) < 0)
137*86d7f5d3SJohn Marino return -1;
138*86d7f5d3SJohn Marino
139*86d7f5d3SJohn Marino if ((lseek(fd, off, SEEK_SET)) < 0) {
140*86d7f5d3SJohn Marino close(fd);
141*86d7f5d3SJohn Marino return -1;
142*86d7f5d3SJohn Marino }
143*86d7f5d3SJohn Marino
144*86d7f5d3SJohn Marino bzero(buf, len);
145*86d7f5d3SJohn Marino if ((read(fd, buf, len)) < 0) {
146*86d7f5d3SJohn Marino close(fd);
147*86d7f5d3SJohn Marino return -1;
148*86d7f5d3SJohn Marino }
149*86d7f5d3SJohn Marino
150*86d7f5d3SJohn Marino close(fd);
151*86d7f5d3SJohn Marino
152*86d7f5d3SJohn Marino return 0;
153*86d7f5d3SJohn Marino }
154