kolibrios-gitea/programs/cmm/misc/osupdate.c

195 lines
5.4 KiB
C
Raw Normal View History

#define MEMSIZE 1024*50
#include "../lib/gui.h"
#include "../lib/copyf.h"
#include "../lib/obj/libini.h"
#include "../lib/obj/libimg.h"
#include "../lib/obj/http.h"
#include "../lib/obj/network.h"
#include "../lib/patterns/restart_process.h"
#ifndef AUTOBUILD
#include "lang.h--"
#endif
bool install_complete = false;
_http http;
#define WINW 460
#define WINH 380
//#define LANG_RUS 1
#ifdef LANG_RUS
#define T_WINDOW_TITLE "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> KolibriOS"
#define T_TITLE_H1 "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
#define T_INTRO "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E0A8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ᪠砥<E1AAA0> <20><><EFBFBD><E1ABA5><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> KolibriOS <20> <20><><EFBFBD><E1AFA0><EFBFBD><EFBFBD> <20><> <20><> RAM-<2D><><EFBFBD><EFBFBD>. <20><><EFBFBD> <20><20><><EFBFBD><EFBFBD> <20><> <20><EFBFBD> <20><><EFBFBD><E0A5A7><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><E5AEA4><EFBFBD> <20><><EFBFBD><EFBFBD><E0A0AD><EFBFBD> <20><><20> <20><><EFBFBD><E0A5A7><EFBFBD><EFBFBD><E3A7A8><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><E0AEA9> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E0A8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><20><><EFBFBD><E7A0AB> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> RAM-<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!";
#define T_INSTALL "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
#define T_COMPLETE "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
#define T_EXIT "<22><>室"
#define IMG_URL "http://builds.kolibrios.org/rus/data/data/kolibri.img"
#define KS "<22><><EFBFBD><EFBFBD><E0A0AD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><E0AEA9>"
#define RA "<22><><EFBFBD><E0A5A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20>ணࠬ<E0AEA3><E0A0AC>"
#else
#define T_WINDOW_TITLE "KolibriOS Online Updater"
#define T_TITLE_H1 "ONLINE UPDATE"
#define T_INTRO "This app will download the latest KolibriOS dirsto and update your RAM-disk with it. Kernel won't be restarted.
Please close all opened apps before start.
Note that all changes on RAM-disk will be lost.";
#define T_INSTALL "Update"
#define T_COMPLETE "Update complete"
#define T_EXIT "Exit"
#define IMG_URL "http://builds.kolibrios.org/eng/data/data/kolibri.img"
#define KS "Keep settings folder"
#define RA "Restart all apps"
#endif
char accept_language[]="en"; //not used, necessary for http.get()
void Operation_Draw_Progress(dword f) {} //not used, necessary for copyf()
checkbox keep_settings = { KS, true };
checkbox restart_apps = { RA, true };
void main()
{
int btn;
sensor progress;
load_dll(libimg, #libimg_init,1);
load_dll(libHTTP, #http_lib_init,1);
@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_STACK);
loop() switch(@WaitEventTimeout(300))
{
case evButton:
btn = @GetButtonID();
if (btn<=2) ExitProcess();
if (btn==9) goto _INSTALL;
keep_settings.click(btn);
restart_apps.click(btn);
break;
case evKey:
switch (@GetKeyScancode()) {
case SCAN_CODE_ESC: ExitProcess();
case SCAN_CODE_ENTER:
if (install_complete) ExitProcess();
else {
_INSTALL:
http.get(IMG_URL);
goto _DRAW_WINDOW;
}
}
break;
case evReDraw:
_DRAW_WINDOW:
sc.get();
DefineAndDrawWindow(screen.width-WINW/2,screen.height-WINH/2,
WINW+9,WINH+skin_height,0x34,sc.work,T_WINDOW_TITLE,0);
WriteText(30, 20, 0x81, 0xEC008C, T_TITLE_H1);
if (!install_complete) {
DrawTextViewArea(30, 50, WINW-60, WINH-80, T_INTRO, -1, sc.work_text);
progress.set_size(30, WINH-130, WINW-60, 20);
if (http.transfer<=0) {
DrawCaptButton(WINW-110/2, WINH-70, 110, 28, 9, 0x0092D8, 0xFFFfff, T_INSTALL);
keep_settings.draw(30, WINH - 210);
restart_apps.draw(30, WINH - 185);
}
} else {
DrawIcon32(WINW-32/2, 140, sc.work, 49);
WriteTextCenter(0,185, WINW, sc.work_text, T_COMPLETE);
DrawCaptButton(WINW-110/2, WINH-70, 110, 28, 2,
0x0092D8, 0xFFFfff, T_EXIT);
}
case evNetwork:
if (http.transfer <= 0) break;
http.receive();
if (http.content_length) {
progress.draw_progress(http.content_length - http.content_received
* progress.w / http.content_length);
}
if (!http.receive_result) {
CreateFile(http.content_received,
http.content_pointer, "/tmp0/1/latest.img");
http.stop();
EventDownloadComplete();
}
}
}
dword GetFreeSpaceOfRamdisk()
{
dword rdempty = malloc(1440*1024);
CreateFile(0, 1440*1024, rdempty, "/rd/1/rdempty");
free(rdempty);
rdempty = get_file_size("/rd/1/rdempty");
DeleteFile("/rd/1/rdempty");
return rdempty;
}
signed CheckFreeSpace(dword _latest, _combined)
{
dword cur_size, new_size, empty;
DIR_SIZE dir_size;
dir_size.get("/sys");
cur_size = dir_size.bytes;
copyf("/sys", _combined);
copyf(_latest, _combined);
dir_size.get(_combined);
new_size = dir_size.bytes;
empty = GetFreeSpaceOfRamdisk();
return cur_size + empty - new_size / 1024;
}
void EventDownloadComplete()
{
dword unimg_id, slot_n;
signed space_delta;
int i=0;
char osupdate[32];
char latest[40];
char backup[40];
char combined[40];
char exract_param[64];
char backup_settings[64];
do { sprintf(#osupdate, "/tmp0/1/osupdate%d", i); i++;
} while (dir_exists(#osupdate));
CreateDir(#osupdate);
sprintf(#latest, "%s/latest", #osupdate);
sprintf(#backup, "%s/rdbackup", #osupdate);
sprintf(#combined, "%s/combined", #osupdate);
sprintf(#backup_settings, "%s/settings", #backup);
sprintf(#exract_param, "/tmp0/1/latest.img %s -e", #latest);
unimg_id = RunProgram("/sys/unimg", #exract_param);
do {
slot_n = GetProcessSlot(unimg_id);
pause(10);
} while (slot_n!=0);
space_delta = CheckFreeSpace(#latest, #combined);
if (space_delta<0) {
sprintf(#param, "'Not enought free space! You need %d Kb more.'E", -space_delta);
notify(#param);
} else {
copyf("/sys", #backup);
copyf(#latest, "/sys");
if (keep_settings.checked) copyf(#backup_settings, "/rd/1/settings");
if (restart_apps.checked) RestartAllProcess();
install_complete = true;
}
}