chiark / gitweb /
fix more compiler warnings ...
[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
39 #include "volume_id.h"
40 #include "logging.h"
41 #include "util.h"
42
43 #include "ext.h"
44 #include "reiserfs.h"
45 #include "fat.h"
46 #include "hfs.h"
47 #include "jfs.h"
48 #include "xfs.h"
49 #include "ufs.h"
50 #include "ntfs.h"
51 #include "iso9660.h"
52 #include "udf.h"
53 #include "highpoint.h"
54 #include "isw_raid.h"
55 #include "lsi_raid.h"
56 #include "via_raid.h"
57 #include "silicon_raid.h"
58 #include "nvidia_raid.h"
59 #include "promise_raid.h"
60 #include "luks.h"
61 #include "linux_swap.h"
62 #include "linux_raid.h"
63 #include "lvm.h"
64 #include "cramfs.h"
65 #include "hpfs.h"
66 #include "romfs.h"
67 #include "sysv.h"
68 #include "minix.h"
69 #include "mac.h"
70 #include "msdos.h"
71 #include "ocfs2.h"
72
73 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
74 {
75         if (id == NULL)
76                 return -EINVAL;
77
78         /* probe for raid first, cause fs probes may be successful on raid members */
79         if (size) {
80                 if (volume_id_probe_linux_raid(id, off, size) == 0)
81                         goto exit;
82
83                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
84                         goto exit;
85
86                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
87                         goto exit;
88
89                 if (volume_id_probe_via_raid(id, off, size) == 0)
90                         goto exit;
91
92                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
93                         goto exit;
94
95                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
96                         goto exit;
97
98                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
99                         goto exit;
100
101                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
102                         goto exit;
103         }
104
105         if (volume_id_probe_lvm1(id, off) == 0)
106                 goto exit;
107
108         if (volume_id_probe_lvm2(id, off) == 0)
109                 goto exit;
110
111         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
112                 goto exit;
113
114         if (volume_id_probe_luks(id, off) == 0)
115                 goto exit;
116
117         /* signature in the first block, only small buffer needed */
118         if (volume_id_probe_vfat(id, off) == 0)
119                 goto exit;
120
121         if (volume_id_probe_mac_partition_map(id, off) == 0)
122                 goto exit;
123
124         if (volume_id_probe_xfs(id, off) == 0)
125                 goto exit;
126
127         /* fill buffer with maximum */
128         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
129
130         if (volume_id_probe_linux_swap(id, off) == 0)
131                 goto exit;
132
133         if (volume_id_probe_ext(id, off) == 0)
134                 goto exit;
135
136         if (volume_id_probe_reiserfs(id, off) == 0)
137                 goto exit;
138
139         if (volume_id_probe_jfs(id, off) == 0)
140                 goto exit;
141
142         if (volume_id_probe_udf(id, off) == 0)
143                 goto exit;
144
145         if (volume_id_probe_iso9660(id, off) == 0)
146                 goto exit;
147
148         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
149                 goto exit;
150
151         if (volume_id_probe_ufs(id, off) == 0)
152                 goto exit;
153
154         if (volume_id_probe_ntfs(id, off)  == 0)
155                 goto exit;
156
157         if (volume_id_probe_cramfs(id, off) == 0)
158                 goto exit;
159
160         if (volume_id_probe_romfs(id, off) == 0)
161                 goto exit;
162
163         if (volume_id_probe_hpfs(id, off) == 0)
164                 goto exit;
165
166         if (volume_id_probe_sysv(id, off) == 0)
167                 goto exit;
168
169         if (volume_id_probe_minix(id, off) == 0)
170                 goto exit;
171
172         if (volume_id_probe_ocfs2(id, off) == 0)
173                 goto exit;
174
175         return -1;
176
177 exit:
178         /* If the filestystem in recognized, we free the allocated buffers,
179            otherwise they will stay in place for the possible next probe call */
180         volume_id_free_buffer(id);
181
182         return 0;
183 }
184
185 /* open volume by already open file descriptor */
186 struct volume_id *volume_id_open_fd(int fd)
187 {
188         struct volume_id *id;
189
190         id = malloc(sizeof(struct volume_id));
191         if (id == NULL)
192                 return NULL;
193         memset(id, 0x00, sizeof(struct volume_id));
194
195         id->fd = fd;
196
197         return id;
198 }
199
200 /* open volume by device node */
201 struct volume_id *volume_id_open_node(const char *path)
202 {
203         struct volume_id *id;
204         int fd;
205
206         fd = open(path, O_RDONLY);
207         if (fd < 0) {
208                 dbg("unable to open '%s'", path);
209                 return NULL;
210         }
211
212         id = volume_id_open_fd(fd);
213         if (id == NULL)
214                 return NULL;
215
216         /* close fd on device close */
217         id->fd_close = 1;
218
219         return id;
220 }
221
222 /* open volume by major/minor */
223 struct volume_id *volume_id_open_dev_t(dev_t devt)
224 {
225         struct volume_id *id;
226         char tmp_node[VOLUME_ID_PATH_MAX];
227
228         snprintf(tmp_node, VOLUME_ID_PATH_MAX,
229                  "/dev/.volume_id-%u-%u-%u", getpid(), major(devt), minor(devt));
230         tmp_node[VOLUME_ID_PATH_MAX-1] = '\0';
231
232         /* create tempory node to open the block device */
233         unlink(tmp_node);
234         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
235                 return NULL;
236
237         id = volume_id_open_node(tmp_node);
238
239         unlink(tmp_node);
240
241         return id;
242 }
243
244 void volume_id_close(struct volume_id *id)
245 {
246         if (id == NULL)
247                 return;
248
249         if (id->fd_close != 0)
250                 close(id->fd);
251
252         volume_id_free_buffer(id);
253
254         if (id->partitions != NULL)
255                 free(id->partitions);
256
257         free(id);
258 }