libfstools: fix foreachdir() to pass dir with a trailing slash
[project/fstools.git] / probe.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 <string.h>
15 #include <libubox/utils.h>
16
17 #include "probe.h"
18 #include "libblkid-tiny/libblkid-tiny.h"
19
20 static struct probe_info *
21 probe_path_tiny(const char *path)
22 {
23         struct probe_info *info = NULL;
24         struct blkid_struct_probe pr = { };
25         char *type, *dev, *uuid, *label, *version;
26
27         if (probe_block((char *)path, &pr) == 0 && pr.id && !pr.err) {
28                 info = calloc_a(sizeof(*info),
29                                 &type,    strlen(pr.id->name) + 1,
30                                 &dev,     strlen(pr.dev)      + 1,
31                                 &uuid,    strlen(pr.uuid)     + 1,
32                                 &label,   strlen(pr.label)    + 1,
33                                 &version, strlen(pr.version)  + 1);
34
35                 if (info) {
36                         info->type = strcpy(type, pr.id->name);
37
38                         if (pr.dev[0])
39                                 info->dev = strcpy(dev, pr.dev);
40
41                         if (pr.uuid[0])
42                                 info->uuid = strcpy(uuid, pr.uuid);
43
44                         if (pr.label[0])
45                                 info->label = strcpy(label, pr.label);
46
47                         if (pr.version[0])
48                                 info->version = strcpy(version, pr.version);
49                 }
50         }
51
52         return info;
53 }
54
55 struct probe_info *
56 probe_path(const char *path)
57 {
58         struct probe_info *info;
59
60         info = probe_path_tiny(path);
61
62         if (!info)
63                 info = probe_path_libblkid(path);
64
65         return info;
66 }
67
68 int
69 make_devs(void)
70 {
71         return mkblkdev();
72 }