From a448eb1dc11a0fd5d10e080fff78a4a012e19a1c Mon Sep 17 00:00:00 2001 From: TaowerfulMAX Date: Wed, 23 Jul 2025 14:50:30 +0800 Subject: [PATCH] fix: hdc support bundle parameter for multiple users. Signed-off-by: TaowerfulMAX --- BUILD.gn | 7 ++++++- src/common/base.cpp | 31 +++++++++++++++++++++++++++++++ src/common/base.h | 6 ++++++ src/common/define.h | 2 ++ src/common/file.cpp | 12 +++++++++--- src/common/transfer.cpp | 10 ++++++++-- src/daemon/daemon_unity.cpp | 7 ++++--- 7 files changed, 66 insertions(+), 9 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index aaef20f8..1f04f79e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -154,7 +154,12 @@ template("hdcd_source_set") { } if (image_name == "system") { defines += [ "HDC_TRACE" ] - external_deps += [ "hitrace:hitrace_meter" ] + external_deps += [ + "hitrace:hitrace_meter", + "os_account:account_iam_innerkits", + "os_account:os_account_innerkits", + ] + defines += [ "HDC_SUPPORT_GETUID" ] } } diff --git a/src/common/base.cpp b/src/common/base.cpp index 95fd3ea2..3404a1b1 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -42,6 +42,9 @@ #endif #include #include "server_cmd_log.h" +#ifdef HDC_SUPPORT_GETUID +#include "os_account_manager.h" +#endif // HDC_SUPPORT_GETUID using namespace std::chrono; namespace Hdc { @@ -3094,5 +3097,33 @@ void CloseOpenFd(void) GetTimeString(timeStr); return StringFormat("[%s] %u %s\n", timeStr.c_str(), targetSessionId, cmdStr.c_str()); } +#ifndef HDC_HOST + std::string GetBundlePath() + { +#ifdef HDC_SUPPORT_GETUID + int32_t id = GetUserId(); + if (id < 0) { + return ""; + } + std::string idString = std::to_string(static_cast(id)); + return DEBUG_BUNDLE_PATH_HALF + idString + DEBUG_BUNDLE_PATH_END; +#else + return DEBUG_BUNDLE_PATH; +#endif // HDC_SUPPORT_GETUID + } + +#ifdef HDC_SUPPORT_GETUID + int32_t GetUserId() + { + std::vector ids; + OHOS::ErrCode err = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids); + if (err != 0) { + WRITE_LOG(LOG_FATAL, "Get OS account local id failed, errno is %d", err); + return -1; + } + return ids.empty() ? -1 : ids[0]; + } +#endif // HDC_SUPPORT_GETUID +#endif // HDC_HOST } // namespace Base } // namespace Hdc diff --git a/src/common/base.h b/src/common/base.h index f593960c..502e9025 100644 --- a/src/common/base.h +++ b/src/common/base.h @@ -306,6 +306,12 @@ namespace Base { void ProcessCmdLogs(); void UpdateEncrpytTCPCache(); bool GetEncrpytTCPSwitch(); +#ifndef HDC_HOST + std::string GetBundlePath(); +#ifdef HDC_SUPPORT_GETUID + int32_t GetUserId(); +#endif +#endif } // namespace base } // namespace Hdc diff --git a/src/common/define.h b/src/common/define.h index 1210ef10..ffafc092 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -138,6 +138,8 @@ constexpr int32_t RSA_KEY_BITS = 3072; const string WHITE_SPACES = " \t\n\r"; const string UT_TMP_PATH = "/tmp/hdc-ut"; const string DEBUG_BUNDLE_PATH = "/mnt/debug/100/debug_hap/"; +const string DEBUG_BUNDLE_PATH_HALF = "/mnt/debug/"; +const string DEBUG_BUNDLE_PATH_END = "/debug_hap/"; const string LOG_FILE_NAME = "hdc.log"; const string LOG_FILE_NAME_PREFIX = "hdc-"; const string LOG_FILE_SUFFIX = ".log"; diff --git a/src/common/file.cpp b/src/common/file.cpp index 591219de..789ec882 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -183,7 +183,8 @@ bool HdcFile::ValidateAndSetPaths(CtxFile *context, int argc, char **argv, int & bool HdcFile::CheckSandboxSubPath(CtxFile *context, string &resolvedPath) { - string fullPath = SANDBOX_ROOT_DIR + context->bundleName; +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + context->bundleName; string appDir(fullPath); appDir = Base::CanonicalizeSpecPath(appDir); fullPath = fullPath + Base::GetPathSep() + context->inputLocalPath; @@ -207,6 +208,10 @@ bool HdcFile::CheckSandboxSubPath(CtxFile *context, string &resolvedPath) return false; } return true; +#else + WRITE_LOG(LOG_DEBUG, "CheckSandboxSubPath is not supported on host"); + return false; +#endif } bool HdcFile::IsPathInsideSandbox(const string &path, const string &appDir) @@ -451,15 +456,16 @@ bool HdcFile::ParseAndValidateOptions(uint8_t *payload, const int payloadSize) bool HdcFile::CheckBundleAndPath() { if (!taskInfo->serverOrDaemon && IsValidBundlePath(ctxNow.bundleName)) { - string fullPath = SANDBOX_ROOT_DIR + ctxNow.bundleName + Base::GetPathSep(); +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + ctxNow.bundleName + Base::GetPathSep(); fullPath.append(ctxNow.inputLocalPath); ctxNow.localPath = fullPath; - string resolvedPath; if (!CheckSandboxSubPath(&ctxNow, resolvedPath)) { WRITE_LOG(LOG_DEBUG, "SlaveCheck CheckSandboxSubPath false."); return false; } +#endif } else if (!taskInfo->serverOrDaemon && ctxNow.bundleName.size() > 0) { LogMsg(MSG_FAIL, "[E005101] Invalid bundle name: %s", ctxNow.bundleName.c_str()); diff --git a/src/common/transfer.cpp b/src/common/transfer.cpp index 96e0bf4e..8d315ae7 100644 --- a/src/common/transfer.cpp +++ b/src/common/transfer.cpp @@ -445,8 +445,12 @@ void HdcTransferBase::OnFileOpenFailed(CtxFile *context) bool HdcTransferBase::IsValidBundlePath(const string &bundleName) { - string fullPath = SANDBOX_ROOT_DIR + bundleName + Base::GetPathSep(); +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + bundleName + Base::GetPathSep(); return Base::CheckBundleName(bundleName) && access(fullPath.c_str(), F_OK) == 0; +#else + return true; +#endif } void HdcTransferBase::RemoveSandboxRootPath(std::string &srcStr, const std::string &bundleName) @@ -454,13 +458,15 @@ void HdcTransferBase::RemoveSandboxRootPath(std::string &srcStr, const std::stri if (taskInfo->serverOrDaemon || !IsValidBundlePath(bundleName)) { return; } - string fullPath = SANDBOX_ROOT_DIR + bundleName + Base::GetPathSep(); +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + bundleName + Base::GetPathSep(); size_t pos = 0; if ((pos = srcStr.find(fullPath)) != std::string::npos) { srcStr = srcStr.replace(pos, fullPath.length(), ""); } else { WRITE_LOG(LOG_DEBUG, "fullPath:%s, srcStr:%s", fullPath.c_str(), srcStr.c_str()); } +#endif } void HdcTransferBase::OnFileOpen(uv_fs_t *req) diff --git a/src/daemon/daemon_unity.cpp b/src/daemon/daemon_unity.cpp index 69161d12..6d635e20 100644 --- a/src/daemon/daemon_unity.cpp +++ b/src/daemon/daemon_unity.cpp @@ -75,13 +75,14 @@ bool HdcDaemonUnity::AsyncCmdOut(bool finish, int64_t exitStatus, const string r bool HdcDaemonUnity::CheckbundlePath(const string &bundleName, string &mountPath) { - if (access(DEBUG_BUNDLE_PATH.c_str(), F_OK) != 0 || !Base::CheckBundleName(bundleName)) { - WRITE_LOG(LOG_FATAL, "debug path %s not found", DEBUG_BUNDLE_PATH.c_str()); + string debugBasePath = Base::GetBundlePath(); + if (access(debugBasePath.c_str(), F_OK) != 0 || !Base::CheckBundleName(bundleName)) { + WRITE_LOG(LOG_FATAL, "debug path %s not found", debugBasePath.c_str()); LogMsg(MSG_FAIL, "[E003001] Invalid bundle name: %s", bundleName.c_str()); return false; } string targetPath = ""; - targetPath += DEBUG_BUNDLE_PATH; + targetPath += debugBasePath; targetPath += bundleName; if ((access(targetPath.c_str(), F_OK) != 0)) { WRITE_LOG(LOG_FATAL, "bundle mount path %s not found", targetPath.c_str()); -- Gitee