chiark / gitweb /
09d1e317257f66b9bc7291b2ae3f8201feaa3b5a
[elogind.git] / extras / volume_id / libvolume_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 program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28
29 #include "volume_id.h"
30 #include "logging.h"
31 #include "util.h"
32
33
34 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
35 {
36         if (id == NULL)
37                 return -EINVAL;
38
39         /* probe for raid first, cause fs probes may be successful on raid members */
40         if (size) {
41                 if (volume_id_probe_linux_raid(id, off, size) == 0)
42                         goto exit;
43
44                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
45                         goto exit;
46
47                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
48                         goto exit;
49
50                 if (volume_id_probe_via_raid(id, off, size) == 0)
51                         goto exit;
52
53                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
54                         goto exit;
55
56                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
57                         goto exit;
58
59                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
60                         goto exit;
61
62                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
63                         goto exit;
64         }
65
66         if (volume_id_probe_lvm1(id, off) == 0)
67                 goto exit;
68
69         if (volume_id_probe_lvm2(id, off) == 0)
70                 goto exit;
71
72         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
73                 goto exit;
74
75         if (volume_id_probe_luks(id, off) == 0)
76                 goto exit;
77
78         /* signature in the first block, only small buffer needed */
79         if (volume_id_probe_vfat(id, off) == 0)
80                 goto exit;
81
82         if (volume_id_probe_xfs(id, off) == 0)
83                 goto exit;
84
85         /* fill buffer with maximum */
86         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
87
88         if (volume_id_probe_linux_swap(id, off) == 0)
89                 goto exit;
90
91         if (volume_id_probe_ext(id, off) == 0)
92                 goto exit;
93
94         if (volume_id_probe_reiserfs(id, off) == 0)
95                 goto exit;
96
97         if (volume_id_probe_jfs(id, off) == 0)
98                 goto exit;
99
100         if (volume_id_probe_udf(id, off) == 0)
101                 goto exit;
102
103         if (volume_id_probe_iso9660(id, off) == 0)
104                 goto exit;
105
106         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
107                 goto exit;
108
109         if (volume_id_probe_ufs(id, off) == 0)
110                 goto exit;
111
112         if (volume_id_probe_ntfs(id, off)  == 0)
113                 goto exit;
114
115         if (volume_id_probe_cramfs(id, off) == 0)
116                 goto exit;
117
118         if (volume_id_probe_romfs(id, off) == 0)
119                 goto exit;
120
121         if (volume_id_probe_hpfs(id, off) == 0)
122                 goto exit;
123
124         if (volume_id_probe_sysv(id, off) == 0)
125                 goto exit;
126
127         if (volume_id_probe_minix(id, off) == 0)
128                 goto exit;
129
130         if (volume_id_probe_ocfs1(id, off) == 0)
131                 goto exit;
132
133         if (volume_id_probe_ocfs2(id, off) == 0)
134                 goto exit;
135
136         if (volume_id_probe_vxfs(id, off) == 0)
137                 goto exit;
138
139         return -1;
140
141 exit:
142         /* If the filestystem in recognized, we free the allocated buffers,
143            otherwise they will stay in place for the possible next probe call */
144         volume_id_free_buffer(id);
145
146         return 0;
147 }
148
149 /* open volume by already open file descriptor */
150 struct volume_id *volume_id_open_fd(int fd)
151 {
152         struct volume_id *id;
153
154         id = malloc(sizeof(struct volume_id));
155         if (id == NULL)
156                 return NULL;
157         memset(id, 0x00, sizeof(struct volume_id));
158
159         id->fd = fd;
160
161         return id;
162 }
163
164 /* open volume by device node */
165 struct volume_id *volume_id_open_node(const char *path)
166 {
167         struct volume_id *id;
168         int fd;
169
170         fd = open(path, O_RDONLY);
171         if (fd < 0) {
172                 dbg("unable to open '%s'", path);
173                 return NULL;
174         }
175
176         id = volume_id_open_fd(fd);
177         if (id == NULL)
178                 return NULL;
179
180         /* close fd on device close */
181         id->fd_close = 1;
182
183         return id;
184 }
185
186 void volume_id_close(struct volume_id *id)
187 {
188         if (id == NULL)
189                 return;
190
191         if (id->fd_close != 0)
192                 close(id->fd);
193
194         volume_id_free_buffer(id);
195
196         if (id->partitions != NULL)
197                 free(id->partitions);
198
199         free(id);
200 }