diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 10feae97b3a229f20b97beec8954ae84e9888be2..e6fda1f77cd1f81529b5355dce80814fae39a840 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(); @@ -116,24 +114,26 @@ 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; } 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(); @@ -277,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()}); @@ -374,6 +375,7 @@ static bool IsAllFileReceived(vector tars) BJsonCachedEntity cachedEntity(UniqueFd(open(indexFile.data(), O_RDONLY))); auto cache = cachedEntity.Structuralize(); set info = cache.GetExtManage(); + auto infoMap = cache.GetExtManageMap(); // 从数量上判断是否已经全部收到 if (tars.size() <= info.size()) { @@ -396,13 +398,16 @@ 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 != false && isbigfile != true ) { + return; + } 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; auto [filePath, sta] = item.second; @@ -449,13 +454,19 @@ 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(); int ret = ERR_OK; for (auto item : tars) { - if (ExtractFileExt(item) == "tar") { + auto userfile = cache.GetExtManageMap(); + auto isusertar = userfile.find("isUserTar") != userfile.end(); + if (ExtractFileExt(item) == "tar" && isusertar == 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 a322a55900ea9ab0ca08e096d4c4c1306ba09e5c..5cbb7779de22cfc0cbb89cefefa803c7f751205d 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: @@ -40,7 +42,7 @@ 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 d367ff3ffdd37f9bb69b0fa9ebe2bd7a2dbf3d0b..3344c55e290123d51813cf2a93171eb147cb2d4b 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,33 @@ 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") && + 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) { + return false; + } + } + return true; +} +bool IsBigFile(const string &tarfile) +{ + 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; +} + Json::Value Stat2JsonValue(struct stat sta) { Json::Value value; @@ -110,6 +137,9 @@ void BJsonEntityExtManage::SetExtManage(const mapsecond.first; value["information"]["stat"] = Stat2JsonValue(item->second.second); + // ??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); @@ -130,7 +160,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 +170,35 @@ 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; + 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); + } + return info; +} + map> BJsonEntityExtManage::GetExtManageInfo() const { if (!obj_) {