Add IStorage::FormatTmpPath static method

Not sure I prefer it, but now it's done. Any opinions?
This commit is contained in:
def 2021-12-20 15:00:21 +01:00
parent 3013466b86
commit 1a02a20fa6
8 changed files with 24 additions and 18 deletions

View file

@ -338,7 +338,7 @@ CClient::CClient() :
m_MapDetailsSha256 = SHA256_ZEROED;
m_MapDetailsCrc = 0;
str_format(m_aDDNetInfoTmp, sizeof(m_aDDNetInfoTmp), DDNET_INFO ".%d.tmp", pid());
IStorage::FormatTmpPath(m_aDDNetInfoTmp, sizeof(m_aDDNetInfoTmp), DDNET_INFO);
m_pDDNetInfoTask = NULL;
m_aNews[0] = '\0';
m_aMapDownloadUrl[0] = '\0';
@ -1232,7 +1232,7 @@ static void FormatMapDownloadFilename(const char *pName, const SHA256_DIGEST *pS
char aSuffix[32];
if(Temp)
{
str_format(aSuffix, sizeof(aSuffix), ".%d.tmp", pid());
IStorage::FormatTmpPath(aSuffix, sizeof(aSuffix), "");
}
else
{

View file

@ -93,8 +93,8 @@ CUpdater::CUpdater()
m_Percent = 0;
m_Lock = lock_create();
str_format(m_aClientExecTmp, sizeof(m_aClientExecTmp), CLIENT_EXEC ".%d.tmp", pid());
str_format(m_aServerExecTmp, sizeof(m_aServerExecTmp), SERVER_EXEC ".%d.tmp", pid());
IStorage::FormatTmpPath(m_aClientExecTmp, sizeof(m_aClientExecTmp), CLIENT_EXEC);
IStorage::FormatTmpPath(m_aServerExecTmp, sizeof(m_aServerExecTmp), SERVER_EXEC);
}
void CUpdater::Init()

View file

@ -44,10 +44,8 @@ bool CConfigManager::Save()
if(!m_pStorage || !g_Config.m_ClSaveSettings)
return true;
char aConfigFileTmp[64];
str_format(aConfigFileTmp, sizeof(aConfigFileTmp), CONFIG_FILE ".%d.tmp", pid());
m_ConfigFile = m_pStorage->OpenFile(aConfigFileTmp, IOFLAG_WRITE, IStorage::TYPE_SAVE);
char aConfigFileTmp[IO_MAX_PATH_LENGTH];
m_ConfigFile = m_pStorage->OpenFile(IStorage::FormatTmpPath(aConfigFileTmp, sizeof(aConfigFileTmp), CONFIG_FILE), IOFLAG_WRITE, IStorage::TYPE_SAVE);
if(!m_ConfigFile)
{

View file

@ -594,6 +594,12 @@ void IStorage::StripPathAndExtension(const char *pFilename, char *pBuffer, int B
str_copy(pBuffer, pExtractedName, Length);
}
const char *IStorage::FormatTmpPath(char *aBuf, unsigned BufSize, const char *pPath)
{
str_format(aBuf, BufSize, "%s.%d.tmp", pPath, pid());
return aBuf;
}
IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments)
{
return CStorage::Create(pApplicationName, StorageType, NumArgs, ppArguments);

View file

@ -39,6 +39,7 @@ public:
virtual const char *GetBinaryPath(const char *pFilename, char *pBuffer, unsigned BufferSize) = 0;
static void StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize);
static const char *FormatTmpPath(char *aBuf, unsigned BufSize, const char *pPath);
};
extern IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments);

View file

@ -421,11 +421,12 @@ int CSkins::FindImpl(const char *pName)
CDownloadSkin Skin;
str_copy(Skin.m_aName, pName, sizeof(Skin.m_aName));
char aUrl[256];
char aUrl[IO_MAX_PATH_LENGTH];
char aEscapedName[256];
EscapeUrl(aEscapedName, sizeof(aEscapedName), pName);
str_format(aUrl, sizeof(aUrl), "%s%s.png", g_Config.m_ClSkinDownloadUrl, aEscapedName);
str_format(Skin.m_aPath, sizeof(Skin.m_aPath), "downloadedskins/%s.%d.tmp", pName, pid());
char aBuf[IO_MAX_PATH_LENGTH];
str_format(Skin.m_aPath, sizeof(Skin.m_aPath), "downloadedskins/%s", IStorage::FormatTmpPath(aBuf, sizeof(aBuf), pName));
Skin.m_pTask = std::make_shared<CGetPngFile>(this, Storage(), aUrl, Skin.m_aPath, IStorage::TYPE_SAVE, CTimeout{0, 0, 0}, HTTPLOG::NONE);
m_pClient->Engine()->AddJob(Skin.m_pTask);
m_aDownloadSkins.add(Skin);

View file

@ -3125,7 +3125,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/)
m_Layers.Init(Kernel());
m_Collision.Init(&m_Layers);
char aMapName[128];
char aMapName[IO_MAX_PATH_LENGTH];
int MapSize;
SHA256_DIGEST MapSha256;
int MapCrc;
@ -3421,10 +3421,8 @@ void CGameContext::DeleteTempfile()
void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize)
{
char aConfig[128];
char aTemp[128];
char aConfig[IO_MAX_PATH_LENGTH];
str_format(aConfig, sizeof(aConfig), "maps/%s.cfg", g_Config.m_SvMap);
str_format(aTemp, sizeof(aTemp), "%s.%d.tmp", pNewMapName, pid());
IOHANDLE File = Storage()->OpenFile(aConfig, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
if(!File)
@ -3539,7 +3537,8 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize)
dbg_msg("mapchange", "imported settings");
free(pSettings);
Reader.Close();
Writer.OpenFile(Storage(), aTemp);
char aTemp[IO_MAX_PATH_LENGTH];
Writer.OpenFile(Storage(), IStorage::FormatTmpPath(aTemp, sizeof(aTemp), pNewMapName));
Writer.Finish();
str_copy(pNewMapName, aTemp, MapNameSize);
@ -3603,7 +3602,7 @@ void CGameContext::LoadMapSettings()
break;
}
char aBuf[128];
char aBuf[IO_MAX_PATH_LENGTH];
str_format(aBuf, sizeof(aBuf), "maps/%s.map.cfg", g_Config.m_SvMap);
Console()->ExecuteFile(aBuf, IConsole::CLIENT_ID_NO_GAME);
}

View file

@ -10,8 +10,9 @@ CTestInfo::CTestInfo()
{
const ::testing::TestInfo *pTestInfo =
::testing::UnitTest::GetInstance()->current_test_info();
str_format(m_aFilename, sizeof(m_aFilename), "%s.%s-%d.tmp",
pTestInfo->test_case_name(), pTestInfo->name(), pid());
char aBuf[IO_MAX_PATH_LENGTH];
str_format(aBuf, sizeof(aBuf), "%s.%s", pTestInfo->test_case_name(), pTestInfo->name());
IStorage::FormatTmpPath(m_aFilename, sizeof(m_aFilename), aBuf);
}
IStorage *CTestInfo::CreateTestStorage()