block: Refactor get_tmp_filename()
PATCH v2 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.
---
Changes in v2:
- Use g_autofree and g_steal_pointer
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
@@ -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
@@ -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
Quoted 56 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.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Useg_autofreeandg_steal_pointerinclude/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@@ -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 +0
-0
block.c int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) · line 864
Quoted 6 lines
block.c +2
-1@@ -860,38 +860,23 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)/* * Create a uniquely-named empty temporary file. -* Return 0 upon success, otherwise a negative errno value. +* Return the actual name used upon success, otherwise NULL. +* The called function is responsible for freeing it.
s/called/caller/.
Quoted 46 lines
block.c +7
-23*/ -int get_tmp_filename (char *filename,int size) +char *get_tmp_filename (void ) { -#ifdef _WIN32 -char temp_dir[MAX_PATH ]; -/* GetTempFileName requires that its output buffer (4th param) -have length MAX_PATH or greater. */ -assert (size >=MAX_PATH ); -return (GetTempPath (MAX_PATH , temp_dir) - &&GetTempFileName (temp_dir,"qem" ,0 , filename) - ?0 : -GetLastError ()); -#else + g_autofree char *filename = NULL;int fd; -const char *tmpdir; - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) { - tmpdir ="/var/tmp" ; - } -if (snprintf (filename, size,"%s/vl.XXXXXX" , tmpdir) >= size) { -return -EOVERFLOW ; - } - fd =mkstemp (filename); + + fd =g_file_open_tmp ("vl.XXXXXX" , &filename,NULL );if (fd <0 ) { -return -errno; +return NULL ; }if (close (fd) !=0 ) {unlink (filename); -return -errno; +return NULL ; } -return 0 ; -#endif +return g_steal_pointer (&filename); }block/vvfat.c +0
-0@@ -3146,10 +3146,10 @@ static int enable_write_target(BlockDriverState *bs, Error **errp)
block/vvfat.c static int enable_write_target(BlockDriverState *bs, Error **errp) · line 3152
Quoted 10 lines
block/vvfat.c +4
-4array_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;
I don't think we can reuse errno here. Wouldn't it be better to pass a Error* to get_tmp_filename, and use the error parameter to g_file_open_tmp()? That would match our Error* style use.
Quoted 3 lines
block/vvfat.c +0
-0goto err; }
Quoted 91 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.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Useg_autofreeandg_steal_pointerinclude/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@@ -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 +7
-21@@ -860,38 +860,23 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)/* * Create a uniquely-named empty temporary file. -* Return 0 upon success, otherwise a negative errno value. +* Return the actual name used upon success, otherwise NULL. +* The called function is responsible for freeing it. */ -int get_tmp_filename (char *filename,int size) +char *get_tmp_filename (void ) { -#ifdef _WIN32 -char temp_dir[MAX_PATH ]; -/* GetTempFileName requires that its output buffer (4th param) -have length MAX_PATH or greater. */ -assert (size >=MAX_PATH ); -return (GetTempPath (MAX_PATH , temp_dir) - &&GetTempFileName (temp_dir,"qem" ,0 , filename) - ?0 : -GetLastError ()); -#else + g_autofree char *filename = NULL;int fd; -const char *tmpdir; - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) { - tmpdir ="/var/tmp" ; - } -if (snprintf (filename, size,"%s/vl.XXXXXX" , tmpdir) >= size) { -return -EOVERFLOW ; - } - fd =mkstemp (filename); + + fd =g_file_open_tmp ("vl.XXXXXX" , &filename,NULL );if (fd <0 ) { -return -errno; +return NULL ;
block.c int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) · line 880
Quoted 10 lines
block.c +2
-3}if (close (fd) !=0 ) {unlink (filename); -return -errno; +return NULL ; } -return 0 ; -#endif +return g_steal_pointer (&filename); }
Oh my, what a lovely mess you're messing with!
The function creates a temporary *file*, not just a filename. Makes sense, as creating a unique filename is inherently racy. The contract is clear enough ("Create a uniquely-named empty temporary file"), but the function name is actively misleading.
Of course, creating a temporary file for the caller to (re)open is also racy. By the time the caller gets around to it, the filename could name anything. Return an open file file descriptor is a better idea. It's basically g_file_open_tmp(). Could we rework the two users of get_tmp_filename() accept a file descriptor?
Anyway, code after the patch:
/*
* Create a uniquely-named empty temporary file.
* Return the actual name used upon success, otherwise NULL.
* The called function is responsible for freeing it. */
char *get_tmp_filename(void)
{
g_autofree char *filename = NULL;
int fd;
fd = g_file_open_tmp("vl.XXXXXX", &filename, NULL);
if (fd < 0) {As Philippe wrote, this throws away possibly useful error information.
return NULL;
}
if (close(fd) != 0) {
unlink(filename);
return NULL;
}
return g_steal_pointer(&filename);}
Other than that, it's an improvement within the limits of a flawed interface.
[...]
Quoted 107 lines · Markus Armbruster
Quoted 91 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.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Useg_autofreeandg_steal_pointerinclude/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@@ -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 +7
-21@@ -860,38 +860,23 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)/* * Create a uniquely-named empty temporary file. -* Return 0 upon success, otherwise a negative errno value. +* Return the actual name used upon success, otherwise NULL. +* The called function is responsible for freeing it. */ -int get_tmp_filename (char *filename,int size) +char *get_tmp_filename (void ) { -#ifdef _WIN32 -char temp_dir[MAX_PATH ]; -/* GetTempFileName requires that its output buffer (4th param) -have length MAX_PATH or greater. */ -assert (size >=MAX_PATH ); -return (GetTempPath (MAX_PATH , temp_dir) - &&GetTempFileName (temp_dir,"qem" ,0 , filename) - ?0 : -GetLastError ()); -#else + g_autofree char *filename = NULL;int fd; -const char *tmpdir; - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) { - tmpdir ="/var/tmp" ; - } -if (snprintf (filename, size,"%s/vl.XXXXXX" , tmpdir) >= size) { -return -EOVERFLOW ; - } - fd =mkstemp (filename); + + fd =g_file_open_tmp ("vl.XXXXXX" , &filename,NULL );if (fd <0 ) { -return -errno; +return NULL ;block.c int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) · line 880
Quoted 10 lines
block.c +2
-3}if (close (fd) !=0 ) {unlink (filename); -return -errno; +return NULL ; } -return 0 ; -#endif +return g_steal_pointer (&filename); }Oh my, what a lovely mess you're messing with!
Quoted 4 lines
The function creates a temporary *file*, not just a filename. Makes sense, as creating a unique filename is inherently racy. The contract is clear enough ("Create a uniquely-named empty temporary file"), but the function name is actively misleading.
Agreed that the name is misleading.
Quoted 5 lines
Of course, creating a temporary file for the caller to (re)open is also racy. By the time the caller gets around to it, the filename could name anything. Return an open file file descriptor is a better idea. It's basically
g_file_open_tmp(). Could we rework the two users ofget_tmp_filename()accept a file descriptor?
I looked at the 2 callers, and it looks like we need to do more than these 2 callers to teach them to accept a file descriptor. :(
Quoted 25 lines
Anyway, code after the patch:
/* * Create a uniquely-named empty temporary file. * Return the actual name used upon success, otherwise NULL. * The called function is responsible for freeing it. */ char *get_tmp_filename(void) { g_autofree char *filename = NULL; int fd; fd = g_file_open_tmp("vl.XXXXXX", &filename, NULL); if (fd < 0) {As Philippe wrote, this throws away possibly useful error information.
return NULL; } if (close(fd) != 0) { unlink(filename); return NULL; } return g_steal_pointer(&filename);
Quoted 7 lines
}
Other than that, it's an improvement within the limits of a flawed interface.
[...]
Regards,
Bin
Quoted 108 lines · Bin Meng
Quoted 107 lines · Markus Armbruster
Quoted 91 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.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Useg_autofreeandg_steal_pointerinclude/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@@ -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 +7
-21@@ -860,38 +860,23 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)/* * Create a uniquely-named empty temporary file. -* Return 0 upon success, otherwise a negative errno value. +* Return the actual name used upon success, otherwise NULL. +* The called function is responsible for freeing it. */ -int get_tmp_filename (char *filename,int size) +char *get_tmp_filename (void ) { -#ifdef _WIN32 -char temp_dir[MAX_PATH ]; -/* GetTempFileName requires that its output buffer (4th param) -have length MAX_PATH or greater. */ -assert (size >=MAX_PATH ); -return (GetTempPath (MAX_PATH , temp_dir) - &&GetTempFileName (temp_dir,"qem" ,0 , filename) - ?0 : -GetLastError ()); -#else + g_autofree char *filename = NULL;int fd; -const char *tmpdir; - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) { - tmpdir ="/var/tmp" ; - } -if (snprintf (filename, size,"%s/vl.XXXXXX" , tmpdir) >= size) { -return -EOVERFLOW ; - } - fd =mkstemp (filename); + + fd =g_file_open_tmp ("vl.XXXXXX" , &filename,NULL );if (fd <0 ) { -return -errno; +return NULL ;block.c int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) · line 880
Quoted 10 lines
block.c +2
-3}if (close (fd) !=0 ) {unlink (filename); -return -errno; +return NULL ; } -return 0 ; -#endif +return g_steal_pointer (&filename); }Oh my, what a lovely mess you're messing with!
Quoted 6 lines
Quoted 4 lines
The function creates a temporary *file*, not just a filename. Makes sense, as creating a unique filename is inherently racy. The contract is clear enough ("Create a uniquely-named empty temporary file"), but the function name is actively misleading.
Agreed that the name is misleading.
Care to fix that?
Quoted 8 lines
Quoted 5 lines
Of course, creating a temporary file for the caller to (re)open is also racy. By the time the caller gets around to it, the filename could name anything. Return an open file file descriptor is a better idea. It's basically
g_file_open_tmp(). Could we rework the two users ofget_tmp_filename()accept a file descriptor?I looked at the 2 callers, and it looks like we need to do more than these 2 callers to teach them to accept a file descriptor. :(
Looks like it requires surgery to bdrv_create() at least. I'm not demanding you do that now.
[...]
Hi Markus,
Quoted 111 lines · Markus Armbruster
Quoted 108 lines · Bin Meng
Quoted 107 lines · Markus Armbruster
Quoted 91 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.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Useg_autofreeandg_steal_pointerinclude/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@@ -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 +7
-21@@ -860,38 +860,23 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)/* * Create a uniquely-named empty temporary file. -* Return 0 upon success, otherwise a negative errno value. +* Return the actual name used upon success, otherwise NULL. +* The called function is responsible for freeing it. */ -int get_tmp_filename (char *filename,int size) +char *get_tmp_filename (void ) { -#ifdef _WIN32 -char temp_dir[MAX_PATH ]; -/* GetTempFileName requires that its output buffer (4th param) -have length MAX_PATH or greater. */ -assert (size >=MAX_PATH ); -return (GetTempPath (MAX_PATH , temp_dir) - &&GetTempFileName (temp_dir,"qem" ,0 , filename) - ?0 : -GetLastError ()); -#else + g_autofree char *filename = NULL;int fd; -const char *tmpdir; - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) { - tmpdir ="/var/tmp" ; - } -if (snprintf (filename, size,"%s/vl.XXXXXX" , tmpdir) >= size) { -return -EOVERFLOW ; - } - fd =mkstemp (filename); + + fd =g_file_open_tmp ("vl.XXXXXX" , &filename,NULL );if (fd <0 ) { -return -errno; +return NULL ;block.c int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) · line 880
Quoted 10 lines
block.c +2
-3}if (close (fd) !=0 ) {unlink (filename); -return -errno; +return NULL ; } -return 0 ; -#endif +return g_steal_pointer (&filename); }Oh my, what a lovely mess you're messing with!
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
The function creates a temporary *file*, not just a filename. Makes sense, as creating a unique filename is inherently racy. The contract is clear enough ("Create a uniquely-named empty temporary file"), but the function name is actively misleading.
Agreed that the name is misleading.
Care to fix that?
How about create_tmp_file()?
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Of course, creating a temporary file for the caller to (re)open is also racy. By the time the caller gets around to it, the filename could name anything. Return an open file file descriptor is a better idea. It's basically
g_file_open_tmp(). Could we rework the two users ofget_tmp_filename()accept a file descriptor?
Quoted 6 lines
Quoted 2 lines
I looked at the 2 callers, and it looks like we need to do more than these 2 callers to teach them to accept a file descriptor. :(
Looks like it requires surgery to
bdrv_create()at least. I'm not demanding you do that now.
Yes, big surgery to struct CreateCo::filename, and various ops in struct CreateCo::drv that takes the filename as an argument :(
Regards,
Bin
Quoted 119 lines · Bin Meng
Hi Markus,
Quoted 116 lines · Markus Armbruster
Quoted 113 lines · Bin Meng
Quoted 111 lines · Markus Armbruster
Quoted 91 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.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Useg_autofreeandg_steal_pointerinclude/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@@ -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 +7
-21@@ -860,38 +860,23 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)/* * Create a uniquely-named empty temporary file. -* Return 0 upon success, otherwise a negative errno value. +* Return the actual name used upon success, otherwise NULL. +* The called function is responsible for freeing it. */ -int get_tmp_filename (char *filename,int size) +char *get_tmp_filename (void ) { -#ifdef _WIN32 -char temp_dir[MAX_PATH ]; -/* GetTempFileName requires that its output buffer (4th param) -have length MAX_PATH or greater. */ -assert (size >=MAX_PATH ); -return (GetTempPath (MAX_PATH , temp_dir) - &&GetTempFileName (temp_dir,"qem" ,0 , filename) - ?0 : -GetLastError ()); -#else + g_autofree char *filename = NULL;int fd; -const char *tmpdir; - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) { - tmpdir ="/var/tmp" ; - } -if (snprintf (filename, size,"%s/vl.XXXXXX" , tmpdir) >= size) { -return -EOVERFLOW ; - } - fd =mkstemp (filename); + + fd =g_file_open_tmp ("vl.XXXXXX" , &filename,NULL );if (fd <0 ) { -return -errno; +return NULL ;block.c int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) · line 880
Quoted 10 lines
block.c +2
-3}if (close (fd) !=0 ) {unlink (filename); -return -errno; +return NULL ; } -return 0 ; -#endif +return g_steal_pointer (&filename); }Oh my, what a lovely mess you're messing with!
The function creates a temporary *file*, not just a filename. Makes sense, as creating a unique filename is inherently racy. The contract is clear enough ("Create a uniquely-named empty temporary file"), but the function name is actively misleading.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Agreed that the name is misleading.
Care to fix that?
How about
create_tmp_file()?
Works for me!
[...]