While reading Chris's code for fd migration I noticed the duplication
between QEMUFilePopen and QEMUFileStdio. This fixes it, and makes
qemu_fopen more similar qemu_fdopen.
Signed-off-by: Paolo Bonzini <pbonzini(a)redhat.com>
---
hw/hw.h | 2 +-
migration-exec.c | 4 +-
savevm.c | 99 +++++++++++++++++++++++++++--------------------------
3 files changed, 53 insertions(+), 52 deletions(-)
diff --git a/hw/hw.h b/hw/hw.h
index 1e5783d..322f077 100644
--- a/hw/hw.h
+++ b/hw/hw.h
@@ -52,7 +52,7 @@ QEMUFile *qemu_fopen(const char *filename, const char *mode);
QEMUFile *qemu_fopen_socket(int fd);
QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
-int qemu_popen_fd(QEMUFile *f);
+int qemu_stdio_fd(QEMUFile *f);
void qemu_fflush(QEMUFile *f);
int qemu_fclose(QEMUFile *f);
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
diff --git a/migration-exec.c b/migration-exec.c
index e3616be..235f3f7 100644
--- a/migration-exec.c
+++ b/migration-exec.c
@@ -117,7 +117,7 @@ static void exec_accept_incoming_migration(void *opaque)
qemu_announce_self();
dprintf("successfully loaded vm state\n");
/* we've successfully migrated, close the fd */
- qemu_set_fd_handler2(qemu_popen_fd(f), NULL, NULL, NULL, NULL);
+ qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
if (autostart)
vm_start();
@@ -136,7 +136,7 @@ int exec_start_incoming_migration(const char *command)
return -errno;
}
- qemu_set_fd_handler2(qemu_popen_fd(f), NULL,
+ qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
exec_accept_incoming_migration, NULL,
(void *)(unsigned long)f);
diff --git a/savevm.c b/savevm.c
index 570377f..975e7ab 100644
--- a/savevm.c
+++ b/savevm.c
@@ -172,11 +172,11 @@ struct QEMUFile {
int has_error;
};
-typedef struct QEMUFilePopen
+typedef struct QEMUFileStdio
{
- FILE *popen_file;
+ FILE *stdio_file;
QEMUFile *file;
-} QEMUFilePopen;
+} QEMUFileStdio;
typedef struct QEMUFileSocket
{
@@ -206,16 +206,16 @@ static int socket_close(void *opaque)
return 0;
}
-static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
+static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
{
- QEMUFilePopen *s = opaque;
- return fwrite(buf, 1, size, s->popen_file);
+ QEMUFileStdio *s = opaque;
+ return fwrite(buf, 1, size, s->stdio_file);
}
-static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
+static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
{
- QEMUFilePopen *s = opaque;
- FILE *fp = s->popen_file;
+ QEMUFileStdio *s = opaque;
+ FILE *fp = s->stdio_file;
int bytes;
do {
@@ -225,31 +225,39 @@ static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
int size)
return bytes;
}
-static int popen_close(void *opaque)
+static int stdio_pclose(void *opaque)
+{
+ QEMUFileStdio *s = opaque;
+ pclose(s->stdio_file);
+ qemu_free(s);
+ return 0;
+}
+
+static int stdio_fclose(void *opaque)
{
- QEMUFilePopen *s = opaque;
- pclose(s->popen_file);
+ QEMUFileStdio *s = opaque;
+ fclose(s->stdio_file);
qemu_free(s);
return 0;
}
-QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
+QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
{
- QEMUFilePopen *s;
+ QEMUFileStdio *s;
- if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0]
!= 'w') || mode[1] != 0) {
+ if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0]
!= 'w') || mode[1] != 0) {
fprintf(stderr, "qemu_popen: Argument validity check failed\n");
return NULL;
}
- s = qemu_mallocz(sizeof(QEMUFilePopen));
+ s = qemu_mallocz(sizeof(QEMUFileStdio));
- s->popen_file = popen_file;
+ s->stdio_file = stdio_file;
if(mode[0] == 'r') {
- s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL, NULL);
+ s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL,
NULL);
} else {
- s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL, NULL);
+ s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL,
NULL);
}
return s->file;
}
@@ -266,13 +274,13 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
return qemu_popen(popen_file, mode);
}
-int qemu_popen_fd(QEMUFile *f)
+int qemu_stdio_fd(QEMUFile *f)
{
- QEMUFilePopen *p;
+ QEMUFileStdio *p;
int fd;
- p = (QEMUFilePopen *)f->opaque;
- fd = fileno(p->popen_file);
+ p = (QEMUFileStdio *)f->opaque;
+ fd = fileno(p->stdio_file);
return fd;
}
@@ -286,53 +294,46 @@ QEMUFile *qemu_fopen_socket(int fd)
return s->file;
}
-typedef struct QEMUFileStdio
-{
- FILE *outfile;
-} QEMUFileStdio;
-
static int file_put_buffer(void *opaque, const uint8_t *buf,
int64_t pos, int size)
{
QEMUFileStdio *s = opaque;
- fseek(s->outfile, pos, SEEK_SET);
- fwrite(buf, 1, size, s->outfile);
+ fseek(s->stdio_file, pos, SEEK_SET);
+ fwrite(buf, 1, size, s->stdio_file);
return size;
}
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
{
QEMUFileStdio *s = opaque;
- fseek(s->outfile, pos, SEEK_SET);
- return fread(buf, 1, size, s->outfile);
-}
-
-static int file_close(void *opaque)
-{
- QEMUFileStdio *s = opaque;
- fclose(s->outfile);
- qemu_free(s);
- return 0;
+ fseek(s->stdio_file, pos, SEEK_SET);
+ return fread(buf, 1, size, s->stdio_file);
}
QEMUFile *qemu_fopen(const char *filename, const char *mode)
{
QEMUFileStdio *s;
+ if (mode == NULL ||
+ (mode[0] != 'r' && mode[0] != 'w') ||
+ mode[1] != 'b' || mode[2] != 0) {
+ fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
+ return NULL;
+ }
+
s = qemu_mallocz(sizeof(QEMUFileStdio));
- s->outfile = fopen(filename, mode);
- if (!s->outfile)
+ s->stdio_file = fopen(filename, mode);
+ if (!s->stdio_file)
goto fail;
- if (!strcmp(mode, "wb"))
- return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL, NULL);
- else if (!strcmp(mode, "rb"))
- return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL, NULL);
-
+ if(mode[0] == 'w') {
+ s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
+ } else {
+ s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
+ }
+ return s->file;
fail:
- if (s->outfile)
- fclose(s->outfile);
qemu_free(s);
return NULL;
}
--
1.6.2.5