From dc298487cee8b460569d97a50028e8469b66e735 Mon Sep 17 00:00:00 2001 From: z30054037 Date: Thu, 14 Dec 2023 19:46:46 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E8=BF=87=E9=95=BF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: I3e161f94d5f1f44126fc63301081c93f928379fd --- frameworks/native/backup_ext/src/tar_file.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index aa1ebee0d..2229da227 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -217,7 +217,7 @@ bool TarFile::WriteFileContent(const string &fileName, off_t size) } } // read buffer from src file - if (ReadAll(fd, ioBuffer_, size) != read) { + if (ReadAll(fd, ioBuffer_, read) != read) { HILOGE("Failed to read all"); break; } @@ -326,11 +326,7 @@ void TarFile::SetCheckSum(TarHeader &hdr) buffer.resize(sizeof(hdr)); buffer.assign(reinterpret_cast(&hdr), reinterpret_cast(&hdr) + sizeof(hdr)); for (int index = 0; index < BLOCK_SIZE; index++) { - if (index < CHKSUM_BASE || index > CHKSUM_BASE + CHKSUM_LEN - 1) { - sum += (buffer[index] & 0xFF); - } else { - sum += BLANK_SPACE; - } + sum += (buffer[index] & 0xFF); } memcpy_s(hdr.chksum, sizeof(hdr.chksum), I2Ocs(sizeof(hdr.chksum), sum).c_str(), sizeof(hdr.chksum) - 1); } -- Gitee From 9f99bd0bc229b868284a7f85034163ef4e6887a3 Mon Sep 17 00:00:00 2001 From: z30054037 Date: Tue, 19 Dec 2023 11:26:33 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=B6=85=E9=95=BF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=88=96=E8=80=85=E5=A4=9A=E5=B1=82=E8=B7=AF=E5=BE=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=89=93=E5=8C=85=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: If8c05f3e5cd33aeaafb618b1d185397a92cc711e --- frameworks/native/backup_ext/src/tar_file.cpp | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 2229da227..f18849fb7 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -37,8 +37,10 @@ const uint64_t MAX_FILE_SIZE = 0777777777777L; const uint32_t OCTSTR_LEN = sizeof(off_t) * 3 + 1; const uint32_t DEFAULT_SLICE_SIZE = 100 * MB_TO_BYTE; // 分片文件大小为100M const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 +const uint32_t PATH_MAX_LEN = 2048; const string VERSION = "1.0"; const string LONG_LINK_SYMBOL = "longLinkSymbol"; +const char SLASH = '/'; } // namespace TarFile &TarFile::GetInstance() @@ -91,7 +93,7 @@ bool TarFile::TraversalFile(string &filePath) HILOGE("File path does not exists, err = %{public}d", errno); return false; } - + HILOGE("TraversalFile, filename=%{public}s", filePath.c_str()); struct stat curFileStat {}; memset_s(&curFileStat, sizeof(curFileStat), 0, sizeof(curFileStat)); if (lstat(filePath.c_str(), &curFileStat) != 0) { @@ -108,7 +110,7 @@ bool TarFile::TraversalFile(string &filePath) FillSplitTailBlocks(); CreateSplitTarFile(); } - + HILOGE("TraversalFile2, filename=%{public}s", filePath.c_str()); // tar包内文件数量大于6000,分片打包 fileCount_++; if (fileCount_ == MAX_FILE_COUNT) { @@ -163,10 +165,16 @@ bool TarFile::AddFile(string &fileName, const struct stat &st, bool isSplit) HILOGE("Failed to I2OcsConvert"); return false; } - + HILOGE("AddFile, filename=%{public}s", fileName.c_str()); if (strncpy_s(hdr.name, sizeof(hdr.name), fileName.c_str(), sizeof(hdr.name) - 1) == 0) { - if (fileName.length() >= sizeof(hdr.name)) { - WriteLongName(fileName, GNUTYPE_LONGNAME); + string absname(fileName); + absname.replace(absname.begin(), absname.begin() + PATH_MAX_LEN + 1, ""); + if (S_ISDIR(st.st_mode)) { + absname += SLASH; + } + if (absname.length() >= sizeof(hdr.name)) { + WriteLongName(absname, GNUTYPE_LONGNAME); + HILOGE("AddFile2, absname=%{public}s, length=%{public}d", absname.c_str(), absname.length()); } } memcpy_s(hdr.magic, sizeof(hdr.magic), TMAGIC.c_str(), sizeof(hdr.magic) - 1); @@ -193,6 +201,7 @@ bool TarFile::AddFile(string &fileName, const struct stat &st, bool isSplit) HILOGE("Failed to write file content"); return false; } + HILOGE("AddFile3, filename2=%{public}s, currentFileName=%{public}s", fileName.c_str(), currentFileName_.c_str()); currentFileName_ = ""; return true; } @@ -217,13 +226,13 @@ bool TarFile::WriteFileContent(const string &fileName, off_t size) } } // read buffer from src file - if (ReadAll(fd, ioBuffer_, read) != read) { + if (ReadAll(fd, ioBuffer_, read) != (int)read) { HILOGE("Failed to read all"); break; } // write buffer to tar file - if (SplitWriteAll(ioBuffer_, read, isFilled) != read) { + if (SplitWriteAll(ioBuffer_, (int)read, isFilled) != (int)read) { HILOGE("Failed to split write all"); break; } @@ -244,7 +253,7 @@ bool TarFile::WriteFileContent(const string &fileName, off_t size) int TarFile::SplitWriteAll(const vector &ioBuffer, int read, bool &isFilled) { size_t len = ioBuffer.size(); - if (len > read) { + if ((int)len > read) { len = read; } size_t count = 0; @@ -407,6 +416,7 @@ string TarFile::I2Ocs(int len, off_t val) bool TarFile::WriteLongName(string &name, char type) { + HILOGE("WriteLongName1, filename=%{public}s", name.c_str()); // fill tar header for long name TarHeader tmp; memset_s(&tmp, sizeof(tmp), 0, sizeof(tmp)); @@ -449,7 +459,7 @@ bool TarFile::WriteLongName(string &name, char type) if (WriteTarHeader(tmp) != BLOCK_SIZE) { return false; } - + HILOGE("WriteLongName3, filename=%{public}s", name.c_str()); // write name to archive vector buffer {}; buffer.resize(sz); @@ -457,7 +467,7 @@ bool TarFile::WriteLongName(string &name, char type) if (WriteAll(buffer, sz) != sz) { return false; } - + HILOGE("WriteLongName4, name=%{public}s", name.c_str()); return CompleteBlock(sz); } -- Gitee From 1444790bd74878c2d63f2f2515de43d1f54ddb86 Mon Sep 17 00:00:00 2001 From: z30054037 Date: Wed, 20 Dec 2023 14:17:06 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=B6=85=E9=95=BF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=88=96=E8=80=85=E5=A4=9A=E5=B1=82=E8=B7=AF=E5=BE=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=89=93=E5=8C=85=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: Icb5039a38133ab5c6b96ad9bc53e0dd693d51db7 --- frameworks/native/backup_ext/src/tar_file.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index f18849fb7..8957c053d 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -172,6 +172,7 @@ bool TarFile::AddFile(string &fileName, const struct stat &st, bool isSplit) if (S_ISDIR(st.st_mode)) { absname += SLASH; } + HILOGE("AddFile1, absname=%{public}s, length=%{public}d", absname.c_str(), absname.length()); if (absname.length() >= sizeof(hdr.name)) { WriteLongName(absname, GNUTYPE_LONGNAME); HILOGE("AddFile2, absname=%{public}s, length=%{public}d", absname.c_str(), absname.length()); -- Gitee From 19234974e5d4aa1843f1a5211e8d485feb712e5b Mon Sep 17 00:00:00 2001 From: z30054037 Date: Wed, 20 Dec 2023 15:53:18 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E8=B6=85=E9=95=BF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=88=96=E8=80=85=E5=A4=9A=E5=B1=82=E8=B7=AF=E5=BE=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=89=93=E5=8C=85=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: I42e098cfc4945fd9fa64608388fe09fd15b66018 --- frameworks/native/backup_ext/src/tar_file.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 8957c053d..e177adc45 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -167,8 +167,14 @@ bool TarFile::AddFile(string &fileName, const struct stat &st, bool isSplit) } HILOGE("AddFile, filename=%{public}s", fileName.c_str()); if (strncpy_s(hdr.name, sizeof(hdr.name), fileName.c_str(), sizeof(hdr.name) - 1) == 0) { + string rootPath; + if (S_ISDIR(st.st_mode)) { + rootPath = fileName; + } else { + rootPath = ExtractFilePath(fileName); + } string absname(fileName); - absname.replace(absname.begin(), absname.begin() + PATH_MAX_LEN + 1, ""); + absname.replace(absname.begin(), absname.begin() + rootPath.size(), ""); if (S_ISDIR(st.st_mode)) { absname += SLASH; } -- Gitee From 3fbc49f3af0efce6a7e217066cc664870797219e Mon Sep 17 00:00:00 2001 From: z30054037 Date: Wed, 20 Dec 2023 16:25:31 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E8=B6=85=E9=95=BF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=88=96=E8=80=85=E5=A4=9A=E5=B1=82=E8=B7=AF=E5=BE=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=89=93=E5=8C=85=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: I175ded0a707ee76aee57d4e9c31bd54706b1cad7 --- frameworks/native/backup_ext/src/tar_file.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index e177adc45..dd12841ec 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -37,7 +37,6 @@ const uint64_t MAX_FILE_SIZE = 0777777777777L; const uint32_t OCTSTR_LEN = sizeof(off_t) * 3 + 1; const uint32_t DEFAULT_SLICE_SIZE = 100 * MB_TO_BYTE; // 分片文件大小为100M const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 -const uint32_t PATH_MAX_LEN = 2048; const string VERSION = "1.0"; const string LONG_LINK_SYMBOL = "longLinkSymbol"; const char SLASH = '/'; @@ -341,7 +340,7 @@ void TarFile::SetCheckSum(TarHeader &hdr) vector buffer {}; buffer.resize(sizeof(hdr)); buffer.assign(reinterpret_cast(&hdr), reinterpret_cast(&hdr) + sizeof(hdr)); - for (int index = 0; index < BLOCK_SIZE; index++) { + for (int index = 0; index < (int)BLOCK_SIZE; index++) { sum += (buffer[index] & 0xFF); } memcpy_s(hdr.chksum, sizeof(hdr.chksum), I2Ocs(sizeof(hdr.chksum), sum).c_str(), sizeof(hdr.chksum) - 1); @@ -352,12 +351,12 @@ void TarFile::FillOwnerName(TarHeader &hdr, const struct stat &st) struct passwd *pw = getpwuid(st.st_uid); if (pw != nullptr) { int ret = snprintf_s(hdr.uname, sizeof(hdr.uname), sizeof(hdr.uname) - 1, "%s", pw->pw_name); - if (ret < 0 || ret >= sizeof(hdr.uname)) { + if (ret < 0 || ret >= (int)sizeof(hdr.uname)) { HILOGE("Fill pw_name failed, err = %{public}d", errno); } } else { int ret = snprintf_s(hdr.uname, sizeof(hdr.uname), sizeof(hdr.uname) - 1, "%d", st.st_uid); - if (ret < 0 || ret >= sizeof(hdr.uname)) { + if (ret < 0 || ret >= (int)sizeof(hdr.uname)) { HILOGE("Fill uid failed, err = %{public}d", errno); } } @@ -365,12 +364,12 @@ void TarFile::FillOwnerName(TarHeader &hdr, const struct stat &st) struct group *gr = getgrgid(st.st_gid); if (gr != nullptr) { int ret = snprintf_s(hdr.gname, sizeof(hdr.gname), sizeof(hdr.gname) - 1, "%s", gr->gr_name); - if (ret < 0 || ret >= sizeof(hdr.gname)) { + if (ret < 0 || ret >= (int)sizeof(hdr.gname)) { HILOGE("Fill gr_name failed, err = %{public}d", errno); } } else { int ret = snprintf_s(hdr.gname, sizeof(hdr.gname), sizeof(hdr.gname) - 1, "%d", st.st_gid); - if (ret < 0 || ret >= sizeof(hdr.gname)) { + if (ret < 0 || ret >= (int)sizeof(hdr.gname)) { HILOGE("Fill gid failed, err = %{public}d", errno); } } -- Gitee