| 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 <[email protected]> |
| 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 |
synced_close (gint fd) { |
| 37 |
gint ret = 0; |
| 38 |
ret = fsync (fd); |
| 39 |
if (close (fd) != 0) |
| 40 |
ret = 1; |
| 41 |
return ret; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
G_GNUC_INTERNAL gboolean |
| 46 |
get_uuid_label (const gchar *device, gchar **uuid, gchar **label, GError **error) { |
| 47 |
blkid_probe probe = NULL; |
| 48 |
gint fd = 0; |
| 49 |
gint status = 0; |
| 50 |
const gchar *value = NULL; |
| 51 |
|
| 52 |
probe = blkid_new_probe (); |
| 53 |
if (!probe) { |
| 54 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 55 |
"Failed to create a probe for the device '%s'", device); |
| 56 |
return FALSE; |
| 57 |
} |
| 58 |
|
| 59 |
fd = open (device, O_RDONLY|O_CLOEXEC); |
| 60 |
if (fd == -1) { |
| 61 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 62 |
"Failed to create a probe for the device '%s': %s", |
| 63 |
device, strerror_l (errno, _C_LOCALE)); |
| 64 |
blkid_free_probe (probe); |
| 65 |
return FALSE; |
| 66 |
} |
| 67 |
|
| 68 |
status = blkid_probe_set_device (probe, fd, 0, 0); |
| 69 |
if (status != 0) { |
| 70 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 71 |
"Failed to create a probe for the device '%s'", device); |
| 72 |
blkid_free_probe (probe); |
| 73 |
synced_close (fd); |
| 74 |
return FALSE; |
| 75 |
} |
| 76 |
|
| 77 |
blkid_probe_enable_partitions (probe, 1); |
| 78 |
|
| 79 |
status = blkid_do_probe (probe); |
| 80 |
if (status != 0) { |
| 81 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 82 |
"Failed to probe the device '%s'", device); |
| 83 |
blkid_free_probe (probe); |
| 84 |
synced_close (fd); |
| 85 |
return FALSE; |
| 86 |
} |
| 87 |
|
| 88 |
status = blkid_probe_has_value (probe, "LABEL"); |
| 89 |
|
| 90 |
if (status == 0) |
| 91 |
*label = g_strdup (""); |
| 92 |
else { |
| 93 |
status = blkid_probe_lookup_value (probe, "LABEL", &value, NULL); |
| 94 |
if (status != 0) { |
| 95 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 96 |
"Failed to get label for the device '%s'", device); |
| 97 |
blkid_free_probe (probe); |
| 98 |
synced_close (fd); |
| 99 |
return FALSE; |
| 100 |
} |
| 101 |
|
| 102 |
if (value) |
| 103 |
*label = g_strdup (value); |
| 104 |
else |
| 105 |
*label = g_strdup (""); |
| 106 |
} |
| 107 |
|
| 108 |
status = blkid_probe_has_value (probe, "UUID"); |
| 109 |
if (status == 0) |
| 110 |
*uuid = g_strdup (""); |
| 111 |
else { |
| 112 |
status = blkid_probe_lookup_value (probe, "UUID", &value, NULL); |
| 113 |
if (status != 0) { |
| 114 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 115 |
"Failed to get UUID for the device '%s'", device); |
| 116 |
blkid_free_probe (probe); |
| 117 |
synced_close (fd); |
| 118 |
g_free (*label); |
| 119 |
*label = NULL; |
| 120 |
return FALSE; |
| 121 |
} |
| 122 |
|
| 123 |
if (value) |
| 124 |
*uuid = g_strdup (value); |
| 125 |
else |
| 126 |
*uuid = g_strdup (""); |
| 127 |
} |
| 128 |
|
| 129 |
blkid_free_probe (probe); |
| 130 |
synced_close (fd); |
| 131 |
|
| 132 |
return TRUE; |
| 133 |
} |
| 134 |
|
| 135 |
G_GNUC_INTERNAL gboolean |
| 136 |
check_uuid (const gchar *uuid, GError **error) { |
| 137 |
g_autofree gchar *lowercase = NULL; |
| 138 |
gint ret = 0; |
| 139 |
uuid_t uu; |
| 140 |
|
| 141 |
if (!g_str_is_ascii (uuid)) { |
| 142 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID, |
| 143 |
"Provided UUID is not a valid RFC-4122 UUID."); |
| 144 |
return FALSE; |
| 145 |
} |
| 146 |
|
| 147 |
lowercase = g_ascii_strdown (uuid, -1); |
| 148 |
ret = uuid_parse (lowercase, uu); |
| 149 |
if (ret < 0){ |
| 150 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID, |
| 151 |
"Provided UUID is not a valid RFC-4122 UUID."); |
| 152 |
return FALSE; |
| 153 |
} |
| 154 |
|
| 155 |
return TRUE; |
| 156 |
} |