2012-06-21 09:49:32 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
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
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
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 "Update.h"
|
|
|
|
#include "Platform.h"
|
2012-06-21 09:49:32 -05:00
|
|
|
|
2015-01-16 16:26:04 -06:00
|
|
|
int update_start(char *data, unsigned int len)
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
2015-08-31 22:33:40 -05:00
|
|
|
char *self = Platform::ExecutableName(), *temp;
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2012-06-21 09:49:32 -05:00
|
|
|
char *p;
|
|
|
|
#endif
|
|
|
|
FILE *f;
|
|
|
|
int res = 1;
|
|
|
|
|
|
|
|
if (!self)
|
|
|
|
return 1;
|
|
|
|
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2012-06-21 09:49:32 -05:00
|
|
|
temp = (char*)malloc(strlen(self)+12);
|
|
|
|
strcpy(temp, self);
|
|
|
|
p = temp + strlen(temp) - 4;
|
|
|
|
if (_stricmp(p, ".exe"))
|
|
|
|
p += 4;
|
|
|
|
strcpy(p, "_update.exe");
|
|
|
|
|
|
|
|
if (!MoveFile(self, temp))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
f = fopen(self, "wb");
|
|
|
|
if (!f)
|
|
|
|
goto fail;
|
|
|
|
if (fwrite(data, 1, len, f) != len)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
DeleteFile(self);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2012-08-01 14:28:43 -05:00
|
|
|
if ((uintptr_t)ShellExecute(NULL, "open", self, NULL, NULL, SW_SHOWNORMAL) <= 32)
|
2012-06-21 09:49:32 -05:00
|
|
|
{
|
|
|
|
DeleteFile(self);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
2012-06-22 11:00:00 -05:00
|
|
|
temp = (char*)malloc(strlen(self)+8);
|
2012-06-21 09:49:32 -05:00
|
|
|
strcpy(temp, self);
|
|
|
|
strcat(temp, "-update");
|
|
|
|
|
|
|
|
f = fopen(temp, "w");
|
|
|
|
if (!f)
|
|
|
|
goto fail;
|
|
|
|
if (fwrite(data, 1, len, f) != len)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
unlink(temp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (chmod(temp, 0755))
|
|
|
|
{
|
|
|
|
unlink(temp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(temp, self))
|
|
|
|
{
|
|
|
|
unlink(temp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
execl(self, "powder-update", NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(temp);
|
|
|
|
free(self);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int update_finish(void)
|
|
|
|
{
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2015-08-31 22:33:40 -05:00
|
|
|
char *temp, *self = Platform::ExecutableName(), *p;
|
2012-06-21 09:49:32 -05:00
|
|
|
int timeout = 60, err;
|
|
|
|
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Update: Current EXE name: %s\n", self);
|
|
|
|
#endif
|
|
|
|
|
2012-06-21 09:49:32 -05:00
|
|
|
temp = (char*)malloc(strlen(self)+12);
|
|
|
|
strcpy(temp, self);
|
|
|
|
p = temp + strlen(temp) - 4;
|
|
|
|
if (_stricmp(p, ".exe"))
|
|
|
|
p += 4;
|
|
|
|
strcpy(p, "_update.exe");
|
|
|
|
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Update: Temp EXE name: %s\n", temp);
|
|
|
|
#endif
|
|
|
|
|
2012-06-21 09:49:32 -05:00
|
|
|
while (!DeleteFile(temp))
|
|
|
|
{
|
|
|
|
err = GetLastError();
|
|
|
|
if (err == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
2012-08-18 06:44:07 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Update: Temp file deleted\n");
|
|
|
|
#endif
|
2012-06-21 09:49:32 -05:00
|
|
|
free(temp);
|
|
|
|
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
|
|
|
free(temp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(temp);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_cleanup(void)
|
|
|
|
{
|
2012-07-29 11:14:14 -05:00
|
|
|
#ifdef WIN
|
2012-06-21 09:49:32 -05:00
|
|
|
update_finish();
|
|
|
|
#endif
|
|
|
|
}
|