|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | #include <glib.h> |
| 21 | +#include <stdio.h> |
21 | 22 | #include <string.h> |
22 | 23 | #include <unistd.h> |
23 | 24 | #include <blockdev/utils.h> |
24 | 25 | #include <bs_size.h> |
| 26 | +#include <btrfsutil.h> |
25 | 27 |
|
26 | 28 | #include "btrfs.h" |
27 | 29 | #include "check_deps.h" |
@@ -258,6 +260,10 @@ static BDBtrfsFilesystemInfo* get_filesystem_info_from_match (GMatchInfo *match_ |
258 | 260 | return ret; |
259 | 261 | } |
260 | 262 |
|
| 263 | +static const char* format_btrfs_error (enum btrfs_util_error err) { |
| 264 | + return g_strdup_printf ("%s: %m", btrfs_util_strerror (err)); |
| 265 | +} |
| 266 | + |
261 | 267 | /** |
262 | 268 | * bd_btrfs_create_volume: |
263 | 269 | * @devices: (array zero-terminated=1): list of devices to create btrfs volume from |
@@ -454,75 +460,37 @@ gboolean bd_btrfs_delete_subvolume (const gchar *mountpoint, const gchar *name, |
454 | 460 | * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_QUERY |
455 | 461 | */ |
456 | 462 | guint64 bd_btrfs_get_default_subvolume_id (const gchar *mountpoint, GError **error) { |
457 | | - GRegex *regex = NULL; |
458 | | - GMatchInfo *match_info = NULL; |
459 | | - gboolean success = FALSE; |
460 | | - gchar *output = NULL; |
461 | | - gchar *match = NULL; |
| 463 | + enum btrfs_util_error err; |
462 | 464 | guint64 ret = 0; |
463 | | - const gchar *argv[5] = {"btrfs", "subvol", "get-default", mountpoint, NULL}; |
464 | 465 |
|
465 | | - if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) || |
466 | | - !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error)) |
467 | | - return 0; |
468 | | - |
469 | | - regex = g_regex_new ("ID (\\d+) .*", 0, 0, error); |
470 | | - if (!regex) { |
471 | | - bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create new GRegex"); |
472 | | - /* error is already populated */ |
473 | | - return 0; |
| 466 | + err = btrfs_util_get_default_subvolume (mountpoint, &ret); |
| 467 | + if (err) { |
| 468 | + g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_NOT_FOUND, format_btrfs_error (err)); |
474 | 469 | } |
475 | 470 |
|
476 | | - success = bd_utils_exec_and_capture_output (argv, NULL, &output, error); |
477 | | - if (!success) { |
478 | | - g_regex_unref (regex); |
479 | | - return 0; |
480 | | - } |
481 | | - |
482 | | - success = g_regex_match (regex, output, 0, &match_info); |
483 | | - if (!success) { |
484 | | - g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_PARSE, "Failed to parse subvolume's ID"); |
485 | | - g_regex_unref (regex); |
486 | | - g_match_info_free (match_info); |
487 | | - g_free (output); |
488 | | - return 0; |
489 | | - } |
490 | | - |
491 | | - match = g_match_info_fetch (match_info, 1); |
492 | | - ret = g_ascii_strtoull (match, NULL, 0); |
493 | | - |
494 | | - g_free (match); |
495 | | - g_match_info_free (match_info); |
496 | | - g_regex_unref (regex); |
497 | | - g_free (output); |
498 | | - |
499 | 471 | return ret; |
500 | 472 | } |
501 | 473 |
|
502 | 474 | /** |
503 | 475 | * bd_btrfs_set_default_subvolume: |
504 | 476 | * @mountpoint: mountpoint of the volume to set the default subvolume ID of |
505 | 477 | * @subvol_id: ID of the subvolume to be set as the default subvolume |
506 | | - * @extra: (nullable) (array zero-terminated=1): extra options for the setting (right now |
507 | | - * passed to the 'btrfs' utility) |
508 | 478 | * @error: (out) (optional): place to store error (if any) |
509 | 479 | * |
510 | 480 | * Returns: whether the @mountpoint volume's default subvolume was correctly set |
511 | 481 | * to @subvol_id or not |
512 | 482 | * |
513 | 483 | * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_MODIFY |
514 | 484 | */ |
515 | | -gboolean bd_btrfs_set_default_subvolume (const gchar *mountpoint, guint64 subvol_id, const BDExtraArg **extra, GError **error) { |
516 | | - const gchar *argv[6] = {"btrfs", "subvol", "set-default", NULL, mountpoint, NULL}; |
| 485 | +gboolean bd_btrfs_set_default_subvolume (const gchar *mountpoint, guint64 subvol_id, GError **error) { |
| 486 | + enum btrfs_util_error err; |
517 | 487 | gboolean ret = FALSE; |
518 | 488 |
|
519 | | - if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) || |
520 | | - !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error)) |
521 | | - return FALSE; |
522 | | - |
523 | | - argv[3] = g_strdup_printf ("%"G_GUINT64_FORMAT, subvol_id); |
524 | | - ret = bd_utils_exec_and_report_error (argv, extra, error); |
525 | | - g_free ((gchar *) argv[3]); |
| 489 | + err = btrfs_util_set_default_subvolume (mountpoint, subvol_id); |
| 490 | + if (err) |
| 491 | + g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_NOT_FOUND, format_btrfs_error (err)); |
| 492 | + else |
| 493 | + ret = TRUE; |
526 | 494 |
|
527 | 495 | return ret; |
528 | 496 | } |
|
0 commit comments