2014-07-10 08:53:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-07-10 08:53:23 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-07-10 08:53:23 +00:00
|
|
|
*/
|
|
|
|
#include <cstdio>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2021-05-03 07:21:07 +00:00
|
|
|
#include <sys/sendfile.h>
|
2014-07-10 08:53:23 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2014-07-26 07:33:00 +00:00
|
|
|
#include <pwd.h>
|
2014-07-10 08:53:23 +00:00
|
|
|
#include <string>
|
|
|
|
#include <deque>
|
2018-10-09 09:13:41 +00:00
|
|
|
#include <vector>
|
2014-07-10 08:53:23 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::deque;
|
2018-10-09 09:13:41 +00:00
|
|
|
using std::vector;
|
2014-07-10 08:53:23 +00:00
|
|
|
using std::cout;
|
|
|
|
|
2014-07-14 05:16:21 +00:00
|
|
|
bool do_mkdir(const char *path) { // from http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux
|
2017-08-15 17:12:57 +00:00
|
|
|
struct stat statbuf;
|
|
|
|
if (stat(path, &statbuf) != 0) {
|
|
|
|
/* Directory does not exist. EEXIST for race condition */
|
|
|
|
if (mkdir(path, S_IRWXU) != 0 && errno != EEXIST) return false;
|
|
|
|
} else if (!S_ISDIR(statbuf.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-14 05:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _debug = false;
|
2017-05-30 18:26:53 +00:00
|
|
|
string updaterDir;
|
|
|
|
string updaterName;
|
|
|
|
string workDir;
|
|
|
|
string exeName;
|
2017-08-15 17:12:57 +00:00
|
|
|
string exePath;
|
2014-07-14 05:16:21 +00:00
|
|
|
|
|
|
|
FILE *_logFile = 0;
|
|
|
|
void openLog() {
|
2017-08-15 17:12:57 +00:00
|
|
|
if (!_debug || _logFile) return;
|
2014-07-14 05:16:21 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
if (!do_mkdir((workDir + "DebugLogs").c_str())) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-14 05:16:21 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
time_t timer;
|
2014-07-14 05:16:21 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
time(&timer);
|
|
|
|
struct tm *t = localtime(&timer);
|
2014-07-14 05:16:21 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
static const int maxFileLen = 65536;
|
|
|
|
char logName[maxFileLen];
|
|
|
|
sprintf(logName, "%sDebugLogs/%04d%02d%02d_%02d%02d%02d_upd.txt", workDir.c_str(),
|
|
|
|
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
|
|
|
|
_logFile = fopen(logName, "w");
|
2014-07-14 05:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void closeLog() {
|
2017-08-15 17:12:57 +00:00
|
|
|
if (!_logFile) return;
|
2014-07-14 05:16:21 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
fclose(_logFile);
|
|
|
|
_logFile = 0;
|
2014-07-14 05:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeLog(const char *format, ...) {
|
2017-08-15 17:12:57 +00:00
|
|
|
if (!_logFile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(_logFile, format, args);
|
|
|
|
fprintf(_logFile, "\n");
|
|
|
|
fflush(_logFile);
|
|
|
|
va_end(args);
|
2014-07-14 05:16:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:16:20 +00:00
|
|
|
bool copyFile(const char *from, const char *to, bool writeprotected) {
|
2017-08-15 17:12:57 +00:00
|
|
|
FILE *ffrom = fopen(from, "rb"), *fto = fopen(to, "wb");
|
|
|
|
if (!ffrom) {
|
|
|
|
if (fto) fclose(fto);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!fto) {
|
|
|
|
fclose(ffrom);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat fst; // from http://stackoverflow.com/questions/5486774/keeping-fileowner-and-permissions-after-copying-file-in-c
|
|
|
|
//let's say this wont fail since you already worked OK on that fp
|
|
|
|
if (fstat(fileno(ffrom), &fst) != 0) {
|
|
|
|
fclose(ffrom);
|
|
|
|
fclose(fto);
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-11 09:11:46 +00:00
|
|
|
|
|
|
|
ssize_t copied = sendfile(
|
|
|
|
fileno(fto),
|
2021-05-03 08:43:23 +00:00
|
|
|
fileno(ffrom),
|
2021-03-11 09:11:46 +00:00
|
|
|
nullptr,
|
|
|
|
fst.st_size);
|
|
|
|
|
|
|
|
if (copied == -1) {
|
2021-05-03 07:21:07 +00:00
|
|
|
writeLog(
|
|
|
|
"Copy by sendfile '%s' to '%s' failed, error: %d, fallback now.",
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
int(errno));
|
2021-03-11 09:11:46 +00:00
|
|
|
static const int BufSize = 65536;
|
|
|
|
char buf[BufSize];
|
|
|
|
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
|
|
|
|
fwrite(buf, 1, size, fto);
|
|
|
|
}
|
2021-05-03 07:21:07 +00:00
|
|
|
} else {
|
|
|
|
writeLog(
|
|
|
|
"Copy by sendfile '%s' to '%s' done, size: %d, result: %d.",
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
int(fst.st_size),
|
|
|
|
int(copied));
|
2021-03-11 09:11:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
//update to the same uid/gid
|
2021-04-27 12:16:20 +00:00
|
|
|
if (!writeprotected && fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
|
2017-08-15 17:12:57 +00:00
|
|
|
fclose(ffrom);
|
|
|
|
fclose(fto);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//update the permissions
|
|
|
|
if (fchmod(fileno(fto), fst.st_mode) != 0) {
|
|
|
|
fclose(ffrom);
|
|
|
|
fclose(fto);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(ffrom);
|
|
|
|
fclose(fto);
|
|
|
|
|
|
|
|
return true;
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool remove_directory(const string &path) { // from http://stackoverflow.com/questions/2256945/removing-a-non-empty-directory-programmatically-in-c-or-c
|
2017-08-15 17:12:57 +00:00
|
|
|
DIR *d = opendir(path.c_str());
|
|
|
|
writeLog("Removing dir '%s'", path.c_str());
|
|
|
|
|
|
|
|
if (!d) {
|
|
|
|
writeLog("Could not open dir '%s'", path.c_str());
|
|
|
|
return (errno == ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (struct dirent *p = readdir(d)) {
|
|
|
|
/* Skip the names "." and ".." as we don't want to recurse on them. */
|
|
|
|
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) continue;
|
|
|
|
|
|
|
|
string fname = path + '/' + p->d_name;
|
|
|
|
struct stat statbuf;
|
|
|
|
writeLog("Trying to get stat() for '%s'", fname.c_str());
|
|
|
|
if (!stat(fname.c_str(), &statbuf)) {
|
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
if (!remove_directory(fname.c_str())) {
|
|
|
|
closedir(d);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog("Unlinking file '%s'", fname.c_str());
|
|
|
|
if (unlink(fname.c_str())) {
|
|
|
|
writeLog("Failed to unlink '%s'", fname.c_str());
|
|
|
|
closedir(d);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog("Failed to call stat() on '%s'", fname.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
writeLog("Finally removing dir '%s'", path.c_str());
|
|
|
|
return !rmdir(path.c_str());
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mkpath(const char *path) {
|
2017-08-15 17:12:57 +00:00
|
|
|
int status = 0, pathsize = strlen(path) + 1;
|
|
|
|
char *copypath = new char[pathsize];
|
|
|
|
memcpy(copypath, path, pathsize);
|
|
|
|
|
|
|
|
char *pp = copypath, *sp;
|
|
|
|
while (status == 0 && (sp = strchr(pp, '/')) != 0) {
|
|
|
|
if (sp != pp) {
|
|
|
|
/* Neither root nor double slash in path */
|
|
|
|
*sp = '\0';
|
|
|
|
if (!do_mkdir(copypath)) {
|
|
|
|
delete[] copypath;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*sp = '/';
|
|
|
|
}
|
|
|
|
pp = sp + 1;
|
|
|
|
}
|
|
|
|
delete[] copypath;
|
|
|
|
return do_mkdir(path);
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool equal(string a, string b) {
|
2017-08-15 17:12:57 +00:00
|
|
|
std::transform(a.begin(), a.end(), a.begin(), ::tolower);
|
|
|
|
std::transform(b.begin(), b.end(), b.begin(), ::tolower);
|
|
|
|
return a == b;
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void delFolder() {
|
2017-08-15 17:12:57 +00:00
|
|
|
string delPathOld = workDir + "tupdates/ready", delPath = workDir + "tupdates/temp", delFolder = workDir + "tupdates";
|
|
|
|
writeLog("Fully clearing old path '%s'..", delPathOld.c_str());
|
|
|
|
if (!remove_directory(delPathOld)) {
|
|
|
|
writeLog("Failed to clear old path! :( New path was used?..");
|
|
|
|
}
|
2015-06-03 18:13:01 +00:00
|
|
|
writeLog("Fully clearing path '%s'..", delPath.c_str());
|
|
|
|
if (!remove_directory(delPath)) {
|
|
|
|
writeLog("Error: failed to clear path! :(");
|
|
|
|
}
|
|
|
|
rmdir(delFolder.c_str());
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:16:20 +00:00
|
|
|
bool update(bool writeprotected) {
|
2017-08-15 17:12:57 +00:00
|
|
|
writeLog("Update started..");
|
2014-07-10 08:53:23 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
string updDir = workDir + "tupdates/temp", readyFilePath = workDir + "tupdates/temp/ready", tdataDir = workDir + "tupdates/temp/tdata";
|
2015-06-03 18:13:01 +00:00
|
|
|
{
|
|
|
|
FILE *readyFile = fopen(readyFilePath.c_str(), "rb");
|
|
|
|
if (readyFile) {
|
|
|
|
fclose(readyFile);
|
2017-08-15 17:12:57 +00:00
|
|
|
writeLog("Ready file found! Using new path '%s'..", updDir.c_str());
|
|
|
|
} else {
|
|
|
|
updDir = workDir + "tupdates/ready"; // old
|
|
|
|
tdataDir = workDir + "tupdates/ready/tdata";
|
|
|
|
writeLog("Ready file not found! Using old path '%s'..", updDir.c_str());
|
|
|
|
}
|
2015-06-03 18:13:01 +00:00
|
|
|
}
|
2014-07-10 08:53:23 +00:00
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
deque<string> dirs;
|
2014-07-10 08:53:23 +00:00
|
|
|
dirs.push_back(updDir);
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
deque<string> from, to, forcedirs;
|
2014-07-10 08:53:23 +00:00
|
|
|
|
|
|
|
do {
|
2017-08-15 17:12:57 +00:00
|
|
|
string dir = dirs.front();
|
2014-07-10 08:53:23 +00:00
|
|
|
dirs.pop_front();
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
string toDir = exePath;
|
2014-07-10 08:53:23 +00:00
|
|
|
if (dir.size() > updDir.size() + 1) {
|
2017-08-15 17:12:57 +00:00
|
|
|
toDir += (dir.substr(updDir.size() + 1) + '/');
|
2014-07-10 08:53:23 +00:00
|
|
|
forcedirs.push_back(toDir);
|
2017-08-15 17:12:57 +00:00
|
|
|
writeLog("Parsing dir '%s' in update tree..", toDir.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *d = opendir(dir.c_str());
|
|
|
|
if (!d) {
|
|
|
|
writeLog("Failed to open dir %s", dir.c_str());
|
|
|
|
return false;
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
while (struct dirent *p = readdir(d)) {
|
|
|
|
/* Skip the names "." and ".." as we don't want to recurse on them. */
|
|
|
|
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) continue;
|
|
|
|
|
|
|
|
string fname = dir + '/' + p->d_name;
|
|
|
|
struct stat statbuf;
|
|
|
|
if (fname.substr(0, tdataDir.size()) == tdataDir && (fname.size() <= tdataDir.size() || fname.at(tdataDir.size()) == '/')) {
|
|
|
|
writeLog("Skipping 'tdata' path '%s'", fname.c_str());
|
|
|
|
} else if (!stat(fname.c_str(), &statbuf)) {
|
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
dirs.push_back(fname);
|
|
|
|
writeLog("Added dir '%s' in update tree..", fname.c_str());
|
|
|
|
} else {
|
|
|
|
string tofname = exePath + fname.substr(updDir.size() + 1);
|
|
|
|
if (equal(tofname, updaterName)) { // bad update - has Updater - delete all dir
|
|
|
|
writeLog("Error: bad update, has Updater! '%s' equal '%s'", tofname.c_str(), updaterName.c_str());
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
} else if (equal(tofname, exePath + "Telegram") && exeName != "Telegram") {
|
|
|
|
string fullBinaryPath = exePath + exeName;
|
2017-05-30 18:26:53 +00:00
|
|
|
writeLog("Target binary found: '%s', changing to '%s'", tofname.c_str(), fullBinaryPath.c_str());
|
|
|
|
tofname = fullBinaryPath;
|
|
|
|
}
|
2015-06-03 18:13:01 +00:00
|
|
|
if (fname == readyFilePath) {
|
|
|
|
writeLog("Skipped ready file '%s'", fname.c_str());
|
2017-08-15 17:12:57 +00:00
|
|
|
} else {
|
2015-06-03 18:13:01 +00:00
|
|
|
from.push_back(fname);
|
|
|
|
to.push_back(tofname);
|
|
|
|
writeLog("Added file '%s' to be copied to '%s'", fname.c_str(), tofname.c_str());
|
|
|
|
}
|
2017-08-15 17:12:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog("Could not get stat() for file %s", fname.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
2014-07-10 08:53:23 +00:00
|
|
|
} while (!dirs.empty());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < forcedirs.size(); ++i) {
|
2017-08-15 17:12:57 +00:00
|
|
|
string forcedir = forcedirs[i];
|
|
|
|
writeLog("Forcing dir '%s'..", forcedir.c_str());
|
|
|
|
if (!forcedir.empty() && !mkpath(forcedir.c_str())) {
|
|
|
|
writeLog("Error: failed to create dir '%s'..", forcedir.c_str());
|
|
|
|
delFolder();
|
|
|
|
return false;
|
2014-07-10 08:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < from.size(); ++i) {
|
2017-08-15 17:12:57 +00:00
|
|
|
string fname = from[i], tofname = to[i];
|
2020-05-26 12:13:38 +00:00
|
|
|
|
|
|
|
// it is necessary to remove the old file to not to get an error if appimage file is used by fuse
|
|
|
|
struct stat statbuf;
|
|
|
|
writeLog("Trying to get stat() for '%s'", tofname.c_str());
|
|
|
|
if (!stat(tofname.c_str(), &statbuf)) {
|
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
writeLog("Fully clearing path '%s'..", tofname.c_str());
|
|
|
|
if (!remove_directory(tofname.c_str())) {
|
|
|
|
writeLog("Error: failed to clear path '%s'", tofname.c_str());
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog("Unlinking file '%s'", tofname.c_str());
|
|
|
|
if (unlink(tofname.c_str())) {
|
|
|
|
writeLog("Error: failed to unlink '%s'", tofname.c_str());
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
writeLog("Copying file '%s' to '%s'..", fname.c_str(), tofname.c_str());
|
|
|
|
int copyTries = 0, triesLimit = 30;
|
|
|
|
do {
|
2021-04-27 12:16:20 +00:00
|
|
|
if (!copyFile(fname.c_str(), tofname.c_str(), writeprotected)) {
|
2017-08-15 17:12:57 +00:00
|
|
|
++copyTries;
|
|
|
|
usleep(100000);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (copyTries < triesLimit);
|
|
|
|
if (copyTries == triesLimit) {
|
|
|
|
writeLog("Error: failed to copy, asking to retry..");
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeLog("Update succeed! Clearing folder..");
|
|
|
|
delFolder();
|
2014-07-10 08:53:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
string CurrentExecutablePath(int argc, char *argv[]) {
|
|
|
|
constexpr auto kMaxPath = 1024;
|
|
|
|
char result[kMaxPath] = { 0 };
|
|
|
|
auto count = readlink("/proc/self/exe", result, kMaxPath);
|
|
|
|
if (count > 0) {
|
|
|
|
return string(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to the first command line argument.
|
|
|
|
return argc ? string(argv[0]) : string();
|
|
|
|
}
|
|
|
|
|
2014-07-10 08:53:23 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2017-12-12 07:52:53 +00:00
|
|
|
bool needupdate = true;
|
|
|
|
bool autostart = false;
|
|
|
|
bool debug = false;
|
2021-04-27 12:16:20 +00:00
|
|
|
bool writeprotected = false;
|
2017-12-12 07:52:53 +00:00
|
|
|
bool tosettings = false;
|
|
|
|
bool startintray = false;
|
|
|
|
bool customWorkingDir = false;
|
|
|
|
|
|
|
|
char *key = 0;
|
|
|
|
char *workdir = 0;
|
2017-08-15 17:12:57 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (equal(argv[i], "-noupdate")) {
|
|
|
|
needupdate = false;
|
|
|
|
} else if (equal(argv[i], "-autostart")) {
|
|
|
|
autostart = true;
|
2015-01-27 16:58:58 +00:00
|
|
|
} else if (equal(argv[i], "-debug")) {
|
|
|
|
debug = _debug = true;
|
|
|
|
} else if (equal(argv[i], "-startintray")) {
|
|
|
|
startintray = true;
|
2017-08-15 17:12:57 +00:00
|
|
|
} else if (equal(argv[i], "-tosettings")) {
|
|
|
|
tosettings = true;
|
2017-12-12 07:52:53 +00:00
|
|
|
} else if (equal(argv[i], "-workdir_custom")) {
|
|
|
|
customWorkingDir = true;
|
2021-05-03 10:21:17 +00:00
|
|
|
} else if (equal(argv[i], "-writeprotected")) {
|
|
|
|
writeprotected = true;
|
2017-08-15 17:12:57 +00:00
|
|
|
} else if (equal(argv[i], "-key") && ++i < argc) {
|
|
|
|
key = argv[i];
|
|
|
|
} else if (equal(argv[i], "-workpath") && ++i < argc) {
|
2017-12-12 07:52:53 +00:00
|
|
|
workDir = workdir = argv[i];
|
2017-05-30 18:26:53 +00:00
|
|
|
} else if (equal(argv[i], "-exename") && ++i < argc) {
|
|
|
|
exeName = argv[i];
|
2017-08-15 17:12:57 +00:00
|
|
|
} else if (equal(argv[i], "-exepath") && ++i < argc) {
|
|
|
|
exePath = argv[i];
|
2016-01-30 18:24:42 +00:00
|
|
|
}
|
2017-08-15 17:12:57 +00:00
|
|
|
}
|
2017-05-30 18:26:53 +00:00
|
|
|
if (exeName.empty() || exeName.find('/') != string::npos) {
|
|
|
|
exeName = "Telegram";
|
|
|
|
}
|
|
|
|
openLog();
|
2015-06-04 10:50:53 +00:00
|
|
|
|
2018-10-09 19:07:38 +00:00
|
|
|
writeLog("Updater started, new argments formatting..");
|
2017-08-15 17:12:57 +00:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
writeLog("Argument: '%s'", argv[i]);
|
|
|
|
}
|
|
|
|
if (needupdate) writeLog("Need to update!");
|
|
|
|
if (autostart) writeLog("From autostart!");
|
2021-04-27 12:16:20 +00:00
|
|
|
if (writeprotected) writeLog("Write Protected folder!");
|
2017-08-15 17:12:57 +00:00
|
|
|
|
|
|
|
updaterName = CurrentExecutablePath(argc, argv);
|
|
|
|
writeLog("Updater binary full path is: %s", updaterName.c_str());
|
|
|
|
if (exePath.empty()) {
|
|
|
|
writeLog("Executable path is not specified :(");
|
|
|
|
} else {
|
|
|
|
writeLog("Executable path: %s", exePath.c_str());
|
|
|
|
}
|
|
|
|
if (updaterName.size() >= 7) {
|
|
|
|
if (equal(updaterName.substr(updaterName.size() - 7), "Updater")) {
|
2017-05-30 18:26:53 +00:00
|
|
|
updaterDir = updaterName.substr(0, updaterName.size() - 7);
|
2017-08-15 17:12:57 +00:00
|
|
|
writeLog("Updater binary dir is: %s", updaterDir.c_str());
|
|
|
|
if (exePath.empty()) {
|
|
|
|
exePath = updaterDir;
|
|
|
|
writeLog("Using updater binary dir.", exePath.c_str());
|
|
|
|
}
|
|
|
|
if (needupdate) {
|
|
|
|
if (workDir.empty()) { // old app launched, update prepared in tupdates/ready (not in tupdates/temp)
|
2017-12-12 07:52:53 +00:00
|
|
|
customWorkingDir = false;
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
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 = exePath;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2021-04-27 12:16:20 +00:00
|
|
|
update(writeprotected);
|
2017-08-15 17:12:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog("Error: bad exe name!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog("Error: short exe name!");
|
|
|
|
}
|
|
|
|
|
2018-10-09 09:13:41 +00:00
|
|
|
auto fullBinaryPath = exePath + exeName;
|
|
|
|
const auto path = fullBinaryPath.c_str();
|
|
|
|
|
2018-10-09 19:07:38 +00:00
|
|
|
auto values = vector<string>();
|
|
|
|
const auto push = [&](string arg) {
|
|
|
|
// Force null-terminated .data() call result.
|
|
|
|
values.push_back(arg + char(0));
|
2018-10-09 09:13:41 +00:00
|
|
|
};
|
|
|
|
push(path);
|
|
|
|
push("-noupdate");
|
|
|
|
if (autostart) push("-autostart");
|
|
|
|
if (debug) push("-debug");
|
|
|
|
if (startintray) push("-startintray");
|
|
|
|
if (tosettings) push("-tosettings");
|
2017-12-12 07:52:53 +00:00
|
|
|
if (key) {
|
2018-10-09 09:13:41 +00:00
|
|
|
push("-key");
|
|
|
|
push(key);
|
2017-12-12 07:52:53 +00:00
|
|
|
}
|
|
|
|
if (customWorkingDir && workdir) {
|
2018-10-09 09:13:41 +00:00
|
|
|
push("-workdir");
|
|
|
|
push(workdir);
|
2016-01-30 18:24:42 +00:00
|
|
|
}
|
2018-10-09 09:13:41 +00:00
|
|
|
|
2018-10-09 19:07:38 +00:00
|
|
|
auto args = vector<char*>();
|
|
|
|
for (auto &arg : values) {
|
|
|
|
args.push_back(arg.data());
|
|
|
|
}
|
|
|
|
args.push_back(nullptr);
|
|
|
|
|
2021-05-03 10:21:17 +00:00
|
|
|
// let the parent launch instead
|
|
|
|
if (!writeprotected) {
|
|
|
|
pid_t pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case -1:
|
|
|
|
writeLog("fork() failed!");
|
|
|
|
return 1;
|
|
|
|
case 0:
|
|
|
|
execv(args[0], args.data());
|
|
|
|
return 1;
|
|
|
|
}
|
2021-04-27 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:12:57 +00:00
|
|
|
writeLog("Executed Telegram, closing log and quitting..");
|
2014-07-10 08:53:23 +00:00
|
|
|
closeLog();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|