From a96fe1dd38b5fccf1078716710c6441455d42a5c Mon Sep 17 00:00:00 2001 From: zhuruigan Date: Mon, 4 Mar 2024 11:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=B6=E5=8F=91=E6=80=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=86=99=E5=85=A5tar=E5=8C=85=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuruigan Change-Id: I51e4a4a35a138902910e6998b2f1292403bb43de --- frameworks/native/backup_ext/src/tar_file.cpp | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 33886e549..d417995ff 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -213,34 +213,35 @@ bool TarFile::AddFile(string &fileName, const struct stat &st, bool isSplit) bool TarFile::WriteFileContent(const string &fileName, off_t size) { - int fd = open(fileName.c_str(), O_RDONLY | O_CLOEXEC); - if (fd < 0) { + FILE *fd = fopen(fileName.c_str(), "rb"); + if (fd == nullptr) { HILOGE("Failed to open file %{public}s, err = %{public}d", fileName.data(), errno); return false; } off_t remain = size; bool isFilled = false; + vector ioBuffer {}; + off_t read = READ_BUFF_SIZE; + ioBuffer.resize(read); while (remain > 0) { - off_t read = ioBuffer_.size(); if (remain < read) { read = remain; } // read buffer from src file - if (ReadAll(fd, ioBuffer_, read) != read) { - HILOGE("Failed to read all"); + if (fread(&ioBuffer[0], sizeof(uint8_t), read, fd) != static_cast(read)) { + HILOGE("Failed to split open filename."); break; } - // write buffer to tar file - if (SplitWriteAll(ioBuffer_, read) != read) { + if (SplitWriteAll(ioBuffer, read) != read) { HILOGE("Failed to split write all"); break; } remain -= read; } - close(fd); + (void)fclose(fd); if (remain == 0) { if (!isFilled) { return CompleteBlock(size); @@ -261,12 +262,8 @@ off_t TarFile::SplitWriteAll(const vector &ioBuffer, off_t read) while (count < len) { auto writeBytes = fwrite(&ioBuffer[count], sizeof(uint8_t), len - count, currentTarFile_); if (writeBytes < 1) { - // 再执行一遍 - 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 count; - } + HILOGE("Failed to fwrite tar file, err = %{public}d", errno); + return count; } count += writeBytes; currentTarFileSize_ += writeBytes; -- Gitee