Debian Codegraph @

src/plugins/fs/udf.c

raw
1 /*
2 * Copyright (C) 2020 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: Vojtech Trefny <[email protected]>
18 */
19
20 #include <blockdev/utils.h>
21 #include <check_deps.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <sys/ioctl.h>
28 #include <linux/fs.h>
29
30 #include "udf.h"
31 #include "fs.h"
32 #include "common.h"
33
34 static volatile guint avail_deps = 0;
35 static GMutex deps_check_lock;
36
37 G_GNUC_INTERNAL
38 void _fs_udf_reset_avail_deps (void) {
39 g_atomic_int_set (&avail_deps, 0);
40 }
41
42 #define DEPS_MKUDFFS 0
43 #define DEPS_MKUDFFS_MASK (1 << DEPS_MKUDFFS)
44 #define DEPS_UDFLABEL 1
45 #define DEPS_UDFLABEL_MASK (1 << DEPS_UDFLABEL)
46 #define DEPS_UDFINFO 2
47 #define DEPS_UDFINFO_MASK (1 << DEPS_UDFINFO)
48
49 #define DEPS_LAST 3
50
51 static const UtilDep deps[DEPS_LAST] = {
52 {"mkudffs", NULL, NULL, NULL},
53 {"udflabel", NULL, NULL, NULL},
54 {"udfinfo", NULL, NULL, NULL},
55 };
56
57 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
58 DEPS_MKUDFFS_MASK, /* mkfs */
59 0, /* wipe */
60 0, /* check */
61 0, /* repair */
62 DEPS_UDFLABEL_MASK, /* set-label */
63 DEPS_UDFINFO_MASK, /* query */
64 0, /* resize */
65 DEPS_UDFLABEL_MASK, /* set-uuid */
66 };
67
68
69 /**
70 * bd_fs_udf_is_tech_avail:
71 * @tech: the queried tech
72 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
73 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
74 *
75 * Returns: whether the @tech-@mode combination is available -- supported by the
76 * plugin implementation and having all the runtime dependencies available
77 */
78 G_GNUC_INTERNAL gboolean
79 bd_fs_udf_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
80 guint32 required = 0;
81 guint i = 0;
82
83 if (mode & BD_FS_TECH_MODE_CHECK || mode & BD_FS_TECH_MODE_REPAIR) {
84 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
85 "UDF doesn't support checking and reparing.");
86 return FALSE;
87 } else if (mode & BD_FS_TECH_MODE_RESIZE) {
88 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
89 "UDF currently doesn't support resizing.");
90 return FALSE;
91 }
92
93 for (i = 0; i <= BD_FS_MODE_LAST; i++)
94 if (mode & (1 << i))
95 required |= fs_mode_util[i];
96
97 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
98 }
99
100 /**
101 * bd_fs_udf_info_copy: (skip)
102 * @data: (nullable): %BDFSUdfInfo to copy
103 *
104 * Creates a new copy of @data.
105 */
106 BDFSUdfInfo* bd_fs_udf_info_copy (BDFSUdfInfo *data) {
107 if (data == NULL)
108 return NULL;
109
110 BDFSUdfInfo *ret = g_new0 (BDFSUdfInfo, 1);
111
112 ret->label = g_strdup (data->label);
113 ret->uuid = g_strdup (data->uuid);
114 ret->revision = g_strdup (data->revision);
115 ret->lvid = g_strdup (data->lvid);
116 ret->vid = g_strdup (data->vid);
117 ret->block_size = data->block_size;
118 ret->block_count = data->block_count;
119 ret->free_blocks = data->free_blocks;
120
121 return ret;
122 }
123
124 /**
125 * bd_fs_udf_info_free: (skip)
126 * @data: (nullable): %BDFSUdfInfo to free
127 *
128 * Frees @data.
129 */
130 void bd_fs_udf_info_free (BDFSUdfInfo *data) {
131 if (data == NULL)
132 return;
133
134 g_free (data->label);
135 g_free (data->uuid);
136 g_free (data->revision);
137 g_free (data->lvid);
138 g_free (data->vid);
139 g_free (data);
140 }
141
142 /* get a valid UDF Volume Identifier from label */
143 static gchar* get_vid (const gchar *label) {
144 gchar *vid = NULL;
145 const gchar *next_p = NULL;
146 gunichar unichar;
147 guint pos = 0;
148
149 if (!g_utf8_validate (label, -1, NULL))
150 return NULL;
151
152 if (g_utf8_strlen (label, -1) <= 15)
153 vid = g_strdup (label);
154 else {
155 /* vid can be at most 30 characters (or 15 > 0xFF) so we'll truncate the label if needed */
156 next_p = label;
157 while (next_p && *next_p) {
158 unichar = g_utf8_get_char (next_p);
159 if (unichar > 0xFF) {
160 if (pos < 15) {
161 /* vid can have at most 15 characters > 0xFF */
162 vid = g_utf8_substring (label, 0, 15);
163 break;
164 } else if (pos < 30) {
165 /* cut before the "problematic" character */
166 vid = g_utf8_substring (label, 0, pos);
167 break;
168 }
169 }
170
171 next_p = g_utf8_next_char (next_p);
172 pos++;
173 }
174
175 if (!vid) {
176 /* we can't have more that 30 characters in vid so cut at 30 */
177 vid = g_utf8_substring (label, 0, 30);
178 }
179 }
180
181 return vid;
182 }
183
184 G_GNUC_INTERNAL BDExtraArg **
185 bd_fs_udf_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
186 GPtrArray *options_array = g_ptr_array_new ();
187 const BDExtraArg **extra_p = NULL;
188 g_autofree gchar *vid = NULL;
189
190 if (options->label && g_strcmp0 (options->label, "") != 0) {
191 vid = get_vid (options->label);
192
193 g_ptr_array_add (options_array, bd_extra_arg_new ("--lvid", options->label));
194 g_ptr_array_add (options_array, bd_extra_arg_new ("--vid", vid));
195 }
196
197 if (options->uuid && g_strcmp0 (options->uuid, "") != 0)
198 g_ptr_array_add (options_array, bd_extra_arg_new ("-u", options->uuid));
199
200 if (extra) {
201 for (extra_p = extra; *extra_p; extra_p++)
202 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
203 }
204
205 g_ptr_array_add (options_array, NULL);
206
207 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
208 }
209
210 static gint get_blocksize (const gchar *device, GError **error) {
211 gint fd = -1;
212 gint blksize = 0;
213
214 fd = open (device, O_RDONLY);
215 if (fd < 0) {
216 g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
217 "Failed to open the device '%s' to get its block size: %s",
218 device, strerror_l (errno, _C_LOCALE));
219 return -1;
220 }
221
222 if (ioctl (fd, BLKSSZGET, &blksize) < 0) {
223 g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
224 "Failed to get block size of the device '%s': %s",
225 device, strerror_l (errno, _C_LOCALE));
226 close (fd);
227 return -1;
228 }
229
230 close (fd);
231
232 return blksize;
233 }
234
235 /**
236 * bd_fs_udf_mkfs:
237 * @device: the device to create a new UDF fs on
238 * @media_type: (nullable): specify the media type or %NULL for default ('hd')
239 * @revision: (nullable): UDF revision to use or %NULL for default ('2.01')
240 * @block_size: block size in bytes or 0 for auto detection (device logical block size)
241 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
242 * passed to the 'mkudffs' utility)
243 * @error: (out) (optional): place to store error (if any)
244 *
245 * Returns: whether a new UDF fs was successfully created on @device or not
246 *
247 * Tech category: %BD_FS_TECH_UDF-%BD_FS_TECH_MODE_MKFS
248 */
249 gboolean bd_fs_udf_mkfs (const gchar *device, const gchar *media_type, gchar *revision, guint64 block_size, const BDExtraArg **extra, GError **error) {
250 const gchar *args[7] = {"mkudffs", "--utf8", NULL, NULL, NULL, device, NULL};
251 gint detected_bs = -1;
252 gboolean ret = FALSE;
253
254 if (!check_deps (&avail_deps, DEPS_MKUDFFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
255 return FALSE;
256
257 if (block_size != 0)
258 args[2] = g_strdup_printf ("--blocksize=%"G_GUINT64_FORMAT"", block_size);
259 else {
260 detected_bs = get_blocksize (device, error);
261 if (detected_bs < 0)
262 return FALSE;
263
264 args[2] = g_strdup_printf ("--blocksize=%d", detected_bs);
265 }
266
267 if (media_type)
268 args[3] = g_strdup_printf ("--media-type=%s", media_type);
269 else
270 args[3] = g_strdup ("--media-type=hd");
271
272 if (revision)
273 args[4] = g_strdup_printf ("--udfrev=%s", revision);
274 else
275 args[4] = g_strdup ("--udfrev=0x201");
276
277 ret = bd_utils_exec_and_report_error (args, extra, error);
278
279 g_free ((gchar *) args[2]);
280 g_free ((gchar *) args[3]);
281 g_free ((gchar *) args[4]);
282
283 return ret;
284 }
285
286 /**
287 * bd_fs_udf_set_label:
288 * @device: the device containing the file system to set label for
289 * @label: label to set
290 * @error: (out) (optional): place to store error (if any)
291 *
292 * Note: This sets both Volume Identifier and Logical Volume Identifier. Volume Identifier
293 * is truncated to 30 or 15 characters to accommodate to the different length limits
294 * of these labels.
295 *
296 * Returns: whether the label of UDF file system on the @device was
297 * successfully set or not
298 *
299 * Tech category: %BD_FS_TECH_UDF-%BD_FS_TECH_MODE_SET_LABEL
300 */
301 gboolean bd_fs_udf_set_label (const gchar *device, const gchar *label, GError **error) {
302 const gchar *args[6] = {"udflabel", "--utf8", NULL, NULL, device, NULL};
303 g_autofree gchar *vid = NULL;
304 gboolean ret = FALSE;
305
306 if (!check_deps (&avail_deps, DEPS_UDFLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error))
307 return FALSE;
308
309 if (!bd_fs_udf_check_label (label, error))
310 return FALSE;
311
312 vid = get_vid (label);
313
314 args[2] = g_strdup_printf ("--lvid=%s", label);
315 args[3] = g_strdup_printf ("--vid=%s", vid);
316
317 ret = bd_utils_exec_and_report_error (args, NULL, error);
318
319 g_free ((gchar *) args[2]);
320 g_free ((gchar *) args[3]);
321
322 return ret;
323 }
324
325 /**
326 * bd_fs_udf_check_label:
327 * @label: label to check
328 * @error: (out) (optional): place to store error
329 *
330 * Note: This checks only whether @label adheres the length limits for Logical Volume Identifier,
331 * not the stricter limits for Volume Identifier.
332 *
333 * Returns: whether @label is a valid label for the UDF file system or not
334 * (reason is provided in @error)
335 *
336 * Tech category: always available
337 */
338 gboolean bd_fs_udf_check_label (const gchar *label, GError **error) {
339 const gchar *next_p = NULL;
340 gunichar unichar;
341 gint len = 0;
342
343 if (g_str_is_ascii (label)) {
344 if (strlen (label) > 126) {
345 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
346 "Label for UDF filesystem can be at most 126 characters long.");
347 return FALSE;
348 }
349
350 return TRUE;
351 }
352
353 if (g_utf8_validate (label, -1, NULL)) {
354 len = g_utf8_strlen (label, -1);
355 if (len <= 63)
356 /* utf-8 and <= 63 will be always valid */
357 return TRUE;
358
359 if (len > 126) {
360 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
361 "Label for UDF filesystem can be at most 126 characters long.");
362 return FALSE;
363 }
364
365 next_p = label;
366 while (next_p && *next_p) {
367 unichar = g_utf8_get_char (next_p);
368 if (unichar > 0xFF) {
369 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
370 "Label for UDF filesystem containing unicode characters above U+FF can "\
371 "be at most 63 characters long.");
372 return FALSE;
373 }
374
375 next_p = g_utf8_next_char (next_p);
376 }
377 } else {
378 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
379 "Label for UDF filesystem must be a valid UTF-8 string.");
380 return FALSE;
381 }
382
383 return TRUE;
384 }
385
386 /**
387 * bd_fs_udf_set_uuid:
388 * @device: the device containing the file system to set the UUID (serial number) for
389 * @uuid: (nullable): UUID to set or %NULL to generate a new one
390 * @error: (out) (optional): place to store error (if any)
391 *
392 * Returns: whether the UUID of the UDF file system on the @device was
393 * successfully set or not
394 *
395 * Tech category: %BD_FS_TECH_UDF-%BD_FS_TECH_MODE_SET_UUID
396 */
397 gboolean bd_fs_udf_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
398 gboolean ret = FALSE;
399 const gchar *args[4] = {"udflabel", NULL, device, NULL};
400
401 if (!check_deps (&avail_deps, DEPS_UDFLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error))
402 return FALSE;
403
404 if (!uuid)
405 args[1] = g_strdup ("--uuid=random");
406 else
407 args[1] = g_strdup_printf ("--uuid=%s", uuid);
408
409 ret = bd_utils_exec_and_report_error (args, NULL, error);
410
411 g_free ((gchar *) args[1]);
412 return ret;
413 }
414
415 /**
416 * bd_fs_udf_check_uuid:
417 * @uuid: UUID to check
418 * @error: (out) (optional): place to store error
419 *
420 * Returns: whether @uuid is a valid UUID for the UDF file system or not
421 * (reason is provided in @error)
422 *
423 * Tech category: always available
424 */
425 gboolean bd_fs_udf_check_uuid (const gchar *uuid, GError **error) {
426 size_t len = 0;
427
428 len = strlen (uuid);
429 if (len != 16) {
430 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
431 "UUID for UDF filesystem must be 16 characters long.");
432 return FALSE;
433 }
434
435 for (size_t i = 0; i < len; i++) {
436 if (!g_ascii_isxdigit (uuid[i]) || (!g_ascii_isdigit (uuid[i]) && !g_ascii_islower (uuid[i]))) {
437 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
438 "UUID for UDF filesystem must be a lowercase hexadecimal number.");
439 return FALSE;
440 }
441 }
442
443 return TRUE;
444 }
445
446 /**
447 * parse_udf_vars:
448 * @str: string to parse
449 * @num_items: (out): number of parsed items
450 *
451 * Returns: (transfer full): a GHashTable containing key-value items parsed from the @string
452 */
453 static GHashTable* parse_udf_vars (const gchar *str, guint *num_items) {
454 GHashTable *table = NULL;
455 gchar **items = NULL;
456 gchar **item_p = NULL;
457 gchar **key_val = NULL;
458
459 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
460 *num_items = 0;
461
462 items = g_strsplit (str, "\n", 0);
463 for (item_p=items; *item_p; item_p++) {
464 if (g_str_has_prefix (*item_p, "start="))
465 continue;
466 key_val = g_strsplit (*item_p, "=", 2);
467 if (g_strv_length (key_val) == 2) {
468 /* we only want to process valid lines (with the '=' character) */
469 g_hash_table_insert (table, key_val[0], key_val[1]);
470 g_free (key_val);
471 (*num_items)++;
472 } else
473 /* invalid line, just free key_val */
474 g_strfreev (key_val);
475 }
476
477 g_strfreev (items);
478 return table;
479 }
480
481 static BDFSUdfInfo* get_udf_data_from_table (GHashTable *table) {
482 BDFSUdfInfo *data = g_new0 (BDFSUdfInfo, 1);
483 gchar *value = NULL;
484
485 data->revision = g_strdup ((gchar*) g_hash_table_lookup (table, "udfrev"));
486 data->vid = g_strdup ((gchar*) g_hash_table_lookup (table, "vid"));
487 data->lvid = g_strdup ((gchar*) g_hash_table_lookup (table, "lvid"));
488
489 value = (gchar*) g_hash_table_lookup (table, "blocksize");
490 if (value)
491 data->block_size = g_ascii_strtoull (value, NULL, 0);
492 else
493 data->block_size = 0;
494
495 value = (gchar*) g_hash_table_lookup (table, "blocks");
496 if (value)
497 data->block_count = g_ascii_strtoull (value, NULL, 0);
498 else
499 data->block_count = 0;
500
501 value = (gchar*) g_hash_table_lookup (table, "freeblocks");
502 if (value)
503 data->free_blocks = g_ascii_strtoull (value, NULL, 0);
504 else
505 data->free_blocks = 0;
506
507 g_hash_table_destroy (table);
508
509 return data;
510 }
511
512 /**
513 * bd_fs_udf_get_info:
514 * @device: the device containing the file system to get info for
515 * @error: (out) (optional): place to store error (if any)
516 *
517 * Returns: (transfer full): information about the file system on @device or
518 * %NULL in case of error
519 *
520 * Tech category: %BD_FS_TECH_UDF-%BD_FS_TECH_MODE_QUERY
521 */
522 BDFSUdfInfo* bd_fs_udf_get_info (const gchar *device, GError **error) {
523 const gchar *args[4] = {"udfinfo", "--utf8", device, NULL};
524 gboolean success = FALSE;
525 gchar *output = NULL;
526 BDFSUdfInfo *ret = NULL;
527 GHashTable *table = NULL;
528 guint num_items = 0;
529
530 if (!check_deps (&avail_deps, DEPS_UDFINFO_MASK, deps, DEPS_LAST, &deps_check_lock, error))
531 return NULL;
532
533 success = bd_utils_exec_and_capture_output (args, NULL, &output, error);
534 if (!success) {
535 /* error is already populated */
536 return NULL;
537 }
538
539 table = parse_udf_vars (output, &num_items);
540 g_free (output);
541 if (!table || (num_items == 0)) {
542 /* something bad happened or some expected items were missing */
543 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse UDF file system information");
544 if (table)
545 g_hash_table_destroy (table);
546 return NULL;
547 }
548
549 ret = get_udf_data_from_table (table);
550 if (!ret) {
551 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse UDF file system information");
552 return NULL;
553 }
554
555 success = get_uuid_label (device, &(ret->uuid), &(ret->label), error);
556 if (!success) {
557 /* error is already populated */
558 bd_fs_udf_info_free (ret);
559 return NULL;
560 }
561
562 return ret;
563 }