[for-2.7] wxx: Truncate files used for character devices

Message ID 1469553069-15350-1-git-send-email-sw@weilnetz.de
State New
Headers show

Commit Message

Stefan Weil July 26, 2016, 5:11 p.m.
On Windows, such files were not truncated like on all other hosts.
Now we also test whether truncation is needed when running on Windows.

Reported-by: Benjamin David Lunt <fys@fysnet.net>
Signed-off-by: Stefan Weil <sw@weilnetz.de>
---
 qemu-char.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Stefan Weil Aug. 2, 2016, 5:10 a.m. | #1
Am 01.08.2016 um 10:31 schrieb Paolo Bonzini:
> On 26/07/2016 19:11, Stefan Weil wrote:
>> On Windows, such files were not truncated like on all other hosts.
>> Now we also test whether truncation is needed when running on Windows.
>>
>> Reported-by: Benjamin David Lunt <fys@fysnet.net>
>> Signed-off-by: Stefan Weil <sw@weilnetz.de>
>> ---
>>  qemu-char.c | 11 ++++++++++-
>>  1 file changed, 10 insertions(+), 1 deletion(-)
>>
>> diff --git a/qemu-char.c b/qemu-char.c
>> index e4b8448..7de63c8 100644
>> --- a/qemu-char.c
>> +++ b/qemu-char.c
>> @@ -4197,14 +4197,23 @@ static CharDriverState *qmp_chardev_open_file(const char *id,
>>      ChardevFile *file = backend->u.file.data;
>>      ChardevCommon *common = qapi_ChardevFile_base(file);
>>      HANDLE out;
>> +    DWORD flags;
>>  
>>      if (file->has_in) {
>>          error_setg(errp, "input file not supported");
>>          return NULL;
>>      }
>>  
>> +    if (file->has_append && file->append) {
>> +        /* Append to file if it already exists. */
>> +        flags = OPEN_ALWAYS;
>> +    } else {
>> +        /* Truncate file if it already exists. */
>> +        flags = CREATE_ALWAYS;
>> +    }
>> +
>>      out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
> 
> GENERIC_WRITE does not include FILE_APPEND_DATA.  You should also change
> the access rights to FILE_GENERIC_WRITE & ~FILE_WRITE_DATA in the append
> case; see commit 52074d0f662fc51293d4cde8077631f754784405 for a similar
> case in qemu-ga.
> 
> Thanks,
> 
> Paolo

Thanks for the hint.

That's a second problem of the old code. I'll send a new patch which
addresses both problems.

Stefan

Patch

diff --git a/qemu-char.c b/qemu-char.c
index e4b8448..7de63c8 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -4197,14 +4197,23 @@  static CharDriverState *qmp_chardev_open_file(const char *id,
     ChardevFile *file = backend->u.file.data;
     ChardevCommon *common = qapi_ChardevFile_base(file);
     HANDLE out;
+    DWORD flags;
 
     if (file->has_in) {
         error_setg(errp, "input file not supported");
         return NULL;
     }
 
+    if (file->has_append && file->append) {
+        /* Append to file if it already exists. */
+        flags = OPEN_ALWAYS;
+    } else {
+        /* Truncate file if it already exists. */
+        flags = CREATE_ALWAYS;
+    }
+
     out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-                     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+                     flags, FILE_ATTRIBUTE_NORMAL, NULL);
     if (out == INVALID_HANDLE_VALUE) {
         error_setg(errp, "open %s failed", file->out);
         return NULL;