Line data Source code
1 : /*
2 : * Copyright (C) 2017 Red Hat, Inc.
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Lesser General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2.1 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Lesser General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public
15 : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 : *
17 : * Author: Vratislav Podzimek <vpodzime@redhat.com>
18 : */
19 :
20 : #include <glib.h>
21 : #include <blkid.h>
22 : #include <sys/types.h>
23 : #include <sys/stat.h>
24 : #include <fcntl.h>
25 : #include <string.h>
26 : #include <unistd.h>
27 : #include <errno.h>
28 : #include <uuid.h>
29 :
30 : #include <blockdev/utils.h>
31 :
32 : #include "fs.h"
33 : #include "common.h"
34 :
35 : G_GNUC_INTERNAL gint
36 360 : synced_close (gint fd) {
37 360 : gint ret = 0;
38 360 : ret = fsync (fd);
39 360 : if (close (fd) != 0)
40 0 : ret = 1;
41 360 : return ret;
42 : }
43 :
44 :
45 : G_GNUC_INTERNAL gboolean
46 114 : get_uuid_label (const gchar *device, gchar **uuid, gchar **label, GError **error) {
47 114 : blkid_probe probe = NULL;
48 114 : gint fd = 0;
49 114 : gint status = 0;
50 114 : const gchar *value = NULL;
51 :
52 114 : probe = blkid_new_probe ();
53 114 : if (!probe) {
54 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
55 : "Failed to create a probe for the device '%s'", device);
56 0 : return FALSE;
57 : }
58 :
59 114 : fd = open (device, O_RDONLY|O_CLOEXEC);
60 114 : if (fd == -1) {
61 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
62 : "Failed to create a probe for the device '%s': %s",
63 0 : device, strerror_l (errno, _C_LOCALE));
64 0 : blkid_free_probe (probe);
65 0 : return FALSE;
66 : }
67 :
68 114 : status = blkid_probe_set_device (probe, fd, 0, 0);
69 114 : if (status != 0) {
70 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
71 : "Failed to create a probe for the device '%s'", device);
72 0 : blkid_free_probe (probe);
73 0 : synced_close (fd);
74 0 : return FALSE;
75 : }
76 :
77 114 : blkid_probe_enable_partitions (probe, 1);
78 :
79 114 : status = blkid_do_probe (probe);
80 114 : if (status != 0) {
81 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
82 : "Failed to probe the device '%s'", device);
83 0 : blkid_free_probe (probe);
84 0 : synced_close (fd);
85 0 : return FALSE;
86 : }
87 :
88 114 : status = blkid_probe_has_value (probe, "LABEL");
89 :
90 114 : if (status == 0)
91 79 : *label = g_strdup ("");
92 : else {
93 35 : status = blkid_probe_lookup_value (probe, "LABEL", &value, NULL);
94 35 : if (status != 0) {
95 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
96 : "Failed to get label for the device '%s'", device);
97 0 : blkid_free_probe (probe);
98 0 : synced_close (fd);
99 0 : return FALSE;
100 : }
101 :
102 35 : if (value)
103 70 : *label = g_strdup (value);
104 : else
105 0 : *label = g_strdup ("");
106 : }
107 :
108 114 : status = blkid_probe_has_value (probe, "UUID");
109 114 : if (status == 0)
110 0 : *uuid = g_strdup ("");
111 : else {
112 114 : status = blkid_probe_lookup_value (probe, "UUID", &value, NULL);
113 114 : if (status != 0) {
114 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
115 : "Failed to get UUID for the device '%s'", device);
116 0 : blkid_free_probe (probe);
117 0 : synced_close (fd);
118 0 : g_free (label);
119 0 : return FALSE;
120 : }
121 :
122 114 : if (value)
123 228 : *uuid = g_strdup (value);
124 : else
125 0 : *uuid = g_strdup ("");
126 : }
127 :
128 114 : blkid_free_probe (probe);
129 114 : synced_close (fd);
130 :
131 114 : return TRUE;
132 : }
133 :
134 : G_GNUC_INTERNAL gboolean
135 17 : check_uuid (const gchar *uuid, GError **error) {
136 17 : g_autofree gchar *lowercase = NULL;
137 17 : gint ret = 0;
138 : uuid_t uu;
139 :
140 17 : if (!g_str_is_ascii (uuid)) {
141 0 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
142 : "Provided UUID is not a valid RFC-4122 UUID.");
143 0 : return FALSE;
144 : }
145 :
146 17 : lowercase = g_ascii_strdown (uuid, -1);
147 17 : ret = uuid_parse (lowercase, uu);
148 17 : if (ret < 0){
149 6 : g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
150 : "Provided UUID is not a valid RFC-4122 UUID.");
151 6 : return FALSE;
152 : }
153 :
154 11 : return TRUE;
155 : }
|