SysCheck-ModMii-Edition/source/update.c

121 lines
2.5 KiB
C
Raw Normal View History

2013-10-12 20:38:34 +02:00
#include <gccore.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <network.h>
#include <dirent.h>
#include <unistd.h>
2013-10-12 20:38:34 +02:00
#include "update.h"
#include "gecko.h"
#include "http.h"
#include "ssl.h"
#include "tools.h"
2013-10-12 20:38:34 +02:00
s32 downloadSyscheckFile(const char* update_dir, const char* fileName) {
2013-10-12 20:38:34 +02:00
int ret = 0;
char buf[128] = {0};
2013-10-12 20:38:34 +02:00
u32 http_status;
u8* outbuf;
u32 length;
snprintf(buf, sizeof(buf), "https://sourceforge.net/p/syscheck-hde/code/HEAD/tree/trunk/SysCheckHDE/%s?format=raw", fileName);
2013-10-12 20:38:34 +02:00
ret = http_request(buf, 1 << 31);
if (!ret)
2013-10-12 20:38:34 +02:00
{
int i;
for (i = 0; i < 10; i++) {
ret = http_request(buf, 1 << 31);
if (ret) break;
if (i >= 10) {
gprintf("Error making http request\n");
return -1;
}
}
}
ret = http_get_result(&http_status, &outbuf, &length);
if (((int)*outbuf & 0xF0000000) == 0xF0000000)
2013-10-12 20:38:34 +02:00
{
return -2;
}
sprintf(buf, "%s%s", update_dir, fileName);
2013-10-12 20:38:34 +02:00
FILE *file = fopen(buf, "w");
if(!file)
{
gprintf("File Error\n");
return -3;
} else {
fwrite(outbuf, length, 1, file);
2013-10-12 20:38:34 +02:00
fclose(file);
}
free(outbuf);
return 0;
}
s32 updateApp(void) {
int ret = net_init();
ssl_init();
char update_dir[21];
sprintf(update_dir, "%s:/apps/SysCheckHDE", arguments.USB ? "usb" : "sd");
mkdir("/apps",S_IWRITE|S_IREAD); // attempt to make dir
mkdir("/apps/SysCheckHDE",S_IWRITE|S_IREAD); // attempt to make dir
chdir(update_dir);
if (ret < 0) {
net_deinit();
return ret;
}
2013-10-12 20:38:34 +02:00
u32 http_status;
u8* outbuf;
2013-10-12 20:56:41 +02:00
u32 length;
ret = http_request("https://sourceforge.net/p/syscheck-hde/code/HEAD/tree/trunk/Version.txt?format=raw", 1 << 31);
if (!ret)
2013-10-12 20:38:34 +02:00
{
gprintf("Error making http request\n");
return -1;
}
ret = http_get_result(&http_status, &outbuf, &length);
if (!strncmp((char*)outbuf, "Version=", sizeof("Version=")))
2013-10-12 20:38:34 +02:00
{
int version = atoi((char*)(outbuf + sizeof("Version=")));
2013-10-12 20:38:34 +02:00
gprintf("INT: %i\n", version);
2013-10-12 20:38:34 +02:00
if (version > REVISION) {
ret = downloadSyscheckFile(update_dir, "boot.dol");
if (ret < 0) {
net_deinit();
return ret;
}
ret = downloadSyscheckFile(update_dir, "meta.xml");
if (ret < 0) {
net_deinit();
return ret;
}
ret = downloadSyscheckFile(update_dir, "icon.png");
if (ret < 0) {
net_deinit();
return ret;
}
2013-10-12 20:38:34 +02:00
} else {
net_deinit();
return 2;
2013-10-12 20:38:34 +02:00
}
2013-10-12 20:38:34 +02:00
} else {
net_deinit();
return -3;
2013-10-12 20:38:34 +02:00
}
net_deinit();
return ret;
}