mirror of https://gitee.com/openkylin/qemu.git
unify popen/fopen qemu wrappers
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_popen. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
parent
a25a0ef51e
commit
7f79dd281c
2
hw/hw.h
2
hw/hw.h
|
@ -52,7 +52,7 @@ QEMUFile *qemu_fopen(const char *filename, const char *mode);
|
||||||
QEMUFile *qemu_fopen_socket(int fd);
|
QEMUFile *qemu_fopen_socket(int fd);
|
||||||
QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
|
QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
|
||||||
QEMUFile *qemu_popen_cmd(const char *command, 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);
|
void qemu_fflush(QEMUFile *f);
|
||||||
int qemu_fclose(QEMUFile *f);
|
int qemu_fclose(QEMUFile *f);
|
||||||
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
|
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
|
||||||
|
|
|
@ -114,7 +114,7 @@ static void exec_accept_incoming_migration(void *opaque)
|
||||||
qemu_announce_self();
|
qemu_announce_self();
|
||||||
dprintf("successfully loaded vm state\n");
|
dprintf("successfully loaded vm state\n");
|
||||||
/* we've successfully migrated, close the fd */
|
/* 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)
|
if (autostart)
|
||||||
vm_start();
|
vm_start();
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ int exec_start_incoming_migration(const char *command)
|
||||||
return -errno;
|
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,
|
exec_accept_incoming_migration, NULL,
|
||||||
(void *)(unsigned long)f);
|
(void *)(unsigned long)f);
|
||||||
|
|
||||||
|
|
99
savevm.c
99
savevm.c
|
@ -172,11 +172,11 @@ struct QEMUFile {
|
||||||
int has_error;
|
int has_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct QEMUFilePopen
|
typedef struct QEMUFileStdio
|
||||||
{
|
{
|
||||||
FILE *popen_file;
|
FILE *stdio_file;
|
||||||
QEMUFile *file;
|
QEMUFile *file;
|
||||||
} QEMUFilePopen;
|
} QEMUFileStdio;
|
||||||
|
|
||||||
typedef struct QEMUFileSocket
|
typedef struct QEMUFileSocket
|
||||||
{
|
{
|
||||||
|
@ -206,16 +206,16 @@ static int socket_close(void *opaque)
|
||||||
return 0;
|
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;
|
QEMUFileStdio *s = opaque;
|
||||||
return fwrite(buf, 1, size, s->popen_file);
|
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;
|
QEMUFileStdio *s = opaque;
|
||||||
FILE *fp = s->popen_file;
|
FILE *fp = s->stdio_file;
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
@ -225,31 +225,39 @@ static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int popen_close(void *opaque)
|
static int stdio_pclose(void *opaque)
|
||||||
{
|
{
|
||||||
QEMUFilePopen *s = opaque;
|
QEMUFileStdio *s = opaque;
|
||||||
pclose(s->popen_file);
|
pclose(s->stdio_file);
|
||||||
qemu_free(s);
|
qemu_free(s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
|
static int stdio_fclose(void *opaque)
|
||||||
{
|
{
|
||||||
QEMUFilePopen *s;
|
QEMUFileStdio *s = opaque;
|
||||||
|
fclose(s->stdio_file);
|
||||||
|
qemu_free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
|
QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
|
||||||
|
{
|
||||||
|
QEMUFileStdio *s;
|
||||||
|
|
||||||
|
if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
|
||||||
fprintf(stderr, "qemu_popen: Argument validity check failed\n");
|
fprintf(stderr, "qemu_popen: Argument validity check failed\n");
|
||||||
return NULL;
|
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') {
|
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 {
|
} 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;
|
return s->file;
|
||||||
}
|
}
|
||||||
|
@ -266,13 +274,13 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
|
||||||
return qemu_popen(popen_file, 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;
|
int fd;
|
||||||
|
|
||||||
p = (QEMUFilePopen *)f->opaque;
|
p = (QEMUFileStdio *)f->opaque;
|
||||||
fd = fileno(p->popen_file);
|
fd = fileno(p->stdio_file);
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
@ -286,53 +294,46 @@ QEMUFile *qemu_fopen_socket(int fd)
|
||||||
return s->file;
|
return s->file;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct QEMUFileStdio
|
|
||||||
{
|
|
||||||
FILE *outfile;
|
|
||||||
} QEMUFileStdio;
|
|
||||||
|
|
||||||
static int file_put_buffer(void *opaque, const uint8_t *buf,
|
static int file_put_buffer(void *opaque, const uint8_t *buf,
|
||||||
int64_t pos, int size)
|
int64_t pos, int size)
|
||||||
{
|
{
|
||||||
QEMUFileStdio *s = opaque;
|
QEMUFileStdio *s = opaque;
|
||||||
fseek(s->outfile, pos, SEEK_SET);
|
fseek(s->stdio_file, pos, SEEK_SET);
|
||||||
fwrite(buf, 1, size, s->outfile);
|
fwrite(buf, 1, size, s->stdio_file);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
|
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
|
||||||
{
|
{
|
||||||
QEMUFileStdio *s = opaque;
|
QEMUFileStdio *s = opaque;
|
||||||
fseek(s->outfile, pos, SEEK_SET);
|
fseek(s->stdio_file, pos, SEEK_SET);
|
||||||
return fread(buf, 1, size, s->outfile);
|
return fread(buf, 1, size, s->stdio_file);
|
||||||
}
|
|
||||||
|
|
||||||
static int file_close(void *opaque)
|
|
||||||
{
|
|
||||||
QEMUFileStdio *s = opaque;
|
|
||||||
fclose(s->outfile);
|
|
||||||
qemu_free(s);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QEMUFile *qemu_fopen(const char *filename, const char *mode)
|
QEMUFile *qemu_fopen(const char *filename, const char *mode)
|
||||||
{
|
{
|
||||||
QEMUFileStdio *s;
|
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 = qemu_mallocz(sizeof(QEMUFileStdio));
|
||||||
|
|
||||||
s->outfile = fopen(filename, mode);
|
s->stdio_file = fopen(filename, mode);
|
||||||
if (!s->outfile)
|
if (!s->stdio_file)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (!strcmp(mode, "wb"))
|
if(mode[0] == 'w') {
|
||||||
return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL, NULL);
|
s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
|
||||||
else if (!strcmp(mode, "rb"))
|
} else {
|
||||||
return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL, NULL);
|
s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
|
||||||
|
}
|
||||||
|
return s->file;
|
||||||
fail:
|
fail:
|
||||||
if (s->outfile)
|
|
||||||
fclose(s->outfile);
|
|
||||||
qemu_free(s);
|
qemu_free(s);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue