BOOL XFilePackage::Save() { m_Mutex.Lock(); for (size_t i = 0; i < m_PakFileTable.size(); ++i) { FILE* pFile = m_PakFileTable[i]; if (pFile) { fflush(pFile); } } m_Mutex.Unlock(); return m_FileIndex.Save(); }
BOOL XFilePackage::ReadFileData(BYTE* pbyBuffer, size_t uBufferSize, uint64_t uOffset, size_t uRequestSize) { BOOL bResult = false; int nRetCode = 0; size_t uBufferPos = 0; m_Mutex.Lock(); XYLOG_FAILED_JUMP(uRequestSize <= uBufferSize); while (uRequestSize > 0) { int nPakFileIndex = (int)(uOffset / MAX_PACK_FILE_SIZE); int32_t nPakFileOffset = (int32_t)(uOffset % MAX_PACK_FILE_SIZE); size_t uRead = Min((size_t)(MAX_PACK_FILE_SIZE - nPakFileOffset), uRequestSize); FILE* pFile = OpenPakFile(nPakFileIndex); assert(nPakFileOffset >= 0); assert(nPakFileOffset <= MAX_PACK_FILE_SIZE); XYLOG_FAILED_JUMP(pFile); nRetCode = fseek(pFile, nPakFileOffset, SEEK_SET); XYLOG_FAILED_JUMP(nRetCode == 0); nRetCode = (int)fread(pbyBuffer + uBufferPos, uRead, 1, pFile); // XYLOG_FAILED_JUMP(nRetCode == 1); if (nRetCode != 1) { Log(eLogDebug, "errno:%d \t uRead:%d \t uBufferPos:%d \t nPakFileOffset:%lld", errno, uRead, uBufferPos, nPakFileOffset); goto Exit0; } EncryptData(pbyBuffer + uBufferPos, uRead, uOffset); uBufferPos += uRead; uOffset += uRead; uRequestSize -= uRead; } bResult = true; Exit0: m_Mutex.Unlock(); return bResult; }
BOOL XFilePackage::WriteFileData(uint64_t uOffset, const BYTE* pbyData, size_t uDataLen) { BOOL bResult = false; int nRetCode = 0; size_t uBufferPos = 0; BYTE* pbyBuffer = (BYTE*)MemDup(pbyData, uDataLen); EncryptData(pbyBuffer, uDataLen, uOffset); m_Mutex.Lock(); while (uDataLen > 0) { int nPakFileIndex = (int)(uOffset / MAX_PACK_FILE_SIZE); int32_t nPakFileOffset = (int32_t)(uOffset % MAX_PACK_FILE_SIZE); size_t uWrite = Min((size_t)(MAX_PACK_FILE_SIZE - nPakFileOffset), uDataLen); FILE* pFile = OpenPakFile(nPakFileIndex, true); assert(nPakFileOffset >= 0); assert(nPakFileOffset <= MAX_PACK_FILE_SIZE); XYLOG_FAILED_JUMP(pFile); nRetCode = fseek(pFile, nPakFileOffset, SEEK_SET); XYLOG_FAILED_JUMP(nRetCode == 0); nRetCode = (int)fwrite(pbyBuffer + uBufferPos, uWrite, 1, pFile); XY_FAILED_JUMP(nRetCode == 1); uBufferPos += uWrite; uOffset += uWrite; uDataLen -= uWrite; } bResult = true; Exit0: m_Mutex.Unlock(); XY_FREE(pbyBuffer); return bResult; }