From 68ed7b75b390f25b9b7eef132d3410f6d1d40324 Mon Sep 17 00:00:00 2001 From: yaohua Date: Tue, 13 May 2025 14:21:30 +0800 Subject: [PATCH] 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 | 226 +++++------------- 2 files changed, 62 insertions(+), 169 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..ede0ce6eb 100644 --- a/frameworks/native/backup_ext/src/installd_un_tar_file.cpp +++ b/frameworks/native/backup_ext/src/installd_un_tar_file.cpp @@ -129,31 +129,45 @@ static FILE *CreateFile(char* path, mode_t mode, char fileType) if (path == nullptr) { return nullptr; } - std::string appendStr = "wb+"; if (fileType == SPLIT_END_TYPE || fileType == SPLIT_CONTINUE_TYPE) { appendStr = "ab+"; } - FILE *f = fopen(path, appendStr.c_str()); - if (f == nullptr) { - char *p = strrchr(path, '/'); - if (p != nullptr) { - *p = '\0'; - if (CreateDir(path, mode) == 0) { - *p = '/'; - f = fopen(path, "wb+"); - } + FILE *fileP = nullptr; + char *canonicalPath = realpath(path, nullptr); + if (canonicalPath == nullptr) { + LOGE("file path is illegal"); + } else { + fileP = fopen(canonicalPath, appendStr.c_str()); + free(canonicalPath); + } + if (fileP == nullptr) { + char *dirEnd = strrchr(path, '/'); + if (dirEnd == nullptr) { + return nullptr; + } + *dirEnd = '\0'; + if (CreateDir(path, mode) != 0) { + *dirEnd = '/'; + return nullptr; + } + canonicalPath = realpath(path, nullptr); + *dirEnd = '/'; + if (canonicalPath == nullptr) { + return nullptr; } + fileP = fopen(canonicalPath, "wb+"); + free(canonicalPath); } - if (f == nullptr) { - return f; + if (fileP == nullptr) { + return nullptr; } - if (fchmod(fileno(f), S_IRUSR | S_IWUSR) == -1) { + if (fchmod(fileno(fileP), S_IRUSR | S_IWUSR) == -1) { LOGE("fail to change file permission"); + (void)fclose(fileP); return nullptr; } - - return f; + return fileP; } static int CreateSoftlink(const char* oldPath, const char* newPath) @@ -175,8 +189,14 @@ UnTarFile::UnTarFile(const char *tarPath): FilePtr(nullptr), tarSize(0), newOwne if (tarPath != nullptr) { LOGI("untarfile begin.."); + char *canonicalPath = realpath(tarPath, nullptr); + if (canonicalPath == nullptr) { + LOGE("tarPath is illegal"); + return; + } m_srcPath = tarPath; - FilePtr = fopen(tarPath, "rb"); + FilePtr = fopen(canonicalPath, "rb"); + free(canonicalPath); if (FilePtr == nullptr) { LOGE("open file fail"); } @@ -210,9 +230,15 @@ void UnTarFile::Reset() int UnTarFile::UnSplitTar(const std::string &tarFile, const std::string &rootpath) { - FilePtr = fopen(tarFile.c_str(), "rb"); - if (FilePtr == nullptr) { - LOGE("UnTarFile::UnSplitPack, untar split failed!"); + char *canonicalPath = realpath(tarFile.c_str(), nullptr); + if (canonicalPath == nullptr) { + LOGE("check canonical path failed"); + } else { + FilePtr = fopen(canonicalPath, "rb"); + free(canonicalPath); + if (FilePtr == nullptr) { + LOGE("UnTarFile::UnSplitPack, untar split failed!"); + } } isSplit = true; std::string destPath(rootpath); @@ -225,16 +251,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) { - FilePtr = fopen(tarFile.c_str(), "rb"); - if (FilePtr == nullptr) { - LOGE("UnTarFile::CheckIsSplitTar, open split failed!"); + char *canonicalPath = realpath(tarFile.c_str(), nullptr); + if (canonicalPath == nullptr) { + LOGE("check canonical path failed"); + } else { + FilePtr = fopen(canonicalPath, "rb"); + free(canonicalPath); + if (FilePtr == nullptr) { + LOGE("UnTarFile::CheckIsSplitTar, open split failed!"); + } } std::string destPath(rootpath); int parseRet = ParseTarFile(destPath.c_str(), eCheckSplit); @@ -243,8 +277,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; } @@ -646,142 +682,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