qemu-block
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-block] [PATCH v6 6/9] qemu-img: Use bdrv_filename() for map


From: Max Reitz
Subject: [Qemu-block] [PATCH v6 6/9] qemu-img: Use bdrv_filename() for map
Date: Mon, 16 Jan 2017 17:15:47 +0100

Replaces bs->filename by the result of bdrv_filename() in the
qemu-img map subcommand. Since that value is queried relatively often,
however, we should try to reuse the last result.

Signed-off-by: Max Reitz <address@hidden>
---
 qemu-img.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 5df66fe661..6f14792025 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2469,12 +2469,14 @@ static void dump_map_entry(OutputFormat output_format, 
MapEntry *e,
 }
 
 static int get_block_status(BlockDriverState *bs, int64_t sector_num,
-                            int nb_sectors, MapEntry *e)
+                            int nb_sectors, MapEntry *e,
+                            BlockDriverState **current_file)
 {
     int64_t ret;
     int depth;
     BlockDriverState *file;
     bool has_offset;
+    char *filename;
 
     /* As an optimization, we could cache the current range of unallocated
      * clusters in each file of the chain, and avoid querying the same
@@ -2503,6 +2505,18 @@ static int get_block_status(BlockDriverState *bs, 
int64_t sector_num,
 
     has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
 
+    if (!file || !has_offset) {
+        g_free(e->filename);
+        filename = NULL;
+    } else if (file == *current_file && e->has_filename) {
+        filename = e->filename;
+    } else {
+        g_free(e->filename);
+        filename = bdrv_filename(file, NULL, 0);
+    }
+
+    *current_file = file;
+
     *e = (MapEntry) {
         .start = sector_num * BDRV_SECTOR_SIZE,
         .length = nb_sectors * BDRV_SECTOR_SIZE,
@@ -2511,8 +2525,8 @@ static int get_block_status(BlockDriverState *bs, int64_t 
sector_num,
         .offset = ret & BDRV_BLOCK_OFFSET_MASK,
         .has_offset = has_offset,
         .depth = depth,
-        .has_filename = file && has_offset,
-        .filename = file && has_offset ? file->filename : NULL,
+        .has_filename = filename,
+        .filename = filename,
     };
 
     return 0;
@@ -2544,10 +2558,10 @@ static int img_map(int argc, char **argv)
     int c;
     OutputFormat output_format = OFORMAT_HUMAN;
     BlockBackend *blk;
-    BlockDriverState *bs;
+    BlockDriverState *bs, *current_file = NULL;
     const char *filename, *fmt, *output;
     int64_t length;
-    MapEntry curr = { .length = 0 }, next;
+    MapEntry curr = { .length = 0 }, next = { 0 };
     int ret = 0;
     bool image_opts = false;
 
@@ -2633,7 +2647,7 @@ static int img_map(int argc, char **argv)
         /* Probe up to 1 GiB at a time.  */
         nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
         n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
-        ret = get_block_status(bs, sector_num, n, &next);
+        ret = get_block_status(bs, sector_num, n, &next, &current_file);
 
         if (ret < 0) {
             error_report("Could not read file metadata: %s", strerror(-ret));
@@ -2648,12 +2662,17 @@ static int img_map(int argc, char **argv)
         if (curr.length > 0) {
             dump_map_entry(output_format, &curr, &next);
         }
+
+        g_free(curr.filename);
         curr = next;
+        curr.filename = g_strdup(curr.filename);
     }
 
     dump_map_entry(output_format, &curr, NULL);
 
 out:
+    g_free(curr.filename);
+    g_free(next.filename);
     blk_unref(blk);
     return ret < 0;
 }
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]