|
22 | 22 | #include <unistd.h> |
23 | 23 | #include <blockdev/utils.h> |
24 | 24 | #include <bs_size.h> |
| 25 | +#include <btrfsutil.h> |
25 | 26 |
|
26 | 27 | #include "btrfs.h" |
27 | 28 | #include "check_deps.h" |
@@ -454,75 +455,37 @@ gboolean bd_btrfs_delete_subvolume (const gchar *mountpoint, const gchar *name, |
454 | 455 | * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_QUERY |
455 | 456 | */ |
456 | 457 | 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; |
| 458 | + enum btrfs_util_error err; |
462 | 459 | guint64 ret = 0; |
463 | | - const gchar *argv[5] = {"btrfs", "subvol", "get-default", mountpoint, NULL}; |
464 | | - |
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; |
474 | | - } |
475 | 460 |
|
476 | | - success = bd_utils_exec_and_capture_output (argv, NULL, &output, error); |
477 | | - if (!success) { |
478 | | - g_regex_unref (regex); |
479 | | - return 0; |
| 461 | + err = btrfs_util_get_default_subvolume (mountpoint, &ret); |
| 462 | + if (err) { |
| 463 | + g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_NOT_FOUND, "%s: %m", btrfs_util_strerror (err)); |
480 | 464 | } |
481 | 465 |
|
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 | 466 | return ret; |
500 | 467 | } |
501 | 468 |
|
502 | 469 | /** |
503 | 470 | * bd_btrfs_set_default_subvolume: |
504 | 471 | * @mountpoint: mountpoint of the volume to set the default subvolume ID of |
505 | 472 | * @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 | 473 | * @error: (out) (optional): place to store error (if any) |
509 | 474 | * |
510 | 475 | * Returns: whether the @mountpoint volume's default subvolume was correctly set |
511 | 476 | * to @subvol_id or not |
512 | 477 | * |
513 | 478 | * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_MODIFY |
514 | 479 | */ |
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}; |
| 480 | +gboolean bd_btrfs_set_default_subvolume (const gchar *mountpoint, guint64 subvol_id, GError **error) { |
| 481 | + enum btrfs_util_error err; |
517 | 482 | gboolean ret = FALSE; |
518 | 483 |
|
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]); |
| 484 | + err = btrfs_util_set_default_subvolume (mountpoint, subvol_id); |
| 485 | + if (err) |
| 486 | + g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_NOT_FOUND, "%s: %m", btrfs_util_strerror (err)); |
| 487 | + else |
| 488 | + ret = TRUE; |
526 | 489 |
|
527 | 490 | return ret; |
528 | 491 | } |
|
0 commit comments