Skip to content

Commit ead83a1

Browse files
philmdmdroth
authored andcommitted
qga: Extract qmp_guest_file_read() to common commands.c
Extract the common code shared by both POSIX/Win32 implementations. Signed-off-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Michael Roth <[email protected]>
1 parent 5d3586b commit ead83a1

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

qga/commands-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ typedef struct GuestFileHandle GuestFileHandle;
1515

1616
GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp);
1717

18+
GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
19+
int64_t count, Error **errp);
20+
1821
#endif

qga/commands-posix.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -461,29 +461,14 @@ void qmp_guest_file_close(int64_t handle, Error **errp)
461461
g_free(gfh);
462462
}
463463

464-
struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
465-
int64_t count, Error **errp)
464+
GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
465+
int64_t count, Error **errp)
466466
{
467-
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
468467
GuestFileRead *read_data = NULL;
469468
guchar *buf;
470-
FILE *fh;
469+
FILE *fh = gfh->fh;
471470
size_t read_count;
472471

473-
if (!gfh) {
474-
return NULL;
475-
}
476-
477-
if (!has_count) {
478-
count = QGA_READ_COUNT_DEFAULT;
479-
} else if (count < 0 || count >= UINT32_MAX) {
480-
error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
481-
count);
482-
return NULL;
483-
}
484-
485-
fh = gfh->fh;
486-
487472
/* explicitly flush when switching from writing to reading */
488473
if (gfh->state == RW_STATE_WRITING) {
489474
int ret = fflush(fh);
@@ -498,7 +483,6 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
498483
read_count = fread(buf, 1, count, fh);
499484
if (ferror(fh)) {
500485
error_setg_errno(errp, errno, "failed to read file");
501-
slog("guest-file-read failed, handle: %" PRId64, handle);
502486
} else {
503487
buf[read_count] = 0;
504488
read_data = g_new0(GuestFileRead, 1);

qga/commands-win32.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,33 +322,19 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
322322
}
323323
}
324324

325-
GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
326-
int64_t count, Error **errp)
325+
GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
326+
int64_t count, Error **errp)
327327
{
328328
GuestFileRead *read_data = NULL;
329329
guchar *buf;
330-
HANDLE fh;
330+
HANDLE fh = gfh->fh;
331331
bool is_ok;
332332
DWORD read_count;
333-
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
334-
335-
if (!gfh) {
336-
return NULL;
337-
}
338-
if (!has_count) {
339-
count = QGA_READ_COUNT_DEFAULT;
340-
} else if (count < 0 || count >= UINT32_MAX) {
341-
error_setg(errp, "value '%" PRId64
342-
"' is invalid for argument count", count);
343-
return NULL;
344-
}
345333

346-
fh = gfh->fh;
347334
buf = g_malloc0(count + 1);
348335
is_ok = ReadFile(fh, buf, count, &read_count, NULL);
349336
if (!is_ok) {
350337
error_setg_win32(errp, GetLastError(), "failed to read file");
351-
slog("guest-file-read failed, handle %" PRId64, handle);
352338
} else {
353339
buf[read_count] = 0;
354340
read_data = g_new0(GuestFileRead, 1);

qga/commands.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qemu/base64.h"
1919
#include "qemu/cutils.h"
2020
#include "qemu/atomic.h"
21+
#include "commands-common.h"
2122

2223
/* Maximum captured guest-exec out_data/err_data - 16MB */
2324
#define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)
@@ -547,3 +548,28 @@ GuestTimezone *qmp_guest_get_timezone(Error **errp)
547548
g_free(info);
548549
return NULL;
549550
}
551+
552+
GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
553+
int64_t count, Error **errp)
554+
{
555+
GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
556+
GuestFileRead *read_data;
557+
558+
if (!gfh) {
559+
return NULL;
560+
}
561+
if (!has_count) {
562+
count = QGA_READ_COUNT_DEFAULT;
563+
} else if (count < 0 || count >= UINT32_MAX) {
564+
error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
565+
count);
566+
return NULL;
567+
}
568+
569+
read_data = guest_file_read_unsafe(gfh, count, errp);
570+
if (!read_data) {
571+
slog("guest-file-write failed, handle: %" PRId64, handle);
572+
}
573+
574+
return read_data;
575+
}

0 commit comments

Comments
 (0)