download to .%pid%.tmp, rename to .map when done

The commit message is misleading. DDNet already had the pid.tmp behavior.
This just uses more the teeworlds style of doing it.

(cherry picked from commit 0da0cb8044)
This commit is contained in:
Robert Müller 2020-04-14 12:00:20 +02:00 committed by ChillerDragon
parent 4ba3d206a0
commit f974117ce0
2 changed files with 72 additions and 78 deletions

View file

@ -313,9 +313,10 @@ CClient::CClient() :
// map download // map download
m_aMapdownloadFilename[0] = 0; m_aMapdownloadFilename[0] = 0;
m_aMapdownloadFilenameTemp[0] = 0;
m_aMapdownloadName[0] = 0; m_aMapdownloadName[0] = 0;
m_pMapdownloadTask = NULL; m_pMapdownloadTask = NULL;
m_MapdownloadFile = 0; m_MapdownloadFileTemp = 0;
m_MapdownloadChunk = 0; m_MapdownloadChunk = 0;
m_MapdownloadSha256Present = false; m_MapdownloadSha256Present = false;
m_MapdownloadSha256 = SHA256_ZEROED; m_MapdownloadSha256 = SHA256_ZEROED;
@ -456,12 +457,12 @@ void CClient::SendReady()
void CClient::SendMapRequest() void CClient::SendMapRequest()
{ {
if(m_MapdownloadFile) if(m_MapdownloadFileTemp)
{ {
io_close(m_MapdownloadFile); io_close(m_MapdownloadFileTemp);
Storage()->RemoveFile(m_aMapdownloadFilename, IStorage::TYPE_SAVE); Storage()->RemoveFile(m_aMapdownloadFilenameTemp, IStorage::TYPE_SAVE);
} }
m_MapdownloadFile = Storage()->OpenFile(m_aMapdownloadFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); m_MapdownloadFileTemp = Storage()->OpenFile(m_aMapdownloadFilenameTemp, IOFLAG_WRITE, IStorage::TYPE_SAVE);
CMsgPacker Msg(NETMSG_REQUEST_MAP_DATA, true); CMsgPacker Msg(NETMSG_REQUEST_MAP_DATA, true);
Msg.AddInt(m_MapdownloadChunk); Msg.AddInt(m_MapdownloadChunk);
SendMsg(&Msg, MSGFLAG_VITAL | MSGFLAG_FLUSH); SendMsg(&Msg, MSGFLAG_VITAL | MSGFLAG_FLUSH);
@ -803,12 +804,12 @@ void CClient::DisconnectWithReason(const char *pReason)
m_MapdownloadChunk = 0; m_MapdownloadChunk = 0;
if(m_pMapdownloadTask) if(m_pMapdownloadTask)
m_pMapdownloadTask->Abort(); m_pMapdownloadTask->Abort();
if(m_MapdownloadFile) if(m_MapdownloadFileTemp)
{ {
io_close(m_MapdownloadFile); io_close(m_MapdownloadFileTemp);
Storage()->RemoveFile(m_aMapdownloadFilename, IStorage::TYPE_SAVE); Storage()->RemoveFile(m_aMapdownloadFilenameTemp, IStorage::TYPE_SAVE);
} }
m_MapdownloadFile = 0; m_MapdownloadFileTemp = 0;
m_MapdownloadSha256Present = false; m_MapdownloadSha256Present = false;
m_MapdownloadSha256 = SHA256_ZEROED; m_MapdownloadSha256 = SHA256_ZEROED;
m_MapdownloadCrc = 0; m_MapdownloadCrc = 0;
@ -1217,24 +1218,43 @@ const char *CClient::LoadMap(const char *pName, const char *pFilename, SHA256_DI
return 0; return 0;
} }
static void FormatMapDownloadFilename(const char *pName, const SHA256_DIGEST *pSha256, int Crc, bool Temp, char *pBuffer, int BufferSize)
{
char aSuffix[32];
if(Temp)
{
str_format(aSuffix, sizeof(aSuffix), ".%d.tmp", pid());
}
else
{
str_copy(aSuffix, ".map", sizeof(aSuffix));
}
if(pSha256)
{
char aSha256[SHA256_MAXSTRSIZE];
sha256_str(*pSha256, aSha256, sizeof(aSha256));
str_format(pBuffer, BufferSize, "downloadedmaps/%s_%s%s", pName, aSha256, aSuffix);
}
else
{
str_format(pBuffer, BufferSize, "downloadedmaps/%s_%08x%s", pName, Crc, aSuffix);
}
}
const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedSha256, int WantedCrc) const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedSha256, int WantedCrc)
{ {
const char *pError = 0; const char *pError = 0;
char aBuf[512]; char aBuf[512];
char aWanted[256]; char aWanted[SHA256_MAXSTRSIZE + 16];
char aWantedSha256[SHA256_MAXSTRSIZE]; aWanted[0] = 0;
if(pWantedSha256) if(pWantedSha256)
{ {
char aWantedSha256[SHA256_MAXSTRSIZE];
sha256_str(*pWantedSha256, aWantedSha256, sizeof(aWantedSha256)); sha256_str(*pWantedSha256, aWantedSha256, sizeof(aWantedSha256));
str_format(aWanted, sizeof(aWanted), "sha256=%s", aWantedSha256); str_format(aWanted, sizeof(aWanted), "sha256=%s ", aWantedSha256);
} }
else str_format(aBuf, sizeof(aBuf), "loading map, map=%s wanted %scrc=%08x", pMapName, aWanted, WantedCrc);
{
str_format(aWanted, sizeof(aWanted), "crc=%08x", WantedCrc);
}
str_format(aBuf, sizeof(aBuf), "loading map, map=%s wanted %s", pMapName, aWanted);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", aBuf); m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", aBuf);
SetState(IClient::STATE_LOADING); SetState(IClient::STATE_LOADING);
@ -1245,20 +1265,19 @@ const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedS
return pError; return pError;
// try the downloaded maps // try the downloaded maps
FormatMapDownloadFilename(pMapName, pWantedSha256, WantedCrc, false, aBuf, sizeof(aBuf));
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
if(!pError)
return pError;
// backward compatibility with old names
if(pWantedSha256) if(pWantedSha256)
{ {
str_format(aBuf, sizeof(aBuf), "downloadedmaps/%s_%s.map", pMapName, aWantedSha256); FormatMapDownloadFilename(pMapName, 0, WantedCrc, false, aBuf, sizeof(aBuf));
}
else
{
str_format(aBuf, sizeof(aBuf), "downloadedmaps/%s_%08x.map", pMapName, WantedCrc);
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc); pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
if(!pError) if(!pError)
return pError; return pError;
} }
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
if(!pError)
return pError;
// search for the map within subfolders // search for the map within subfolders
char aFilename[128]; char aFilename[128];
@ -1512,28 +1531,6 @@ bool CClient::ShouldSendChatTimeoutCodeHeuristic()
return IsDDNet(&m_CurrentServerInfo); return IsDDNet(&m_CurrentServerInfo);
} }
static void FormatMapDownloadFilename(const char *pName, SHA256_DIGEST *pSha256, int Crc, bool Temp, char *pBuffer, int BufferSize)
{
char aHash[SHA256_MAXSTRSIZE];
if(pSha256)
{
sha256_str(*pSha256, aHash, sizeof(aHash));
}
else
{
str_format(aHash, sizeof(aHash), "%08x", Crc);
}
if(Temp)
{
str_format(pBuffer, BufferSize, "%s_%s.map.%d.tmp", pName, aHash, pid());
}
else
{
str_format(pBuffer, BufferSize, "%s_%s.map", pName, aHash);
}
}
static CServerCapabilities GetServerCapabilities(int Version, int Flags) static CServerCapabilities GetServerCapabilities(int Version, int Flags)
{ {
CServerCapabilities Result; CServerCapabilities Result;
@ -1664,16 +1661,18 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
} }
else else
{ {
char aFilename[256]; if(m_MapdownloadFileTemp)
FormatMapDownloadFilename(pMap, pMapSha256, MapCrc, false, aFilename, sizeof(aFilename)); {
io_close(m_MapdownloadFileTemp);
Storage()->RemoveFile(m_aMapdownloadFilenameTemp, IStorage::TYPE_SAVE);
}
char aTempFilename[256]; // start map download
FormatMapDownloadFilename(pMap, pMapSha256, MapCrc, true, aTempFilename, sizeof(aTempFilename)); FormatMapDownloadFilename(pMap, pMapSha256, MapCrc, false, m_aMapdownloadFilename, sizeof(m_aMapdownloadFilename));
FormatMapDownloadFilename(pMap, pMapSha256, MapCrc, true, m_aMapdownloadFilenameTemp, sizeof(m_aMapdownloadFilenameTemp));
str_format(m_aMapdownloadFilename, sizeof(m_aMapdownloadFilename), "downloadedmaps/%s", aTempFilename);
char aBuf[256]; char aBuf[256];
str_format(aBuf, sizeof(aBuf), "starting to download map to '%s'", m_aMapdownloadFilename); str_format(aBuf, sizeof(aBuf), "starting to download map to '%s'", m_aMapdownloadFilenameTemp);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client/network", aBuf); m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client/network", aBuf);
m_MapdownloadChunk = 0; m_MapdownloadChunk = 0;
@ -1691,11 +1690,11 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
{ {
char aUrl[256]; char aUrl[256];
char aEscaped[256]; char aEscaped[256];
EscapeUrl(aEscaped, sizeof(aEscaped), aFilename); EscapeUrl(aEscaped, sizeof(aEscaped), m_aMapdownloadFilename + 15); // cut off downloadedmaps/
bool UseConfigUrl = str_comp(g_Config.m_ClMapDownloadUrl, "https://maps2.ddnet.tw") != 0 || m_aMapDownloadUrl[0] == '\0'; bool UseConfigUrl = str_comp(g_Config.m_ClMapDownloadUrl, "https://maps2.ddnet.tw") != 0 || m_aMapDownloadUrl[0] == '\0';
str_format(aUrl, sizeof(aUrl), "%s/%s", UseConfigUrl ? g_Config.m_ClMapDownloadUrl : m_aMapDownloadUrl, aEscaped); str_format(aUrl, sizeof(aUrl), "%s/%s", UseConfigUrl ? g_Config.m_ClMapDownloadUrl : m_aMapDownloadUrl, aEscaped);
m_pMapdownloadTask = std::make_shared<CGetFile>(Storage(), aUrl, m_aMapdownloadFilename, IStorage::TYPE_SAVE, CTimeout{g_Config.m_ClMapDownloadConnectTimeoutMs, g_Config.m_ClMapDownloadLowSpeedLimit, g_Config.m_ClMapDownloadLowSpeedTime}); m_pMapdownloadTask = std::make_shared<CGetFile>(Storage(), aUrl, m_aMapdownloadFilenameTemp, IStorage::TYPE_SAVE, CTimeout{g_Config.m_ClMapDownloadConnectTimeoutMs, g_Config.m_ClMapDownloadLowSpeedLimit, g_Config.m_ClMapDownloadLowSpeedTime});
Engine()->AddJob(m_pMapdownloadTask); Engine()->AddJob(m_pMapdownloadTask);
} }
else else
@ -1712,19 +1711,19 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
const unsigned char *pData = Unpacker.GetRaw(Size); const unsigned char *pData = Unpacker.GetRaw(Size);
// check for errors // check for errors
if(Unpacker.Error() || Size <= 0 || MapCRC != m_MapdownloadCrc || Chunk != m_MapdownloadChunk || !m_MapdownloadFile) if(Unpacker.Error() || Size <= 0 || MapCRC != m_MapdownloadCrc || Chunk != m_MapdownloadChunk || !m_MapdownloadFileTemp)
return; return;
io_write(m_MapdownloadFile, pData, Size); io_write(m_MapdownloadFileTemp, pData, Size);
m_MapdownloadAmount += Size; m_MapdownloadAmount += Size;
if(Last) if(Last)
{ {
if(m_MapdownloadFile) if(m_MapdownloadFileTemp)
{ {
io_close(m_MapdownloadFile); io_close(m_MapdownloadFileTemp);
m_MapdownloadFile = 0; m_MapdownloadFileTemp = 0;
} }
FinishMapDownload(); FinishMapDownload();
} }
@ -2335,7 +2334,7 @@ void CClient::ResetMapDownload()
m_pMapdownloadTask->Abort(); m_pMapdownloadTask->Abort();
m_pMapdownloadTask = NULL; m_pMapdownloadTask = NULL;
} }
m_MapdownloadFile = 0; m_MapdownloadFileTemp = 0;
m_MapdownloadAmount = 0; m_MapdownloadAmount = 0;
} }
@ -2348,18 +2347,11 @@ void CClient::FinishMapDownload()
m_MapdownloadTotalsize = -1; m_MapdownloadTotalsize = -1;
SHA256_DIGEST *pSha256 = m_MapdownloadSha256Present ? &m_MapdownloadSha256 : 0; SHA256_DIGEST *pSha256 = m_MapdownloadSha256Present ? &m_MapdownloadSha256 : 0;
char aTmp[MAX_PATH_LENGTH]; Storage()->RemoveFile(m_aMapdownloadFilename, IStorage::TYPE_SAVE);
char aMapFileTemp[MAX_PATH_LENGTH]; Storage()->RenameFile(m_aMapdownloadFilenameTemp, m_aMapdownloadFilename, IStorage::TYPE_SAVE);
char aMapFile[MAX_PATH_LENGTH];
FormatMapDownloadFilename(m_aMapdownloadName, pSha256, m_MapdownloadCrc, true, aTmp, sizeof(aTmp));
str_format(aMapFileTemp, sizeof(aMapFileTemp), "downloadedmaps/%s", aTmp);
FormatMapDownloadFilename(m_aMapdownloadName, pSha256, m_MapdownloadCrc, false, aTmp, sizeof(aTmp));
str_format(aMapFile, sizeof(aMapFileTemp), "downloadedmaps/%s", aTmp);
Storage()->RenameFile(aMapFileTemp, aMapFile, IStorage::TYPE_SAVE);
// load map // load map
pError = LoadMap(m_aMapdownloadName, aMapFile, pSha256, m_MapdownloadCrc); pError = LoadMap(m_aMapdownloadName, m_aMapdownloadFilename, pSha256, m_MapdownloadCrc);
if(!pError) if(!pError)
{ {
ResetMapDownload(); ResetMapDownload();
@ -2374,10 +2366,11 @@ void CClient::FinishMapDownload()
} }
else else
{ {
if(m_MapdownloadFile) if(m_MapdownloadFileTemp)
{ {
io_close(m_MapdownloadFile); io_close(m_MapdownloadFileTemp);
m_MapdownloadFile = 0; m_MapdownloadFileTemp = 0;
Storage()->RemoveFile(m_aMapdownloadFilenameTemp, IStorage::TYPE_SAVE);
} }
ResetMapDownload(); ResetMapDownload();
DisconnectWithReason(pError); DisconnectWithReason(pError);

View file

@ -180,8 +180,9 @@ class CClient : public IClient, public CDemoPlayer::IListener
// map download // map download
std::shared_ptr<CGetFile> m_pMapdownloadTask; std::shared_ptr<CGetFile> m_pMapdownloadTask;
char m_aMapdownloadFilename[256]; char m_aMapdownloadFilename[256];
char m_aMapdownloadFilenameTemp[256];
char m_aMapdownloadName[256]; char m_aMapdownloadName[256];
IOHANDLE m_MapdownloadFile; IOHANDLE m_MapdownloadFileTemp;
int m_MapdownloadChunk; int m_MapdownloadChunk;
int m_MapdownloadCrc; int m_MapdownloadCrc;
int m_MapdownloadAmount; int m_MapdownloadAmount;