diff --git a/src/engine/client/autoupdate.cpp b/src/engine/client/autoupdate.cpp index 9153c4b87..8bd1149a1 100644 --- a/src/engine/client/autoupdate.cpp +++ b/src/engine/client/autoupdate.cpp @@ -70,7 +70,7 @@ void CAutoUpdate::FetchFile(const char *pFile, const char *pDestPath) if(!pDestPath) pDestPath = pFile; CFetchTask *Task = new CFetchTask; - m_pFetcher->QueueAdd(Task, aBuf, pDestPath, 2, this, &CAutoUpdate::CompletionCallback, &CAutoUpdate::ProgressCallback); + m_pFetcher->QueueAdd(Task, aBuf, pDestPath, -2, this, &CAutoUpdate::CompletionCallback, &CAutoUpdate::ProgressCallback); } void CAutoUpdate::Update() @@ -113,11 +113,15 @@ void CAutoUpdate::ReplaceClient() dbg_msg("autoupdate", "Replacing " PLAT_CLIENT_EXEC); //Replace running executable by renaming twice... - m_pStorage->RemoveFile("DDNet.old", 2); - m_pStorage->RenameFile(PLAT_CLIENT_EXEC, "DDNet.old", 2); - m_pStorage->RenameFile("DDNet.tmp", PLAT_CLIENT_EXEC, 2); + m_pStorage->RemoveBinaryFile("DDNet.old"); + m_pStorage->RenameBinaryFile(PLAT_CLIENT_EXEC, "DDNet.old"); + m_pStorage->RenameBinaryFile("DDNet.tmp", PLAT_CLIENT_EXEC); #if !defined(CONF_FAMILY_WINDOWS) - if (system("chmod +x " PLAT_CLIENT_EXEC)) + char aPath[512]; + m_pStorage->GetBinaryPath(PLAT_CLIENT_EXEC, aPath, sizeof aPath); + char aBuf[512]; + str_format(aBuf, sizeof aBuf, "chmod +x %s", aPath); + if (system(aBuf)) dbg_msg("autoupdate", "Error setting client executable bit"); #endif } @@ -127,18 +131,23 @@ void CAutoUpdate::ReplaceServer() dbg_msg("autoupdate", "Replacing " PLAT_SERVER_EXEC); //Replace running executable by renaming twice... - m_pStorage->RemoveFile("DDNet-Server.old", 2); - m_pStorage->RenameFile(PLAT_SERVER_EXEC, "DDNet-Server.old", 2); - m_pStorage->RenameFile("DDNet-Server.tmp", PLAT_SERVER_EXEC, 2); + m_pStorage->RemoveBinaryFile("DDNet-Server.old"); + m_pStorage->RenameBinaryFile(PLAT_SERVER_EXEC, "DDNet-Server.old"); + m_pStorage->RenameBinaryFile("DDNet-Server.tmp", PLAT_SERVER_EXEC); #if !defined(CONF_FAMILY_WINDOWS) - if (system("chmod +x " PLAT_SERVER_EXEC)) + char aPath[512]; + m_pStorage->GetBinaryPath(PLAT_SERVER_EXEC, aPath, sizeof aPath); + char aBuf[512]; + str_format(aBuf, sizeof aBuf, "chmod +x %s", aPath); + if (system(aBuf)) dbg_msg("autoupdate", "Error setting server executable bit"); #endif } void CAutoUpdate::ParseUpdate() { - IOHANDLE File = m_pStorage->OpenFile("update.json", IOFLAG_READ, IStorage::TYPE_ALL); + char aPath[512]; + IOHANDLE File = m_pStorage->OpenFile(m_pStorage->GetBinaryPath("update.json", aPath, sizeof aPath), IOFLAG_READ, IStorage::TYPE_ALL); if(File) { char aBuf[4096*4]; @@ -208,7 +217,7 @@ void CAutoUpdate::PerformUpdate() } while(!m_RemovedFiles.empty()) { - m_pStorage->RemoveFile(m_RemovedFiles.back().c_str(), IStorage::TYPE_SAVE); + m_pStorage->RemoveBinaryFile(m_RemovedFiles.back().c_str()); m_RemovedFiles.pop_back(); } if(m_ServerUpdate) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 964098bd1..cff9cd88c 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1032,8 +1032,7 @@ void CClient::DebugRender() void CClient::Restart() { char aBuf[512]; - Storage()->GetCompletePath(2, PLAT_CLIENT_EXEC, aBuf, sizeof aBuf); - shell_execute(aBuf); + shell_execute(Storage()->GetBinaryPath(PLAT_CLIENT_EXEC, aBuf, sizeof aBuf)); Quit(); } diff --git a/src/engine/client/fetcher.cpp b/src/engine/client/fetcher.cpp index 81172ad84..3eacf5f9e 100644 --- a/src/engine/client/fetcher.cpp +++ b/src/engine/client/fetcher.cpp @@ -93,20 +93,15 @@ void CFetcher::FetcherThread(void *pUser) void CFetcher::FetchFile(CFetchTask *pTask) { - for(int i = 0; pTask->m_pDest[i] != '\0'; i++) - { - if(pTask->m_pDest[i] == '/') - { - pTask->m_pDest[i] = '\0'; - m_pStorage->CreateFolder(pTask->m_pDest, pTask->m_StorageType); - pTask->m_pDest[i] = '/'; - } - } - - char aPath[256]; - m_pStorage->GetCompletePath(pTask->m_StorageType, pTask->m_pDest, aPath, sizeof(aPath)); + char aPath[512]; + if(pTask->m_StorageType == -2) + m_pStorage->GetBinaryPath(pTask->m_pDest, aPath, sizeof(aPath)); + else + m_pStorage->GetCompletePath(pTask->m_StorageType, pTask->m_pDest, aPath, sizeof(aPath)); IOHANDLE File = io_open(aPath, IOFLAG_WRITE); + char aCAFile[512]; + m_pStorage->GetBinaryPath("data/ca-ddnet.pem", aCAFile, sizeof aCAFile); char aErr[CURL_ERROR_SIZE]; curl_easy_setopt(m_pHandle, CURLOPT_ERRORBUFFER, aErr); @@ -115,7 +110,7 @@ void CFetcher::FetchFile(CFetchTask *pTask) curl_easy_setopt(m_pHandle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(m_pHandle, CURLOPT_MAXREDIRS, 4L); curl_easy_setopt(m_pHandle, CURLOPT_FAILONERROR, 1L); - curl_easy_setopt(m_pHandle, CURLOPT_CAINFO, "data/ca-ddnet.pem"); + curl_easy_setopt(m_pHandle, CURLOPT_CAINFO, aCAFile); curl_easy_setopt(m_pHandle, CURLOPT_URL, pTask->m_pUrl); curl_easy_setopt(m_pHandle, CURLOPT_WRITEDATA, File); curl_easy_setopt(m_pHandle, CURLOPT_WRITEFUNCTION, &CFetcher::WriteToFile); diff --git a/src/engine/client/fetcher.h b/src/engine/client/fetcher.h index 9096e75e9..61ccb2fa0 100644 --- a/src/engine/client/fetcher.h +++ b/src/engine/client/fetcher.h @@ -22,7 +22,7 @@ public: virtual bool Init(); ~CFetcher(); - virtual void QueueAdd(CFetchTask *pTask, const char *pUrl, const char *pDest, int StorageType = 2, void *pUser = 0, COMPFUNC pfnCompCb = 0, PROGFUNC pfnProgCb = 0); + virtual void QueueAdd(CFetchTask *pTask, const char *pUrl, const char *pDest, int StorageType = -2, void *pUser = 0, COMPFUNC pfnCompCb = 0, PROGFUNC pfnProgCb = 0); virtual void Escape(char *pBud, size_t size, const char *pStr); static void FetcherThread(void *pUser); void FetchFile(CFetchTask *pTask); diff --git a/src/engine/fetcher.h b/src/engine/fetcher.h index c6de82f59..d8b9f903e 100644 --- a/src/engine/fetcher.h +++ b/src/engine/fetcher.h @@ -17,7 +17,6 @@ class CFetchTask char m_pUrl[256]; char m_pDest[128]; - int m_StorageType; PROGFUNC m_pfnProgressCallback; COMPFUNC m_pfnCompCallback; void *m_pUser; @@ -27,6 +26,7 @@ class CFetchTask int m_Progress; int m_State; bool m_Abort; + int m_StorageType; public: CFetchTask(); @@ -53,7 +53,7 @@ class IFetcher : public IInterface MACRO_INTERFACE("fetcher", 0) public: virtual bool Init() = 0; - virtual void QueueAdd(CFetchTask *pTask,const char *pUrl, const char *pDest, int StorageType = 2, void *pUser = 0, COMPFUNC pfnCompCb = 0, PROGFUNC pfnProgCb = 0) = 0; + virtual void QueueAdd(CFetchTask *pTask, const char *pUrl, const char *pDest, int StorageType = -2, void *pUser = 0, COMPFUNC pfnCompCb = 0, PROGFUNC pfnProgCb = 0) = 0; virtual void Escape(char *pBud, size_t size, const char *pStr) = 0; }; diff --git a/src/engine/shared/storage.cpp b/src/engine/shared/storage.cpp index 8896bfa1b..31afd9e5c 100644 --- a/src/engine/shared/storage.cpp +++ b/src/engine/shared/storage.cpp @@ -21,6 +21,7 @@ public: char m_aDatadir[MAX_PATH_LENGTH]; char m_aUserdir[MAX_PATH_LENGTH]; char m_aCurrentdir[MAX_PATH_LENGTH]; + char m_aBinarydir[MAX_PATH_LENGTH]; CStorage() { @@ -163,6 +164,7 @@ public: if(fs_is_dir("data/mapres")) { str_copy(m_aDatadir, "data", sizeof(m_aDatadir)); + str_copy(m_aBinarydir, "", sizeof(m_aBinarydir)); return; } @@ -170,6 +172,7 @@ public: if(fs_is_dir(DATA_DIR "/mapres")) { str_copy(m_aDatadir, DATA_DIR, sizeof(m_aDatadir)); + str_copy(m_aBinarydir, "", sizeof(m_aBinarydir)); return; } @@ -184,6 +187,7 @@ public: { char aBaseDir[MAX_PATH_LENGTH]; str_copy(aBaseDir, pArgv0, Pos+1); + str_copy(m_aBinarydir, aBaseDir, sizeof(m_aBinarydir)); str_format(m_aDatadir, sizeof(m_aDatadir), "%s/data", aBaseDir); str_append(aBaseDir, "/data/mapres", sizeof(aBaseDir)); @@ -213,6 +217,7 @@ public: str_format(aBuf, sizeof(aBuf), "%s/mapres", aDirs[i]); if(fs_is_dir(aBuf)) { + str_copy(m_aBinarydir, aDirs[i], sizeof(aDirs[i])-5); str_copy(m_aDatadir, aDirs[i], sizeof(m_aDatadir)); return; } @@ -240,7 +245,7 @@ public: } } - const char *GetPath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize) + virtual const char *GetPath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize) { str_format(pBuffer, BufferSize, "%s%s%s", m_aaStoragePaths[Type], !m_aaStoragePaths[Type][0] ? "" : "/", pDir); return pBuffer; @@ -364,6 +369,12 @@ public: return !fs_remove(GetPath(Type, pFilename, aBuffer, sizeof(aBuffer))); } + virtual bool RemoveBinaryFile(const char *pFilename) + { + char aBuffer[MAX_PATH_LENGTH]; + return !fs_remove(GetBinaryPath(pFilename, aBuffer, sizeof(aBuffer))); + } + virtual bool RenameFile(const char* pOldFilename, const char* pNewFilename, int Type) { if(Type < 0 || Type >= m_NumPaths) @@ -373,6 +384,13 @@ public: return !fs_rename(GetPath(Type, pOldFilename, aOldBuffer, sizeof(aOldBuffer)), GetPath(Type, pNewFilename, aNewBuffer, sizeof (aNewBuffer))); } + virtual bool RenameBinaryFile(const char* pOldFilename, const char* pNewFilename) + { + char aOldBuffer[MAX_PATH_LENGTH]; + char aNewBuffer[MAX_PATH_LENGTH]; + return !fs_rename(GetBinaryPath(pOldFilename, aOldBuffer, sizeof(aOldBuffer)), GetBinaryPath(pNewFilename, aNewBuffer, sizeof (aNewBuffer))); + } + virtual bool CreateFolder(const char *pFoldername, int Type) { if(Type < 0 || Type >= m_NumPaths) @@ -394,6 +412,12 @@ public: GetPath(Type, pDir, pBuffer, BufferSize); } + virtual const char* GetBinaryPath(const char *pDir, char *pBuffer, unsigned BufferSize) + { + str_format(pBuffer, BufferSize, "%s%s%s", m_aBinarydir, !m_aBinarydir[0] ? "" : "/", pDir); + return pBuffer; + } + static IStorage *Create(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments) { CStorage *p = new CStorage(); diff --git a/src/engine/storage.h b/src/engine/storage.h index 25aa8b3ec..05a3ac540 100644 --- a/src/engine/storage.h +++ b/src/engine/storage.h @@ -26,6 +26,10 @@ public: virtual bool RenameFile(const char* pOldFilename, const char* pNewFilename, int Type) = 0; virtual bool CreateFolder(const char *pFoldername, int Type) = 0; virtual void GetCompletePath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize) = 0; + + virtual bool RemoveBinaryFile(const char *pFilename) = 0; + virtual bool RenameBinaryFile(const char* pOldFilename, const char* pNewFilename) = 0; + virtual const char* GetBinaryPath(const char *pDir, char *pBuffer, unsigned BufferSize) = 0; }; extern IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments);