From 8aeb6ad4ed9cde635b7fb66ff5c4cb4c76f4a101 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Sun, 22 Oct 2023 17:42:55 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85-?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0isUserTar=E5=88=B0manage.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 5 +++ .../include/b_json/b_json_entity_ext_manage.h | 2 + utils/src/b_json/b_json_entity_ext_manage.cpp | 41 ++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 10feae97b..3c6b3ad87 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -375,6 +375,11 @@ static bool IsAllFileReceived(vector tars) auto cache = cachedEntity.Structuralize(); set info = cache.GetExtManage(); + map infoMap = cache.GetExtManageMap(); + for (auto it = infoMap.begin(); it != infoMap.end(); ++it) { + HILOGD("GetExtManageMap, fileName:%{public}s, isUserTar:%{public}d", it->first.c_str(), it->second); + } + // 从数量上判断是否已经全部收到 if (tars.size() <= info.size()) { return false; diff --git a/utils/include/b_json/b_json_entity_ext_manage.h b/utils/include/b_json/b_json_entity_ext_manage.h index a322a5590..38f6cb213 100644 --- a/utils/include/b_json/b_json_entity_ext_manage.h +++ b/utils/include/b_json/b_json_entity_ext_manage.h @@ -40,6 +40,8 @@ public: * @return std::set */ std::set GetExtManage() const; + std::map GetExtManageMap() const; + /** * @brief 获取索引文件及详细信息 diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index d367ff3ff..a136c7480 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -27,6 +27,18 @@ namespace OHOS::FileManagement::Backup { using namespace std; +bool IsCompressedFile(const string &filePath) { + // ȡļչ + size_t lastDotPos = filePath.find_last_of('.'); + std::string extension = filePath.substr(lastDotPos); + + // жļչǷΪѹļչ + if (extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar") { + return true; + } + return false; +} + Json::Value Stat2JsonValue(struct stat sta) { Json::Value value; @@ -110,6 +122,9 @@ void BJsonEntityExtManage::SetExtManage(const mapsecond.first; value["information"]["stat"] = Stat2JsonValue(item->second.second); + // жϸļǷûtarļ + value["isUserTar"] = IsCompressedFile(item->second.first); + set lks = FindLinks(item, index); for (const auto &lk : lks) { value["hardlinks"].append(lk); @@ -130,7 +145,7 @@ set BJsonEntityExtManage::GetExtManage() const return {}; } - set info; + set info {}; for (Json::Value &item : obj_) { string fileName = item.isObject() && item.isMember("fileName") && item["fileName"].isString() ? item["fileName"].asString() @@ -140,6 +155,30 @@ set BJsonEntityExtManage::GetExtManage() const return info; } +map BJsonEntityExtManage::GetExtManageMap() const +{ + if (!obj_) { + HILOGE("Uninitialized JSon Object reference"); + return {}; + } + if (!obj_.isArray()) { + HILOGE("json object isn't an array"); + return {}; + } + + map info {}; + for (Json::Value &item : obj_) { + string fileName = item.isObject() && item.isMember("fileName") && item["fileName"].isString() + ? item["fileName"].asString() + : ""; + bool isUserTar = item.isObject() && item.isMember("isUserTar") && item["isUserTar"].isBool() + ? item["isUserTar"].asBool() + : false; + info.emplace(make_pair(fileName, isUserTar)); + } + return info; +} + map> BJsonEntityExtManage::GetExtManageInfo() const { if (!obj_) { -- Gitee From 709f503299d819d0ac30a6df10ec905bb8c196d5 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Mon, 23 Oct 2023 17:48:28 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 45 +++++++++++-------- .../include/b_json/b_json_entity_ext_manage.h | 1 - utils/src/b_json/b_json_entity_ext_manage.cpp | 12 +++-- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 3c6b3ad87..aab62ad43 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -402,14 +402,17 @@ static void RestoreBigFiles() auto cache = cachedEntity.Structuralize(); auto info = cache.GetExtManageInfo(); - for (auto &item : info) { - if (item.first.empty() || item.first.size() < BConstants::BIG_FILE_NAME_SIZE || - item.first.rfind('.') != string::npos) { - continue; - } - string fileName = path + item.first; - auto [filePath, sta] = item.second; + auto infoMap = cache.GetExtManageMap(); + for (auto &itemMap : infoMap) { + if (itemMap.second == false) { + for (auto &item : info) { + if (item.first.empty() || item.first.size() < BConstants::BIG_FILE_NAME_SIZE || + item.first.rfind('.') != string::npos) { + continue; + } + string fileName = path + item.first; + auto [filePath, sta] = item.second; if (access(fileName.data(), F_OK) != 0) { HILOGI("file dose not exist"); @@ -434,13 +437,15 @@ static void RestoreBigFiles() } } - struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; - UniqueFd fd(open(filePath.data(), O_RDONLY)); - if (futimens(fd.Get(), tv) != 0) { - HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); - } + struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; + UniqueFd fd(open(filePath.data(), O_RDONLY)); + if (futimens(fd.Get(), tv) != 0) { + HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); + } + } } } +} void BackupExtExtension::AsyncTaskRestore() { @@ -454,13 +459,15 @@ void BackupExtExtension::AsyncTaskRestore() return; } - // 解压 - int ret = ERR_OK; - for (auto item : tars) { - if (ExtractFileExt(item) == "tar") { - ret = ptr->DoRestore(item); - } - } + + // 解压 + int ret = ERR_OK; + for (auto item : tars) { + if (item.compare("backup") == 0 && ExtractFileExt(item) == "tar") { + ret = ptr->DoRestore(item); + } + } + // 恢复大文件 RestoreBigFiles(); diff --git a/utils/include/b_json/b_json_entity_ext_manage.h b/utils/include/b_json/b_json_entity_ext_manage.h index 38f6cb213..7410aa541 100644 --- a/utils/include/b_json/b_json_entity_ext_manage.h +++ b/utils/include/b_json/b_json_entity_ext_manage.h @@ -42,7 +42,6 @@ public: std::set GetExtManage() const; std::map GetExtManageMap() const; - /** * @brief 获取索引文件及详细信息 * diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index a136c7480..2958e083e 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -28,14 +28,18 @@ namespace OHOS::FileManagement::Backup { using namespace std; bool IsCompressedFile(const string &filePath) { - // ȡļչ + // ??????? size_t lastDotPos = filePath.find_last_of('.'); std::string extension = filePath.substr(lastDotPos); - // жļչǷΪѹļչ - if (extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar") { + // ????????????????? ?? ?tar???????????tar????? + if ((extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar" ) && std::count(filePath.begin(), filePath.end(), '/') >= 2) { + size_t pos = filePath.rfind('/'); + std::string directory = filePath.substr(0, pos); + if (directory.find("backup") != std::string::npos) { return true; } + } return false; } @@ -122,7 +126,7 @@ void BJsonEntityExtManage::SetExtManage(const mapsecond.first; value["information"]["stat"] = Stat2JsonValue(item->second.second); - // жϸļǷûtarļ + // ??isUserTar?? value["isUserTar"] = IsCompressedFile(item->second.first); set lks = FindLinks(item, index); -- Gitee From bbb631bcc5fd13ceab0cc404aa5e0d7764637a9e Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Mon, 23 Oct 2023 20:15:47 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index aab62ad43..847d47167 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -459,13 +459,23 @@ void BackupExtExtension::AsyncTaskRestore() return; } + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string indexFile = path + string(BConstants::EXT_BACKUP_MANAGE); + + BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); + auto cache = cachedEntity.Structuralize(); + auto infoMap = cache.GetExtManageMap(); + // 解压 int ret = ERR_OK; - for (auto item : tars) { - if (item.compare("backup") == 0 && ExtractFileExt(item) == "tar") { - ret = ptr->DoRestore(item); - } + + for (auto item : tars) { + auto it = infoMap.find(item); // 查找item在infoMap中的迭代器 + if (ExtractFileExt(item) == "tar" && it->second == false){ + ret = ptr->DoRestore(item); + } + } // 恢复大文件 -- Gitee From 6c3ea14fae7fe1508c50e7ccdca2ea316d025638 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 24 Oct 2023 14:11:48 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 53 +++++++++++-------- .../include/b_json/b_json_entity_ext_manage.h | 2 + utils/src/b_json/b_json_entity_ext_manage.cpp | 38 +++++++++++-- 3 files changed, 67 insertions(+), 26 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 847d47167..c9a8ae2ac 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -375,7 +375,8 @@ static bool IsAllFileReceived(vector tars) auto cache = cachedEntity.Structuralize(); set info = cache.GetExtManage(); - map infoMap = cache.GetExtManageMap(); + // map> infoMap = cache.GetExtManageMap(); + auto infoMap = cache.GetExtManageMap(); for (auto it = infoMap.begin(); it != infoMap.end(); ++it) { HILOGD("GetExtManageMap, fileName:%{public}s, isUserTar:%{public}d", it->first.c_str(), it->second); } @@ -403,9 +404,10 @@ static void RestoreBigFiles() auto info = cache.GetExtManageInfo(); - auto infoMap = cache.GetExtManageMap(); - for (auto &itemMap : infoMap) { - if (itemMap.second == false) { + auto usertar = cache.GetUsertar(path); + + //std::tie(isUserTar, isbigfile) = item.second; + if (usertar == false){ for (auto &item : info) { if (item.first.empty() || item.first.size() < BConstants::BIG_FILE_NAME_SIZE || item.first.rfind('.') != string::npos) { @@ -443,8 +445,8 @@ static void RestoreBigFiles() HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); } } - } -} + } + } void BackupExtExtension::AsyncTaskRestore() @@ -458,25 +460,34 @@ void BackupExtExtension::AsyncTaskRestore() if (!IsAllFileReceived(tars)) { return; } + + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string indexFile = path + string(BConstants::EXT_BACKUP_MANAGE); + BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); + auto cache = cachedEntity.Structuralize(); - string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); - string indexFile = path + string(BConstants::EXT_BACKUP_MANAGE); - BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); - auto cache = cachedEntity.Structuralize(); - auto infoMap = cache.GetExtManageMap(); + auto usertar = cache.GetUsertar(path); - // 解压 - int ret = ERR_OK; - - for (auto item : tars) { - auto it = infoMap.find(item); // 查找item在infoMap中的迭代器 - if (ExtractFileExt(item) == "tar" && it->second == false){ - ret = ptr->DoRestore(item); - } - - } + // 解压 + // int ret = ERR_OK; + // for (auto item : tars) { + // auto user :infoMap; + // std::tie(isUserTar, isbigfile) = user.second; + // auto it = infoMap.find(item); // 查找item在infoMap中的迭代器 + // if (ExtractFileExt(item) == "tar" && isUserTar == false){ + // ret = ptr->DoRestore(item); + // } + // } + + int ret = ERR_OK; + for (auto item : tars) { + //auto it = infoMap.find(item); // 查找item在infoMap中的迭代器 + if (ExtractFileExt(item) == "tar" && usertar == false){ + ret = ptr->DoRestore(item); + } + } // 恢复大文件 RestoreBigFiles(); diff --git a/utils/include/b_json/b_json_entity_ext_manage.h b/utils/include/b_json/b_json_entity_ext_manage.h index 7410aa541..6ef3eae1e 100644 --- a/utils/include/b_json/b_json_entity_ext_manage.h +++ b/utils/include/b_json/b_json_entity_ext_manage.h @@ -59,6 +59,8 @@ public: */ bool SetHardLinkInfo(const std::string origin, const std::set hardLinks); + bool GetUsertar(const string &fileback); + /** * @brief Get the hard link Information * diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index 2958e083e..a2c9f55ed 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -36,11 +36,11 @@ bool IsCompressedFile(const string &filePath) { if ((extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar" ) && std::count(filePath.begin(), filePath.end(), '/') >= 2) { size_t pos = filePath.rfind('/'); std::string directory = filePath.substr(0, pos); - if (directory.find("backup") != std::string::npos) { - return true; - } + if (directory.find(BConstants::PATH_BUNDLE_BACKUP_HOME.data()) != std::string::npos) { + return false; + } } - return false; + return true; } Json::Value Stat2JsonValue(struct stat sta) @@ -219,6 +219,34 @@ map> BJsonEntityExtManage::GetExtManageInfo() return info; } +bool BJsonEntityExtManage::GetUsertar(const string &fileback) +{ + if (fileback.empty()) { + HILOGE("file file name can not empty"); + return false; + } + if (!obj_) { + HILOGE("Uninitialized JSon Object reference"); + return {}; + } + if (!obj_.isArray()) { + HILOGE("json object isn't an array"); + return {}; + } + + + if (std::count(fileback.begin(), fileback.end(), '/') >= 2) { + size_t pos = fileback.rfind('/'); + std::string directory = fileback.substr(0, pos); + if (directory.find(BConstants::PATH_BUNDLE_BACKUP_HOME.data()) != std::string::npos) { + return false; + } + } + return true; + +} + + bool BJsonEntityExtManage::SetHardLinkInfo(const string origin, const set hardLinks) { if (origin.empty()) { @@ -272,7 +300,7 @@ const set BJsonEntityExtManage::GetHardLinkInfo(const string origin) string fileName = item.isMember("fileName") && item["fileName"].isString() ? item["fileName"].asString() : ""; if (origin != fileName) { continue; - } + } if (!(item.isMember("hardlinks") && item["hardlinks"].isArray())) { break; } -- Gitee From d0d3210304a894f29db7f47c3111592e763f7fb7 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 24 Oct 2023 16:38:57 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E5=88=86=E7=89=87=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- frameworks/native/backup_ext/BUILD.gn | 1 + .../native/backup_ext/include/ext_tarfile.h | 163 ++++++++++++++++++ .../native/backup_ext/include/ext_untarfile.h | 46 +++++ 3 files changed, 210 insertions(+) create mode 100644 frameworks/native/backup_ext/include/ext_tarfile.h create mode 100644 frameworks/native/backup_ext/include/ext_untarfile.h diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index ec99742a2..2c0f9d0ee 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -21,6 +21,7 @@ ohos_shared_library("backup_extension_ability_native") { "src/ext_backup_loader.cpp", "src/ext_extension.cpp", "src/ext_extension_stub.cpp", + "src/ext_tarfile.cpp", ] defines = [ diff --git a/frameworks/native/backup_ext/include/ext_tarfile.h b/frameworks/native/backup_ext/include/ext_tarfile.h new file mode 100644 index 000000000..9907844b9 --- /dev/null +++ b/frameworks/native/backup_ext/include/ext_tarfile.h @@ -0,0 +1,163 @@ +#ifndef EXT_TARFILE_H_ +#define EXT_TARFILE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* ustar and a null */ +#define TMAGIC "ustar" +#define TMAGIC_LEN 6 + +#define TMODE_BASE 100 +#define TMODE_LEN 8 +#define TUID_BASE 108 +#define TUID_LEN 8 +#define TGID_BASE 116 +#define TGID_LEN 8 +#define TSIZE_BASE 124 +#define TSIZE_LEN 12 + +#define CHKSUM_BASE 148 +#define CHKSUM_LEN 8 + +#define BLOCK_SIZE 512 +#define BLANK_SPACE 0x20 + +#define PATH_MAX_LEN 2048 +#define READ_BUFF_SIZE (512 * 1024) +const int MB_TO_BYTE = (1024 * 1024); + +const int SPLIT_MAX_CMD_LEN = 7; + +#define REGTYPE '0' // regular file +#define AREGTYPE '\0' // regular file +#define SYMTYPE '2' // reserved +#define DIRTYPE '5' // directory +#define SPLIT_START_TYPE '8' +#define SPLIT_CONTINUE_TYPE '9' +#define SPLIT_END_TYPE 'A' + +#define GNUTYPE_LONGLINK 'K' +#define GNUTYPE_LONGNAME 'L' + +#define PERMISSION_MASK 07777 +#define MAX_FILESIZE 0777777777777LL + +#define OCTSTRING_LENGTH (sizeof(off_t) * 3 + 1) + + +#define DO_TRAVERSAL 1 +#define DO_ERROR 2 +#define DO_IGNORETHIS 3 +#define VERSION "verston" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +// typedef int (*TarCallBack)(const char*, unsigned int); +class TarCallBack { +public: + TarCallBack() {}; + ~TarCallBack() = default; + + bool IsAborted() const { return false; } + void OnTaskProgress(int read) {} + void WaitTaskLocked() {} + void OnPackagedOneSplitTar(string){} +}; + +typedef struct TarHeader { + char name[100]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char typeflag; + char linkname[100]; + char magic[6]; + char version[2]; + char uname[32]; + char gname[32]; + char devmajor[8]; + char devminor[8]; + char prefix[155]; + char pad[12]; +} TarHeader; + + + +class ExtTarfile { +public: + ExtTarfile(TarCallBack* cb); + ExtTarfile(TarCallBack* cb, const size_t index); + map> PackSplitDir(const std::string &srcDir, const std::string &tarFileName,std::string args[], uid_t owner); + +private: + bool PackSplitPre(std::string args[], uid_t owner); + bool AddFile(const char *name, const struct stat *st, bool isSplit); + int DoPack(const char* path); + bool TraversalPackDir(char* currentdir); + bool WriteFileContent(const char* name, off_t size); + int SplitWriteAll(const char* buf, int len, bool& isFilled); + bool CreateSplitFile(); + bool CompleteBlock(int size); + bool FillSplitTailBlocks(bool isEnd); + bool FillSplitHeaderBlocks(); + void FlushTarSizeAndFlag(bool isSplit, const off_t fileSize, off_t& hdrSize, char& typeFlag); + bool IsFirstSplitPKT(const off_t fileSize); + off_t GetSizeOfFile(const off_t fileSize); + off_t GetSplitPKTRemainSize(); + off_t GetFirstSizeOfSplitFile(const off_t fileSize); + string HandleSplitInfo(std::string tarName, unsigned long long tarSize, unsigned int num); + bool CreateCheckFile(); + void FillSum(TarHeader& hdr); + void FillOwnerName(TarHeader& hdr, const struct stat* st); + bool FillLongString(char* target, int targetlen, const char* name, char type); + bool FillLinkName(char* target, int targetlen, const char* name, int size); + int ReadAll(int fd, char* buf, int len); + int WriteAll(const char* buf, int len); + bool ReadLink(const char* name, char* buf, int size); + void I2Ocs(char *str, int len, off_t val); + + TarCallBack* m_pCB; + + char * m_pFileIOBuf; + //std::string m_pFileIOBuf; + + char* m_szRootPath; + //char* m_szRootPath[PATH_MAX_LEN]; + struct stat m_stTar; + FILE*__restrict m_fdTar; + //std::ifstream* m_fdTar; + bool m_Split; + //std::vector m_args; + std::string m_args[SPLIT_MAX_CMD_LEN]; + uid_t m_TaskOwner; + + // the file adding to tar file + string m_curFile; + off_t m_fileRemainSize; + off_t m_cursorFileSize; + + std::string m_baseTarName; + unsigned int m_number; + std::string m_currentTarName; + off_t m_slice; + + // contain the sizeof TAR and the size of padding block + off_t m_tarFileSize; + vector m_splitSize; + size_t m_lockIndex; +}; +}// namespace OHOS::FileManagement::Backup +#endif // EXT_TARFILE_H_ \ No newline at end of file diff --git a/frameworks/native/backup_ext/include/ext_untarfile.h b/frameworks/native/backup_ext/include/ext_untarfile.h new file mode 100644 index 000000000..690d0eb0f --- /dev/null +++ b/frameworks/native/backup_ext/include/ext_untarfile.h @@ -0,0 +1,46 @@ +#ifndef EXT_UNTARFILE_H_ +#define EXT_UNTARFILE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace OHOS::FileManagement::Backup { +using namespace std; + +typedef int (*TarCallBack)(const char*, unsigned int); + +class ExtUnTarfile { +public: + //void UnTarFile(const char *tarPath,TarCallBack* cb); + static map> untarfile(const std::string& path , const std::string& pathName); + + typedef enum { + eList = 0, + eUnpack = 1 + } EParseType; + + + +private: + + //FILE* FilePtr; + //off_t tarSize; + //uid_t newOwner; + std::string m_srcPath; + //bool isSplit = false; + + //TarCallBack* pTarCallback; + + std::vector file_names; + std::vector file_sizes; + std::vector file_data_addrs; + +}; +}// namespace OHOS::FileManagement::Backup +#endif // EXT_UNTARFILE_H_ \ No newline at end of file -- Gitee From 466e80892b09d59dadc8eb64c9edba2817cf6598 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 24 Oct 2023 21:55:25 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- frameworks/native/backup_ext/BUILD.gn | 1 - .../native/backup_ext/src/ext_extension.cpp | 43 ++++++---------- .../include/b_json/b_json_entity_ext_manage.h | 5 +- utils/src/b_json/b_json_entity_ext_manage.cpp | 50 ++++++++----------- 4 files changed, 38 insertions(+), 61 deletions(-) diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index 2c0f9d0ee..ec99742a2 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -21,7 +21,6 @@ ohos_shared_library("backup_extension_ability_native") { "src/ext_backup_loader.cpp", "src/ext_extension.cpp", "src/ext_extension_stub.cpp", - "src/ext_tarfile.cpp", ] defines = [ diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index c9a8ae2ac..d146dce84 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -377,9 +377,9 @@ static bool IsAllFileReceived(vector tars) // map> infoMap = cache.GetExtManageMap(); auto infoMap = cache.GetExtManageMap(); - for (auto it = infoMap.begin(); it != infoMap.end(); ++it) { - HILOGD("GetExtManageMap, fileName:%{public}s, isUserTar:%{public}d", it->first.c_str(), it->second); - } + // for (auto it = infoMap.begin(); it != infoMap.end(); ++it) { + // HILOGD("GetExtManageMap, fileName:%{public}s, isUserTar:%{public}d", it->first.c_str(), it->second); + // } // 从数量上判断是否已经全部收到 if (tars.size() <= info.size()) { @@ -402,15 +402,13 @@ static void RestoreBigFiles() BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); auto info = cache.GetExtManageInfo(); - - - auto usertar = cache.GetUsertar(path); - - //std::tie(isUserTar, isbigfile) = item.second; - if (usertar == false){ + + auto userfile = cache.GetExtManageMap(); + auto isusertar = userfile.find("isUserTar") != userfile.end(); + auto isbigfile = userfile.find("isBigFile") != userfile.end(); + if (isusertar ==true || isbigfile == true) { for (auto &item : info) { - if (item.first.empty() || item.first.size() < BConstants::BIG_FILE_NAME_SIZE || - item.first.rfind('.') != string::npos) { + if (item.first.empty() ||item.first.rfind('.') != string::npos) { continue; } string fileName = path + item.first; @@ -466,25 +464,16 @@ void BackupExtExtension::AsyncTaskRestore() BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); - - auto usertar = cache.GetUsertar(path); - - - // 解压 - // int ret = ERR_OK; - // for (auto item : tars) { - // auto user :infoMap; - // std::tie(isUserTar, isbigfile) = user.second; - // auto it = infoMap.find(item); // 查找item在infoMap中的迭代器 - // if (ExtractFileExt(item) == "tar" && isUserTar == false){ - // ret = ptr->DoRestore(item); - // } - // } + + int ret = ERR_OK; for (auto item : tars) { - //auto it = infoMap.find(item); // 查找item在infoMap中的迭代器 - if (ExtractFileExt(item) == "tar" && usertar == false){ + //auto it = userfile.find(item); // 查找item在infoMap中的迭代器 + //auto usertar = cache.GetUsertar(path); + auto userfile = cache.GetExtManageMap(); + auto isusertar = userfile.find("isUserTar") != userfile.end(); + if (ExtractFileExt(item) == "tar" && isusertar== false){ ret = ptr->DoRestore(item); } } diff --git a/utils/include/b_json/b_json_entity_ext_manage.h b/utils/include/b_json/b_json_entity_ext_manage.h index 6ef3eae1e..bc76b7c3e 100644 --- a/utils/include/b_json/b_json_entity_ext_manage.h +++ b/utils/include/b_json/b_json_entity_ext_manage.h @@ -40,8 +40,7 @@ public: * @return std::set */ std::set GetExtManage() const; - std::map GetExtManageMap() const; - + std::map> GetExtManageMap() const; /** * @brief 获取索引文件及详细信息 * @@ -59,7 +58,7 @@ public: */ bool SetHardLinkInfo(const std::string origin, const std::set hardLinks); - bool GetUsertar(const string &fileback); + // bool GetUsertar(const string &fileback); /** * @brief Get the hard link Information diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index a2c9f55ed..6f11a250c 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -42,6 +42,14 @@ bool IsCompressedFile(const string &filePath) { } return true; } + // жϽѹļ +bool IsBigFile(const string &tarfile) { + + if ( tarfile.size() < BConstants::BIG_FILE_NAME_SIZE ) { + return false; + } + return true; +} Json::Value Stat2JsonValue(struct stat sta) { @@ -126,9 +134,9 @@ void BJsonEntityExtManage::SetExtManage(const mapsecond.first; value["information"]["stat"] = Stat2JsonValue(item->second.second); - // ??isUserTar?? + // isUserTarֶ value["isUserTar"] = IsCompressedFile(item->second.first); - + value["isbigfile"] = IsBigFile(item->second.first); set lks = FindLinks(item, index); for (const auto &lk : lks) { value["hardlinks"].append(lk); @@ -159,7 +167,7 @@ set BJsonEntityExtManage::GetExtManage() const return info; } -map BJsonEntityExtManage::GetExtManageMap() const +map> BJsonEntityExtManage::GetExtManageMap() const { if (!obj_) { HILOGE("Uninitialized JSon Object reference"); @@ -170,7 +178,7 @@ map BJsonEntityExtManage::GetExtManageMap() const return {}; } - map info {}; + map> info {}; for (Json::Value &item : obj_) { string fileName = item.isObject() && item.isMember("fileName") && item["fileName"].isString() ? item["fileName"].asString() @@ -178,11 +186,18 @@ map BJsonEntityExtManage::GetExtManageMap() const bool isUserTar = item.isObject() && item.isMember("isUserTar") && item["isUserTar"].isBool() ? item["isUserTar"].asBool() : false; - info.emplace(make_pair(fileName, isUserTar)); + bool isbigfile = item.isObject() && item.isMember("isbigfile") && item["isbigfile"].isBool() + ? item["isbigfile"].asBool() + : false; + + pair> infoPair(fileName, std::make_tuple(isUserTar, isbigfile)); + info.emplace(infoPair); + //info.emplace(make_pair(fileName, {isUserTar,isbigfile})); } return info; } + map> BJsonEntityExtManage::GetExtManageInfo() const { if (!obj_) { @@ -219,33 +234,8 @@ map> BJsonEntityExtManage::GetExtManageInfo() return info; } -bool BJsonEntityExtManage::GetUsertar(const string &fileback) -{ - if (fileback.empty()) { - HILOGE("file file name can not empty"); - return false; - } - if (!obj_) { - HILOGE("Uninitialized JSon Object reference"); - return {}; - } - if (!obj_.isArray()) { - HILOGE("json object isn't an array"); - return {}; - } - if (std::count(fileback.begin(), fileback.end(), '/') >= 2) { - size_t pos = fileback.rfind('/'); - std::string directory = fileback.substr(0, pos); - if (directory.find(BConstants::PATH_BUNDLE_BACKUP_HOME.data()) != std::string::npos) { - return false; - } - } - return true; - -} - bool BJsonEntityExtManage::SetHardLinkInfo(const string origin, const set hardLinks) { -- Gitee From 44e68a544184201514ace2d2764bad9b410b5ba3 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 25 Oct 2023 09:24:06 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- utils/src/b_json/b_json_entity_ext_manage.cpp | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index 6f11a250c..a57823399 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -27,28 +27,31 @@ namespace OHOS::FileManagement::Backup { using namespace std; -bool IsCompressedFile(const string &filePath) { - // ??????? - size_t lastDotPos = filePath.find_last_of('.'); - std::string extension = filePath.substr(lastDotPos); +bool IsCompressedFile(const string &filePath) +{ + // ??????? + size_t lastDotPos = filePath.find_last_of('.'); + std::string extension = filePath.substr(lastDotPos); // ????????????????? ?? ?tar???????????tar????? - if ((extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar" ) && std::count(filePath.begin(), filePath.end(), '/') >= 2) { - size_t pos = filePath.rfind('/'); - std::string directory = filePath.substr(0, pos); - if (directory.find(BConstants::PATH_BUNDLE_BACKUP_HOME.data()) != std::string::npos) { - return false; + if ((extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar") && + std::count(filePath.begin(), filePath.end(), '/') >= 2) { + size_t pos = filePath.rfind('/'); + std::string directory = filePath.substr(0, pos); + if (directory.find(BConstants::PATH_BUNDLE_BACKUP_HOME.data()) != std::string::npos) { + return false; } } - return true; + return true; } - // жϽѹļ -bool IsBigFile(const string &tarfile) { - - if ( tarfile.size() < BConstants::BIG_FILE_NAME_SIZE ) { - return false; +// ????????? +bool IsBigFile(const string &tarfile) +{ + + if (tarfile.size() < BConstants::BIG_FILE_NAME_SIZE) { + return false; } - return true; + return true; } Json::Value Stat2JsonValue(struct stat sta) @@ -134,7 +137,7 @@ void BJsonEntityExtManage::SetExtManage(const mapsecond.first; value["information"]["stat"] = Stat2JsonValue(item->second.second); - // isUserTarֶ + // ??isUserTar?? value["isUserTar"] = IsCompressedFile(item->second.first); value["isbigfile"] = IsBigFile(item->second.first); set lks = FindLinks(item, index); @@ -167,7 +170,7 @@ set BJsonEntityExtManage::GetExtManage() const return info; } -map> BJsonEntityExtManage::GetExtManageMap() const +map> BJsonEntityExtManage::GetExtManageMap() const { if (!obj_) { HILOGE("Uninitialized JSon Object reference"); @@ -184,20 +187,19 @@ map> BJsonEntityExtManage::GetExtManageMap() const ? item["fileName"].asString() : ""; bool isUserTar = item.isObject() && item.isMember("isUserTar") && item["isUserTar"].isBool() - ? item["isUserTar"].asBool() - : false; + ? item["isUserTar"].asBool() + : false; bool isbigfile = item.isObject() && item.isMember("isbigfile") && item["isbigfile"].isBool() - ? item["isbigfile"].asBool() - : false; + ? item["isbigfile"].asBool() + : false; pair> infoPair(fileName, std::make_tuple(isUserTar, isbigfile)); - info.emplace(infoPair); - //info.emplace(make_pair(fileName, {isUserTar,isbigfile})); + info.emplace(infoPair); + // info.emplace(make_pair(fileName, {isUserTar,isbigfile})); } return info; } - map> BJsonEntityExtManage::GetExtManageInfo() const { if (!obj_) { @@ -234,9 +236,6 @@ map> BJsonEntityExtManage::GetExtManageInfo() return info; } - - - bool BJsonEntityExtManage::SetHardLinkInfo(const string origin, const set hardLinks) { if (origin.empty()) { @@ -290,7 +289,7 @@ const set BJsonEntityExtManage::GetHardLinkInfo(const string origin) string fileName = item.isMember("fileName") && item["fileName"].isString() ? item["fileName"].asString() : ""; if (origin != fileName) { continue; - } + } if (!(item.isMember("hardlinks") && item["hardlinks"].isArray())) { break; } -- Gitee From 15e2bd6e4d29bbc40284bb0d71ab5a7d2bb4d3e7 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 25 Oct 2023 13:07:04 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/include/ext_tarfile.h | 163 ------------------ .../native/backup_ext/include/ext_untarfile.h | 46 ----- 2 files changed, 209 deletions(-) delete mode 100644 frameworks/native/backup_ext/include/ext_tarfile.h delete mode 100644 frameworks/native/backup_ext/include/ext_untarfile.h diff --git a/frameworks/native/backup_ext/include/ext_tarfile.h b/frameworks/native/backup_ext/include/ext_tarfile.h deleted file mode 100644 index 9907844b9..000000000 --- a/frameworks/native/backup_ext/include/ext_tarfile.h +++ /dev/null @@ -1,163 +0,0 @@ -#ifndef EXT_TARFILE_H_ -#define EXT_TARFILE_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -/* ustar and a null */ -#define TMAGIC "ustar" -#define TMAGIC_LEN 6 - -#define TMODE_BASE 100 -#define TMODE_LEN 8 -#define TUID_BASE 108 -#define TUID_LEN 8 -#define TGID_BASE 116 -#define TGID_LEN 8 -#define TSIZE_BASE 124 -#define TSIZE_LEN 12 - -#define CHKSUM_BASE 148 -#define CHKSUM_LEN 8 - -#define BLOCK_SIZE 512 -#define BLANK_SPACE 0x20 - -#define PATH_MAX_LEN 2048 -#define READ_BUFF_SIZE (512 * 1024) -const int MB_TO_BYTE = (1024 * 1024); - -const int SPLIT_MAX_CMD_LEN = 7; - -#define REGTYPE '0' // regular file -#define AREGTYPE '\0' // regular file -#define SYMTYPE '2' // reserved -#define DIRTYPE '5' // directory -#define SPLIT_START_TYPE '8' -#define SPLIT_CONTINUE_TYPE '9' -#define SPLIT_END_TYPE 'A' - -#define GNUTYPE_LONGLINK 'K' -#define GNUTYPE_LONGNAME 'L' - -#define PERMISSION_MASK 07777 -#define MAX_FILESIZE 0777777777777LL - -#define OCTSTRING_LENGTH (sizeof(off_t) * 3 + 1) - - -#define DO_TRAVERSAL 1 -#define DO_ERROR 2 -#define DO_IGNORETHIS 3 -#define VERSION "verston" - -namespace OHOS::FileManagement::Backup { -using namespace std; - -// typedef int (*TarCallBack)(const char*, unsigned int); -class TarCallBack { -public: - TarCallBack() {}; - ~TarCallBack() = default; - - bool IsAborted() const { return false; } - void OnTaskProgress(int read) {} - void WaitTaskLocked() {} - void OnPackagedOneSplitTar(string){} -}; - -typedef struct TarHeader { - char name[100]; - char mode[8]; - char uid[8]; - char gid[8]; - char size[12]; - char mtime[12]; - char chksum[8]; - char typeflag; - char linkname[100]; - char magic[6]; - char version[2]; - char uname[32]; - char gname[32]; - char devmajor[8]; - char devminor[8]; - char prefix[155]; - char pad[12]; -} TarHeader; - - - -class ExtTarfile { -public: - ExtTarfile(TarCallBack* cb); - ExtTarfile(TarCallBack* cb, const size_t index); - map> PackSplitDir(const std::string &srcDir, const std::string &tarFileName,std::string args[], uid_t owner); - -private: - bool PackSplitPre(std::string args[], uid_t owner); - bool AddFile(const char *name, const struct stat *st, bool isSplit); - int DoPack(const char* path); - bool TraversalPackDir(char* currentdir); - bool WriteFileContent(const char* name, off_t size); - int SplitWriteAll(const char* buf, int len, bool& isFilled); - bool CreateSplitFile(); - bool CompleteBlock(int size); - bool FillSplitTailBlocks(bool isEnd); - bool FillSplitHeaderBlocks(); - void FlushTarSizeAndFlag(bool isSplit, const off_t fileSize, off_t& hdrSize, char& typeFlag); - bool IsFirstSplitPKT(const off_t fileSize); - off_t GetSizeOfFile(const off_t fileSize); - off_t GetSplitPKTRemainSize(); - off_t GetFirstSizeOfSplitFile(const off_t fileSize); - string HandleSplitInfo(std::string tarName, unsigned long long tarSize, unsigned int num); - bool CreateCheckFile(); - void FillSum(TarHeader& hdr); - void FillOwnerName(TarHeader& hdr, const struct stat* st); - bool FillLongString(char* target, int targetlen, const char* name, char type); - bool FillLinkName(char* target, int targetlen, const char* name, int size); - int ReadAll(int fd, char* buf, int len); - int WriteAll(const char* buf, int len); - bool ReadLink(const char* name, char* buf, int size); - void I2Ocs(char *str, int len, off_t val); - - TarCallBack* m_pCB; - - char * m_pFileIOBuf; - //std::string m_pFileIOBuf; - - char* m_szRootPath; - //char* m_szRootPath[PATH_MAX_LEN]; - struct stat m_stTar; - FILE*__restrict m_fdTar; - //std::ifstream* m_fdTar; - bool m_Split; - //std::vector m_args; - std::string m_args[SPLIT_MAX_CMD_LEN]; - uid_t m_TaskOwner; - - // the file adding to tar file - string m_curFile; - off_t m_fileRemainSize; - off_t m_cursorFileSize; - - std::string m_baseTarName; - unsigned int m_number; - std::string m_currentTarName; - off_t m_slice; - - // contain the sizeof TAR and the size of padding block - off_t m_tarFileSize; - vector m_splitSize; - size_t m_lockIndex; -}; -}// namespace OHOS::FileManagement::Backup -#endif // EXT_TARFILE_H_ \ No newline at end of file diff --git a/frameworks/native/backup_ext/include/ext_untarfile.h b/frameworks/native/backup_ext/include/ext_untarfile.h deleted file mode 100644 index 690d0eb0f..000000000 --- a/frameworks/native/backup_ext/include/ext_untarfile.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef EXT_UNTARFILE_H_ -#define EXT_UNTARFILE_H_ - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace OHOS::FileManagement::Backup { -using namespace std; - -typedef int (*TarCallBack)(const char*, unsigned int); - -class ExtUnTarfile { -public: - //void UnTarFile(const char *tarPath,TarCallBack* cb); - static map> untarfile(const std::string& path , const std::string& pathName); - - typedef enum { - eList = 0, - eUnpack = 1 - } EParseType; - - - -private: - - //FILE* FilePtr; - //off_t tarSize; - //uid_t newOwner; - std::string m_srcPath; - //bool isSplit = false; - - //TarCallBack* pTarCallback; - - std::vector file_names; - std::vector file_sizes; - std::vector file_data_addrs; - -}; -}// namespace OHOS::FileManagement::Backup -#endif // EXT_UNTARFILE_H_ \ No newline at end of file -- Gitee From eab8547863fd4714098251f93f3f9df78ad8ad06 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 25 Oct 2023 14:23:11 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 105 ++++++++---------- utils/src/b_json/b_json_entity_ext_manage.cpp | 6 +- 2 files changed, 46 insertions(+), 65 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index d146dce84..6cc88e421 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -106,9 +106,9 @@ ErrCode BackupExtExtension::HandleClear() static ErrCode IndexFileReady(const map> &pkgInfo, sptr proxy) { - string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME). - append(BConstants::SA_BUNDLE_BACKUP_BACKUP). - append(BConstants::EXT_BACKUP_MANAGE); + string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME) + .append(BConstants::SA_BUNDLE_BACKUP_BACKUP) + .append(BConstants::EXT_BACKUP_MANAGE); BJsonCachedEntity cachedEntity( UniqueFd(open(indexFile.data(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR))); auto cache = cachedEntity.Structuralize(); @@ -131,9 +131,9 @@ static ErrCode IndexFileReady(const map> &pkgI static ErrCode BigFileReady(sptr proxy) { - string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME). - append(BConstants::SA_BUNDLE_BACKUP_BACKUP). - append(BConstants::EXT_BACKUP_MANAGE); + string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME) + .append(BConstants::SA_BUNDLE_BACKUP_BACKUP) + .append(BConstants::EXT_BACKUP_MANAGE); BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); map> pkgInfo = cache.GetExtManageInfo(); @@ -374,12 +374,7 @@ static bool IsAllFileReceived(vector tars) BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); set info = cache.GetExtManage(); - - // map> infoMap = cache.GetExtManageMap(); auto infoMap = cache.GetExtManageMap(); - // for (auto it = infoMap.begin(); it != infoMap.end(); ++it) { - // HILOGD("GetExtManageMap, fileName:%{public}s, isUserTar:%{public}d", it->first.c_str(), it->second); - // } // 从数量上判断是否已经全部收到 if (tars.size() <= info.size()) { @@ -402,49 +397,45 @@ static void RestoreBigFiles() BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); auto info = cache.GetExtManageInfo(); - - auto userfile = cache.GetExtManageMap(); - auto isusertar = userfile.find("isUserTar") != userfile.end(); - auto isbigfile = userfile.find("isBigFile") != userfile.end(); - if (isusertar ==true || isbigfile == true) { - for (auto &item : info) { - if (item.first.empty() ||item.first.rfind('.') != string::npos) { - continue; - } - string fileName = path + item.first; - auto [filePath, sta] = item.second; - - if (access(fileName.data(), F_OK) != 0) { - HILOGI("file dose not exist"); - continue; - } - if (filePath.empty()) { - HILOGE("file path is empty. %{public}s", filePath.c_str()); - continue; - } - if (rename(fileName.data(), filePath.data()) != 0) { - HILOGE("failed to rename the file, try to copy it. err = %{public}d", errno); - if (!BFile::CopyFile(fileName, filePath)) { - HILOGE("failed to copy the file. err = %{public}d", errno); + auto userfile = cache.GetExtManageMap(); + auto isusertar = userfile.find("isUserTar") != userfile.end(); + auto isbigfile = userfile.find("isBigFile") != userfile.end(); + if (isusertar == true || isbigfile == true) { + for (auto &item : info) { + if (item.first.empty() || item.first.rfind('.') != string::npos) { continue; } - HILOGI("succeed to rename or copy the file"); - } - set lks = cache.GetHardLinkInfo(item.first); - for (const auto &lksPath : lks) { - if (link(filePath.data(), lksPath.data())) { - HILOGE("failed to create hard link file %{public}s errno : %{public}d", lksPath.c_str(), errno); + string fileName = path + item.first; + auto [filePath, sta] = item.second; + if (access(fileName.data(), F_OK) != 0) { + HILOGI("file dose not exist"); + continue; } - } - - struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; - UniqueFd fd(open(filePath.data(), O_RDONLY)); - if (futimens(fd.Get(), tv) != 0) { - HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); + if (filePath.empty()) { + HILOGE("file path is empty. %{public}s", filePath.c_str()); + continue; + } + if (rename(fileName.data(), filePath.data()) != 0) { + HILOGE("failed to rename the file, try to copy it. err = %{public}d", errno); + if (!BFile::CopyFile(fileName, filePath)) { + HILOGE("failed to copy the file. err = %{public}d", errno); + continue; } + HILOGI("succeed to rename or copy the file"); + } + set lks = cache.GetHardLinkInfo(item.first); + for (const auto &lksPath : lks) { + if (link(filePath.data(), lksPath.data())) { + HILOGE("failed to create hard link file %{public}s errno : %{public}d", lksPath.c_str(), errno); + } + } + struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; + UniqueFd fd(open(filePath.data(), O_RDONLY)); + if (futimens(fd.Get(), tv) != 0) { + HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); } } - + } } void BackupExtExtension::AsyncTaskRestore() @@ -458,24 +449,18 @@ void BackupExtExtension::AsyncTaskRestore() if (!IsAllFileReceived(tars)) { return; } - + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); string indexFile = path + string(BConstants::EXT_BACKUP_MANAGE); BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); - - - - - int ret = ERR_OK; + int ret = ERR_OK; for (auto item : tars) { - //auto it = userfile.find(item); // 查找item在infoMap中的迭代器 - //auto usertar = cache.GetUsertar(path); - auto userfile = cache.GetExtManageMap(); - auto isusertar = userfile.find("isUserTar") != userfile.end(); - if (ExtractFileExt(item) == "tar" && isusertar== false){ + auto userfile = cache.GetExtManageMap(); + auto isusertar = userfile.find("isUserTar") != userfile.end(); + if (ExtractFileExt(item) == "tar" && isusertar == false) { ret = ptr->DoRestore(item); - } + } } // 恢复大文件 diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index a57823399..2d422e707 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -29,11 +29,8 @@ using namespace std; bool IsCompressedFile(const string &filePath) { - // ??????? size_t lastDotPos = filePath.find_last_of('.'); std::string extension = filePath.substr(lastDotPos); - - // ????????????????? ?? ?tar???????????tar????? if ((extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar") && std::count(filePath.begin(), filePath.end(), '/') >= 2) { size_t pos = filePath.rfind('/'); @@ -44,7 +41,7 @@ bool IsCompressedFile(const string &filePath) } return true; } -// ????????? + bool IsBigFile(const string &tarfile) { @@ -195,7 +192,6 @@ map> BJsonEntityExtManage::GetExtManageMap() const pair> infoPair(fileName, std::make_tuple(isUserTar, isbigfile)); info.emplace(infoPair); - // info.emplace(make_pair(fileName, {isUserTar,isbigfile})); } return info; } -- Gitee From 2e9655f35b32e3d5b4dfd16bb7995be2afe7b4c9 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Fri, 27 Oct 2023 10:15:07 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 73 +++++++++---------- .../include/b_json/b_json_entity_ext_manage.h | 4 +- utils/src/b_json/b_json_entity_ext_manage.cpp | 3 +- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 6cc88e421..df762593e 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -106,9 +106,7 @@ ErrCode BackupExtExtension::HandleClear() static ErrCode IndexFileReady(const map> &pkgInfo, sptr proxy) { - string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME) - .append(BConstants::SA_BUNDLE_BACKUP_BACKUP) - .append(BConstants::EXT_BACKUP_MANAGE); + string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_BACKUP).append(BConstants::EXT_BACKUP_MANAGE); BJsonCachedEntity cachedEntity( UniqueFd(open(indexFile.data(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR))); auto cache = cachedEntity.Structuralize(); @@ -131,9 +129,7 @@ static ErrCode IndexFileReady(const map> &pkgI static ErrCode BigFileReady(sptr proxy) { - string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME) - .append(BConstants::SA_BUNDLE_BACKUP_BACKUP) - .append(BConstants::EXT_BACKUP_MANAGE); + string indexFile = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_BACKUP).append(BConstants::EXT_BACKUP_MANAGE); BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); map> pkgInfo = cache.GetExtManageInfo(); @@ -400,41 +396,44 @@ static void RestoreBigFiles() auto userfile = cache.GetExtManageMap(); auto isusertar = userfile.find("isUserTar") != userfile.end(); auto isbigfile = userfile.find("isBigFile") != userfile.end(); - if (isusertar == true || isbigfile == true) { - for (auto &item : info) { - if (item.first.empty() || item.first.rfind('.') != string::npos) { - continue; - } - string fileName = path + item.first; - auto [filePath, sta] = item.second; - if (access(fileName.data(), F_OK) != 0) { - HILOGI("file dose not exist"); - continue; - } - if (filePath.empty()) { - HILOGE("file path is empty. %{public}s", filePath.c_str()); + if (isusertar != true || isbigfile != true) { + return; + } + for (auto &item : info) { + if (item.first.empty() || item.first.rfind('.') != string::npos) { + continue; + } + string fileName = path + item.first; + auto [filePath, sta] = item.second; + + if (access(fileName.data(), F_OK) != 0) { + HILOGI("file dose not exist"); + continue; + } + if (filePath.empty()) { + HILOGE("file path is empty. %{public}s", filePath.c_str()); + continue; + } + if (rename(fileName.data(), filePath.data()) != 0) { + HILOGE("failed to rename the file, try to copy it. err = %{public}d", errno); + if (!BFile::CopyFile(fileName, filePath)) { + HILOGE("failed to copy the file. err = %{public}d", errno); continue; } - if (rename(fileName.data(), filePath.data()) != 0) { - HILOGE("failed to rename the file, try to copy it. err = %{public}d", errno); - if (!BFile::CopyFile(fileName, filePath)) { - HILOGE("failed to copy the file. err = %{public}d", errno); - continue; - } - HILOGI("succeed to rename or copy the file"); - } - set lks = cache.GetHardLinkInfo(item.first); - for (const auto &lksPath : lks) { - if (link(filePath.data(), lksPath.data())) { - HILOGE("failed to create hard link file %{public}s errno : %{public}d", lksPath.c_str(), errno); - } - } - struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; - UniqueFd fd(open(filePath.data(), O_RDONLY)); - if (futimens(fd.Get(), tv) != 0) { - HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); + HILOGI("succeed to rename or copy the file"); + } + set lks = cache.GetHardLinkInfo(item.first); + for (const auto &lksPath : lks) { + if (link(filePath.data(), lksPath.data())) { + HILOGE("failed to create hard link file %{public}s errno : %{public}d", lksPath.c_str(), errno); } } + + struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; + UniqueFd fd(open(filePath.data(), O_RDONLY)); + if (futimens(fd.Get(), tv) != 0) { + HILOGI("failed to change the file time. %{public}s , %{public}d", filePath.c_str(), errno); + } } } diff --git a/utils/include/b_json/b_json_entity_ext_manage.h b/utils/include/b_json/b_json_entity_ext_manage.h index bc76b7c3e..5cbb7779d 100644 --- a/utils/include/b_json/b_json_entity_ext_manage.h +++ b/utils/include/b_json/b_json_entity_ext_manage.h @@ -24,6 +24,8 @@ #include "b_json/b_json_cached_entity.h" #include "json/json.h" +#define LEN 2 + namespace OHOS::FileManagement::Backup { class BJsonEntityExtManage : public BJsonEntity { public: @@ -58,8 +60,6 @@ public: */ bool SetHardLinkInfo(const std::string origin, const std::set hardLinks); - // bool GetUsertar(const string &fileback); - /** * @brief Get the hard link Information * diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index 2d422e707..f24cf0110 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -32,7 +32,7 @@ bool IsCompressedFile(const string &filePath) size_t lastDotPos = filePath.find_last_of('.'); std::string extension = filePath.substr(lastDotPos); if ((extension == ".zip" || extension == ".rar" || extension == ".7z" || extension == ".tar") && - std::count(filePath.begin(), filePath.end(), '/') >= 2) { + std::count(filePath.begin(), filePath.end(), '/') >= LEN) { size_t pos = filePath.rfind('/'); std::string directory = filePath.substr(0, pos); if (directory.find(BConstants::PATH_BUNDLE_BACKUP_HOME.data()) != std::string::npos) { @@ -41,7 +41,6 @@ bool IsCompressedFile(const string &filePath) } return true; } - bool IsBigFile(const string &tarfile) { -- Gitee From 32d9da9eb812ae4d5f96bc2ba234bc75d5fff6a9 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Fri, 27 Oct 2023 10:50:28 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E7=89=87=E6=89=93=E5=8C=85=E2=80=94?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=A4=87=E4=BB=BD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../native/backup_ext/src/ext_extension.cpp | 15 ++++++++++----- utils/src/b_json/b_json_entity_ext_manage.cpp | 8 ++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index df762593e..e6fda1f77 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -114,15 +114,19 @@ static ErrCode IndexFileReady(const map> &pkgI cachedEntity.Persist(); close(cachedEntity.GetFd().Release()); + auto userfile = cache.GetExtManageMap(); + auto isusertar = userfile.find("isUserTar") != userfile.end(); + + if(isusertar == true){ + HILOGI("The file is a isuserrtar==true"); + } + ErrCode ret = proxy->AppFileReady(string(BConstants::EXT_BACKUP_MANAGE), UniqueFd(open(indexFile.data(), O_RDONLY))); if (SUCCEEDED(ret)) { HILOGI("The application is packaged successfully"); } else { - HILOGI( - "The application is packaged successfully but the AppFileReady interface fails to be invoked: " - "%{public}d", - ret); + HILOGI("The application is packaged successfully but the AppFileReady interface fails to be invoked: ""%{public}d",ret); } return ret; } @@ -273,6 +277,7 @@ int BackupExtExtension::DoBackup(const BJsonEntityExtensionConfig &usrConfig) string tarName = path + pkgName; string root = "/"; + // 打包 auto tarballTar = BTarballFactory::Create("cmdline", tarName); (tarballTar->tar)(root, {includes.begin(), includes.end()}, {excludes.begin(), excludes.end()}); @@ -396,7 +401,7 @@ static void RestoreBigFiles() auto userfile = cache.GetExtManageMap(); auto isusertar = userfile.find("isUserTar") != userfile.end(); auto isbigfile = userfile.find("isBigFile") != userfile.end(); - if (isusertar != true || isbigfile != true) { + if (isusertar != false && isbigfile != true ) { return; } for (auto &item : info) { diff --git a/utils/src/b_json/b_json_entity_ext_manage.cpp b/utils/src/b_json/b_json_entity_ext_manage.cpp index f24cf0110..3344c55e2 100644 --- a/utils/src/b_json/b_json_entity_ext_manage.cpp +++ b/utils/src/b_json/b_json_entity_ext_manage.cpp @@ -43,8 +43,12 @@ bool IsCompressedFile(const string &filePath) } bool IsBigFile(const string &tarfile) { - - if (tarfile.size() < BConstants::BIG_FILE_NAME_SIZE) { + struct stat sta; + int ret = stat(tarfile.c_str(), &sta); + if (ret != 0) { + HILOGE("stat file failed, file:%{public}s", tarfile.c_str()); + } + if (sta.st_size > BConstants::BIG_FILE_BOUNDARY) { return false; } return true; -- Gitee