Skip to contentSkip to conversation

wxx: Fix handling of files used for character devices

PATCH Applied Stefan Weil 3 participants 3 messages Aug 2, 2016 – Aug 5, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 wxx: Fix handling of files used for character devices

On Windows, such files were not truncated like on all other hosts. Now we also test whether truncation is needed when running on Windows.

The append case was also incorrect because it needs a different value for the desired access mode.

---

This patch supersedes my previous patch for qemu-char which only fixed the truncate case. Paolo gave the hint to the wrong access mode (see also commit 52074d0f662fc51293d4cde8077631f754784405).

Regards
Stefan

 qemu-char.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
qemu-char.c +14 -2
index e4b8448..e7d3399 100644
@@ -4197,14 +4197,26 @@ static CharDriverState *qmp_chardev_open_file(const char *id,
     ChardevFile *file = backend->u.file.data;
     ChardevCommon *common = qapi_ChardevFile_base(file);
     HANDLE out;
+    DWORD accessmode;
+    DWORD flags;
 
     if (file->has_in) {
         error_setg(errp, "input file not supported");
         return NULL;
     }
 
-    out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-                     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (file->has_append && file->append) {
+        /* Append to file if it already exists. */
+        accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
+        flags = OPEN_ALWAYS;
+    } else {
+        /* Truncate file if it already exists. */
+        accessmode = GENERIC_WRITE;
+        flags = CREATE_ALWAYS;
+    }
+
+    out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
+                     FILE_ATTRIBUTE_NORMAL, NULL);
     if (out == INVALID_HANDLE_VALUE) {
         error_setg(errp, "open %s failed", file->out);
         return NULL;
Signature · 1 line
2.1.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 44 lines · Stefan Weil

On Windows, such files were not truncated like on all other hosts. Now we also test whether truncation is needed when running on Windows.

The append case was also incorrect because it needs a different value for the desired access mode.

---

This patch supersedes my previous patch for qemu-char which only fixed the truncate case. Paolo gave the hint to the wrong access mode (see also commit 52074d0f662fc51293d4cde8077631f754784405).

Regards
Stefan

 qemu-char.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
qemu-char.c +7 -2
index e4b8448..e7d3399 100644
@@ -4197,14 +4197,26 @@ static CharDriverState *qmp_chardev_open_file(const char *id,
     ChardevFile *file = backend->u.file.data;
     ChardevCommon *common = qapi_ChardevFile_base(file);
     HANDLE out;
+    DWORD accessmode;
+    DWORD flags;
 
     if (file->has_in) {
         error_setg(errp, "input file not supported");
         return NULL;
     }
 
-    out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-                     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (file->has_append && file->append) {
+        /* Append to file if it already exists. */
+        accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
+        flags = OPEN_ALWAYS;
+    } else {

qemu-char.c static CharDriverState *qmp_chardev_open_file(const char *id, · line 4222

Quoted 10 lines
qemu-char.c +7 -0
+        /* Truncate file if it already exists. */
+        accessmode = GENERIC_WRITE;
+        flags = CREATE_ALWAYS;
+    }
+
+    out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
+                     FILE_ATTRIBUTE_NORMAL, NULL);
     if (out == INVALID_HANDLE_VALUE) {
         error_setg(errp, "open %s failed", file->out);
         return NULL;

Matches the access mode and flags for qemu-ga.

Thanks!

#2 lore raw
Quoted 60 lines · Paolo Bonzini
Quoted 44 lines · Stefan Weil

On Windows, such files were not truncated like on all other hosts. Now we also test whether truncation is needed when running on Windows.

The append case was also incorrect because it needs a different value for the desired access mode.

---

This patch supersedes my previous patch for qemu-char which only fixed the truncate case. Paolo gave the hint to the wrong access mode (see also commit 52074d0f662fc51293d4cde8077631f754784405).

Regards
Stefan

 qemu-char.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
qemu-char.c +7 -2
index e4b8448..e7d3399 100644
@@ -4197,14 +4197,26 @@ static CharDriverState *qmp_chardev_open_file(const char *id,
     ChardevFile *file = backend->u.file.data;
     ChardevCommon *common = qapi_ChardevFile_base(file);
     HANDLE out;
+    DWORD accessmode;
+    DWORD flags;
 
     if (file->has_in) {
         error_setg(errp, "input file not supported");
         return NULL;
     }
 
-    out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-                     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (file->has_append && file->append) {
+        /* Append to file if it already exists. */
+        accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
+        flags = OPEN_ALWAYS;
+    } else {

qemu-char.c static CharDriverState *qmp_chardev_open_file(const char *id, · line 4222

Quoted 10 lines
qemu-char.c +7 -0
+        /* Truncate file if it already exists. */
+        accessmode = GENERIC_WRITE;
+        flags = CREATE_ALWAYS;
+    }
+
+    out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
+                     FILE_ATTRIBUTE_NORMAL, NULL);
     if (out == INVALID_HANDLE_VALUE) {
         error_setg(errp, "open %s failed", file->out);
         return NULL;

Matches the access mode and flags for qemu-ga.

Quoted 1 line

Applied to master, thanks.

-- PMM