[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH 2/8] qga: implement file commands for Windows gu
From: |
Michael Roth |
Subject: |
Re: [Qemu-devel] [PATCH 2/8] qga: implement file commands for Windows guest |
Date: |
Tue, 03 Feb 2015 15:15:20 -0600 |
User-agent: |
alot/0.3.4 |
Quoting Denis V. Lunev (2014-12-31 07:06:48)
> From: Olga Krishtal <address@hidden>
>
> The following commands are implemented:
> - guest_file_open
> - guest_file_close
> - guest_file_write
> - guest_file_read
> - guest_file_seek
> - guest_file_flush
>
> Motivation is quite simple: Windows guests should be supported with the
> same set of features as Linux one. Also this patch is a prerequisite for
> Windows guest-exec command support.
>
> Signed-off-by: Olga Krishtal <address@hidden>
> Signed-off-by: Denis V. Lunev <address@hidden>
> CC: Michael Roth <address@hidden>
> ---
> qga/commands-win32.c | 271
> +++++++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 250 insertions(+), 21 deletions(-)
>
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index 3bcbeae..5db96ef 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -14,10 +14,13 @@
> #include <glib.h>
> #include <wtypes.h>
> #include <powrprof.h>
> +#include <stdio.h>
> +#include <string.h>
> #include "qga/guest-agent-core.h"
> #include "qga/vss-win32.h"
> #include "qga-qmp-commands.h"
> #include "qapi/qmp/qerror.h"
> +#include "qemu/queue.h"
>
> #ifndef SHTDN_REASON_FLAG_PLANNED
> #define SHTDN_REASON_FLAG_PLANNED 0x80000000
> @@ -29,6 +32,146 @@
> (365 * (1970 - 1601) + \
> (1970 - 1601) / 4 - 3))
>
> +#define INVALID_SET_FILE_POINTER ((DWORD)-1)
> +
> +typedef struct GuestFileHandle {
> + int64_t id;
> + HANDLE fh;
> + QTAILQ_ENTRY(GuestFileHandle) next;
> +} GuestFileHandle;
> +
> +static struct {
> + QTAILQ_HEAD(, GuestFileHandle) filehandles;
> +} guest_file_state;
> +
> +
> +typedef struct OpenFlags {
> + const char *forms;
> + DWORD desired_access;
> + DWORD creation_disposition;
> +} OpenFlags;
> +static OpenFlags guest_file_open_modes[] = {
> + {"r", GENERIC_READ, OPEN_EXISTING},
> + {"rb", GENERIC_READ, OPEN_EXISTING},
> + {"w", GENERIC_WRITE, CREATE_ALWAYS},
> + {"wb", GENERIC_WRITE, CREATE_ALWAYS},
> + {"a", GENERIC_WRITE, OPEN_ALWAYS },
> + {"r+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING},
> + {"rb+", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING},
> + {"r+b", GENERIC_WRITE|GENERIC_READ, OPEN_EXISTING},
> + {"w+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS},
> + {"wb+", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS},
> + {"w+b", GENERIC_WRITE|GENERIC_READ, CREATE_ALWAYS},
> + {"a+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS },
> + {"ab+", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS },
> + {"a+b", GENERIC_WRITE|GENERIC_READ, OPEN_ALWAYS }
> +};
> +
> +static OpenFlags *find_open_flag(const char *mode_str)
> +{
> + int mode;
> + Error **errp = NULL;
> +
> + for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
> + OpenFlags *flags = guest_file_open_modes + mode;
> +
> + if (strcmp(flags->forms, mode_str) == 0) {
> + return flags;
> + }
> + }
> +
> + error_setg(errp, "invalid file open mode '%s'", mode_str);
> + return NULL;
> +}
> +
> +static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
> +{
> + GuestFileHandle *gfh;
> + int64_t handle;
> +
> + handle = ga_get_fd_handle(ga_state, errp);
> + if (handle < 0) {
> + return -1;
> + }
> + gfh = g_malloc0(sizeof(GuestFileHandle));
> + gfh->id = handle;
> + gfh->fh = fh;
> + QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
> +
> + return handle;
> +}
> +
> +static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
> +{
> + GuestFileHandle *gfh;
> + QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next) {
> + if (gfh->id == id) {
> + return gfh;
> + }
> + }
> + error_setg(errp, "handle '%" PRId64 "' has not been found", id);
> + return NULL;
> +}
> +
> +int64_t qmp_guest_file_open(const char *path, bool has_mode,
> + const char *mode, Error **errp)
> +{
> + int64_t fd;
> + HANDLE fh;
> + HANDLE templ_file = NULL;
> + DWORD share_mode = FILE_SHARE_READ;
> + DWORD flags_and_attr = FILE_ATTRIBUTE_NORMAL;
> + LPSECURITY_ATTRIBUTES sa_attr = NULL;
> + OpenFlags *guest_flags;
> +
> + if (!has_mode) {
> + mode = "r";
> + }
> + slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
> + guest_flags = find_open_flag(mode);
> + if (guest_flags == NULL) {
> + error_setg(errp, "invalid file open mode");
> + return -1;
> + }
> +
> + fh = CreateFile(path, guest_flags->desired_access, share_mode, sa_attr,
> + guest_flags->creation_disposition, flags_and_attr,
> + templ_file);
> + if (fh == INVALID_HANDLE_VALUE) {
> + error_setg_errno(errp, GetLastError(), "failed to open file '%s'",
> + path);
For working with GetLastError() I think error_setg_win32() is what you
want. Same for all the other occurrences.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: [Qemu-devel] [PATCH 2/8] qga: implement file commands for Windows guest,
Michael Roth <=