Skip to contentSkip to conversation

block: Refactor get_tmp_filename()

PATCH Superseded Bin Meng 2 participants 2 messages Sep 24, 2022 – Sep 26, 2022 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 block: Refactor get_tmp_filename()

From: Bin Meng <[email protected]>

At present there are two callers of get_tmp_filename() and they are inconsistent.

One does:

    /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
    char *tmp_filename = g_malloc0(PATH_MAX + 1);
    ...
    ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);

while the other does:

    s->qcow_filename = g_malloc(PATH_MAX);
    ret = get_tmp_filename(s->qcow_filename, PATH_MAX);

As we can see different 'size' arguments are passed. There are also platform specific implementations inside the function, and this use of snprintf is really undesirable.

Refactor this routine by changing its signature to:

char *get_tmp_filename(void)

and use g_file_open_tmp() for a consistent implementation.

---

 include/block/block_int-common.h |  2 +-
 block.c                          | 42 ++++++++++----------------------
 block/vvfat.c                    |  8 +++---
 3 files changed, 18 insertions(+), 34 deletions(-)
include/block/block_int-common.h +1 -1
index 8947abab76..ea69a9349c 100644
@@ -1230,7 +1230,7 @@ static inline BlockDriverState *child_bs(BdrvChild *child)
 }
 
 int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp);
-int get_tmp_filename(char *filename, int size);
+char *get_tmp_filename(void);
 void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
                                       QDict *options);
 
block.c +13 -29

68 lines not on this page · raw message

block/vvfat.c +4 -4
index d6dd919683..135fafb166 100644
@@ -3146,10 +3146,10 @@ static int enable_write_target(BlockDriverState *bs, Error **errp)
 
     array_init(&(s->commits), sizeof(commit_t));
 
-    s->qcow_filename = g_malloc(PATH_MAX);
-    ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
-    if (ret < 0) {
-        error_setg_errno(errp, -ret, "can't create temporary file");
+    s->qcow_filename = get_tmp_filename();
+    if (!s->qcow_filename) {
+        error_setg_errno(errp, errno, "can't create temporary file");
+        ret = -errno;
         goto err;
     }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Bin Meng

From: Bin Meng <[email protected]>

At present there are two callers of get_tmp_filename() and they are inconsistent.

One does:

    /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
    char *tmp_filename = g_malloc0(PATH_MAX + 1);
    ...
    ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);

while the other does:

    s->qcow_filename = g_malloc(PATH_MAX);
    ret = get_tmp_filename(s->qcow_filename, PATH_MAX);

As we can see different 'size' arguments are passed. There are also platform specific implementations inside the function, and this use of snprintf is really undesirable.

Refactor this routine by changing its signature to:

char *get_tmp_filename(void)

and use g_file_open_tmp() for a consistent implementation.

Quoted 7 lines

---

 include/block/block_int-common.h |  2 +-
 block.c                          | 42 ++++++++++----------------------
 block/vvfat.c                    |  8 +++---
 3 files changed, 18 insertions(+), 34 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|