2019-04-20 07:12:32 -05:00
|
|
|
#include "Update.h"
|
|
|
|
|
2016-07-17 22:37:24 -05:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifndef WIN
|
2012-06-21 09:49:32 -05:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#if !defined(MACOSX) && !defined(BSD)
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2016-07-17 22:37:24 -05:00
|
|
|
#include <cstring>
|
2019-01-07 00:08:52 -06:00
|
|
|
#include <cstdint>
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2018-05-17 20:49:46 -05:00
|
|
|
#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
|
|
|
|
|
2015-08-31 22:33:40 -05:00
|
|
|
#include "Platform.h"
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2018-04-23 21:32:03 -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
|
|
|
{
|
2018-04-30 13:13:24 -05:00
|
|
|
ByteString exeName = Platform::ExecutableName(), updName;
|
2012-06-21 09:49:32 -05:00
|
|
|
FILE *f;
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
if (!exeName.length())
|
2012-06-21 09:49:32 -05:00
|
|
|
return 1;
|
|
|
|
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2018-04-21 18:28:36 -05:00
|
|
|
updName = exeName;
|
2018-04-30 13:13:24 -05:00
|
|
|
ByteString extension = exeName.substr(exeName.length() - 4);
|
2018-04-21 18:28:36 -05:00
|
|
|
if (extension == ".exe")
|
|
|
|
updName = exeName.substr(0, exeName.length() - 4);
|
|
|
|
updName = updName + "_upd.exe";
|
|
|
|
|
|
|
|
if (!MoveFile(exeName.c_str(), updName.c_str()))
|
2018-04-23 21:32:03 -05:00
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
f = fopen(exeName.c_str(), "wb");
|
2012-06-21 09:49:32 -05:00
|
|
|
if (!f)
|
2018-04-23 21:32:03 -05:00
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
if (fwrite(data, 1, len, f) != len)
|
|
|
|
{
|
|
|
|
fclose(f);
|
2018-04-21 18:28:36 -05:00
|
|
|
DeleteFile(exeName.c_str());
|
2018-04-23 21:32:03 -05:00
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
if ((uintptr_t)ShellExecute(NULL, "open", exeName.c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32)
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
2018-04-21 18:28:36 -05:00
|
|
|
DeleteFile(exeName.c_str());
|
2018-04-23 21:32:03 -05:00
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
2018-04-21 18:28:36 -05:00
|
|
|
updName = exeName + "-update";
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
f = fopen(updName.c_str(), "w");
|
2012-06-21 09:49:32 -05:00
|
|
|
if (!f)
|
2018-04-21 18:28:36 -05:00
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
if (fwrite(data, 1, len, f) != len)
|
|
|
|
{
|
|
|
|
fclose(f);
|
2018-04-21 18:28:36 -05:00
|
|
|
unlink(updName.c_str());
|
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
if (chmod(updName.c_str(), 0755))
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
2018-04-21 18:28:36 -05:00
|
|
|
unlink(updName.c_str());
|
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
}
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
if (rename(updName.c_str(), exeName.c_str()))
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
2018-04-21 18:28:36 -05:00
|
|
|
unlink(updName.c_str());
|
|
|
|
return 1;
|
2012-06-21 09:49:32 -05:00
|
|
|
}
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
execl(exeName.c_str(), "powder-update", NULL);
|
|
|
|
return 0;
|
2012-06-21 09:49:32 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-04-23 21:32:03 -05:00
|
|
|
// returns 1 on failure, 0 on success
|
|
|
|
int update_finish()
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2018-04-30 13:13:24 -05:00
|
|
|
ByteString exeName = Platform::ExecutableName(), updName;
|
2018-03-13 21:13:15 -05:00
|
|
|
int timeout = 5, err;
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
2018-04-21 18:28:36 -05:00
|
|
|
printf("Update: Current EXE name: %s\n", exeName.c_str());
|
2012-08-18 06:44:07 -05:00
|
|
|
#endif
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
updName = exeName;
|
2018-04-30 13:13:24 -05:00
|
|
|
ByteString extension = exeName.substr(exeName.length() - 4);
|
2018-04-21 18:28:36 -05:00
|
|
|
if (extension == ".exe")
|
|
|
|
updName = exeName.substr(0, exeName.length() - 4);
|
|
|
|
updName = updName + "_upd.exe";
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
2018-04-21 18:28:36 -05:00
|
|
|
printf("Update: Temp EXE name: %s\n", updName.c_str());
|
2012-08-18 06:44:07 -05:00
|
|
|
#endif
|
|
|
|
|
2018-04-21 18:28:36 -05:00
|
|
|
while (!DeleteFile(updName.c_str()))
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
|
|
|
err = GetLastError();
|
|
|
|
if (err == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
2018-04-21 18:28:36 -05:00
|
|
|
printf("Update: Temp file not deleted\n");
|
2012-08-18 06:44:07 -05:00
|
|
|
#endif
|
2018-03-13 21:13:15 -05:00
|
|
|
// Old versions of powder toy name their update files with _update.exe, delete that upgrade file here
|
2018-04-21 18:28:36 -05:00
|
|
|
updName = exeName;
|
2018-04-30 13:13:24 -05:00
|
|
|
ByteString extension = exeName.substr(exeName.length() - 4);
|
2018-04-21 18:28:36 -05:00
|
|
|
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)
|
|
|
|
{
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Update: Delete timeout\n");
|
|
|
|
#endif
|
2012-06-21 09:49:32 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-23 21:32:03 -05:00
|
|
|
void update_cleanup()
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2012-06-21 09:49:32 -05:00
|
|
|
update_finish();
|
|
|
|
#endif
|
|
|
|
}
|