9pfs: introduce v9fs_path_sprintf() helper

This helper is similar to v9fs_string_sprintf(), but it includes the
terminating NUL character in the size field.

This is to avoid doing v9fs_string_sprintf((V9fsString *) &path) and
then bumping the size.

Affected users are changed to use this new helper.

Signed-off-by: Greg Kurz <groug@kaod.org>
Reviewed-by: Cédric Le Goater <clg@kaod.org>
master
Greg Kurz 2016-09-16 08:56:15 +02:00
parent abdf008640
commit e3e83f2e21
4 changed files with 21 additions and 13 deletions

View File

@ -1060,13 +1060,10 @@ static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
const char *name, V9fsPath *target) const char *name, V9fsPath *target)
{ {
if (dir_path) { if (dir_path) {
v9fs_string_sprintf((V9fsString *)target, "%s/%s", v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
dir_path->data, name);
} else { } else {
v9fs_string_sprintf((V9fsString *)target, "%s", name); v9fs_path_sprintf(target, "%s", name);
} }
/* Bump the size for including terminating NULL */
target->size++;
return 0; return 0;
} }

View File

@ -1023,13 +1023,10 @@ static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
const char *name, V9fsPath *target) const char *name, V9fsPath *target)
{ {
if (dir_path) { if (dir_path) {
v9fs_string_sprintf((V9fsString *)target, "%s/%s", v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
dir_path->data, name);
} else { } else {
v9fs_string_sprintf((V9fsString *)target, "%s", name); v9fs_path_sprintf(target, "%s", name);
} }
/* Bump the size for including terminating NULL */
target->size++;
return 0; return 0;
} }

View File

@ -12,6 +12,7 @@
*/ */
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include <glib/gprintf.h>
#include "hw/virtio/virtio.h" #include "hw/virtio/virtio.h"
#include "qapi/error.h" #include "qapi/error.h"
#include "qemu/error-report.h" #include "qemu/error-report.h"
@ -179,6 +180,20 @@ void v9fs_path_free(V9fsPath *path)
path->size = 0; path->size = 0;
} }
void GCC_FMT_ATTR(2, 3)
v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
{
va_list ap;
v9fs_path_free(path);
va_start(ap, fmt);
/* Bump the size for including terminating NULL */
path->size = g_vasprintf(&path->data, fmt, ap) + 1;
va_end(ap);
}
void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs) void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
{ {
v9fs_path_free(lhs); v9fs_path_free(lhs);
@ -917,10 +932,8 @@ static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
V9fsPath str; V9fsPath str;
v9fs_path_init(&str); v9fs_path_init(&str);
v9fs_path_copy(&str, dst); v9fs_path_copy(&str, dst);
v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len); v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
v9fs_path_free(&str); v9fs_path_free(&str);
/* +1 to include terminating NULL */
dst->size++;
} }
static inline bool is_ro_export(FsContext *ctx) static inline bool is_ro_export(FsContext *ctx)

View File

@ -327,6 +327,7 @@ static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
extern void v9fs_reclaim_fd(V9fsPDU *pdu); extern void v9fs_reclaim_fd(V9fsPDU *pdu);
extern void v9fs_path_init(V9fsPath *path); extern void v9fs_path_init(V9fsPath *path);
extern void v9fs_path_free(V9fsPath *path); extern void v9fs_path_free(V9fsPath *path);
extern void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs); extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
const char *name, V9fsPath *path); const char *name, V9fsPath *path);