diff --git a/common/Darwin/DarwinMisc.cpp b/common/Darwin/DarwinMisc.cpp index c40d4f4a51..cae34b04ce 100644 --- a/common/Darwin/DarwinMisc.cpp +++ b/common/Darwin/DarwinMisc.cpp @@ -364,7 +364,7 @@ void* HostSys::MapMapping(void* handle, size_t size, const PageProtectionMode& m { const u32 mmap_prot = (mode.CanWrite() ? (PROT_READ | PROT_WRITE) : (PROT_READ)) | (mode.CanExecute() ? PROT_EXEC : 0); - return mmap(nullptr, size, mmap_prot, MAP_PRIVATE | MAP_ANON, static_cast(reinterpret_cast(handle)), 0); + return mmap(nullptr, size, mmap_prot, MAP_PRIVATE, static_cast(reinterpret_cast(handle)), 0); } void HostSys::DestroyMapping(void* handle) diff --git a/pcsx2/SIO/Memcard/MemoryCardFile.cpp b/pcsx2/SIO/Memcard/MemoryCardFile.cpp index 632afeae5b..fea2fc8c00 100644 --- a/pcsx2/SIO/Memcard/MemoryCardFile.cpp +++ b/pcsx2/SIO/Memcard/MemoryCardFile.cpp @@ -338,43 +338,60 @@ void FileMemoryCard::Open() } if (!m_file[slot]) + goto memoryCardOpenFailed; + + m_fileSize[slot] = FileSystem::FSize64(m_file[slot]); + + m_mapping_handles[slot] = HostSys::CreateMappingFromFile(m_file[slot]); + if (!m_mapping_handles[slot]) { - Host::ReportErrorAsync(TRANSLATE_SV("MemoryCard", "Memory Card Read Failed"), - fmt::format(TRANSLATE_FS("MemoryCard", "Unable to access memory card:\n\n{}\n\n" - "Another instance of PCSX2 may be using this memory card " - "or the memory card is stored in a write-protected folder.\n" - "Close any other instances of PCSX2, or restart your computer.\n"), - fname)); + Console.Warning("MemoryCardFile: CreateMappingFromFile failed!"); + goto memoryCardOpenFailed; } - else // Load memory map and checksum + + m_mappings[slot] = static_cast(HostSys::MapMapping(m_mapping_handles[slot], m_fileSize[slot], PageAccess_ReadWrite())); + if (!m_mappings[slot] || reinterpret_cast(m_mappings[slot]) < 0) { - m_fileSize[slot] = FileSystem::FSize64(m_file[slot]); - - m_mapping_handles[slot] = HostSys::CreateMappingFromFile(m_file[slot]); - if (!m_mapping_handles[slot]) - { - Console.Warning("CreateMappingFromFile failed!"); - } - - m_mappings[slot] = static_cast(HostSys::MapMapping(m_mapping_handles[slot], m_fileSize[slot], PageAccess_ReadWrite())); - if (!m_mappings[slot]) - { - Console.Warning("MapSharedMemory failed! %d. %s", errno, strerror(errno)); - } - - Console.WriteLnFmt(Color_Green, "McdSlot {} [File]: {} [{} MB, {}]", slot, Path::GetFileName(fname), - (m_fileSize[slot] + (MCD_SIZE + 1)) / MC2_MBSIZE, - FileMcd_IsMemoryCardFormatted(m_file[slot]) ? "Formatted" : "UNFORMATTED"); - - m_filenames[slot] = std::move(fname); - m_ispsx[slot] = m_fileSize[slot] == 0x20000; - m_chkaddr = 0x210; - - if (!m_ispsx[slot]) - { - std::memcpy(&m_chksum[slot], m_mappings[slot] + m_chkaddr, sizeof(m_chksum[slot])); - } + Console.Warning("MemoryCardFile: MapSharedMemory failed! %d. %s", errno, strerror(errno)); + goto memoryCardOpenFailed; } + + Console.WriteLnFmt(Color_Green, "McdSlot {} [File]: {} [{} MB, {}]", slot, Path::GetFileName(fname), + (m_fileSize[slot] + (MCD_SIZE + 1)) / MC2_MBSIZE, + FileMcd_IsMemoryCardFormatted(m_file[slot]) ? "Formatted" : "UNFORMATTED"); + + m_filenames[slot] = std::move(fname); + m_ispsx[slot] = m_fileSize[slot] == 0x20000; + m_chkaddr = 0x210; + + if (!m_ispsx[slot]) + { + std::memcpy(&m_chksum[slot], m_mappings[slot] + m_chkaddr, sizeof(m_chksum[slot])); + } + + continue; + +memoryCardOpenFailed: + Host::ReportErrorAsync(TRANSLATE_SV("MemoryCard", "Memory Card Read Failed"), + fmt::format(TRANSLATE_FS("MemoryCard", "Unable to access memory card:\n\n{}\n\n" + "Another instance of PCSX2 may be using this memory card " + "or the memory card is stored in a write-protected folder.\n" + "Close any other instances of PCSX2, or restart your computer.\n"), + fname)); + + if(m_mapping_handles[slot]) + { + HostSys::DestroyMapping(m_mapping_handles[slot]); + } + + if(m_file[slot]) + { + std::fclose(m_file[slot]); + m_file[slot] = nullptr; + } + + m_filenames[slot] = {}; + m_fileSize[slot] = -1; } }