diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index 1310a35fa2c63803b9408685b7026d350ed13270..f948312c2e4db7da3a645c4e5aef39d614c1e5ee 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -132,9 +132,8 @@ private: * * @param ioBuffer 写入的文件信息 * @param read 读取文件 - * @param isFilled 是否写完 */ - off_t SplitWriteAll(const std::vector &ioBuffer, off_t read, bool &isFilled); + off_t SplitWriteAll(const std::vector &ioBuffer, off_t read); /** * @brief creaat split tarfile diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 8f81a35c1f71c9f0043929bffabb7f939bf14066..b411c4701772aa5dae0e014df197f7090eb44b4a 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1173,7 +1173,7 @@ static CompareFilesResult CompareFiles(const UniqueFd &cloudFd, const UniqueFd & } allFiles.try_emplace(path, item.second); - if (!isExist || (item.second.isDir == false && item.second.isIncremental == true && + if (item.second.isDir == false && item.second.isIncremental == true && (!isExist || cloudFiles.find(path)->second.hash != item.second.hash)) { // 在云空间简报里不存在或者hash不一致 if (item.second.size < BConstants::BIG_FILE_BOUNDARY) { diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index c16510809d25a85d7a07b5da7e6befcab2bf937d..1a68cd62d46db4a760db419e8486c1a6459b4fbd 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -223,21 +223,17 @@ bool TarFile::WriteFileContent(const string &fileName, off_t size) while (remain > 0) { off_t read = ioBuffer_.size(); - if (size < static_cast(ioBuffer_.size())) { - read = size; - } else { - if (read > remain) { - read = remain; - } + if (remain < read) { + read = remain; } // read buffer from src file - if (ReadAll(fd, ioBuffer_, size) != read) { + if (ReadAll(fd, ioBuffer_, read) != read) { HILOGE("Failed to read all"); break; } // write buffer to tar file - if (SplitWriteAll(ioBuffer_, read, isFilled) != read) { + if (SplitWriteAll(ioBuffer_, read) != read) { HILOGE("Failed to split write all"); break; } @@ -255,7 +251,7 @@ bool TarFile::WriteFileContent(const string &fileName, off_t size) return false; } -off_t TarFile::SplitWriteAll(const vector &ioBuffer, off_t read, bool &isFilled) +off_t TarFile::SplitWriteAll(const vector &ioBuffer, off_t read) { off_t len = ioBuffer.size(); if (len > read) { @@ -265,8 +261,12 @@ off_t TarFile::SplitWriteAll(const vector &ioBuffer, off_t read, bool & while (count < len) { auto writeBytes = fwrite(&ioBuffer[count], sizeof(uint8_t), len - count, currentTarFile_); if (writeBytes < 1) { - HILOGE("Failed to fwrite tar file, err = %{public}d", errno); - return writeBytes; + // 再执行一遍 + auto writeBytes = fwrite(&ioBuffer[count], sizeof(uint8_t), len - count, currentTarFile_); + if (writeBytes < 1) { + HILOGE("Failed to fwrite tar file, err = %{public}d", errno); + return writeBytes; + } } count += writeBytes; currentTarFileSize_ += writeBytes; @@ -365,12 +365,12 @@ void TarFile::FillOwnerName(TarHeader &hdr, const struct stat &st) struct passwd *pw = getpwuid(st.st_uid); if (pw != nullptr) { auto ret = snprintf_s(hdr.uname, sizeof(hdr.uname), sizeof(hdr.uname) - 1, "%s", pw->pw_name); - if (ret < 0 || ret >= static_cast(sizeof(hdr.uname))) { + if (ret < 0) { HILOGE("Fill pw_name failed, err = %{public}d", errno); } } else { auto ret = snprintf_s(hdr.uname, sizeof(hdr.uname), sizeof(hdr.uname) - 1, "%d", st.st_uid); - if (ret < 0 || ret >= static_cast(sizeof(hdr.uname))) { + if (ret < 0) { HILOGE("Fill uid failed, err = %{public}d", errno); } } @@ -378,12 +378,12 @@ void TarFile::FillOwnerName(TarHeader &hdr, const struct stat &st) struct group *gr = getgrgid(st.st_gid); if (gr != nullptr) { auto ret = snprintf_s(hdr.gname, sizeof(hdr.gname), sizeof(hdr.gname) - 1, "%s", gr->gr_name); - if (ret < 0 || static_cast(sizeof(hdr.gname))) { + if (ret < 0) { HILOGE("Fill gr_name failed, err = %{public}d", errno); } } else { auto ret = snprintf_s(hdr.gname, sizeof(hdr.gname), sizeof(hdr.gname) - 1, "%d", st.st_gid); - if (ret < 0 || ret >= static_cast(sizeof(hdr.gname))) { + if (ret < 0) { HILOGE("Fill gid failed, err = %{public}d", errno); } } @@ -418,9 +418,11 @@ int TarFile::WriteAll(const vector &buf, size_t len) { size_t count = 0; while (count < len) { - auto i = fwrite(&buf[0] + count, sizeof(char), len - count, currentTarFile_); - count += i; - currentTarFileSize_ += i; + auto i = fwrite(&buf[0] + count, sizeof(uint8_t), len - count, currentTarFile_); + if (i > 0) { + count += i; + currentTarFileSize_ += i; + } } return count; }