<inttypes.h>: Small sync with FreeBSD.
[dragonfly.git] / lib / libfsid / libfsid.c
blob9858565782d3bdf5cbf6ad9c0f179b4db3a71a1a
1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Ákos Kovács <akoskovacs@gmx.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
35 #include <sys/stat.h>
37 #include <errno.h>
39 #include "libfsid.h"
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 }
50 const char *
51 fsid_fsname(fsid_t id)
53 if (id < 1)
54 return 0;
56 return fs_types[id-1].fs_name;
59 int
60 fsid_fs_count(void)
62 int count;
64 for (count = 0; fs_types[count].fs_name != NULL; count++)
65 ; /* nothing */
67 return count;
70 fsid_t
71 fsid_probe(const char *dev, const char *fs_type)
73 int i;
75 if (dev == NULL || fs_type == NULL)
76 return FSID_UNKNOWN;
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);
82 return FSID_UNKNOWN;
85 fsid_t
86 fsid_probe_all(const char *dev)
88 int i;
89 fsid_t ret;
91 if (dev == NULL)
92 return FSID_UNKNOWN;
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;
98 return FSID_UNKNOWN;
101 char *
102 fsid_volname(const char *dev, const char *fs_type)
104 int i;
106 if (dev == NULL || fs_type == NULL)
107 return NULL;
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);
114 return NULL;
117 char *
118 fsid_volname_all(const char *dev)
120 int fs_id;
122 if (dev == NULL)
123 return NULL;
125 if ((fs_id = fsid_probe_all(dev)) != 0)
126 return fs_types[fs_id - 1].fs_volname(dev);
127 else
128 return NULL;
132 fsid_dev_read(const char *dev, off_t off, size_t len, char *buf)
134 int fd;
136 if ((fd = open(dev, O_RDONLY)) < 0)
137 return -1;
139 if ((lseek(fd, off, SEEK_SET)) < 0) {
140 close(fd);
141 return -1;
144 bzero(buf, len);
145 if ((read(fd, buf, len)) < 0) {
146 close(fd);
147 return -1;
150 close(fd);
152 return 0;