From dd518fcafb7efc6c790140c77b5750f2d4f248a3 Mon Sep 17 00:00:00 2001 From: Eladash Date: Mon, 2 Oct 2023 14:35:19 +0300 Subject: [PATCH] Savestates/sys_fs: Fix file saving --- rpcs3/Emu/Cell/lv2/sys_fs.cpp | 24 ++++++++++++++++-------- rpcs3/util/serialization.hpp | 8 ++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/rpcs3/Emu/Cell/lv2/sys_fs.cpp b/rpcs3/Emu/Cell/lv2/sys_fs.cpp index dabbb03268..1abd6320ec 100644 --- a/rpcs3/Emu/Cell/lv2/sys_fs.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_fs.cpp @@ -502,20 +502,28 @@ void lv2_file::save(utils::serial& ar) fs::file_id test_s = test.get_id(); fs::file_id file_s = file.get_id(); - return test_s.is_coherent_with(file_s); + return !test_s.is_coherent_with(file_s); }(); - if (in_mem) - { - sys_fs.error("Saving \'%s\' LV2 file descriptor in memory! (exists=%s, type=%s, flags=0x%x)", name.data(), fs::is_file(real_path), type, flags); - } - ar(in_mem); if (in_mem) { - ar(file.to_vector()); - ar(file.get_stat()); + fs::stat_t stats = file.get_stat(); + + sys_fs.error("Saving \'%s\' LV2 file descriptor in memory! (exists=%s, type=%s, flags=0x%x, size=0x%x)", name.data(), fs::is_file(real_path), type, flags, stats.size); + + const usz old_end = ar.pad_from_end(stats.size); + + if (usz read_size = file.read_at(0, &ar.data[old_end], stats.size); read_size != stats.size) + { + ensure(read_size < stats.size); + sys_fs.error("Read less than expected! (new-size=0x%x)", read_size); + stats.size = read_size; + ar.data.resize(old_end + stats.size); + } + + ar(stats); } ar(file.pos()); diff --git a/rpcs3/util/serialization.hpp b/rpcs3/util/serialization.hpp index f93498adda..cb127af9c3 100644 --- a/rpcs3/util/serialization.hpp +++ b/rpcs3/util/serialization.hpp @@ -324,6 +324,14 @@ namespace utils return pos; } + usz pad_from_end(usz forwards) + { + ensure(is_writing()); + pos = data.size(); + data.resize(pos + forwards); + return pos; + } + template requires (std::is_copy_constructible_v>) && (std::is_constructible_v> || Bitcopy> || std::is_constructible_v, stx::exact_t> || TupleAlike>) operator T()