chiark / gitweb /
usb_id: use libudev
[elogind.git] / extras / volume_id / lib / volume_id.c
1 /*
2  * volume_id - reads volume label and uuid
3  *
4  * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32
33 #include "libvolume_id.h"
34 #include "libvolume_id-private.h"
35
36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
37
38 struct prober {
39         volume_id_probe_fn_t prober;
40         const char *name[4];
41 };
42
43 static const struct prober prober_raid[] = {
44         { volume_id_probe_linux_raid, { "linux_raid", } },
45         { volume_id_probe_ddf_raid, { "ddf_raid", } },
46         { volume_id_probe_intel_software_raid, { "isw_raid", } },
47         { volume_id_probe_lsi_mega_raid, { "lsi_mega_raid", } },
48         { volume_id_probe_via_raid, { "via_raid", } },
49         { volume_id_probe_silicon_medley_raid, { "silicon_medley_raid", } },
50         { volume_id_probe_nvidia_raid, { "nvidia_raid", } },
51         { volume_id_probe_promise_fasttrack_raid, { "promise_fasttrack_raid", } },
52         { volume_id_probe_highpoint_45x_raid, { "highpoint_raid", } },
53         { volume_id_probe_adaptec_raid, { "adaptec_raid", } },
54         { volume_id_probe_jmicron_raid, { "jmicron_raid", } },
55         { volume_id_probe_lvm1, { "lvm1", } },
56         { volume_id_probe_lvm2, { "lvm2", } },
57         { volume_id_probe_highpoint_37x_raid, { "highpoint_raid", } },
58 };
59
60 static const struct prober prober_filesystem[] = {
61         { volume_id_probe_vfat, { "vfat", } },
62         { volume_id_probe_linux_swap, { "swap", } },
63         { volume_id_probe_luks, { "luks", } },
64         { volume_id_probe_xfs, { "xfs", } },
65         { volume_id_probe_ext, { "ext2", "ext3", "jbd", } },
66         { volume_id_probe_reiserfs, { "reiserfs", "reiser4", } },
67         { volume_id_probe_jfs, { "jfs", } },
68         { volume_id_probe_udf, { "udf", } },
69         { volume_id_probe_iso9660, { "iso9660", } },
70         { volume_id_probe_hfs_hfsplus, { "hfs", "hfsplus", } },
71         { volume_id_probe_ufs, { "ufs", } },
72         { volume_id_probe_ntfs, { "ntfs", } },
73         { volume_id_probe_cramfs, { "cramfs", } },
74         { volume_id_probe_romfs, { "romfs", } },
75         { volume_id_probe_hpfs, { "hpfs", } },
76         { volume_id_probe_sysv, { "sysv", "xenix", } },
77         { volume_id_probe_minix, { "minix",  } },
78         { volume_id_probe_gfs, { "gfs", } },
79         { volume_id_probe_gfs2, { "gfs2", } },
80         { volume_id_probe_ocfs1, { "ocfs1", } },
81         { volume_id_probe_ocfs2, { "ocfs2", } },
82         { volume_id_probe_vxfs, { "vxfs", } },
83         { volume_id_probe_squashfs, { "squashfs", } },
84         { volume_id_probe_netware, { "netware", } },
85         { volume_id_probe_oracleasm, { "oracleasm", } },
86 };
87
88 /* the user can overwrite this log function */
89 static void default_log(int priority, const char *file, int line, const char *format, ...)
90 {
91         return;
92 }
93
94 volume_id_log_fn_t volume_id_log_fn = default_log;
95
96 /**
97  * volume_id_get_prober_by_type:
98  * @type: Type string.
99  *
100  * Lookup the probing function for a specific type.
101  *
102  * Returns: The probing function for the given type, #NULL otherwise.
103  **/
104 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
105 {
106         unsigned int p, n;
107
108         if (type == NULL)
109                 return NULL;
110
111         for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
112                 for (n = 0; prober_raid[p].name[n] !=  NULL; n++)
113                         if (strcmp(type, prober_raid[p].name[n]) == 0)
114                                 return &prober_raid[p].prober;
115         for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
116                 for (n = 0; prober_filesystem[p].name[n] !=  NULL; n++)
117                         if (strcmp(type, prober_filesystem[p].name[n]) == 0)
118                                 return &prober_filesystem[p].prober;
119         return NULL;
120 }
121
122 /**
123  * volume_id_get_label:
124  * @id: Probing context.
125  * @label: Label string. Must not be freed by the caller.
126  *
127  * Get the label string after a successful probe. Unicode
128  * is translated to UTF-8.
129  *
130  * Returns: 1 if the value was set, 0 otherwise.
131  **/
132 int volume_id_get_label(struct volume_id *id, const char **label)
133 {
134         if (id == NULL)
135                 return 0;
136         if (label == NULL)
137                 return 0;
138         if (id->usage_id == VOLUME_ID_UNUSED)
139                 return 0;
140
141         *label = id->label;
142         return 1;
143 }
144
145 /**
146  * volume_id_get_label_raw:
147  * @id: Probing context.
148  * @label: Label byte array. Must not be freed by the caller.
149  * @len: Length of raw label byte array.
150  *
151  * Get the raw label byte array after a successful probe. It may
152  * contain undecoded multibyte character streams.
153  *
154  * Returns: 1 if the value was set, 0 otherwise.
155  **/
156 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
157 {
158         if (id == NULL)
159                 return 0;
160         if (label == NULL)
161                 return 0;
162         if (len == NULL)
163                 return 0;
164         if (id->usage_id == VOLUME_ID_UNUSED)
165                 return 0;
166
167         *label = id->label_raw;
168         *len = id->label_raw_len;
169         return 1;
170 }
171
172 /**
173  * volume_id_get_uuid:
174  * @id: Probing context.
175  * @uuid: UUID string. Must not be freed by the caller.
176  *
177  * Get the raw UUID string after a successful probe.
178  *
179  * Returns: 1 if the value was set, 0 otherwise.
180  **/
181 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
182 {
183         if (id == NULL)
184                 return 0;
185         if (uuid == NULL)
186                 return 0;
187         if (id->usage_id == VOLUME_ID_UNUSED)
188                 return 0;
189
190         *uuid = id->uuid;
191         return 1;
192 }
193
194 /**
195  * volume_id_get_uuid_raw:
196  * @id: Probing context.
197  * @uuid: UUID byte array. Must not be freed by the caller.
198  * @len: Length of raw UUID byte array.
199  *
200  * Get the raw UUID byte array after a successful probe. It may
201  * contain unconverted endianes values.
202  *
203  * Returns: 1 if the value was set, 0 otherwise.
204  **/
205 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
206 {
207         if (id == NULL)
208                 return 0;
209         if (uuid == NULL)
210                 return 0;
211         if (len == NULL)
212                 return 0;
213         if (id->usage_id == VOLUME_ID_UNUSED)
214                 return 0;
215
216         *uuid = id->uuid_raw;
217         *len = id->uuid_raw_len;
218         return 1;
219 }
220
221 /**
222  * volume_id_get_usage:
223  * @id: Probing context.
224  * @usage: Usage string. Must not be freed by the caller.
225  *
226  * Get the usage string after a successful probe.
227  *
228  * Returns: 1 if the value was set, 0 otherwise.
229  **/
230 int volume_id_get_usage(struct volume_id *id, const char **usage)
231 {
232         if (id == NULL)
233                 return 0;
234         if (usage == NULL)
235                 return 0;
236         if (id->usage_id == VOLUME_ID_UNUSED)
237                 return 0;
238
239         *usage = id->usage;
240         return 1;
241 }
242
243 /**
244  * volume_id_get_type:
245  * @id: Probing context
246  * @type: Type string. Must not be freed by the caller.
247  *
248  * Get the type string after a successful probe.
249  *
250  * Returns: 1 if the value was set, 0 otherwise.
251  **/
252 int volume_id_get_type(struct volume_id *id, const char **type)
253 {
254         if (id == NULL)
255                 return 0;
256         if (type == NULL)
257                 return 0;
258         if (id->usage_id == VOLUME_ID_UNUSED)
259                 return 0;
260
261         *type = id->type;
262         return 1;
263 }
264
265 /**
266  * volume_id_get_type_version:
267  * @id: Probing context.
268  * @type_version: Type version string. Must not be freed by the caller.
269  *
270  * Get the Type version string after a successful probe.
271  *
272  * Returns: 1 if the value was set, 0 otherwise.
273  **/
274 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
275 {
276         if (id == NULL)
277                 return 0;
278         if (type_version == NULL)
279                 return 0;
280         if (id->usage_id == VOLUME_ID_UNUSED)
281                 return 0;
282
283         *type_version = id->type_version;
284         return 1;
285 }
286
287 static int needs_encoding(const char c)
288 {
289         if ((c >= '0' && c <= '9') ||
290             (c >= 'A' && c <= 'Z') ||
291             (c >= 'a' && c <= 'z') ||
292             strchr(ALLOWED_CHARS, c))
293                 return 0;
294         return 1;
295 }
296
297 /**
298  * volume_id_encode_string:
299  * @str: Input string to be encoded.
300  * @str_enc: Target string to store the encoded input.
301  * @len: Location to store the encoded string. The target string,
302  * which may be four times as long as the input string.
303  *
304  * Encode all potentially unsafe characters of a string to the
305  * corresponding hex value prefixed by '\x'.
306  *
307  * Returns: 1 if the entire string was copied, 0 otherwise.
308  **/
309 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
310 {
311         size_t i, j;
312
313         if (str == NULL || str_enc == NULL || len == 0)
314                 return 0;
315
316         str_enc[0] = '\0';
317         for (i = 0, j = 0; str[i] != '\0'; i++) {
318                 int seqlen;
319
320                 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
321                 if (seqlen > 1) {
322                         memcpy(&str_enc[j], &str[i], seqlen);
323                         j += seqlen;
324                         i += (seqlen-1);
325                 } else if (str[i] == '\\' || needs_encoding(str[i])) {
326                         sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
327                         j += 4;
328                 } else {
329                         str_enc[j] = str[i];
330                         j++;
331                 }
332                 if (j+3 >= len)
333                         goto err;
334         }
335         str_enc[j] = '\0';
336         return 1;
337 err:
338         return 0;
339 }
340
341 /* run only once into a timeout for unreadable devices */
342 static int device_is_readable(struct volume_id *id, uint64_t off)
343 {
344         if (volume_id_get_buffer(id, off, 0x200) != NULL)
345                 return 1;
346         return 0;
347 }
348
349 /**
350  * volume_id_probe_raid:
351  * @id: Probing context.
352  * @off: Probing offset relative to the start of the device.
353  * @size: Total size of the device.
354  *
355  * Probe device for all known raid signatures.
356  *
357  * Returns: 0 on successful probe, otherwise negative value.
358  **/
359 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
360 {
361         unsigned int i;
362
363         if (id == NULL)
364                 return -EINVAL;
365
366         if (!device_is_readable(id, off))
367                 return -1;
368
369         info("probing at offset 0x%llx, size 0x%llx\n",
370             (unsigned long long) off, (unsigned long long) size);
371
372         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
373                 if (prober_raid[i].prober(id, off, size) == 0)
374                         goto found;
375         return -1;
376
377 found:
378         /* If recognized, we free the allocated buffers */
379         volume_id_free_buffer(id);
380         return 0;
381 }
382
383 /**
384  * volume_id_probe_filesystem:
385  * @id: Probing context.
386  * @off: Probing offset relative to the start of the device.
387  * @size: Total size of the device.
388  *
389  * Probe device for all known filesystem signatures.
390  *
391  * Returns: 0 on successful probe, otherwise negative value.
392  **/
393 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
394 {
395         unsigned int i;
396
397         if (id == NULL)
398                 return -EINVAL;
399
400         if (!device_is_readable(id, off))
401                 return -1;
402
403         info("probing at offset 0x%llx, size 0x%llx\n",
404             (unsigned long long) off, (unsigned long long) size);
405
406         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
407                 if (prober_filesystem[i].prober(id, off, size) == 0)
408                         goto found;
409         return -1;
410
411 found:
412         /* If recognized, we free the allocated buffers */
413         volume_id_free_buffer(id);
414         return 0;
415 }
416
417 /**
418  * volume_id_probe_all:
419  * @id: Probing context.
420  * @off: Probing offset relative to the start of the device.
421  * @size: Total size of the device.
422  *
423  * Probe device for all known raid and filesystem signatures.
424  *
425  * Returns: 0 on successful probe, otherwise negative value.
426  **/
427 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
428 {
429         if (id == NULL)
430                 return -EINVAL;
431
432         if (!device_is_readable(id, off))
433                 return -1;
434
435         /* probe for raid first, because fs probes may be successful on raid members */
436         if (volume_id_probe_raid(id, off, size) == 0)
437                 return 0;
438
439         if (volume_id_probe_filesystem(id, off, size) == 0)
440                 return 0;
441
442         return -1;
443 }
444
445 /**
446  * volume_id_probe_raid:
447  * @all_probers_fn: prober function to called for all known probing routines.
448  * @id: Context passed to prober function.
449  * @off: Offset value passed to prober function.
450  * @size: Size value passed to prober function.
451  * @data: Arbitrary data passed to the prober function.
452  *
453  * Run a custom function for all known probing routines.
454  **/
455 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
456                            struct volume_id *id, uint64_t off, uint64_t size,
457                            void *data)
458 {
459         unsigned int i;
460
461         if (all_probers_fn == NULL)
462                 return;
463
464         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
465                 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
466                         goto out;
467         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
468                 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
469                         goto out;
470 out:
471         return;
472 }
473
474 /**
475  * volume_id_open_fd:
476  * @id: Probing context.
477  * @fd: Open file descriptor of device to read from.
478  *
479  * Create the context for probing.
480  *
481  * Returns: Probing context, or #NULL on failure.
482  **/
483 struct volume_id *volume_id_open_fd(int fd)
484 {
485         struct volume_id *id;
486
487         id = malloc(sizeof(struct volume_id));
488         if (id == NULL)
489                 return NULL;
490         memset(id, 0x00, sizeof(struct volume_id));
491
492         id->fd = fd;
493
494         return id;
495 }
496
497 /**
498  * volume_id_close:
499  * @id: Probing context.
500  *
501  * Release probing context and free all associated data.
502  */
503 void volume_id_close(struct volume_id *id)
504 {
505         if (id == NULL)
506                 return;
507
508         volume_id_free_buffer(id);
509
510         free(id);
511 }