diff --git a/base/include/mapped_file.h b/base/include/mapped_file.h index 0ccca64f2804152293862c09d3f48d548dd70038..d6206f9a60d4593764e546199ecb05b5887d8d62 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 594d9d45647e261c2f57225b5c7eb5bfe49fc66f..59aadd585a165af7e0a69ef94f16fe97601c6f3b 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) {