diff --git a/frameworks/config_policy/src/config_policy_utils.c b/frameworks/config_policy/src/config_policy_utils.c index 2e6998fa3726560cf22943b2cd5085e3f50a74ad..77b0e6f3b1aba9b81cbca0f583448466a191f268 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -464,7 +464,41 @@ bool BuildPath(const char* baseDir, const char* subDir, const char* pathSuffix, return true; } -bool CheckPath(const char* path) + +bool GetFirstLevelDir(const char *path, char *buf, size_t bufLen) +{ + if (!path || !buf || bufLen == 0) { + return false; + } + + const char *start = path; + while (*start == '/') { + start++; + } + + if (*start == '\0') { + return false; + } + + const char *end = strchr(start, '/'); + if (end == NULL) { + end = start + strlen(start); + } + size_t len = end - start; + if (len >= bufLen) { + return false; + } + + errno_t err = memcpy_s(buf, bufLen, start, len); + if (err != 0) { + return false; + } + + buf[len] = '\0'; + return true; +} + +bool CheckPath(const char *path) { if (path == NULL) { return false; @@ -473,7 +507,12 @@ bool CheckPath(const char* path) #ifdef HILOG_BASE int err = errno; if (err != ENOENT) { - HILOG_BASE_ERROR(LOG_CORE, "access failed. errno:%{public}d", err); + char buf[MAX_PATH_LEN]; + if (GetFirstLevelDir(path, buf, sizeof(buf))) { + HILOG_BASE_ERROR(LOG_CORE, "access failed. errno:%{public}d, dir:%{public}s", err, buf); + } else { + HILOG_BASE_ERROR(LOG_CORE, "Failed to extract dir."); + } } #endif return false;