From a224dd96d13a278a40a9f385be9f1cc5549da020 Mon Sep 17 00:00:00 2001 From: yaohua Date: Mon, 12 May 2025 16:01:27 +0800 Subject: [PATCH 1/3] fix code style check problem Signed-off-by: yaohua --- .../backup_ext/include/installd_un_tar_file.h | 5 - .../backup_ext/src/installd_un_tar_file.cpp | 161 +++--------------- 2 files changed, 23 insertions(+), 143 deletions(-) diff --git a/frameworks/native/backup_ext/include/installd_un_tar_file.h b/frameworks/native/backup_ext/include/installd_un_tar_file.h index 1d35bbb5a..b4e9108c2 100644 --- a/frameworks/native/backup_ext/include/installd_un_tar_file.h +++ b/frameworks/native/backup_ext/include/installd_un_tar_file.h @@ -45,9 +45,6 @@ public: virtual ~UnTarFile(void); public: - std::vector GetFileNames(); - int UnSplitPack(const char *path, uid_t owner = 0); - int UnPack(const char *path, uid_t owner = 0); void Reset(); bool CheckIsSplitTar(const std::string &tarFile, const std::string &rootpath); int UnSplitTar(const std::string &tarFile, const std::string &rootpath); @@ -70,8 +67,6 @@ private: bool ProcessTarBlock(char *buff, EParseType type, ParseTarPath *parseTarPath, bool &isSkip, bool &isSoftLink); bool IsValidTarBlock(const TarHeader *tarHeader); bool VerifyChecksum(const TarHeader *tarHeader); - bool CheckSliceTar(const char *tarInfo, const char *dstPathName, std::vector &fileNameVector); - bool HandleCheckFile(const char *tarBaseName, std::vector &fileNameVector, int &num); void FreePointer(ParseTarPath *parseTarPath); void FreeLongTypePointer(ParseTarPath *parseTarPath); bool CreateDirWithRecursive(const std::string &filePath, mode_t mode = (mode_t)448); diff --git a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp index da86712a3..ffb632b19 100644 --- a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp +++ b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp @@ -134,6 +134,11 @@ static FILE *CreateFile(char* path, mode_t mode, char fileType) if (fileType == SPLIT_END_TYPE || fileType == SPLIT_CONTINUE_TYPE) { appendStr = "ab+"; } + char realPath[PATH_MAX]{0}; + if (realpath(path, realPath) == nullptr) { + LOGE("check realpath failed"); + return nullptr; + } FILE *f = fopen(path, appendStr.c_str()); if (f == nullptr) { char *p = strrchr(path, '/'); @@ -150,6 +155,7 @@ static FILE *CreateFile(char* path, mode_t mode, char fileType) } if (fchmod(fileno(f), S_IRUSR | S_IWUSR) == -1) { LOGE("fail to change file permission"); + (void)fclose(f); return nullptr; } @@ -175,6 +181,11 @@ UnTarFile::UnTarFile(const char *tarPath): FilePtr(nullptr), tarSize(0), newOwne if (tarPath != nullptr) { LOGI("untarfile begin.."); + char realPath[PATH_MAX] {0}; + if (realpath(tarPath, realPath) == nullptr) { + LOGE("check realpath failed"); + return; + } m_srcPath = tarPath; FilePtr = fopen(tarPath, "rb"); if (FilePtr == nullptr) { @@ -210,9 +221,15 @@ void UnTarFile::Reset() int UnTarFile::UnSplitTar(const std::string &tarFile, const std::string &rootpath) { + char realPath[PATH_MAX]{0}; + if (realpath(tarFile.c_str(), realPath) == nullptr) { + LOGE("check realpath failed"); + return -1; + } FilePtr = fopen(tarFile.c_str(), "rb"); if (FilePtr == nullptr) { LOGE("UnTarFile::UnSplitPack, untar split failed!"); + return -1; } isSplit = true; std::string destPath(rootpath); @@ -232,9 +249,15 @@ int UnTarFile::UnSplitTar(const std::string &tarFile, const std::string &rootpat bool UnTarFile::CheckIsSplitTar(const std::string &tarFile, const std::string &rootpath) { + char realPath[PATH_MAX]{0}; + if (realpath(tarFile.c_str(), realPath) == nullptr) { + LOGE("check realpath failed"); + return false; + } FilePtr = fopen(tarFile.c_str(), "rb"); if (FilePtr == nullptr) { LOGE("UnTarFile::CheckIsSplitTar, open split failed!"); + return false; } std::string destPath(rootpath); int parseRet = ParseTarFile(destPath.c_str(), eCheckSplit); @@ -646,142 +669,4 @@ bool UnTarFile::CreateDirWithRecursive(const std::string &filePath, mode_t mode) } while (index != std::string::npos); return access(filePath.c_str(), F_OK) == 0; } - -std::vector UnTarFile::GetFileNames() -{ - if (file_names.empty()) { - ParseTarFile(); - } - - return file_names; -} - -int UnTarFile::UnPack(const char *path, uid_t owner) -{ - newOwner = owner; - int ret = ParseTarFile(path, eUnpack); - if (remove(m_srcPath.c_str()) != 0) { - LOGI("delete failed"); - } - return ret; -} - -int UnTarFile::UnSplitPack(const char *path, uid_t owner) -{ - LOGI("Start UnSplitPack"); - newOwner = owner; - isSplit = true; - int num = 0; - std::vector taskSrcPathName; - std::string pathDir(path); - pathDir = pathDir.substr(0, pathDir.find_last_of('/')); - - if (!HandleCheckFile(m_srcPath.c_str(), taskSrcPathName, num)) { - LOGE("UnTarFile::UnSplitPack, handleCheckFile failed!"); - return -1; - } - - int ret = 0; - if (FilePtr != nullptr) { - (void)fclose(FilePtr); - FilePtr = nullptr; - LOGE("FilePtr is not null!"); - } - for (int i = 0; i < num; i++) { - FilePtr = fopen(taskSrcPathName[i].c_str(), "rb"); - if (FilePtr != nullptr) { - int parseRet = ParseTarFile(pathDir.c_str(), eUnpack); - if (parseRet != 0) { - LOGE("UnTarFile::UnSplitPack, untar split failed!"); - (void)fclose(FilePtr); - FilePtr = nullptr; - return parseRet; - } else { - LOGI("UnTarFile::UnSplitPack, untar split suc!"); - (void)fclose(FilePtr); - FilePtr = nullptr; - } - } - if (remove(taskSrcPathName[i].c_str()) != 0) { - LOGI("delete failed"); - } - } - - LOGI("UnTarFile::UnSplitPack, untar split finish!"); - taskSrcPathName.clear(); - return ret; -} - -// check slice tar file according the tar info -bool UnTarFile::CheckSliceTar(const char *tarInfo, const char *dstPathName, std::vector &fileNameVector) -{ - if (tarInfo == nullptr) { - LOGE("error, tarInfo is NULL"); - return false; - } - std::string info(tarInfo); - size_t pos = info.find_last_of("|"); - std::string tarName(dstPathName); - tarName.append("/"); - tarName.append(info.substr(0, pos)); - - std::string subSize = info.substr(pos + 1); - if (subSize.empty()) { - LOGE("error subSize is empty"); - return false; - } - off_t size = strtol(subSize.c_str(), nullptr, 0); - - if (access(tarName.c_str(), F_OK)) { - LOGE("error, slice tar file don't exist, error:%{public}s", strerror(errno)); - return true; - } - - struct stat stSliceTar; - if (stat(tarName.c_str(), &stSliceTar) == -1) { - LOGE("error, failed to get stat of tar file, error: %{public}s", strerror(errno)); - return false; - } - - if (size != stSliceTar.st_size) { - LOGE("error, the size of the tar file is not equal to the size in the check file\n"); - return false; - } - fileNameVector.push_back(tarName); - return true; -} - -bool UnTarFile::HandleCheckFile(const char *tarBaseName, std::vector &fileNameVector, int &num) -{ - LOGI("HandleCheckFile"); - if (tarBaseName == nullptr) { - LOGE("tarBaseName is nullptr"); - return false; - } - - FILE *fd = fopen(tarBaseName, "rb"); - if (fd == nullptr) { - LOGE("open check file error, error:%{public}s", strerror(errno)); - return false; - } - - std::string taskSPath(tarBaseName); - std::string dirName = taskSPath.substr(0, taskSPath.find_last_of('/')); - - char tarInfo[PATH_MAX_LEN] = {0}; - bool ret = true; - while ((fgets(tarInfo, (sizeof(tarInfo) - 1), fd)) != nullptr) { - if (!CheckSliceTar(tarInfo, dirName.c_str(), fileNameVector)) { - LOGE("handleCheckFile: failed"); - ret = false; - break; - } - num++; - memset_s(tarInfo, PATH_MAX_LEN, 0, PATH_MAX_LEN); - } - LOGI("HandleCheckFile end"); - (void)fclose(fd); - fd = nullptr; - return ret; -} } // namespace installd -- Gitee From e2b590401ec1e800fc70d31f40134ced4929e83a Mon Sep 17 00:00:00 2001 From: yaohua Date: Mon, 12 May 2025 20:15:45 +0800 Subject: [PATCH 2/3] fix code style check problem Signed-off-by: yaohua --- .../backup_ext/src/installd_un_tar_file.cpp | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp index ffb632b19..465e5b35d 100644 --- a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp +++ b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp @@ -134,12 +134,12 @@ static FILE *CreateFile(char* path, mode_t mode, char fileType) if (fileType == SPLIT_END_TYPE || fileType == SPLIT_CONTINUE_TYPE) { appendStr = "ab+"; } - char realPath[PATH_MAX]{0}; - if (realpath(path, realPath) == nullptr) { - LOGE("check realpath failed"); + char *canonicalPath = realPath(path, nullptr); + if (canonicalPath == nullptr) { + LOGE("check canonical path failed"); return nullptr; } - FILE *f = fopen(path, appendStr.c_str()); + FILE *f = fopen(canonicalPath, appendStr.c_str()); if (f == nullptr) { char *p = strrchr(path, '/'); if (p != nullptr) { @@ -150,6 +150,7 @@ static FILE *CreateFile(char* path, mode_t mode, char fileType) } } } + free(canonicalPath); if (f == nullptr) { return f; } @@ -181,13 +182,13 @@ UnTarFile::UnTarFile(const char *tarPath): FilePtr(nullptr), tarSize(0), newOwne if (tarPath != nullptr) { LOGI("untarfile begin.."); - char realPath[PATH_MAX] {0}; - if (realpath(tarPath, realPath) == nullptr) { - LOGE("check realpath failed"); + char *canonicalPath = realPath(tarPath, nullptr); + if (canonicalPath == nullptr) { + LOGE("check canonical path failed"); return; } m_srcPath = tarPath; - FilePtr = fopen(tarPath, "rb"); + FilePtr = fopen(canonicalPath, "rb"); if (FilePtr == nullptr) { LOGE("open file fail"); } @@ -221,15 +222,15 @@ void UnTarFile::Reset() int UnTarFile::UnSplitTar(const std::string &tarFile, const std::string &rootpath) { - char realPath[PATH_MAX]{0}; - if (realpath(tarFile.c_str(), realPath) == nullptr) { - LOGE("check realpath failed"); + char *canonicalPath = realPath(tarFile.c_str(), nullptr); + if (canonicalPath == nullptr) { + LOGE("check canonical path failed"); return -1; } - FilePtr = fopen(tarFile.c_str(), "rb"); + FilePtr = fopen(canonicalPath, "rb"); + free(canonicalPath); if (FilePtr == nullptr) { LOGE("UnTarFile::UnSplitPack, untar split failed!"); - return -1; } isSplit = true; std::string destPath(rootpath); @@ -242,22 +243,24 @@ int UnTarFile::UnSplitTar(const std::string &tarFile, const std::string &rootpat } else { LOGI("UnTarFile::UnSplitPack, untar split suc!"); } - (void)fclose(FilePtr); - FilePtr = nullptr; + if (FilePtr != nullptr) { + (void)fclose(FilePtr); + FilePtr = nullptr; + } return parseRet; } bool UnTarFile::CheckIsSplitTar(const std::string &tarFile, const std::string &rootpath) { - char realPath[PATH_MAX]{0}; - if (realpath(tarFile.c_str(), realPath) == nullptr) { - LOGE("check realpath failed"); - return false; - } - FilePtr = fopen(tarFile.c_str(), "rb"); - if (FilePtr == nullptr) { - LOGE("UnTarFile::CheckIsSplitTar, open split failed!"); - return false; + char *canonicalPath = realPath(tarFile.c_str(), nullptr); + if (canonicalPath == nullptr) { + LOGE("check canonical path failed"); + } else { + FilePtr = fopen(canonicalPath, "rb"); + if (FilePtr == nullptr) { + LOGE("UnTarFile::CheckIsSplitTar, open split failed!"); + } + free(canonicalPath); } std::string destPath(rootpath); int parseRet = ParseTarFile(destPath.c_str(), eCheckSplit); @@ -266,8 +269,10 @@ bool UnTarFile::CheckIsSplitTar(const std::string &tarFile, const std::string &r } else { LOGI("UnTarFile::CheckIsSplitTar, check is split, %{public}d", isSplit); } - (void)fclose(FilePtr); - FilePtr = nullptr; + if (FilePtr != nullptr) { + (void)fclose(FilePtr); + FilePtr = nullptr; + } if (isSplit) { return true; } -- Gitee From 9481219049fb4aa6d2b05309efc4254a49dc0da1 Mon Sep 17 00:00:00 2001 From: yaohua Date: Mon, 12 May 2025 20:36:08 +0800 Subject: [PATCH 3/3] fix code style check problem Signed-off-by: yaohua --- frameworks/native/backup_ext/src/installd_un_tar_file.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp index 465e5b35d..79d7e56a6 100644 --- a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp +++ b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp @@ -192,6 +192,7 @@ UnTarFile::UnTarFile(const char *tarPath): FilePtr(nullptr), tarSize(0), newOwne if (FilePtr == nullptr) { LOGE("open file fail"); } + free(canonicalPath); } } -- Gitee