byte_stream: fix Windows atomic save leaving .tmp behind (stale saves)

The VFS conversion (db694177) broke the atomic-update save path on
Windows.  The pre-VFS code opened the temporary with CreateFileW and
FILE_SHARE_DELETE - the load-bearing flag that let MoveFileExW rename
it over the original while still open, and let DeleteFileW reap a
discarded temporary.  rfopen() maps to _wfopen, which shares
READ|WRITE but never DELETE, so Commit()'s rename of the open .tmp
fails with a sharing violation, m_discarded is set, the destructor's
remove() fails for the same reason, and the new memory-card data is
stranded in <save>.srm.tmp while the stale .srm loads next session.
Independently, the plain rename() also lost MOVEFILE_REPLACE_EXISTING:
MSVCRT rename cannot replace an existing destination even on a closed
file, so overwriting an existing save was doubly broken.

Fix:
- Commit() now flushes and closes the handle before renaming, and
  routes the rename/delete through filestream_rename/filestream_delete
  so frontend-backed paths work too.  If the backend's rename lacks
  replace-existing semantics, fall back to delete-then-rename (the
  atomic single-rename is preserved wherever the backend supports it).
- The destructor closes before deleting a discarded temporary, and
  the base FileByteStream tolerates an already-closed handle.
- vfs_implementation.c (ahead of upstream): the Win32 rename impl now
  uses MoveFileExW(MOVEFILE_REPLACE_EXISTING) instead of _wrename,
  restoring POSIX overwrite semantics for every rename caller.
- The temporary is now always created fresh: the old append-mode
  passthrough maps to VFS UPDATE_EXISTING, which fails when the .tmp
  does not yet exist, so non-truncating atomic opens could never
  create their temporary (also broken by db694177; no in-tree caller
  hit it since memory cards open with TRUNCATE).

Verified: ASan/UBSan lifecycle test covering fresh save, overwrite,
discard, destructor-driven commit, non-truncate copy-in, and double
commit - all green, no .tmp litter; mingw-w64 syntax check confirms
the Win32 branches compile.
This commit is contained in:
LibretroAdmin 2026-08-11 14:36:51 +00:00 • committed by libretroadmin
commit cd5d83db9f
3 changed files with 185 additions and 38 deletions

5
.gitignore vendored
View file

@ -45,3 +45,8 @@ __pycache__
# UWP crap
Generated Files
/packages
# static Makefile build artifacts
*.o
*.d
/swanstation_libretro.so

View file

@ -1280,7 +1280,37 @@ int retro_vfs_file_remove_impl(const char *path)
#if defined(_WIN32) && !defined(_XBOX)
/* Win32 (no Xbox) */
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500
/* AHEAD OF UPSTREAM: this was the other _WIN32_WINNT < 0x0500
* straggler named by the LEGACY_WIN32 migration note in
* encodings/utf.h; selected like the rest of this file now. */
#if defined(LEGACY_WIN32_RUNTIME)
if (win32_needs_local_encoding())
{
char *path_local = NULL;
if ((path_local = utf8_to_local_string_alloc(path)))
{
/* We need to check if path is a directory */
if ((retro_vfs_stat_impl(path, NULL) & RETRO_VFS_STAT_IS_DIRECTORY) != 0)
ret = _rmdir(path_local);
else
ret = remove(path_local);
free(path_local);
}
}
else
{
wchar_t *path_wide = NULL;
if ((path_wide = utf8_to_utf16_string_alloc(path)))
{
/* We need to check if path is a directory */
if ((retro_vfs_stat_impl(path, NULL) & RETRO_VFS_STAT_IS_DIRECTORY) != 0)
ret = _wrmdir(path_wide);
else
ret = _wremove(path_wide);
free(path_wide);
}
}
#elif defined(LEGACY_WIN32)
char *path_local = NULL;
if ((path_local = utf8_to_local_string_alloc(path)))
{
@ -1339,46 +1369,113 @@ int retro_vfs_file_rename_impl(const char *old_path, const char *new_path)
#if defined(_WIN32) && !defined(_XBOX)
/* Win32 (no Xbox) */
int ret = -1;
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500
char *old_path_local = NULL;
#else
wchar_t *old_path_wide = NULL;
#endif
if (!old_path || !*old_path || !new_path || !*new_path)
return -1;
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500
old_path_local = utf8_to_local_string_alloc(old_path);
if (old_path_local)
/* AHEAD OF UPSTREAM: two divergences from upstream master here.
*
* 1. Replace-existing semantics. Neither rename() nor _wrename()
* can replace an existing destination on Windows, which breaks
* every atomic save pattern of the form write tmp -> rename
* over original. The wide branch uses MoveFileExW with
* MOVEFILE_REPLACE_EXISTING - the Win32 spelling of the POSIX
* overwrite guarantee, and in the app API partition so WinRT
* takes it too. The ANSI branch cannot: the flag is
* unsupported on 9x and the Xbox XDK has no MoveFileEx at all,
* so replace there is delete-then-rename - plain rename first
* (the only path taken when the destination does not exist),
* removing the destination only after it fails.
*
* 2. Legacy selection. This function still keyed the ANSI/wide
* split off _WIN32_WINNT < 0x0500, one of the stragglers the
* LEGACY_WIN32 migration note in encodings/utf.h describes:
* that test forced ANSI on Unicode-native NT4 and let a real
* 9x build (which has no reason to set an NT macro low) fall
* into the wide branch, where MoveFileExW is a stub. Now
* selected like the rest of this file: LEGACY_WIN32_RUNTIME
* probes per call, LEGACY_WIN32 is compile-time ANSI, default
* is wide. */
#if defined(LEGACY_WIN32_RUNTIME)
if (win32_needs_local_encoding())
{
char *new_path_local = utf8_to_local_string_alloc(new_path);
char *old_path_local = utf8_to_local_string_alloc(old_path);
if (new_path_local)
if (old_path_local)
{
if (rename(old_path_local, new_path_local) == 0)
ret = 0;
free(new_path_local);
}
char *new_path_local = utf8_to_local_string_alloc(new_path);
free(old_path_local);
if (new_path_local)
{
if (rename(old_path_local, new_path_local) == 0)
ret = 0;
else if (remove(new_path_local) == 0 &&
rename(old_path_local, new_path_local) == 0)
ret = 0;
free(new_path_local);
}
free(old_path_local);
}
}
else
{
wchar_t *old_path_wide = utf8_to_utf16_string_alloc(old_path);
if (old_path_wide)
{
wchar_t *new_path_wide = utf8_to_utf16_string_alloc(new_path);
if (new_path_wide)
{
if (MoveFileExW(old_path_wide, new_path_wide,
MOVEFILE_REPLACE_EXISTING))
ret = 0;
free(new_path_wide);
}
free(old_path_wide);
}
}
#elif defined(LEGACY_WIN32)
{
char *old_path_local = utf8_to_local_string_alloc(old_path);
if (old_path_local)
{
char *new_path_local = utf8_to_local_string_alloc(new_path);
if (new_path_local)
{
if (rename(old_path_local, new_path_local) == 0)
ret = 0;
else if (remove(new_path_local) == 0 &&
rename(old_path_local, new_path_local) == 0)
ret = 0;
free(new_path_local);
}
free(old_path_local);
}
}
#else
old_path_wide = utf8_to_utf16_string_alloc(old_path);
if (old_path_wide)
{
wchar_t *new_path_wide = utf8_to_utf16_string_alloc(new_path);
wchar_t *old_path_wide = utf8_to_utf16_string_alloc(old_path);
if (new_path_wide)
if (old_path_wide)
{
if (_wrename(old_path_wide, new_path_wide) == 0)
ret = 0;
free(new_path_wide);
}
wchar_t *new_path_wide = utf8_to_utf16_string_alloc(new_path);
free(old_path_wide);
if (new_path_wide)
{
if (MoveFileExW(old_path_wide, new_path_wide,
MOVEFILE_REPLACE_EXISTING))
ret = 0;
free(new_path_wide);
}
free(old_path_wide);
}
}
#endif
return ret;

View file

@ -149,6 +149,10 @@ public:
if (m_errorState)
return false;
// Closed (post-commit) streams have nothing left to flush.
if (!m_pFile)
return true;
if (filestream_flush(m_pFile) != 0)
{
m_errorState = true;
@ -163,6 +167,18 @@ public:
virtual bool Discard() override { return false; }
protected:
/// Close the underlying handle early (idempotent). Windows cannot
/// rename or delete a file that is held open by a handle without
/// FILE_SHARE_DELETE - which is what the VFS-opened handle is - so
/// the atomic-update commit path must close before renaming. The
/// rf* wrappers are null-safe, so subsequent stream calls degrade
/// to errors rather than crashes.
void CloseFile()
{
rfclose(m_pFile);
m_pFile = nullptr;
}
RFILE* m_pFile;
};
@ -179,31 +195,55 @@ public:
{
if (m_discarded)
{
// delete the temporary file
if (remove(m_temporaryFileName.c_str()) < 0) { }
// The handle must be closed before the temporary file can be
// deleted on Windows (no FILE_SHARE_DELETE on VFS handles).
CloseFile();
filestream_delete(m_temporaryFileName.c_str());
}
else if (!m_committed)
{
Commit();
}
// rfclose called by FileByteStream destructor
// rfclose called by FileByteStream destructor (null-safe if
// Commit already closed the handle)
}
bool Commit() override
{
if (m_committed)
return Flush();
return true;
// The pre-VFS Windows path opened the temporary with
// FILE_SHARE_DELETE and renamed it over the original while still
// open (MoveFileExW). The VFS handle is a plain _wfopen without
// that sharing mode, so renaming - or deleting - the temporary
// while it is open fails with a sharing violation, leaving the
// new save stranded in .tmp and the original untouched. Flush
// and close first, then rename through the VFS (which also
// routes correctly for frontend-backed paths).
filestream_flush(m_pFile);
CloseFile();
// move the atomic file name to the original file name
if (rename(m_temporaryFileName.c_str(), m_originalFileName.c_str()) < 0)
m_discarded = true;
else
m_committed = true;
if (filestream_rename(m_temporaryFileName.c_str(), m_originalFileName.c_str()) != 0)
{
// A VFS backend without replace-existing rename semantics
// (e.g. a frontend whose Windows rename is plain _wrename)
// fails when the destination exists: fall back to
// delete-then-rename. Not atomic, but strictly better than
// losing the new data; the atomic single-rename path is
// preserved wherever the backend supports it.
filestream_delete(m_originalFileName.c_str());
if (filestream_rename(m_temporaryFileName.c_str(), m_originalFileName.c_str()) != 0)
{
m_discarded = true;
filestream_delete(m_temporaryFileName.c_str());
return false;
}
}
return (!m_discarded);
m_committed = true;
return true;
}
bool Discard() override
@ -649,8 +689,13 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint
char* temporaryFileName = (char*)alloca(fileNameLength + 8);
std::snprintf(temporaryFileName, fileNameLength + 8, "%s.tmp", fileName);
// open the temporary file through libretro VFS
RFILE* pTemporaryFile = rfopen(temporaryFileName, modeString);
// open the temporary file through libretro VFS. The temporary
// must always be created fresh (pre-VFS this was CREATE_NEW):
// passing the caller's append mode through maps to VFS
// UPDATE_EXISTING, which fails when the tmp does not exist yet -
// and would be wrong anyway, since the copy-in loop below is what
// provides the original content for non-truncating opens.
RFILE* pTemporaryFile = rfopen(temporaryFileName, (openMode & BYTESTREAM_OPEN_READ) ? "w+b" : "wb");
if (!pTemporaryFile)
return nullptr;