[PATCH] daemon: Fix btrfs volume device reverse-translation
by Arye Yurkovsky
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.
---
daemon/device-name-translation.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/daemon/device-name-translation.c b/daemon/device-name-translation.c
index cfebc6495..eaf9a4686 100644
--- a/daemon/device-name-translation.c
+++ b/daemon/device-name-translation.c
@@ -251,9 +251,28 @@ device_name_translation (const char *device)
char *
reverse_device_name_translation (const char *device)
{
+ const char *original_device = device;
char *ret = NULL;
size_t i;
+ const char *prefix = "";
+ const char *suffix = "";
+ bool btrfsvol = STRPREFIX (device, "btrfsvol:");
+ if (btrfsvol) {
+ prefix = "btrfsvol:";
+
+ const char *device_start = device + strlen (prefix);
+ const char *device_end = strchr (device_start + strlen ("/dev/"), '/');
+ device = strndup(device_start, device_end - device_start);
+ if (device == NULL) {
+ perror ("strndup");
+ return NULL;
+ }
+
+ suffix = device_end;
+
+ }
+
/* Look it up in the cache, and if found return the canonical name.
* If not found return a copy of the original string.
*/
@@ -265,13 +284,19 @@ reverse_device_name_translation (const char *device)
char drv[16];
guestfs_int_drive_name (i, drv);
- if (asprintf (&ret, "/dev/sd%s%s", drv, &device[len]) == -1) {
+ if (asprintf (&ret, "%s/dev/sd%s%s%s", prefix, drv, &device[len], suffix) == -1) {
reply_with_perror ("asprintf");
+ if (btrfsvol)
+ free (device);
return NULL;
}
break;
}
}
+ if (btrfsvol) {
+ free (device);
+ device = original_device;
+ }
if (ret == NULL) {
ret = strdup (device);
--
2.52.0.460.gd25c4c69ec-goog
2 weeks
[PATCH] deamon: btrfs: Fix invalid memory access
by Arye Yurkovsky
---
daemon/btrfs.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index 4b6059621..161cae3cd 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -971,11 +971,11 @@ do_btrfs_subvolume_show (const char *subvolume)
return NULL;
if (ss_len != 0)
- ss[ss_len++] = ',';
+ ss[ss_len - 1] = ',';
memcpy (ss + ss_len, key, strlen (key));
ss_len += strlen (key);
- ss[ss_len] = '\0';
+ ss[ss_len++] = '\0';
p = analyze_line (p, &key, &value, ':');
}
--
2.52.0.rc1.455.g30608eb744-goog
1 month
[nbdkit PATCH] linuxdisk: Simplify computation of last line of du
by Eric Blake
No need to re-strdup things in a loop of getline - since we are using
popen to get the output in the first place, we can use the shell to
hand us just the final line.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
Even though the POSIX folks ruled today that our "ab"use of a
getline() loop to determine the final line of du output was
non-portable [1], I was still miffed at the malloc overhead that our
workaround for rawhide glibc entailed. So this is the result I came
up with.
[1] https://www.austingroupbugs.net/bug_view_page.php?bug_id=1953
plugins/linuxdisk/filesystem.c | 28 +++++++---------------------
1 file changed, 7 insertions(+), 21 deletions(-)
diff --git a/plugins/linuxdisk/filesystem.c b/plugins/linuxdisk/filesystem.c
index 283af61a..aa4615e6 100644
--- a/plugins/linuxdisk/filesystem.c
+++ b/plugins/linuxdisk/filesystem.c
@@ -148,7 +148,7 @@ create_filesystem (struct virtual_disk *disk)
static int64_t
estimate_size (void)
{
- CLEANUP_FREE char *command = NULL, *line = NULL, *lastline = NULL;
+ CLEANUP_FREE char *command = NULL, *line = NULL;
size_t len = 0;
FILE *fp;
int64_t ret;
@@ -162,6 +162,7 @@ estimate_size (void)
}
fprintf (fp, "du -c -k -s ");
shell_quote (dir, fp);
+ fprintf (fp, "| tail -n1");
if (fclose (fp) == EOF) {
nbdkit_error ("memstream failed: %m");
return -1;
@@ -175,21 +176,11 @@ estimate_size (void)
return -1;
}
- /* Ignore everything up to the last line. */
- len = 0;
- while (getline (&line, &len, fp) != -1) {
- nbdkit_debug ("du: %s", line);
- free (lastline);
- lastline = strndup (line, len);
- if (lastline == NULL) {
- nbdkit_error ("strndup: %m");
- pclose (fp);
- return -1;
- }
- }
- if (ferror (fp)) {
+ /* Should only be one line of input. */
+ if (getline (&line, &len, fp) == -1 || ferror (fp)) {
nbdkit_error ("getline failed: %m");
pclose (fp);
+ free (line);
return -1;
}
@@ -201,14 +192,9 @@ estimate_size (void)
if (exit_status_to_nbd_error (r, "pclose: du") == -1)
return -1;
- if (lastline == NULL) {
- nbdkit_error ("no output from du command");
- return -1;
- }
-
/* Parse the last line. */
- if (sscanf (lastline, "%" SCNi64, &ret) != 1 || ret < 0) {
- nbdkit_error ("could not parse last line from du command: %s", lastline);
+ if (sscanf (line, "%" SCNi64, &ret) != 1 || ret < 0) {
+ nbdkit_error ("could not parse last line from du command: %s", line);
return -1;
}
--
2.51.1
1 month, 1 week