diff options
author | B. Stack <bgstack15@gmail.com> | 2021-05-10 08:05:45 -0400 |
---|---|---|
committer | B. Stack <bgstack15@gmail.com> | 2021-05-10 08:05:45 -0400 |
commit | 0d0f8635218a2893fcd00385019089253474f634 (patch) | |
tree | 9261c60b81eb28e068f0f2f44fd8e60214462b2a /zen/sys_info.cpp | |
parent | Merge branch '11.9' into 'master' (diff) | |
download | FreeFileSync-0d0f8635218a2893fcd00385019089253474f634.tar.gz FreeFileSync-0d0f8635218a2893fcd00385019089253474f634.tar.bz2 FreeFileSync-0d0f8635218a2893fcd00385019089253474f634.zip |
add upstream 11.10
Diffstat (limited to 'zen/sys_info.cpp')
-rw-r--r-- | zen/sys_info.cpp | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/zen/sys_info.cpp b/zen/sys_info.cpp index 6eeab276..edb8dd9d 100644 --- a/zen/sys_info.cpp +++ b/zen/sys_info.cpp @@ -24,7 +24,7 @@ using namespace zen; -Zstring zen::getUserName() //throw FileError +Zstring zen::getLoginUser() //throw FileError { const uid_t userIdNo = ::getuid(); //never fails @@ -66,7 +66,8 @@ Zstring zen::getUserName() //throw FileError if (const char* userName = tryGetNonRootUser("SUDO_USER")) return userName; if (const char* userName = tryGetNonRootUser("LOGNAME")) return userName; - throw FileError(_("Cannot get process information."), L"Failed to determine non-root user name"); //should not happen? + //apparently the current user really IS root: https://freefilesync.org/forum/viewtopic.php?t=8405 + return "root"; } @@ -152,6 +153,19 @@ Zstring zen::getRealProcessPath() //throw FileError } +namespace +{ +Zstring getUserDir() //throw FileError +{ + const Zstring loginUser = getLoginUser(); //throw FileError + if (loginUser == "root") + return "/root"; + else + return "/home/" + loginUser; +} +} + + Zstring zen::getUserDataPath() //throw FileError { if (::getuid() != 0) //nofail; root(0) => consider as request for elevation, NOT impersonation @@ -159,7 +173,7 @@ Zstring zen::getUserDataPath() //throw FileError xdgCfgPath && xdgCfgPath[0] != 0) return xdgCfgPath; - return "/home/" + getUserName() + "/.config"; //throw FileError + return getUserDir() + "/.config"; //throw FileError } @@ -167,18 +181,17 @@ Zstring zen::getUserDownloadsPath() //throw FileError { try { - const Zstring cmdLine = ::getuid() == 0 ? //nofail; root(0) => consider as request for elevation, NOT impersonation - //sudo better be installed :> - "sudo -u " + getUserName() + " xdg-user-dir DOWNLOAD" : //throw FileError - "xdg-user-dir DOWNLOAD"; - - const auto& [exitCode, output] = consoleExecute(cmdLine, std::nullopt /*timeoutMs*/); //throw SysError - if (exitCode != 0) //fallback: probably correct 99.9% of the time anyway... - return "/home/" + getUserName() + "/Downloads"; //throw FileError - - const Zstring& downloadsPath = trimCpy(output); - ASSERT_SYSERROR(!downloadsPath.empty()); - return downloadsPath; + if (::getuid() != 0) //nofail; root(0) => consider as request for elevation, NOT impersonation + if (const auto& [exitCode, output] = consoleExecute("xdg-user-dir DOWNLOAD", std::nullopt /*timeoutMs*/); //throw SysError + exitCode == 0) + { + const Zstring& downloadsPath = trimCpy(output); + ASSERT_SYSERROR(!downloadsPath.empty()); + return downloadsPath; + } + + //fallback: probably correct 99.9% of the time anyway... + return getUserDir() + "/Downloads"; //throw FileError } catch (const SysError& e) { throw FileError(_("Cannot get process information."), e.toString()); } } |