Skip to content

Commit

Permalink
Merge pull request #617 from matlo/fix-remove
Browse files Browse the repository at this point in the history
Handle temp folder with non-ascii chars
  • Loading branch information
Mathieu Laurendeau authored Mar 24, 2019
2 parents 0448cde + d85e61b commit cb44734
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 35 deletions.
20 changes: 11 additions & 9 deletions core/gimx.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,17 @@ int main(int argc, char *argv[])
#ifndef WIN32
char * file = "/tmp/gimx.status";
#else
char file[MAX_PATH];
int ret = GetTempPath(sizeof(file), file);
if (ret > 0 && (unsigned int) ret < MAX_PATH - sizeof("/gimx.status"))
char file[MAX_PATH + 1] = {};
wchar_t wtmp[MAX_PATH + 1];
int ret = GetTempPathW(MAX_PATH, wtmp);
if (ret > 0)
{
strcat(file, "/gimx.status");
}
else
{
file[0] = '\0';
char * tmp = utf16le_to_utf8(wtmp);
if (strlen(tmp) + sizeof("/gimx.status") <= sizeof(file))
{
sprintf(file, "%s/gimx.status", tmp);
}
free(tmp);
}
#endif
if (file != NULL && file[0] != '\0')
Expand All @@ -652,9 +654,9 @@ int main(int argc, char *argv[])
if(gimx_params.logfile)
{
fclose(gimx_params.logfile);
#ifndef WIN32
char file_path[PATH_MAX];
snprintf(file_path, sizeof(file_path), "%s%s%s%s", gimx_params.homedir, GIMX_DIR, LOG_DIR, gimx_params.logfilename);
#ifndef WIN32
int ret = chown(file_path, getpwuid(getuid())->pw_uid, getpwuid(getuid())->pw_gid);
if (ret < 0)
{
Expand Down
4 changes: 2 additions & 2 deletions launcher/gimx-launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ void launcherFrame::OnProcessTerminated(wxProcess *process __attribute__((unused
stringstream ss(line);
ss >> status;
}
remove(statusFile.mb_str(wxConvUTF8));
wxRemoveFile(statusFile);
}

switch(status)
Expand Down Expand Up @@ -1752,7 +1752,7 @@ void launcherFrame::OnProcessTerminated(wxProcess *process __attribute__((unused
if (answer == wxYES)
{
wxString logfile = gimxLogDir + wxT(LOG_FILE);
remove(logfile.mb_str(wxConvUTF8));
wxRemoveFile(logfile);
ProcessOutputChoice->SetSelection(ProcessOutputChoice->FindString(_("log file")));
wxCommandEvent event;
OnButtonStartClick(event);
Expand Down
44 changes: 29 additions & 15 deletions shared/gimxconfigupdater/configupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ static wchar_t * utf8_to_utf16le(const char * inbuf)
IFBUF(path) \
std::istream name (&fb);

#ifndef WIN32
static int remove2(const char *path) {
return remove(path);
}
#else
static int remove2(const char *path) {
wchar_t * wpath = utf8_to_utf16le(path);
int result = _wremove(wpath);
free(wpath);
return result;
}
#endif

#ifdef WIN32
const char * configupdater::configs_url = "https://api.github.com/repos/matlo/GIMX-configurations/contents/Windows";
const char * configupdater::configs_download_url = "https://raw.githubusercontent.com/matlo/GIMX-configurations/master/Windows/";
Expand Down Expand Up @@ -108,28 +121,29 @@ configupdater::ConfigUpdaterStatus configupdater::getconfiglist(std::list<std::s
Downloader::DownloaderStatus downloadStatus = Downloader().download(configs_url, tempFile, progressCallback, this);

if (downloadStatus != Downloader::DownloaderStatusOk) {
remove(tempFile.c_str());
return convertDowloadStatus(downloadStatus);
}

IFSTREAM(tempFile, infile)

while (infile.good()) {
std::string line;
std::getline(infile, line);
size_t pos1 = line.find("\"name\": ");
if (pos1 != std::string::npos) {
size_t pos2 = line.find("\"", pos1 + strlen("\"name\": "));
if (pos2 != std::string::npos) {
size_t pos3 = line.find(".xml\",", pos2 + 1);
if (pos3 != std::string::npos) {
cl.push_back(line.substr(pos2 + 1, pos3 + 4 - (pos2 + 1)));
{
IFSTREAM(tempFile, infile)

while (infile.good()) {
std::string line;
std::getline(infile, line);
size_t pos1 = line.find("\"name\": ");
if (pos1 != std::string::npos) {
size_t pos2 = line.find("\"", pos1 + strlen("\"name\": "));
if (pos2 != std::string::npos) {
size_t pos3 = line.find(".xml\",", pos2 + 1);
if (pos3 != std::string::npos) {
cl.push_back(line.substr(pos2 + 1, pos3 + 4 - (pos2 + 1)));
}
}
}
}
}
} // tempFile is closed at the end of this block, allowing removal

remove(tempFile.c_str());
remove2(tempFile.c_str());

return configupdater::ConfigUpdaterStatusOk;
}
Expand Down
41 changes: 37 additions & 4 deletions shared/gimxdownloader/Downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,38 @@ int Downloader::progress(double dlnow, double dltotal) {
return m_callback(m_clientp, status, m_progress, dltotal);
}

#ifdef WIN32
char * utf16le_to_utf8(const wchar_t * inbuf)
{
char * outbuf = NULL;
int outsize = WideCharToMultiByte(CP_UTF8, 0, inbuf, -1, NULL, 0, NULL, NULL);
if (outsize != 0) {
outbuf = (char*) malloc(outsize * sizeof(*outbuf));
if (outbuf != NULL) {
int res = WideCharToMultiByte(CP_UTF8, 0, inbuf, -1, outbuf, outsize, NULL, NULL);
if (res == 0) {
free(outbuf);
outbuf = NULL;
}
}
}

return outbuf;
}
#endif

std::string Downloader::generateTempFile(const std::string& file) {

std::string value;

#ifdef WIN32
char temp[MAX_PATH];
if (!GetTempPathA(sizeof(temp), temp)) {
wchar_t temp[MAX_PATH + 1];
if (!GetTempPathW(MAX_PATH, temp)) {
return "";
}
value.append(temp);
char * utf8 = utf16le_to_utf8(temp);
value.append(utf8);
free(utf8);
#endif

value += file;
Expand All @@ -201,6 +223,10 @@ std::string Downloader::generateTempFile(const std::string& file) {
static FILE *fopen2(const char *path, const char *mode) {
return fopen(path, mode);
}

static int remove2(const char *path) {
return remove(path);
}
#else
static wchar_t * utf8_to_utf16le(const char * inbuf)
{
Expand Down Expand Up @@ -228,6 +254,13 @@ static FILE *fopen2(const char *path, const char *mode) {
free(wpath);
return file;
}

static int remove2(const char *path) {
wchar_t * wpath = utf8_to_utf16le(path);
int result = _wremove(wpath);
free(wpath);
return result;
}
#endif

Downloader::DownloaderStatus Downloader::download(const std::string& url, const std::string& file,
Expand Down Expand Up @@ -278,7 +311,7 @@ Downloader::DownloaderStatus Downloader::download(const std::string& url, const
}

if (status != Downloader::DownloaderStatusOk) {
remove(file.c_str());
remove2(file.c_str());
}

return status;
Expand Down
34 changes: 29 additions & 5 deletions shared/gimxupdater/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ int progressCallback(void *clientp, Downloader::DownloaderStatus status, double
return updater->progress(convertDowloadStatus(status), progress, total);
}

#ifdef WIN32
static wchar_t * utf8_to_utf16le(const char * inbuf)
{
wchar_t * outbuf = NULL;
int outsize = MultiByteToWideChar(CP_UTF8, 0, inbuf, -1, NULL, 0);
if (outsize != 0) {
outbuf = (wchar_t*) malloc(outsize * sizeof(*outbuf));
if (outbuf != NULL) {
int res = MultiByteToWideChar(CP_UTF8, 0, inbuf, -1, outbuf, outsize);
if (res == 0) {
free(outbuf);
outbuf = NULL;
}
}
}

return outbuf;
}
#endif

Updater::UpdaterStatus Updater::update(std::string url, ProgressCallback callback, void *clientp, bool wait) {

clientCallback = callback;
Expand All @@ -122,18 +142,21 @@ Updater::UpdaterStatus Updater::update(std::string url, ProgressCallback callbac
UpdaterStatus status = UpdaterStatusOk;

#ifdef WIN32
SHELLEXECUTEINFO shExInfo = SHELLEXECUTEINFO();
wchar_t * utf16 = utf8_to_utf16le(tempFile.c_str());

SHELLEXECUTEINFOW shExInfo = SHELLEXECUTEINFOW();
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = wait ? (SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC) : SEE_MASK_DEFAULT;
shExInfo.hwnd = 0;
shExInfo.lpVerb = "runas";
shExInfo.lpFile = tempFile.c_str();
shExInfo.lpParameters = "";
shExInfo.lpVerb = L"runas";
shExInfo.lpFile = utf16;
shExInfo.lpParameters = L"";
shExInfo.lpDirectory = 0;
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;

if (!ShellExecuteEx(&shExInfo)) {
if (!ShellExecuteExW(&shExInfo)) {
free(utf16);
if (GetLastError() == ERROR_CANCELLED) {
return UpdaterStatusCancelled;
}
Expand All @@ -148,6 +171,7 @@ Updater::UpdaterStatus Updater::update(std::string url, ProgressCallback callbac
}

CloseHandle(shExInfo.hProcess);
free(utf16);
#else
(void)wait;
std::string cmd = "";
Expand Down

0 comments on commit cb44734

Please sign in to comment.