diff --git a/Common/FileDownloader/FileDownloader_curl.cpp b/Common/FileDownloader/FileDownloader_curl.cpp index 2ed104347f..3ceaf88d13 100644 --- a/Common/FileDownloader/FileDownloader_curl.cpp +++ b/Common/FileDownloader/FileDownloader_curl.cpp @@ -35,14 +35,114 @@ #include #include "../../DesktopEditor/common/Directory.h" -#include -#include - -#include +#ifndef USE_EXTERNAL_DOWNLOAD +#include #include #include +#include +#else + +#include +#include + +int download_external(const std::wstring& sUrl, const std::wstring& sOutput) +{ + int nReturnCode = -1; + + std::string sUrlA = U_TO_UTF8(sUrl); + //sUrlA =("\"" + sUrlA + "\""); + std::string sOutputA = U_TO_UTF8(sOutput); + //sOutputA =("\"" + sOutputA + "\""); + + if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/curl")) + { + pid_t pid = fork(); // create child process + int status; + + switch (pid) + { + case -1: // error + break; + + case 0: // child process + { + const char* nargs[6]; + nargs[0] = "/usr/bin/curl"; + nargs[1] = sUrlA.c_str(); + nargs[2] = "--output"; + nargs[3] = sOutputA.c_str(); + nargs[4] = "--silent"; + nargs[5] = NULL; + + const char* nenv[3]; + nenv[0] = "LD_PRELOAD="; + nenv[1] = "LD_LIBRARY_PATH="; + nenv[2] = NULL; + + execve("/usr/bin/curl", (char * const *)nargs, (char * const *)nenv); + exit(EXIT_SUCCESS); + break; + } + default: // parent process, pid now contains the child pid + while (-1 == waitpid(pid, &status, 0)); // wait for child to complete + if (WIFEXITED(status)) + { + nReturnCode = WEXITSTATUS(status); + } + break; + } + } + + if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/wget")) + { + pid_t pid = fork(); // create child process + int status; + + switch (pid) + { + case -1: // error + break; + + case 0: // child process + { + const char* nargs[6]; + nargs[0] = "/usr/bin/wget"; + nargs[1] = sUrlA.c_str(); + nargs[2] = "-O"; + nargs[3] = sOutputA.c_str(); + nargs[4] = "-q"; + nargs[5] = NULL; + + const char* nenv[2]; + nenv[0] = "LD_PRELOAD="; + nenv[1] = NULL; + + execve("/usr/bin/wget", (char * const *)nargs, (char * const *)nenv); + exit(EXIT_SUCCESS); + break; + } + default: // parent process, pid now contains the child pid + while (-1 == waitpid(pid, &status, 0)); // wait for child to complete + if (WIFEXITED(status)) + { + nReturnCode = WEXITSTATUS(status); + } + break; + } + } + + if (0 == nReturnCode) + { + if (!NSFile::CFileBinary::Exists(sOutput)) + nReturnCode = -1; + } + + return nReturnCode; +} + +#endif class CFileDownloaderBaseCURL : public CFileDownloaderBase { @@ -60,6 +160,7 @@ public : } } +#ifndef USE_EXTERNAL_DOWNLOAD static size_t write_data(void *ptr, size_t size, size_t nmemb, int fd) { size_t written = write(fd, ptr, size * nmemb); return written; @@ -119,6 +220,19 @@ protected: filename = sTempPath; return fd; } +#else + virtual int DownloadFile() + { + if (m_sFilePath.empty()) + { + m_sFilePath = NSFile::CFileBinary::CreateTempFileWithUniqueName(NSDirectory::GetTempPath(), L"DW"); + if (NSFile::CFileBinary::Exists(m_sFilePath)) + NSFile::CFileBinary::Remove(m_sFilePath); + } + return download_external(m_sFileUrl, m_sFilePath); + } + +#endif }; CFileDownloader_private::CFileDownloader_private(std::wstring sFileUrl, bool bDelete) diff --git a/Common/kernel.pro b/Common/kernel.pro index 7ee26eb8a4..289946e302 100644 --- a/Common/kernel.pro +++ b/Common/kernel.pro @@ -25,8 +25,6 @@ include(../OfficeUtils/OfficeUtils.pri) CONFIG += core_static_link_xml_full include(../DesktopEditor/xml/build/qt/libxml2.pri) -include(../Common/3dParty/curl/curl.pri) - # DOWNLOADER HEADERS += \ ./FileDownloader/FileDownloader.h \ @@ -44,6 +42,14 @@ core_windows { LIBS += -lShell32 } core_linux { + CONFIG += use_external_download + + use_external_download { + DEFINES += USE_EXTERNAL_DOWNLOAD + } else { + include(../Common/3dParty/curl/curl.pri) + } + SOURCES += \ ./FileDownloader/FileDownloader_curl.cpp }