diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index aa1ebee0d791db60bebe4ff751123d2d3e011507..dd12841eca5029203b69e5bc44559fff92c6ce5b 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -39,6 +39,7 @@ const uint32_t DEFAULT_SLICE_SIZE = 100 * MB_TO_BYTE; // 分片文件大小为10 const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 const string VERSION = "1.0"; const string LONG_LINK_SYMBOL = "longLinkSymbol"; +const char SLASH = '/'; } // namespace TarFile &TarFile::GetInstance() @@ -91,7 +92,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 +109,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 +164,23 @@ 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 rootPath; + if (S_ISDIR(st.st_mode)) { + rootPath = fileName; + } else { + rootPath = ExtractFilePath(fileName); + } + string absname(fileName); + absname.replace(absname.begin(), absname.begin() + rootPath.size(), ""); + 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()); } } memcpy_s(hdr.magic, sizeof(hdr.magic), TMAGIC.c_str(), sizeof(hdr.magic) - 1); @@ -193,6 +207,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 +232,13 @@ 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) != (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 +259,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; @@ -325,12 +340,8 @@ 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++) { - if (index < CHKSUM_BASE || index > CHKSUM_BASE + CHKSUM_LEN - 1) { - sum += (buffer[index] & 0xFF); - } else { - sum += BLANK_SPACE; - } + 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); } @@ -340,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); } } @@ -353,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); } } @@ -411,6 +422,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)); @@ -453,7 +465,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); @@ -461,7 +473,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); }