mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
daemon/device-name-translation.c: Fix btrfs volume reverse translation
Devices associated with btrfs volumes are not reverse-translated
(e.g., btrfsvol:/dev/sdX to sdY).
Forward translation occurs, creating a path mismatch. This causes
errors in subsequent btrfs commands.
Thanks: Arye Yurkovsky
(cherry picked from commit c7b204bce3)
This commit is contained in:
@@ -248,12 +248,17 @@ device_name_translation (const char *device)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *reverse_btrfsvol (const char *device);
|
||||
|
||||
char *
|
||||
reverse_device_name_translation (const char *device)
|
||||
{
|
||||
char *ret = NULL;
|
||||
size_t i;
|
||||
|
||||
if (STRPREFIX (device, "btrfsvol:"))
|
||||
return reverse_btrfsvol (device);
|
||||
|
||||
/* Look it up in the cache, and if found return the canonical name.
|
||||
* If not found return a copy of the original string.
|
||||
*/
|
||||
@@ -287,3 +292,34 @@ reverse_device_name_translation (const char *device)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* btrfsvol:/dev/sdX also needs reversing. */
|
||||
static char *
|
||||
reverse_btrfsvol (const char *device)
|
||||
{
|
||||
const char prefix[] = "btrfsvol:";
|
||||
const char *device_start, *device_end;
|
||||
CLEANUP_FREE char *device_name = NULL;
|
||||
CLEANUP_FREE char *reversed_device = NULL;
|
||||
char *ret;
|
||||
|
||||
device_start = device + strlen (prefix);
|
||||
device_end = strchr (device_start + strlen ("/dev/"), '/');
|
||||
device_name = strndup (device_start, device_end - device_start);
|
||||
if (device_name == NULL) {
|
||||
reply_with_perror ("strndup");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
reversed_device = reverse_device_name_translation (device_name);
|
||||
if (reversed_device == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Construct the final btrfsvol: and return it, caller frees. */
|
||||
if (asprintf (&ret, "%s%s%s", prefix, reversed_device, device_end) == -1) {
|
||||
reply_with_perror ("asprintf");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user