diff options
author | B Stack <bgstack15@gmail.com> | 2021-02-02 21:49:34 +0000 |
---|---|---|
committer | B Stack <bgstack15@gmail.com> | 2021-02-02 21:49:34 +0000 |
commit | 26b8bd6eb07b78adad36049e03494a2931b231af (patch) | |
tree | 4d7c950512836f473a6a8cbb521c61e800db6584 /zen/sys_info.cpp | |
parent | Merge branch '11.5' into 'master' (diff) | |
parent | add upstream 11.6 (diff) | |
download | FreeFileSync-26b8bd6eb07b78adad36049e03494a2931b231af.tar.gz FreeFileSync-26b8bd6eb07b78adad36049e03494a2931b231af.tar.bz2 FreeFileSync-26b8bd6eb07b78adad36049e03494a2931b231af.zip |
Merge branch '11.6' into 'master'11.6
add upstream 11.6
See merge request opensource-tracking/FreeFileSync!30
Diffstat (limited to 'zen/sys_info.cpp')
-rw-r--r-- | zen/sys_info.cpp | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/zen/sys_info.cpp b/zen/sys_info.cpp index 70658a68..f6045f7e 100644 --- a/zen/sys_info.cpp +++ b/zen/sys_info.cpp @@ -8,14 +8,15 @@ #include "crc.h" #include "file_access.h" #include "sys_version.h" +#include "symlink_target.h" - #include "symlink_target.h" #include "file_io.h" #include <ifaddrs.h> #include <net/if.h> //IFF_LOOPBACK #include <netpacket/packet.h> //sockaddr_ll + #include "process_exec.h" #include <unistd.h> //getuid() #include <pwd.h> //getpwuid_r() @@ -24,7 +25,7 @@ using namespace zen; std::wstring zen::getUserName() //throw FileError { - const uid_t userIdNo = ::getuid(); //never fails + const uid_t userIdNo = ::getuid(); //"real user ID"; never fails std::vector<char> buffer(std::max<long>(10000, ::sysconf(_SC_GETPW_R_SIZE_MAX))); //::sysconf may return long(-1) struct passwd buffer2 = {}; @@ -110,26 +111,38 @@ std::wstring zen::getOsDescription() //throw FileError -Zstring zen::getDesktopPath() //throw FileError +Zstring zen::getRealProcessPath() //throw FileError { - try - { - const char* path = ::getenv("HOME"); //no extended error reporting - if (!path) - throw SysError(L"Cannot find HOME environment variable."); - - return appendSeparator(path) + "Desktop"; - } - catch (const SysError& e) - { - throw FileError(_("Cannot get process information."), e.toString() ); - } + return getSymlinkRawContent("/proc/self/exe").targetPath; //throw FileError + //path does not contain symlinks => no need for ::realpath() } -Zstring zen::getProcessPath() //throw FileError +Zstring zen::getUserDownloadsPath() //throw FileError { - return getSymlinkRawContent("/proc/self/exe").targetPath; //throw FileError + try + { + Zstring cmdLine; + if (getuid() == 0) //nofail; root(0) => consider as request for elevation, NOT impersonation + { + const char* loginUser = getlogin(); //https://linux.die.net/man/3/getlogin + if (!loginUser) + THROW_LAST_SYS_ERROR("getlogin"); + + cmdLine = Zstring("sudo -u ") + loginUser + " xdg-user-dir DOWNLOAD"; //sudo better be installed :> + } + else + cmdLine = "xdg-user-dir DOWNLOAD"; + + const auto& [exitCode, output] = consoleExecute(cmdLine, std::nullopt /*timeoutMs*/); //throw SysError + if (exitCode != 0) + throw SysError(formatSystemError(cmdLine.c_str(), + replaceCpy(_("Exit code %x"), L"%x", numberTo<std::wstring>(exitCode)), utfTo<std::wstring>(output))); + const Zstring& downloadsPath = trimCpy(output); + ASSERT_SYSERROR(!downloadsPath.empty()); + return downloadsPath; + } + catch (const SysError& e) { throw FileError(_("Cannot get process information."), e.toString()); } } |