chiark / gitweb /
because is better than cause
[elogind.git] / extras / volume_id / lib / volume_id.c
1 /*
2  * volume_id - reads volume 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 "libvolume_id.h"
30 #include "util.h"
31
32 /* the user can overwrite this log function */
33 static void default_log(int priority, const char *file, int line, const char *format, ...)
34 {
35         return;
36 }
37
38 volume_id_log_fn volume_id_log = default_log;
39
40 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
41 {
42         if (id == NULL)
43                 return -EINVAL;
44
45         /* probe for raid first, because fs probes may be successful on raid members */
46         if (size) {
47                 if (volume_id_probe_linux_raid(id, off, size) == 0)
48                         goto found;
49
50                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
51                         goto found;
52
53                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
54                         goto found;
55
56                 if (volume_id_probe_via_raid(id, off, size) == 0)
57                         goto found;
58
59                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
60                         goto found;
61
62                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
63                         goto found;
64
65                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
66                         goto found;
67
68                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
69                         goto found;
70         }
71
72         if (volume_id_probe_lvm1(id, off) == 0)
73                 goto found;
74
75         if (volume_id_probe_lvm2(id, off) == 0)
76                 goto found;
77
78         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
79                 goto found;
80
81         return -1;
82
83 found:
84         /* If recognized, we free the allocated buffers */
85         volume_id_free_buffer(id);
86         return 0;
87 }
88
89 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
90 {
91         if (id == NULL)
92                 return -EINVAL;
93
94         if (volume_id_probe_luks(id, off) == 0)
95                 goto found;
96
97         /* signature in the first block, only small buffer needed */
98         if (volume_id_probe_vfat(id, off) == 0)
99                 goto found;
100
101         if (volume_id_probe_xfs(id, off) == 0)
102                 goto found;
103
104         /* fill buffer with maximum */
105         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
106
107         if (volume_id_probe_linux_swap(id, off) == 0)
108                 goto found;
109
110         if (volume_id_probe_ext(id, off) == 0)
111                 goto found;
112
113         if (volume_id_probe_reiserfs(id, off) == 0)
114                 goto found;
115
116         if (volume_id_probe_jfs(id, off) == 0)
117                 goto found;
118
119         if (volume_id_probe_udf(id, off) == 0)
120                 goto found;
121
122         if (volume_id_probe_iso9660(id, off) == 0)
123                 goto found;
124
125         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
126                 goto found;
127
128         if (volume_id_probe_ufs(id, off) == 0)
129                 goto found;
130
131         if (volume_id_probe_ntfs(id, off)  == 0)
132                 goto found;
133
134         if (volume_id_probe_cramfs(id, off) == 0)
135                 goto found;
136
137         if (volume_id_probe_romfs(id, off) == 0)
138                 goto found;
139
140         if (volume_id_probe_hpfs(id, off) == 0)
141                 goto found;
142
143         if (volume_id_probe_sysv(id, off) == 0)
144                 goto found;
145
146         if (volume_id_probe_minix(id, off) == 0)
147                 goto found;
148
149         if (volume_id_probe_ocfs1(id, off) == 0)
150                 goto found;
151
152         if (volume_id_probe_ocfs2(id, off) == 0)
153                 goto found;
154
155         if (volume_id_probe_vxfs(id, off) == 0)
156                 goto found;
157
158         if (volume_id_probe_squashfs(id, off) == 0)
159                 goto found;
160
161         return -1;
162
163 found:
164         /* If recognized, we free the allocated buffers */
165         volume_id_free_buffer(id);
166         return 0;
167 }
168
169 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
170 {
171         if (id == NULL)
172                 return -EINVAL;
173
174         if (volume_id_probe_raid(id, off, size) == 0)
175                 return 0;
176
177         if (volume_id_probe_filesystem(id, off, size) == 0)
178                 return 0;
179
180         return -1;
181 }
182
183 /* open volume by already open file descriptor */
184 struct volume_id *volume_id_open_fd(int fd)
185 {
186         struct volume_id *id;
187
188         id = malloc(sizeof(struct volume_id));
189         if (id == NULL)
190                 return NULL;
191         memset(id, 0x00, sizeof(struct volume_id));
192
193         id->fd = fd;
194
195         return id;
196 }
197
198 /* open volume by device node */
199 struct volume_id *volume_id_open_node(const char *path)
200 {
201         struct volume_id *id;
202         int fd;
203
204         fd = open(path, O_RDONLY);
205         if (fd < 0) {
206                 dbg("unable to open '%s'", path);
207                 return NULL;
208         }
209
210         id = volume_id_open_fd(fd);
211         if (id == NULL)
212                 return NULL;
213
214         /* close fd on device close */
215         id->fd_close = 1;
216
217         return id;
218 }
219
220 void volume_id_close(struct volume_id *id)
221 {
222         if (id == NULL)
223                 return;
224
225         if (id->fd_close != 0)
226                 close(id->fd);
227
228         volume_id_free_buffer(id);
229
230         free(id);
231 }