[hatari-devel] GEMDOS HD issue when running under Windows |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/hatari-devel Archives
]
- To: hatari-devel@xxxxxxxxxxxxxxxxxxx
- Subject: [hatari-devel] GEMDOS HD issue when running under Windows
- From: Christian Zietz <czietz@xxxxxxx>
- Date: Thu, 28 Mar 2024 16:41:28 +0100
- Autocrypt: addr=czietz@xxxxxxx; keydata= xsFNBGMHkrYBEACc4fljFVcoEo+DzmhTRd8pOfnj39wkNL+VEIzUpz5OfxFNx/KYWhtHxLN9 VWD3rojS5ww3bNgWiYdqDLisuaO6jLXZ7JNBQU3ruJg+g4iCuwfwFf/tVAHvMCr5U/ibiE94 VZuHs6yYJnXHuKrZEBzWQTEPHltqFLVq+cr4dzMV14SIWP8/OnUCaQeeCE1jdh8itXw75Cv9 Bc4wqhT1eU75WmcUwJ1hNrwZm6M2acFoABmZL0CWm0L8+7PXDgZXlwyNoWuPoupjuAvjsdsY 5x+uWtfyufrC/auTcc7LKiAxRQcZ/ABtLhnAa13Su4BsrVwJIxFIGDrZe/CpX48CvYdWljQF JqElP5ShsaM01odrLhmS8OreMEODo6Vhr3zqs3wUA/bl8gEkxDbSz0LewqC07sajTiYIVABW bVWkyn2T8JANSbtVV9YgUnbK+CsMckruarab1iSrTBB+aTvK5TN7LP4iKHaXfZAbq5wtQfXe yrvyPjkbmzvbYb+lnVe24fqLQS1RVB6p/LGAkKFBT1SjEQWVtzVIiAAlbjhRxIsdOqJK1kl/ 6GyQyGfUlPByUETzzFKe6qcCtQlUZPwd7vquryw+3PSVkhL9PiEtUSMiOIVpRzfomxwKXNGT avDoYjTZL1ROuzQYfL+ekpGu4Ti53GGxagxJT1tBhon1qUkMwwARAQABzSBDaHJpc3RpYW4g WmlldHogPGN6aWV0ekBnbXgubmV0PsLBkQQTAQgAOxYhBElYYBdDcemT9uBa0ocIs0yCexWe BQJjB5K2AhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4HAheAAAoJEIcIs0yCexWer/EP/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Ay8=
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1711640489; x=1712245289; i=czietz@xxxxxxx; bh=Lew1ALV5wL30LzM0JLqTgb7DskXyhWNK7NLfENMgdnM=; h=X-UI-Sender-Class:Date:To:From:Subject; b=MMkSvAGaokeKWB7Nvs885O8FTNC+93W/489wiPogpzanoqNaqXtECjPyLKEmYVWQ blB+BoF4t5x7NzewhLCAZKJNADOr84WLVsw6333kODyMNXdpT1M/ltSd9xkov3n6S foJcxnu4ftXz4DYw2VS+k49JHRfMPiVs9PNkTPsxljVJh+R/bkMbrx4ML3YxngzNH x26jZbULmbdXHUxWROZTgJvzSS7DaxP5hb+iBRUrM1zrmhfTLSYuCl3sDsPmDa+aF 8NZW2WGysDXzSH1fzCP+f+pPkDU+tacl/wTx4X97FhOdWlwVqW/LeG9cwSSdCFhta aAWGziDn4YlNvgV06g==
- Ui-outboundreport: notjunk:1;M01:P0:1JHnqLs0JA8=;Ru+Vu3FP0VDXUtI1IJet86wIpGd OEQub/bWzLgK+XJC8AInU6E21659LQ6pK8gEP8rF/9XrLfTszQ1V+wCqcgB/JmQizE7rHVMN3 4UQfG03xYvQ5KBSuqX0t70C7HneTkWGw7FQQlEd1dZ62nx8R6l4vZSfLVOIr8GYFfkde5i2I4 NkO1hU2GfIKpp3p7uYSZeL4lHlSvbizi8x6vT6z+MVoS3X2u054mtNj/YU1p3Y6JIVhPqzs3E pD1vwEL3Q5DpiIjs5BIU8T0LvDLU3jqU0UhbhGQvro+VBjAzT71TJVufIfvpEtRFqqV78Vdsa dgqH7skTf8DPQE8UlTjWaeMVlNVHCjzqr5BQ7R7dO3e03+EjlkaJbE5SNHPeLgG0PHB9PyMIA pSwSEUacB7w5GfSaa7twrQWSXKb+szv5cZgxg/jIJHqV+2MOjCds9LKQAk0ArLKHEbZxOWih9 zO/xi6MDkwDF+vShPgjkCUQBwQzvUHdRbKn9Jz2eMN5Hsb8Eq9VysLcWnk9dUeEUTjqdmaqaL yLPLCBKqmy0PDBTfTgDmZDPu2pPSH6h6EMj2VH+x6OgLw4HB0NpPkXdHJk51cBVHxZi455SZT 1fhiSrN1b4ZbgxoRBTZRmaHXkJOjCtwHL2JunKqEtdW5xJOF2isFTH5PEkRPqaZvwzVZYvWnL kpQ01zMh5/5werw5B3YSMgn2WnKb8Fw5JD9f9braRd1vN+qsKwxfO29/VJbYbr3WS+iTwJ+iq 0oqSMHFBOeviIoekhbUrFqKDFAXL2a11Mn77HEi6i0Vv9GOBP1N69CMWo64wMi1OgHnhAlFa8 xXi3u9HpuasxuoRr01xz8bhDRWvjk00Dja/x4poFMW3zQ=
Some days ago, I investigated an issue I observed with SoftPC, a PC
emulator, running on a GEMDOS-emulated hard disk. I only managed to
write it up today. Upon further inspection, I found that the issue was
with Hatari running under Windows, but not a regression compared to
Hatari 2.4.1. To create an example, I used the following code snippet:
int fid;
long res;
fid = (int)Fopen("FBUG.DAT", 2);
Fread(fid, 512, buf);
strcpy(buf, "MARKER");
res = Fwrite(fid, 512, buf);
Fclose(fid);
When this code is run with a file called FBUG.DAT, which is at least
1024 bytes in size, the Fwrite call is successful according to GEMDOS.
Hatari's GEMDOS trace doesn't show any abnormalities, either:
GEMDOS 0x3D Fopen("FBUG.DAT", read/write) at PC=0x13FF2
GEMDOS: FBUG.DAT -> host: C:\[REDACTED]\fbug.dat
-> FD 64 (read/write -> read+write)
GEMDOS 0x3F Fread(64, 512, 0x30010) at PC 0x1400E
GEMDOS 0x40 Fwrite(64, 512, 0x30010) at PC 0x14050
GEMDOS 0x3E Fclose(64) at PC 0x1405E
However, when running Hatari on Windows, the write operation does in
fact *not* succeed. If you check FBUG.DAT with a hex editor, you won't
find the "MARKER" string.
Initially, this observation puzzled me because Hatari calls the standard
library functions fread and fwrite in the end. It would be unlikely that
these functions are buggy. However, upon reading the C standard, I
discovered the following:
"When a file is opened with update mode ('+' as the second or third
character in the above list of mode argument values), both input and
output may be performed on the associated stream. However, output shall
not be directly followed by input without an intervening call to the
fflush function or to a file positioning function (fseek, fsetpos, or
rewind), and input shall not be directly followed by output without an
intervening call to a file positioning function, unless the input
operation encounters end-of-file."
(Source: ISO/IEC 9899:1999)
In Hatari, GEMDOS_Write calls fflush after each write, which makes the
transition from writing to reading safe. However, the other way around,
i.e., reading to writing, is not. There is no "file positioning
function" enforced before writing.
To resolve the issue, I have attached a patch that fixes the problem and
ensures that the test program and SoftPC run correctly with Hatari's
GEMDOS HD emulation on Windows. Feel free to rework it into a more
clever solution, if you think the unconditional dummy fseek should be
avoided.
Regards
Christian
Attachment:
fbug.zip
Description: Zip compressed data
From 08af7debbd4de22c0ea959f0a75884d05581036f Mon Sep 17 00:00:00 2001
From: Christian Zietz <czietz@xxxxxxx>
Date: Thu, 28 Mar 2024 16:34:49 +0100
Subject: [PATCH] GEMDOS emulation: Fix Fread/Fwrite combination on Windows
The C99 standard mandates that a "file positioning function" is called
when following fread() by fwrite(). To simplify the logic, a dummy
fseek() is performed before every write operation.
---
src/gemdos.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/gemdos.c b/src/gemdos.c
index f13f7bb2..036deed6 100644
--- a/src/gemdos.c
+++ b/src/gemdos.c
@@ -2408,6 +2408,7 @@ static bool GemDOS_Write(uint32_t Params)
}
pBuffer = (char *)STMemory_STAddrToPointer(Addr);
+ fseek(fp, 0, SEEK_CUR);
nBytesWritten = fwrite(pBuffer, 1, Size, fp);
if (fh_idx >= 0 && ferror(fp))
{
--
2.30.2