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