fs: alternative fs::pending_file implementation (Win32)

Use MOVEFILE_WRITE_THROUGH instead of sync() on commit().
This commit is contained in:
Nekotekina 2021-06-20 14:45:33 +03:00
parent aaa20c0ff0
commit 3c614d95b8
3 changed files with 21 additions and 1 deletions

View file

@ -1984,15 +1984,31 @@ bool fs::pending_file::commit(bool overwrite)
} }
// The temporary file's contents must be on disk before rename // The temporary file's contents must be on disk before rename
#ifndef _WIN32
file.sync(); file.sync();
#endif
file.close(); file.close();
#ifdef _WIN32
const auto ws1 = to_wchar(m_path);
const auto ws2 = to_wchar(m_dest);
if (MoveFileExW(ws1.get(), ws2.get(), overwrite ? MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH : MOVEFILE_WRITE_THROUGH))
{
// Disable the destructor
m_path.clear();
return true;
}
g_tls_error = to_error(GetLastError());
#else
if (fs::rename(m_path, m_dest, overwrite)) if (fs::rename(m_path, m_dest, overwrite))
{ {
// Disable the destructor // Disable the destructor
m_path.clear(); m_path.clear();
return true; return true;
} }
#endif
return false; return false;
} }

View file

@ -649,6 +649,8 @@ namespace fs
bool commit(bool overwrite = true); bool commit(bool overwrite = true);
pending_file(const std::string& path); pending_file(const std::string& path);
pending_file(const pending_file&) = delete;
pending_file& operator=(const pending_file&) = delete;
~pending_file(); ~pending_file();
private: private:

View file

@ -1946,7 +1946,9 @@ static NEVER_INLINE error_code savedata_op(ppu_thread& ppu, u32 operation, u32 v
if (auto file = pair.second.release()) if (auto file = pair.second.release())
{ {
auto&& fvec = static_cast<fs::container_stream<std::vector<uchar>>&>(*file); auto&& fvec = static_cast<fs::container_stream<std::vector<uchar>>&>(*file);
ensure(fs::write_file<true>(new_path + vfs::escape(pair.first), fs::rewrite, fvec.obj)); fs::pending_file f(new_path + vfs::escape(pair.first));
f.file.write(fvec.obj);
ensure(f.commit());
} }
} }