diff --git a/frameworks/native/backup_ext/include/untar_file.h b/frameworks/native/backup_ext/include/untar_file.h index e08be8853dd047865d5dd11d28c8617e64720fff..adf1a07eee358bdc3a499749209bbda78d0a4932 100644 --- a/frameworks/native/backup_ext/include/untar_file.h +++ b/frameworks/native/backup_ext/include/untar_file.h @@ -102,6 +102,14 @@ private: */ FILE *CreateFile(std::string &path); + /** + * @brief mod and stat regular file + * + * @param info 文件属性结构体 + * @param errFileInfo 失败文件信息 + */ + void ModAndStatFile(FileStatInfo &info, ErrFileInfo &errFileInfo); + /** * @brief parse regular file * diff --git a/frameworks/native/backup_ext/src/untar_file.cpp b/frameworks/native/backup_ext/src/untar_file.cpp index cbba3debb084bfd41616999fd6c8cace0c5d79f2..22fcbd0b45dc33d03056601fdaf835ae712f17e7 100644 --- a/frameworks/native/backup_ext/src/untar_file.cpp +++ b/frameworks/native/backup_ext/src/untar_file.cpp @@ -447,6 +447,28 @@ std::tuple UntarFile::ParseIncrementalFileByTypeFlag(cha return {0, isFilter, errFileInfo}; } +void UntarFile::ModAndStatFile(FileStatInfo &info, ErrFileInfo &errFileInfo) +{ + if (chmod(info.fullPath.data(), info.mode) != 0) { + HILOGE("Failed to chmod of %{public}s, err = %{public}d", GetAnonyPath(info.fullPath).c_str(), errno); + errFileInfo[info.fullPath].push_back(errno); + } + struct utimbuf times; + struct stat attr; + if (stat(info.fullPath.c_str(), &attr) != 0) { + errFileInfo[info.fullPath].push_back(errno); + HILOGE("Failed to get stat of %{public}s, err = %{public}d", GetAnonyPath(info.fullPath).c_str(), errno); + times.actime = info.mtime; + } else { + times.actime = attr.st_atime; + } + times.modtime = info.mtime; + if (info.mtime != 0 && utime(info.fullPath.c_str(), ×) != 0) { + errFileInfo[info.fullPath].push_back(errno); + HILOGE("Failed to set mtime of %{public}s, err = %{public}d", GetAnonyPath(info.fullPath).c_str(), errno); + } +} + ErrFileInfo UntarFile::ParseRegularFile(FileStatInfo &info) { ErrFileInfo errFileInfo; @@ -461,6 +483,9 @@ ErrFileInfo UntarFile::ParseRegularFile(FileStatInfo &info) readBuffSize = remainSize; } auto readSize = fread(&destStr[0], sizeof(char), readBuffSize, tarFilePtr_); + if (readSize == 0) { + break; + } if (readSize != readBuffSize) { readBuffSize = readSize; } @@ -471,24 +496,7 @@ ErrFileInfo UntarFile::ParseRegularFile(FileStatInfo &info) remainSize -= readBuffSize; } fclose(destFile); - if (chmod(info.fullPath.data(), info.mode) != 0) { - HILOGE("Failed to chmod of %{public}s, err = %{public}d", GetAnonyPath(info.fullPath).c_str(), errno); - errFileInfo[info.fullPath].push_back(errno); - } - struct utimbuf times; - struct stat attr; - if (stat(info.fullPath.c_str(), &attr) != 0) { - errFileInfo[info.fullPath].push_back(errno); - HILOGE("Failed to get stat of %{public}s, err = %{public}d", GetAnonyPath(info.fullPath).c_str(), errno); - times.actime = info.mtime; - } else { - times.actime = attr.st_atime; - } - times.modtime = info.mtime; - if (info.mtime != 0 && utime(info.fullPath.c_str(), ×) != 0) { - errFileInfo[info.fullPath].push_back(errno); - HILOGE("Failed to set mtime of %{public}s, err = %{public}d", GetAnonyPath(info.fullPath).c_str(), errno); - } + ModAndStatFile(info, errFileInfo); // anyway, go to correct fseeko(tarFilePtr_, pos_ + tarFileBlockCnt_ * BLOCK_SIZE, SEEK_SET); } else {