chiark / gitweb /
builtin: blkid - add missing newline
[elogind.git] / udev / udev-builtin-blkid.c
1 /*
2  * probe disks for filesystems and partitions
3  *
4  * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
5  * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <getopt.h>
29 #include <sys/stat.h>
30 #include <blkid/blkid.h>
31
32 #include "udev.h"
33
34 static void print_property(struct udev_device *dev, bool test, const char *name, const char *value)
35 {
36         char s[265];
37
38         s[0] = '\0';
39
40         if (!strcmp(name, "TYPE")) {
41                 udev_builtin_add_property(dev, test, "ID_FS_TYPE", value);
42
43         } else if (!strcmp(name, "VERSION")) {
44                 udev_builtin_add_property(dev, test, "ID_FS_VERSION", value);
45
46         } else if (!strcmp(name, "UUID")) {
47                 blkid_safe_string(value, s, sizeof(s));
48                 udev_builtin_add_property(dev, test, "ID_FS_UUID", s);
49                 blkid_encode_string(value, s, sizeof(s));
50                 udev_builtin_add_property(dev, test, "ID_FS_UUID_ENC", s);
51
52         } else if (!strcmp(name, "UUID_SUB")) {
53                 blkid_safe_string(value, s, sizeof(s));
54                 udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB", s);
55                 blkid_encode_string(value, s, sizeof(s));
56                 udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB_ENC", s);
57
58         } else if (!strcmp(name, "LABEL")) {
59                 blkid_safe_string(value, s, sizeof(s));
60                 udev_builtin_add_property(dev, test, "ID_FS_LABEL", s);
61                 blkid_encode_string(value, s, sizeof(s));
62                 udev_builtin_add_property(dev, test, "ID_FS_LABEL_ENC", s);
63
64         } else if (!strcmp(name, "PTTYPE")) {
65                 udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value);
66
67         } else if (!strcmp(name, "PART_ENTRY_NAME")) {
68                 blkid_encode_string(value, s, sizeof(s));
69                 udev_builtin_add_property(dev, test, "PART_ENTRY_NAME", s);
70
71         } else if (!strcmp(name, "PART_ENTRY_TYPE")) {
72                 blkid_encode_string(value, s, sizeof(s));
73                 udev_builtin_add_property(dev, test, "PART_ENTRY_TYPE", s);
74
75         } else if (!strncmp(name, "PART_ENTRY_", 11)) {
76                 util_strscpyl(s, sizeof(s), "ID_", name, NULL);
77                 udev_builtin_add_property(dev, test, name, value);
78         }
79 }
80
81 static int probe_superblocks(blkid_probe pr)
82 {
83         struct stat st;
84         int rc;
85
86         if (fstat(blkid_probe_get_fd(pr), &st))
87                 return -1;
88
89         blkid_probe_enable_partitions(pr, 1);
90
91         if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
92             blkid_probe_is_wholedisk(pr)) {
93                 /*
94                  * check if the small disk is partitioned, if yes then
95                  * don't probe for filesystems.
96                  */
97                 blkid_probe_enable_superblocks(pr, 0);
98
99                 rc = blkid_do_fullprobe(pr);
100                 if (rc < 0)
101                         return rc;      /* -1 = error, 1 = nothing, 0 = succes */
102
103                 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
104                         return 0;       /* partition table detected */
105         }
106
107         blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
108         blkid_probe_enable_superblocks(pr, 1);
109
110         return blkid_do_safeprobe(pr);
111 }
112
113 static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test)
114 {
115         struct udev *udev = udev_device_get_udev(dev);
116         int64_t offset = 0;
117         bool noraid = false;
118         int fd = -1;
119         blkid_probe pr;
120         const char *data;
121         const char *name;
122         int nvals;
123         int i;
124         size_t len;
125         int err = 0;
126
127         static const struct option options[] = {
128                 { "offset", optional_argument, NULL, 'o' },
129                 { "noraid", no_argument, NULL, 'R' },
130                 {}
131         };
132
133         for (;;) {
134                 int option;
135
136                 option = getopt_long(argc, argv, "oR", options, NULL);
137                 if (option == -1)
138                         break;
139
140                 switch (option) {
141                 case 'o':
142                         offset = strtoull(optarg, NULL, 0);
143                         break;
144                 case 'R':
145                         noraid = true;
146                         break;
147                 }
148         }
149
150         pr = blkid_new_probe();
151         if (!pr) {
152                 err = -ENOMEM;
153                 return EXIT_FAILURE;
154         }
155
156         blkid_probe_set_superblocks_flags(pr,
157                 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
158                 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
159                 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
160
161         if (noraid)
162                 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
163
164         fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC);
165         if (fd < 0) {
166                 fprintf(stderr, "error: %s: %m\n", udev_device_get_devnode(dev));
167                 goto out;
168         }
169
170         err = blkid_probe_set_device(pr, fd, offset, 0);
171         if (err < 0)
172                 goto out;
173
174         info(udev, "probe %s %sraid offset=%llu\n",
175              udev_device_get_devnode(dev),
176              noraid ? "no" : "", (unsigned long long) offset);
177
178         err = probe_superblocks(pr);
179         if (err < 0)
180                 goto out;
181
182         nvals = blkid_probe_numof_values(pr);
183         for (i = 0; i < nvals; i++) {
184                 if (blkid_probe_get_value(pr, i, &name, &data, &len))
185                         continue;
186                 len = strnlen((char *) data, len);
187                 print_property(dev, test, name, (char *) data);
188         }
189
190         blkid_free_probe(pr);
191 out:
192         if (fd > 0)
193                 close(fd);
194         if (err < 0)
195                 return EXIT_FAILURE;
196         return EXIT_SUCCESS;
197 }
198
199 const struct udev_builtin udev_builtin_blkid = {
200         .name = "blkid",
201         .cmd = builtin_blkid,
202         .help = "filesystem and partition probing",
203         .run_once = true,
204 };