diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index 5b9dbd3d11a0ad16f70b71992d0e13a523ee76fe..cd30c5000ff2e2a268883e58f1288dfba89b6271 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -56,7 +56,7 @@ const char AREGTYPE = '\0'; // regular file const char SYMTYPE = '2'; // reserved const char DIRTYPE = '5'; // directory const char GNUTYPE_LONGNAME = 'L'; -} +} // namespace // 512 bytes using TarHeader = struct { diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index aa1ebee0d791db60bebe4ff751123d2d3e011507..b3547b994b51780acf886ba9518e60c8214f5925 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -164,10 +164,10 @@ bool TarFile::AddFile(string &fileName, const struct stat &st, bool isSplit) return false; } - 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); - } + if (fileName.length() < TNAME_LEN) { + memcpy_s(hdr.name, sizeof(hdr.name), fileName.c_str(), sizeof(hdr.name) - 1); + } else { + WriteLongName(fileName, GNUTYPE_LONGNAME); } memcpy_s(hdr.magic, sizeof(hdr.magic), TMAGIC.c_str(), sizeof(hdr.magic) - 1); memcpy_s(hdr.version, sizeof(hdr.version), VERSION.c_str(), sizeof(hdr.version) - 1); @@ -385,7 +385,7 @@ int TarFile::WriteTarHeader(TarHeader &header) { vector buffer {}; buffer.resize(sizeof(header)); - buffer.assign(reinterpret_cast(&header), reinterpret_cast(&header) + sizeof(header)); + buffer.assign(header.begin(), header.end()); return WriteAll(buffer, BLOCK_SIZE); } @@ -451,14 +451,16 @@ bool TarFile::WriteLongName(string &name, char type) // write long name head to archive if (WriteTarHeader(tmp) != BLOCK_SIZE) { + HILOGE("Failed to write long name header"); return false; } // write name to archive vector buffer {}; buffer.resize(sz); - buffer.assign(reinterpret_cast(&name), reinterpret_cast(&name) + sz); + buffer.assign(name.begin(), name.end()); if (WriteAll(buffer, sz) != sz) { + HILOGE("Failed to write long name buffer"); return false; } diff --git a/frameworks/native/backup_ext/src/untar_file.cpp b/frameworks/native/backup_ext/src/untar_file.cpp index 6e8ff73f2fd4a9438110061fcf8f4a7a9657d785..0e188ab36ec12c828398980a6364e01673cfaf97 100644 --- a/frameworks/native/backup_ext/src/untar_file.cpp +++ b/frameworks/native/backup_ext/src/untar_file.cpp @@ -55,6 +55,21 @@ static off_t ParseOctalStr(const string &octalStr, size_t destLen) return ret; } +static bool ForceCreateDirectoryWithMode(const string& path, mode_t mode) +{ + string::size_type index = 0; + do { + index = path.find('/', index + 1); + string subPath = (index == string::npos) ? path : path.substr(0, index); + if (access(subPath.c_str(), F_OK) != 0) { + if (mkdir(subPath.c_str(), mode) != 0) { + return false; + } + } + } while (index != string::npos); + return access(path.c_str(), F_OK) == 0; +} + UntarFile &UntarFile::GetInstance() { static UntarFile instance; @@ -93,6 +108,7 @@ void UntarFile::HandleTarBuffer(const string &buff, const string &name, FileStat string realName = name; if (!info.longName.empty()) { realName = info.longName; + info.longName.clear(); } info.fullPath = GenRealPath(rootPath_, realName); } @@ -134,6 +150,7 @@ int UntarFile::ParseTarFile(const string &rootPath) HILOGE("Invalid tar file format"); ret = ERR_FORMAT; } + HILOGE("invalid tar block"); return ret; } HandleTarBuffer(string(buff, BLOCK_SIZE), header->name, info); @@ -164,7 +181,10 @@ void UntarFile::ParseFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &i case GNUTYPE_LONGNAME: { size_t nameLen = static_cast(tarFileSize_); if (nameLen < PATH_MAX_LEN) { - fread(&(info.longName[0]), sizeof(char), nameLen, tarFilePtr_); + string tempName(""); + tempName.resize(nameLen); + fread(&(tempName[0]), sizeof(char), nameLen, tarFilePtr_); + info.longName = tempName; } isSkip = true; fseeko(tarFilePtr_, pos_ + tarFileBlockCnt_ * BLOCK_SIZE, SEEK_SET); @@ -275,9 +295,9 @@ void UntarFile::CreateDir(string &path, mode_t mode) path[len - 1] = '\0'; } if (access(path.c_str(), F_OK) != 0) { - HILOGE("%{public}s does not exist, err = %{public}d", path.c_str(), errno); - if (mkdir(path.c_str(), mode) != 0) { - HILOGE("Failed to mkdir %{public}s, err = %{public}d", path.c_str(), errno); + HILOGE("directory does not exist, path:%{public}s, err = %{public}d", path.c_str(), errno); + if (!ForceCreateDirectoryWithMode(path, mode)) { + HILOGE("Failed to force create directory %{public}s, err = %{public}d", path.c_str(), errno); } } }