MCPcopy Create free account
hub / github.com/PCSX2/pcsx2 / WriteToFile

Method WriteToFile

pcsx2/SIO/Memcard/MemoryCardFolder.cpp:1501–1562  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1499}
1500
1501bool FolderMemoryCard::WriteToFile(const u8* src, u32 adr, u32 dataLength)
1502{
1503 const u32 cluster = adr / ClusterSizeRaw;
1504 const u32 page = adr / PageSizeRaw;
1505 const u32 offset = adr % PageSizeRaw;
1506 const u32 fatCluster = cluster - m_superBlock.data.alloc_offset;
1507
1508 // if the cluster is unused according to FAT, just skip all this, we're not gonna find anything anyway
1509 if ((m_fat.data[0][0][fatCluster] & DataClusterInUseMask) == 0)
1510 {
1511 return false;
1512 }
1513
1514 // figure out which file to write to
1515 auto it = m_fileMetadataQuickAccess.find(fatCluster);
1516 if (it != m_fileMetadataQuickAccess.end())
1517 {
1518 const MemoryCardFileEntry* const entry = it->second.entry;
1519 const u32 clusterNumber = it->second.consecutiveCluster;
1520
1521 if (m_performFileWrites)
1522 {
1523 std::FILE* file = m_lastAccessedFile.ReOpen(m_folderName, &it->second, true);
1524 if (file)
1525 {
1526 const u32 clusterOffset = (page % 2) * PageSize + offset;
1527 const u32 fileSize = entry->entry.data.length;
1528 const u32 fileOffsetStart = std::min(clusterNumber * ClusterSize + clusterOffset, fileSize);
1529 const u32 fileOffsetEnd = std::min(fileOffsetStart + dataLength, fileSize);
1530 const u32 bytesToWrite = fileOffsetEnd - fileOffsetStart;
1531
1532 u32 actualFileSize = static_cast<u32>(std::clamp<s64>(FileSystem::FSize64(file), 0, std::numeric_limits<u32>::max()));
1533 if (actualFileSize < fileOffsetStart)
1534 {
1535 FileSystem::FSeek64(file, actualFileSize, SEEK_SET);
1536 const u32 diff = fileOffsetStart - actualFileSize;
1537 u8 temp = 0xFF;
1538 for (u32 i = 0; i < diff; ++i)
1539 {
1540 std::fwrite(&temp, 1, 1, file);
1541 }
1542 }
1543
1544 if (FileSystem::FTell64(file) == fileOffsetStart || FileSystem::FSeek64(file, fileOffsetStart, SEEK_SET) == 0)
1545 {
1546 if (bytesToWrite > 0)
1547 {
1548 std::fwrite(src, bytesToWrite, 1, file);
1549 }
1550 }
1551 }
1552 else
1553 {
1554 return false;
1555 }
1556 }
1557
1558 return true;

Callers

nothing calls this directly

Calls 9

ReOpenMethod · 0.80
minFunction · 0.50
maxFunction · 0.50
ReadFunction · 0.50
findMethod · 0.45
endMethod · 0.45
c_strMethod · 0.45
ErrorMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected