The-Powder-Toy/src/Update.cpp

154 lines
2.8 KiB
C++
Raw Normal View History

2019-04-20 07:12:32 -05:00
#include "Update.h"
#include <cstdio>
#include <cstdlib>
#ifndef WIN
2012-06-21 09:49:32 -05:00
#include <sys/param.h>
#endif
#if !defined(MACOSX) && !defined(BSD)
#include <malloc.h>
#endif
#include <cstring>
#include <cstdint>
2012-06-21 09:49:32 -05:00
#ifdef WIN
#define NOMINMAX
2012-06-21 09:49:32 -05:00
#include <windows.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#endif
#ifdef MACOSX
#include <mach-o/dyld.h>
#include <errno.h>
#endif
#include "Platform.h"
2012-06-21 09:49:32 -05:00
// returns 1 on failure, 0 on success
2015-01-16 16:26:04 -06:00
int update_start(char *data, unsigned int len)
2012-06-21 09:49:32 -05:00
{
ByteString exeName = Platform::ExecutableName(), updName;
2012-06-21 09:49:32 -05:00
FILE *f;
if (!exeName.length())
2012-06-21 09:49:32 -05:00
return 1;
#ifdef WIN
updName = exeName;
ByteString extension = exeName.substr(exeName.length() - 4);
if (extension == ".exe")
updName = exeName.substr(0, exeName.length() - 4);
updName = updName + "_upd.exe";
if (!MoveFile(exeName.c_str(), updName.c_str()))
return 1;
2012-06-21 09:49:32 -05:00
f = fopen(exeName.c_str(), "wb");
2012-06-21 09:49:32 -05:00
if (!f)
return 1;
2012-06-21 09:49:32 -05:00
if (fwrite(data, 1, len, f) != len)
{
fclose(f);
DeleteFile(exeName.c_str());
return 1;
2012-06-21 09:49:32 -05:00
}
fclose(f);
if ((uintptr_t)ShellExecute(NULL, "open", exeName.c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32)
2012-06-21 09:49:32 -05:00
{
DeleteFile(exeName.c_str());
return 1;
2012-06-21 09:49:32 -05:00
}
return 0;
#else
updName = exeName + "-update";
2012-06-21 09:49:32 -05:00
f = fopen(updName.c_str(), "w");
2012-06-21 09:49:32 -05:00
if (!f)
return 1;
2012-06-21 09:49:32 -05:00
if (fwrite(data, 1, len, f) != len)
{
fclose(f);
unlink(updName.c_str());
return 1;
2012-06-21 09:49:32 -05:00
}
fclose(f);
if (chmod(updName.c_str(), 0755))
2012-06-21 09:49:32 -05:00
{
unlink(updName.c_str());
return 1;
2012-06-21 09:49:32 -05:00
}
if (rename(updName.c_str(), exeName.c_str()))
2012-06-21 09:49:32 -05:00
{
unlink(updName.c_str());
return 1;
2012-06-21 09:49:32 -05:00
}
execl(exeName.c_str(), "powder-update", NULL);
return 0;
2012-06-21 09:49:32 -05:00
#endif
}
// returns 1 on failure, 0 on success
int update_finish()
2012-06-21 09:49:32 -05:00
{
#ifdef WIN
ByteString exeName = Platform::ExecutableName(), updName;
int timeout = 5, err;
2012-06-21 09:49:32 -05:00
#ifdef DEBUG
printf("Update: Current EXE name: %s\n", exeName.c_str());
#endif
updName = exeName;
ByteString extension = exeName.substr(exeName.length() - 4);
if (extension == ".exe")
updName = exeName.substr(0, exeName.length() - 4);
updName = updName + "_upd.exe";
2012-06-21 09:49:32 -05:00
#ifdef DEBUG
printf("Update: Temp EXE name: %s\n", updName.c_str());
#endif
while (!DeleteFile(updName.c_str()))
2012-06-21 09:49:32 -05:00
{
err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND)
{
#ifdef DEBUG
printf("Update: Temp file not deleted\n");
#endif
// Old versions of powder toy name their update files with _update.exe, delete that upgrade file here
updName = exeName;
ByteString extension = exeName.substr(exeName.length() - 4);
if (extension == ".exe")
updName = exeName.substr(0, exeName.length() - 4);
updName = updName + "_update.exe";
DeleteFile(updName.c_str());
2012-06-21 09:49:32 -05:00
return 0;
}
Sleep(500);
timeout--;
if (timeout <= 0)
{
#ifdef DEBUG
printf("Update: Delete timeout\n");
#endif
2012-06-21 09:49:32 -05:00
return 1;
}
}
#endif
return 0;
}
void update_cleanup()
2012-06-21 09:49:32 -05:00
{
#ifdef WIN
2012-06-21 09:49:32 -05:00
update_finish();
#endif
}