fixed Updater for Linux version (working with home dir data)

This commit is contained in:
John Preston 2014-07-26 11:33:00 +04:00
parent a4e49a1f52
commit 905d3b6a85
2 changed files with 48 additions and 8 deletions

View File

@ -21,6 +21,7 @@ Copyright (c) 2014 John Preston, https://tdesktop.com
#include <cstdlib>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
#include <string>
#include <deque>
#include <cstring>
@ -191,7 +192,7 @@ bool mkpath(const char *path) {
return do_mkdir(path);
}
string exeName, exeDir;
string exeName, exeDir, workDir;
bool equal(string a, string b) {
std::transform(a.begin(), a.end(), a.begin(), ::tolower);
@ -200,7 +201,7 @@ bool equal(string a, string b) {
}
void delFolder() {
string delPath = "tupdates/ready", delFolder = "tupdates";
string delPath = workDir + "tupdates/ready", delFolder = workDir + "tupdates";
writeLog("Fully clearing path '%s'..", delPath.c_str());
if (!remove_directory(delPath)) {
writeLog("Error: failed to clear path! :(");
@ -211,7 +212,7 @@ void delFolder() {
bool update() {
writeLog("Update started..");
string updDir = "tupdates/ready";
string updDir = workDir + "tupdates/ready";
deque<string> dirs;
dirs.push_back(updDir);
@ -293,16 +294,17 @@ bool update() {
}
writeLog("Update succeed! Clearing folder..");
delFolder();
delFolder();
return true;
}
int main(int argc, char *argv[]) {
openLog();
openLog();
writeLog("Updater started..");
bool needupdate = true, autostart = false, debug = false, tosettings = false;
char *key = 0;
for (int i = 1; i < argc; ++i) {
if (equal(argv[i], "-noupdate")) {
@ -316,6 +318,8 @@ int main(int argc, char *argv[]) {
tosettings = true;
} else if (equal(argv[i], "-key") && ++i < argc) {
key = argv[i];
} else if (equal(argv[i], "-workpath") && ++i < argc) {
workDir = argv[i];
}
}
if (needupdate) writeLog("Need to update!");
@ -328,6 +332,37 @@ int main(int argc, char *argv[]) {
exeDir = exeName.substr(0, exeName.size() - 7);
writeLog("Exe dir is: %s", exeDir.c_str());
if (needupdate) {
if (workDir.empty()) { // old app launched
writeLog("No workdir, trying to figure it out");
struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_dir && strlen(pw->pw_dir)) {
string tryDir = pw->pw_dir + string("/.TelegramDesktop/");
struct stat statbuf;
writeLog("Trying to use '%s' as workDir, getting stat() for tupdates/ready", tryDir.c_str());
if (!stat((tryDir + "tupdates/ready").c_str(), &statbuf)) {
writeLog("Stat got");
if (S_ISDIR(statbuf.st_mode)) {
writeLog("It is directory, using home work dir");
workDir = tryDir;
}
}
}
if (workDir.empty()) {
workDir = exeDir;
struct stat statbuf;
writeLog("Trying to use current as workDir, getting stat() for tupdates/ready");
if (!stat("tupdates/ready", &statbuf)) {
writeLog("Stat got");
if (S_ISDIR(statbuf.st_mode)) {
writeLog("It is directory, using current dir");
workDir = string();
}
}
}
} else {
writeLog("Passed workpath is '%s'", workDir.c_str());
}
update();
}
} else {

View File

@ -923,7 +923,6 @@ bool psCheckReadyUpdate() {
return false;
}
#elif defined Q_OS_LINUX
QFileInfo to(curUpdater);
if (!moveFile(updater.absoluteFilePath().toUtf8().constData(), curUpdater.toUtf8().constData())) {
PsUpdateDownloader::clearAll();
return false;
@ -957,8 +956,8 @@ bool _execUpdater(bool update = true) {
QByteArray data((cExeDir() + "Updater").toUtf8());
memcpy(path, data.constData(), data.size());
char *args[MaxArgsCount] = {0}, p_noupdate[] = "-noupdate", p_autostart[] = "-autostart", p_debug[] = "-debug", p_tosettings[] = "-tosettings", p_key[] = "-key";
char p_datafile[MaxLen] = {0};
char *args[MaxArgsCount] = {0}, p_noupdate[] = "-noupdate", p_autostart[] = "-autostart", p_debug[] = "-debug", p_tosettings[] = "-tosettings", p_key[] = "-key", p_path[] = "-workpath";
char p_datafile[MaxLen] = {0}, p_pathbuf[MaxLen] = {0};
int argIndex = 0;
args[argIndex++] = path;
if (!update) {
@ -975,6 +974,12 @@ bool _execUpdater(bool update = true) {
args[argIndex++] = p_datafile;
}
}
QByteArray pathf = cWorkingDir().toLocal8Bit();
if (pathf.size() < MaxLen) {
memcpy(p_pathbuf, pathf.constData(), pathf.size());
args[argIndex++] = p_path;
args[argIndex++] = p_pathbuf;
}
pid_t pid = fork();
switch (pid) {