From b8d660edc8347b08f370a53779cd5f036908c92f Mon Sep 17 00:00:00 2001 From: huangyuchen Date: Tue, 23 May 2023 11:01:55 +0800 Subject: [PATCH] Modify MappedFile. Add canonicalization for specified path. Issue: I77TB4 Test: UT Signed-off-by: huangyuchen Change-Id: I275daf235338e487465d4fceb0acc9c628b12dbd --- base/include/mapped_file.h | 3 ++- base/src/mapped_file.cpp | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/base/include/mapped_file.h b/base/include/mapped_file.h index 0ccca64..d6206f9 100644 --- a/base/include/mapped_file.h +++ b/base/include/mapped_file.h @@ -149,7 +149,8 @@ private: return (input % PageSize() == 0) ? input : (input / PageSize() + 1) * PageSize(); } - bool ValidMappedSize(off_t &targetSize, const struct stat &stat); + bool ValidMappedSize(off_t& targetSize, const struct stat& stat); + bool NormalizePath(); bool NormalizeSize(); bool NormalizeMode(); bool OpenFile(); diff --git a/base/src/mapped_file.cpp b/base/src/mapped_file.cpp index 594d9d4..59aadd5 100644 --- a/base/src/mapped_file.cpp +++ b/base/src/mapped_file.cpp @@ -47,6 +47,20 @@ bool MappedFile::ValidMappedSize(off_t &targetSize, const struct stat &stb) return true; } +bool MappedFile::NormalizePath() +{ + char canonicalPath[PATH_MAX]; + if (realpath(path_.c_str(), canonicalPath) == nullptr) { + if (errno != ENOENT) { + UTILS_LOGE("%{public}s get realpath failed.", __FUNCTION__); + return false; + } + } else { + path_ = canonicalPath; + } + + return true; +} bool MappedFile::NormalizeSize() { @@ -57,6 +71,10 @@ bool MappedFile::NormalizeSize() } errno = 0; + if (!NormalizePath()) { + UTILS_LOGE("%{public}s normalize path failed. %{public}s", __FUNCTION__, strerror(errno)); + return false; + } if (!FileExists(path_)) { if ((mode_ & MapMode::CREATE_IF_ABSENT) == MapMode::DEFAULT) { UTILS_LOGD("%{public}s: Failed. %{public}s", __FUNCTION__, strerror(errno)); @@ -162,6 +180,10 @@ bool MappedFile::OpenFile() } if (isNewFile_) { + if (!NormalizePath()) { + UTILS_LOGE("%{public}s normalize path failed. %{public}s", __FUNCTION__, strerror(errno)); + return false; + } if (ftruncate(fd, EndOffset() + 1) == -1) { UTILS_LOGD("%{public}s: Failed. Cannot change file size: %{public}s.", __FUNCTION__, strerror(errno)); if (close(fd) == -1) { -- Gitee