Use proper path for autoupdater on *nix

This commit is contained in:
def 2015-03-14 20:01:18 +01:00
parent bccc94193e
commit d835826461
7 changed files with 61 additions and 30 deletions

View file

@ -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)

View file

@ -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();
}

View file

@ -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);

View file

@ -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);

View file

@ -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;
};

View file

@ -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();

View file

@ -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);