summaryrefslogtreecommitdiff
path: root/zen/sys_info.cpp
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2021-05-10 12:09:24 +0000
committerB. Stack <bgstack15@gmail.com>2021-05-10 12:09:24 +0000
commit692fd264ae30b5b844f748626262e79bc3d5a7e2 (patch)
tree9261c60b81eb28e068f0f2f44fd8e60214462b2a /zen/sys_info.cpp
parentMerge branch '11.9' into 'master' (diff)
parentadd upstream 11.10 (diff)
downloadFreeFileSync-692fd264ae30b5b844f748626262e79bc3d5a7e2.tar.gz
FreeFileSync-692fd264ae30b5b844f748626262e79bc3d5a7e2.tar.bz2
FreeFileSync-692fd264ae30b5b844f748626262e79bc3d5a7e2.zip
Merge branch '11.10' into 'master'11.10
add upstream 11.10 See merge request opensource-tracking/FreeFileSync!33
Diffstat (limited to 'zen/sys_info.cpp')
-rw-r--r--zen/sys_info.cpp43
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()); }
}
bgstack15