Use autoclear for overlay loopback device
[oweals/fstools.git] / probe-libblkid.c
1 /*
2  * Copyright (C) 2016 Jo-Philipp Wich <jo@mein.io>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <dlfcn.h>
15 #include <string.h>
16 #include <stdbool.h>
17 #include <blkid/blkid.h>
18 #include <libubox/utils.h>
19
20 #include "probe.h"
21
22
23 static struct {
24         bool loaded;
25         blkid_probe (*alloc)(const char *);
26         int (*probe)(blkid_probe);
27         int (*lookup)(blkid_probe, const char *, const char **, size_t *);
28         void (*free)(blkid_probe);
29 } libblkid = { };
30
31
32 static bool
33 load_libblkid(void)
34 {
35         void *lib;
36
37         if (!libblkid.loaded) {
38                 lib = dlopen("libblkid.so", RTLD_GLOBAL);
39
40                 if (lib == NULL)
41                         lib = dlopen("libblkid.so.1", RTLD_GLOBAL);
42
43                 if (lib) {
44                         libblkid.alloc  = dlsym(lib, "blkid_new_probe_from_filename");
45                         libblkid.probe  = dlsym(lib, "blkid_do_probe");
46                         libblkid.lookup = dlsym(lib, "blkid_probe_lookup_value");
47                         libblkid.free   = dlsym(lib, "blkid_free_probe");
48                 }
49
50                 libblkid.loaded = true;
51         }
52
53         return (libblkid.alloc && libblkid.probe && libblkid.lookup && libblkid.free);
54 }
55
56 struct probe_info *
57 probe_path_libblkid(const char *path)
58 {
59         blkid_probe pr;
60         struct probe_info *info = NULL;
61         size_t type_len, uuid_len, label_len, version_len;
62         char *dev_ptr, *type_ptr, *uuid_ptr, *label_ptr, *version_ptr;
63         const char *type_val, *uuid_val, *label_val, *version_val;
64
65         if (!load_libblkid())
66                 return NULL;
67
68         pr = libblkid.alloc(path);
69
70         if (!pr)
71                 return NULL;
72
73         if (libblkid.probe(pr) == 0) {
74                 if (libblkid.lookup(pr, "TYPE", &type_val, &type_len))
75                         type_len = 0;
76
77                 if (libblkid.lookup(pr, "UUID", &uuid_val, &uuid_len))
78                         uuid_len = 0;
79
80                 if (libblkid.lookup(pr, "LABEL", &label_val, &label_len))
81                         label_len = 0;
82
83                 if (libblkid.lookup(pr, "VERSION", &version_val, &version_len))
84                         version_len = 0;
85
86                 if (type_len) {
87                         info = calloc_a(sizeof(*info),
88                                         &dev_ptr,     strlen(path) + 1,
89                                         &type_ptr,    type_len,
90                                         &uuid_ptr,    uuid_len,
91                                         &label_ptr,   label_len,
92                                         &version_ptr, version_len);
93
94                         if (info) {
95                                 info->dev = strcpy(dev_ptr, path);
96                                 info->type = strcpy(type_ptr, type_val);
97
98                                 if (uuid_len)
99                                         info->uuid = strcpy(uuid_ptr, uuid_val);
100
101                                 if (label_len)
102                                         info->label = strcpy(label_ptr, label_val);
103
104                                 if (version_len)
105                                         info->version = strcpy(version_ptr, version_val);
106                         }
107                 }
108         }
109
110         libblkid.free(pr);
111
112         return info;
113 }