chiark / gitweb /
[PATCH] udev_volume_id: volume_id version 032
[elogind.git] / extras / volume_id / volume_id / volume_id.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #include <asm/types.h>
39
40 #include "volume_id.h"
41 #include "logging.h"
42 #include "util.h"
43
44 #include "ext/ext.h"
45 #include "reiserfs/reiserfs.h"
46 #include "fat/fat.h"
47 #include "hfs/hfs.h"
48 #include "jfs/jfs.h"
49 #include "xfs/xfs.h"
50 #include "ufs/ufs.h"
51 #include "ntfs/ntfs.h"
52 #include "iso9660/iso9660.h"
53 #include "udf/udf.h"
54 #include "highpoint/highpoint.h"
55 #include "linux_swap/linux_swap.h"
56 #include "linux_raid/linux_raid.h"
57 #include "lvm/lvm.h"
58 #include "cramfs/cramfs.h"
59 #include "mac/mac.h"
60 #include "msdos/msdos.h"
61
62 int volume_id_probe_all(struct volume_id *id, unsigned long long off, unsigned long long size)
63 {
64         if (id == NULL)
65                 return -EINVAL;
66
67         /* probe for raid first, cause fs probes may be successful on raid members */
68         if (volume_id_probe_linux_raid(id, off, size) == 0)
69                 goto exit;
70
71         if (volume_id_probe_lvm1(id, off) == 0)
72                 goto exit;
73
74         if (volume_id_probe_lvm2(id, off) == 0)
75                 goto exit;
76
77         if (volume_id_probe_highpoint_ataraid(id, off) == 0)
78                 goto exit;
79
80         /* signature in the first block, only small buffer needed */
81         if (volume_id_probe_vfat(id, off) == 0)
82                 goto exit;
83
84         if (volume_id_probe_mac_partition_map(id, off) == 0)
85                 goto exit;
86
87         if (volume_id_probe_xfs(id, off) == 0)
88                 goto exit;
89
90         /* fill buffer with maximum */
91         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
92
93         if (volume_id_probe_linux_swap(id, off) == 0)
94                 goto exit;
95
96         if (volume_id_probe_ext(id, off) == 0)
97                 goto exit;
98
99         if (volume_id_probe_reiserfs(id, off) == 0)
100                 goto exit;
101
102         if (volume_id_probe_jfs(id, off) == 0)
103                 goto exit;
104
105         if (volume_id_probe_udf(id, off) == 0)
106                 goto exit;
107
108         if (volume_id_probe_iso9660(id, off) == 0)
109                 goto exit;
110
111         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
112                 goto exit;
113
114         if (volume_id_probe_ufs(id, off) == 0)
115                 goto exit;
116
117         if (volume_id_probe_ntfs(id, off)  == 0)
118                 goto exit;
119
120         if (volume_id_probe_cramfs(id, off) == 0)
121                 goto exit;
122
123         return -1;
124
125 exit:
126         /* If the filestystem in recognized, we free the allocated buffers,
127            otherwise they will stay in place for the possible next probe call */
128         volume_id_free_buffer(id);
129
130         return 0;
131 }
132
133 /* open volume by already open file descriptor */
134 struct volume_id *volume_id_open_fd(int fd)
135 {
136         struct volume_id *id;
137
138         id = malloc(sizeof(struct volume_id));
139         if (id == NULL)
140                 return NULL;
141         memset(id, 0x00, sizeof(struct volume_id));
142
143         id->fd = fd;
144
145         return id;
146 }
147
148 /* open volume by device node */
149 struct volume_id *volume_id_open_node(const char *path)
150 {
151         struct volume_id *id;
152         int fd;
153
154         fd = open(path, O_RDONLY);
155         if (fd < 0) {
156                 dbg("unable to open '%s'", path);
157                 return NULL;
158         }
159
160         id = volume_id_open_fd(fd);
161         if (id == NULL)
162                 return NULL;
163
164         /* close fd on device close */
165         id->fd_close = 1;
166
167         return id;
168 }
169
170 /* open volume by major/minor */
171 struct volume_id *volume_id_open_dev_t(dev_t devt)
172 {
173         struct volume_id *id;
174         __u8 tmp_node[VOLUME_ID_PATH_MAX];
175
176         snprintf(tmp_node, VOLUME_ID_PATH_MAX,
177                  "/dev/.volume_id-%u-%u-%u", getpid(), major(devt), minor(devt));
178         tmp_node[VOLUME_ID_PATH_MAX] = '\0';
179
180         /* create tempory node to open the block device */
181         unlink(tmp_node);
182         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
183                 return NULL;
184
185         id = volume_id_open_node(tmp_node);
186
187         unlink(tmp_node);
188
189         return id;
190 }
191
192 void volume_id_close(struct volume_id *id)
193 {
194         if (id == NULL)
195                 return;
196
197         if (id->fd_close != 0)
198                 close(id->fd);
199
200         volume_id_free_buffer(id);
201
202         if (id->partitions != NULL)
203                 free(id->partitions);
204
205         free(id);
206 }