diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 68184723ea589a1ec62e7e8eed9e53b6813f6448..ed2bbdb688ac70689d9901ae378401be2fc0760f 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -58,8 +58,10 @@ public: void AsyncTaskIncrementalRestoreForUpgrade(void); public: - explicit BackupExtExtension(const std::shared_ptr &extension) : extension_(extension) + explicit BackupExtExtension(const std::shared_ptr &extension, + const std::string &bundleName) : extension_(extension) { + bundleName_ = bundleName; threadPool_.Start(BConstants::EXTENSION_THREAD_POOL_COUNT); } ~BackupExtExtension() diff --git a/frameworks/native/backup_ext/src/ext_backup.cpp b/frameworks/native/backup_ext/src/ext_backup.cpp index 1a4bc1f78913a20c534f4c31e9885eb123eb20c9..cae8e3e911cd46e27dc0e72f949e001c968186d6 100644 --- a/frameworks/native/backup_ext/src/ext_backup.cpp +++ b/frameworks/native/backup_ext/src/ext_backup.cpp @@ -188,7 +188,8 @@ sptr ExtBackup::OnConnect(const AAFwk::Want &want) Extension::OnConnect(want); auto remoteObject = - sptr(new BackupExtExtension(std::static_pointer_cast(shared_from_this()))); + sptr(new BackupExtExtension(std::static_pointer_cast(shared_from_this()), + want.GetBundle())); return remoteObject->AsObject(); } catch (const BError &e) { return nullptr; @@ -207,7 +208,7 @@ void ExtBackup::OnDisconnect(const AAFwk::Want &want) HILOGI("begin disconnect"); if (isClearData_) { auto remoteObject = sptr( - new BackupExtExtension(std::static_pointer_cast(shared_from_this()))); + new BackupExtExtension(std::static_pointer_cast(shared_from_this()), want.GetBundle())); remoteObject->ExtClear(); } Extension::OnDisconnect(want); diff --git a/frameworks/native/backup_ext/src/ext_backup_context.cpp b/frameworks/native/backup_ext/src/ext_backup_context.cpp index 87a8a03ddb2ab4ae805533871ac0b2c7b52703c6..3df12d334c76ba820e9388b8ba3533540f5a8fc2 100644 --- a/frameworks/native/backup_ext/src/ext_backup_context.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_context.cpp @@ -15,6 +15,7 @@ #include "ext_backup_context.h" +#include "b_filesystem/b_file.h" #include "b_resources/b_constants.h" namespace OHOS::FileManagement::Backup { @@ -24,7 +25,16 @@ const std::string ExtBackupContext::GetBackupDir() if (area > 1) { // 1 : el2 area return ""; } - return std::string(BConstants::BACKUP_DIR_PRE) + std::string(BConstants::CONTEXT_ELS[area]) + + + std::string path = std::string(BConstants::BACKUP_DIR_PRE) + std::string(BConstants::CONTEXT_ELS[area]) + std::string(BConstants::BACKUP_DIR_END); + std::string bundleName = GetBundleName(); + if (BFile::EndsWith(bundleName, BConstants::BUNDLE_FILE_MANAGER) && bundleName.size() == BConstants::FM_LEN) { + path = std::string(BConstants::PATH_FILEMANAGE_BACKUP_HOME) + BConstants::FILE_SEPARATOR_CHAR; + } else if (bundleName == BConstants::BUNDLE_MEDIAL_DATA) { + path = std::string(BConstants::PATH_MEDIALDATA_BACKUP_HOME) + BConstants::FILE_SEPARATOR_CHAR; + } + + return path; } } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index c75e71b562e1e90369c7c02673d407b9beb748ed..36d1c48bc59098a63859f6b5acecb0dc577f04dc 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -57,7 +57,6 @@ #include "b_anony/b_anony.h" namespace OHOS::FileManagement::Backup { -const string DEFAULT_TAR_PKG = "1.tar"; const string INDEX_FILE_BACKUP = string(BConstants::PATH_BUNDLE_BACKUP_HOME). append(BConstants::SA_BUNDLE_BACKUP_BACKUP). append(BConstants::EXT_BACKUP_MANAGE); @@ -68,18 +67,41 @@ const string INDEX_FILE_INCREMENTAL_BACKUP = string(BConstants::PATH_BUNDLE_BACK append(BConstants::SA_BUNDLE_BACKUP_BACKUP); using namespace std; -namespace { -const int64_t DEFAULT_SLICE_SIZE = 100 * 1024 * 1024; // 分片文件大小为100M -const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 -const uint32_t MAX_FD_GROUP_USE_TIME = 1000; // 每组打开最大时间1000ms -const int FILE_AND_MANIFEST_FD_COUNT = 2; // 每组文件和简报数量统计 +const uint32_t MAX_FD_GROUP_USE_TIME = 1000; // 每组打开最大时间1000ms + +static string GetIndexFileRestorePath(const string &bundleName) +{ + if (BFile::EndsWith(bundleName, BConstants::BUNDLE_FILE_MANAGER) && bundleName.size() == BConstants::FM_LEN) { + return string(BConstants::PATH_FILEMANAGE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE). + append(BConstants::EXT_BACKUP_MANAGE); + } else if (bundleName == BConstants::BUNDLE_MEDIAL_DATA) { + return string(BConstants::PATH_MEDIALDATA_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE). + append(BConstants::EXT_BACKUP_MANAGE); + } + return INDEX_FILE_RESTORE; +} + +static string GetRestoreTempPath(const string &bundleName) +{ + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + if (BFile::EndsWith(bundleName, BConstants::BUNDLE_FILE_MANAGER) && bundleName.size() == BConstants::FM_LEN) { + if (mkdir(string(BConstants::PATH_FILEMANAGE_BACKUP_HOME).data(), S_IRWXU) && errno != EEXIST) { + string str = string("Failed to create .backup folder. ").append(std::generic_category().message(errno)); + throw BError(BError::Codes::EXT_INVAL_ARG, str); + } + path = string(BConstants::PATH_FILEMANAGE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + } else if (bundleName == BConstants::BUNDLE_MEDIAL_DATA) { + path = string(BConstants::PATH_MEDIALDATA_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + } + return path; } -static std::set GetIdxFileData() +static std::set GetIdxFileData(const string &bundleName) { - UniqueFd idxFd(open(INDEX_FILE_RESTORE.data(), O_RDONLY)); + string indexFileRestorePath = GetIndexFileRestorePath(bundleName); + UniqueFd idxFd(open(indexFileRestorePath.data(), O_RDONLY)); if (idxFd < 0) { - HILOGE("Failed to open idxFile = %{private}s, err = %{public}d", INDEX_FILE_RESTORE.c_str(), errno); + HILOGE("Failed to open idxFile = %{private}s, err = %{public}d", indexFileRestorePath.c_str(), errno); return std::set(); } BJsonCachedEntity cachedEntity(std::move(idxFd)); @@ -87,9 +109,10 @@ static std::set GetIdxFileData() return cache.GetExtManage(); } -static std::vector GetExtManageInfo() +static std::vector GetExtManageInfo(const string &bundleName) { - string filePath = BExcepUltils::Canonicalize(INDEX_FILE_RESTORE); + string indexFileRestorePath = GetIndexFileRestorePath(bundleName); + string filePath = BExcepUltils::Canonicalize(indexFileRestorePath); UniqueFd idxFd(open(filePath.data(), O_RDONLY)); if (idxFd < 0) { HILOGE("Failed to open cano_idxFile = %{private}s, err = %{public}d", filePath.c_str(), errno); @@ -182,7 +205,7 @@ UniqueFd BackupExtExtension::GetFileHandle(const string &fileName, int32_t &errC return fd; } - string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string path = GetRestoreTempPath(bundleName_); if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { string str = string("Failed to create restore folder. ").append(std::generic_category().message(errno)); throw BError(BError::Codes::EXT_INVAL_ARG, str); @@ -246,9 +269,18 @@ static ErrCode GetIncreFileHandleForSpecialVersion(const string &fileName) return ERR_OK; } -static string GetIncrementalFileHandlePath(const string &fileName) +static string GetIncrementalFileHandlePath(const string &fileName, const string &bundleName) { string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + if (BFile::EndsWith(bundleName, BConstants::BUNDLE_FILE_MANAGER) && bundleName.size() == BConstants::FM_LEN) { + if (mkdir(string(BConstants::PATH_FILEMANAGE_BACKUP_HOME).data(), S_IRWXU) && errno != EEXIST) { + string str = string("Failed to create .backup folder. ").append(std::generic_category().message(errno)); + throw BError(BError::Codes::EXT_INVAL_ARG, str); + } + path = string(BConstants::PATH_FILEMANAGE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + } else if (bundleName == BConstants::BUNDLE_MEDIAL_DATA) { + path = string(BConstants::PATH_MEDIALDATA_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + } if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { string str = string("Failed to create restore folder. ").append(std::generic_category().message(errno)); throw BError(BError::Codes::EXT_INVAL_ARG, str); @@ -272,7 +304,7 @@ ErrCode BackupExtExtension::GetIncrementalFileHandle(const string &fileName) return GetIncreFileHandleForSpecialVersion(fileName); } HILOGI("extension: GetIncrementalFileHandle single to single Name:%{public}s", GetAnonyPath(fileName).c_str()); - string tarName = GetIncrementalFileHandlePath(fileName); + string tarName = GetIncrementalFileHandlePath(fileName, bundleName_); int32_t errCode = ERR_OK; if (access(tarName.c_str(), F_OK) == 0) { HILOGE("The file already exists, tarname = %{private}s, err =%{public}d", tarName.c_str(), errno); @@ -407,7 +439,7 @@ ErrCode BackupExtExtension::PublishFile(const std::string &fileName) VerifyCaller(); // 异步执行解压操作 if (extension_->AllowToBackupRestore()) { - AsyncTaskRestore(GetIdxFileData(), GetExtManageInfo()); + AsyncTaskRestore(GetIdxFileData(bundleName_), GetExtManageInfo(bundleName_)); } HILOGI("End publish file"); return ERR_OK; @@ -559,7 +591,7 @@ static ErrCode TarFileReady(const TarMap &tarFileInfo, sptr proxy) if (SUCCEEDED(ret)) { HILOGI("TarFileReady: AppFileReady success for %{public}s", tarName.c_str()); // 删除文件 - RemoveFile(tarName); + RemoveFile(tarPath); } else { HILOGE("TarFileReady AppFileReady fail to be invoked for %{public}s: ret = %{public}d", tarName.c_str(), ret); } @@ -580,7 +612,7 @@ void BackupExtExtension::DoPacket(const map &srcFiles, TarMap &t totalSize += small.second; fileCount += 1; packFiles.emplace_back(small.first); - if (totalSize >= DEFAULT_SLICE_SIZE || fileCount >= MAX_FILE_COUNT) { + if (totalSize >= BConstants::DEFAULT_SLICE_SIZE || fileCount >= BConstants::MAX_FILE_COUNT) { TarMap tarMap {}; TarFile::GetInstance().Packet(packFiles, "part", path, tarMap); tar.insert(tarMap.begin(), tarMap.end()); @@ -590,7 +622,7 @@ void BackupExtExtension::DoPacket(const map &srcFiles, TarMap &t totalSize = 0; fileCount = 0; packFiles.clear(); - fdNum += FILE_AND_MANIFEST_FD_COUNT; + fdNum += BConstants::FILE_AND_MANIFEST_FD_COUNT; RefreshTimeInfo(startTime, fdNum); } } @@ -675,7 +707,7 @@ int BackupExtExtension::DoRestore(const string &fileName) } // REM: 给定version // REM: 解压启动Extension时即挂载好的备份目录中的数据 - string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string path = GetRestoreTempPath(bundleName_); string tarName = path + fileName; // 当用户指定fullBackupOnly字段或指定版本的恢复,解压目录当前在/backup/restore @@ -720,8 +752,8 @@ int BackupExtExtension::DoIncrementalRestore() HILOGE("Failed to do incremental restore, extension is nullptr"); throw BError(BError::Codes::EXT_INVAL_ARG, "Extension is nullptr"); } - auto fileSet = GetIdxFileData(); - auto extManageInfo = GetExtManageInfo(); + auto fileSet = GetIdxFileData(bundleName_); + auto extManageInfo = GetExtManageInfo(bundleName_); ErrCode err = ERR_OK; for (auto item : fileSet) { // 处理要解压的tar文件 if (ExtractFileExt(item) == "tar" && !IsUserTar(item, extManageInfo)) { @@ -730,7 +762,7 @@ int BackupExtExtension::DoIncrementalRestore() } // REM: 给定version // REM: 解压启动Extension时即挂载好的备份目录中的数据 - string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string path = GetRestoreTempPath(bundleName_); string tarName = path + item; // 当用户指定fullBackupOnly字段或指定版本的恢复,解压目录当前在/backup/restore @@ -900,14 +932,15 @@ static void RestoreBigFileAfter(const string &filePath, const struct stat &sta) } } -static void RestoreBigFiles(bool appendTargetPath) +static void RestoreBigFiles(bool appendTargetPath, const string &bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); // 获取索引文件内容 - string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); - UniqueFd fd(open(INDEX_FILE_RESTORE.data(), O_RDONLY)); + string path = GetRestoreTempPath(bundleName); + string indexFileRestorePath = GetIndexFileRestorePath(bundleName); + UniqueFd fd(open(indexFileRestorePath.data(), O_RDONLY)); if (fd < 0) { - HILOGE("Failed to open index json file = %{private}s, err = %{public}d", INDEX_FILE_RESTORE.c_str(), errno); + HILOGE("Failed to open index json file = %{private}s, err = %{public}d", indexFileRestorePath.c_str(), errno); return; } BJsonCachedEntity cachedEntity(move(fd)); @@ -955,17 +988,18 @@ void BackupExtExtension::DeleteBackupTars() return; } HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - UniqueFd fd(open(INDEX_FILE_RESTORE.data(), O_RDONLY)); + string indexFileRestorePath = GetIndexFileRestorePath(bundleName_); + UniqueFd fd(open(indexFileRestorePath.data(), O_RDONLY)); if (fd < 0) { - HILOGE("Failed to open index json file = %{private}s, err = %{public}d", INDEX_FILE_RESTORE.c_str(), errno); + HILOGE("Failed to open index json file = %{private}s, err = %{public}d", indexFileRestorePath.c_str(), errno); return; } // The directory include tars and manage.json which would be deleted BJsonCachedEntity cachedEntity(move(fd)); auto cache = cachedEntity.Structuralize(); auto info = cache.GetExtManage(); - auto path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); - auto extManageInfo = GetExtManageInfo(); + auto path = GetRestoreTempPath(bundleName_); + auto extManageInfo = GetExtManageInfo(bundleName_); for (auto &item : info) { if (ExtractFileExt(item) != "tar" || IsUserTar(item, extManageInfo)) { continue; @@ -975,8 +1009,8 @@ void BackupExtExtension::DeleteBackupTars() HILOGE("Failed to delete the backup tar %{public}s", tarPath.c_str()); } } - if (!RemoveFile(INDEX_FILE_RESTORE)) { - HILOGE("Failed to delete the backup index %{public}s", INDEX_FILE_RESTORE.c_str()); + if (!RemoveFile(indexFileRestorePath)) { + HILOGE("Failed to delete the backup index %{public}s", indexFileRestorePath.c_str()); } HILOGI("End execute DeleteBackupTars"); } @@ -987,17 +1021,18 @@ void BackupExtExtension::DeleteBackupIncrementalTars() HILOGI("configured not clear data."); return; } - UniqueFd fd(open(INDEX_FILE_RESTORE.data(), O_RDONLY)); + string indexFileRestorePath = GetIndexFileRestorePath(bundleName_); + UniqueFd fd(open(indexFileRestorePath.data(), O_RDONLY)); if (fd < 0) { - HILOGE("Failed to open index json file = %{private}s, err = %{public}d", INDEX_FILE_RESTORE.c_str(), errno); + HILOGE("Failed to open index json file = %{private}s, err = %{public}d", indexFileRestorePath.c_str(), errno); return; } // The directory include tars and manage.json which would be deleted BJsonCachedEntity cachedEntity(move(fd)); auto cache = cachedEntity.Structuralize(); auto info = cache.GetExtManage(); - auto path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); - auto extManageInfo = GetExtManageInfo(); + auto path = GetRestoreTempPath(bundleName_); + auto extManageInfo = GetExtManageInfo(bundleName_); for (auto &item : info) { if (ExtractFileExt(item) != "tar" || IsUserTar(item, extManageInfo)) { continue; @@ -1012,10 +1047,10 @@ void BackupExtExtension::DeleteBackupIncrementalTars() HILOGE("Failed to delete the backup report %{private}s, err = %{public}d", reportPath.c_str(), errno); } } - if (!RemoveFile(INDEX_FILE_RESTORE)) { - HILOGE("Failed to delete the backup index %{public}s", INDEX_FILE_RESTORE.c_str()); + if (!RemoveFile(indexFileRestorePath)) { + HILOGE("Failed to delete the backup index %{public}s", indexFileRestorePath.c_str()); } - string reportManagePath = GetReportFileName(INDEX_FILE_RESTORE); // GetIncrementalFileHandle创建的空fd + string reportManagePath = GetReportFileName(indexFileRestorePath); // GetIncrementalFileHandle创建的空fd if (!RemoveFile(reportManagePath)) { HILOGE("Failed to delete the backup report index %{public}s", reportManagePath.c_str()); } @@ -1057,7 +1092,7 @@ void BackupExtExtension::AsyncTaskRestore(std::set fileSet, // 目的地址是否需要拼接path(临时目录),FullBackupOnly为true并且非特殊场景 bool appendTargetPath = ptr->extension_->UseFullBackupOnly() && !ptr->extension_->SpecialVersionForCloneAndCloud(); - RestoreBigFiles(appendTargetPath); + RestoreBigFiles(appendTargetPath, ptr->bundleName_); ptr->DeleteBackupTars(); if (ret == ERR_OK) { ptr->AsyncTaskRestoreForUpgrade(); @@ -1100,7 +1135,7 @@ void BackupExtExtension::AsyncTaskIncrementalRestore() // 目的地址是否需要拼接path(临时目录),FullBackupOnly为true并且非特殊场景 bool appendTargetPath = ptr->extension_->UseFullBackupOnly() && !ptr->extension_->SpecialVersionForCloneAndCloud(); - RestoreBigFiles(appendTargetPath); + RestoreBigFiles(appendTargetPath, ptr->bundleName_); // delete 1.tar/manage.json ptr->DeleteBackupIncrementalTars(); @@ -1295,6 +1330,7 @@ void BackupExtExtension::DoClear() } string backupCache = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_BACKUP); string restoreCache = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string specialRestoreCache = GetRestoreTempPath(bundleName_); if (!ForceRemoveDirectory(backupCache)) { HILOGI("Failed to delete the backup cache %{public}s", backupCache.c_str()); @@ -1303,6 +1339,10 @@ void BackupExtExtension::DoClear() if (!ForceRemoveDirectory(restoreCache)) { HILOGI("Failed to delete the restore cache %{public}s", restoreCache.c_str()); } + + if (!ForceRemoveDirectory(specialRestoreCache)) { + HILOGI("Failed to delete cache for filemanager or medialibrary %{public}s", specialRestoreCache.c_str()); + } // delete el1 backup/restore ForceRemoveDirectory( string(BConstants::PATH_BUNDLE_BACKUP_HOME_EL1).append(BConstants::SA_BUNDLE_BACKUP_BACKUP)); @@ -1679,7 +1719,7 @@ ErrCode BackupExtExtension::IncrementalBigFileReady(const TarMap &pkgInfo, } else { HILOGE("IncrementalBigFileReady interface fails to be invoked: %{public}d", ret); } - fdNum += FILE_AND_MANIFEST_FD_COUNT; + fdNum += BConstants::FILE_AND_MANIFEST_FD_COUNT; RefreshTimeInfo(startTime, fdNum); } HILOGI("IncrementalBigFileReady End"); @@ -1801,7 +1841,7 @@ void BackupExtExtension::IncrementalPacket(const vector & fileCount += 1; packFiles.emplace_back(small.filePath); tarInfos.emplace_back(small); - if (totalSize >= DEFAULT_SLICE_SIZE || fileCount >= MAX_FILE_COUNT) { + if (totalSize >= BConstants::DEFAULT_SLICE_SIZE || fileCount >= BConstants::MAX_FILE_COUNT) { TarMap tarMap {}; TarFile::GetInstance().Packet(packFiles, partName, path, tarMap); tar.insert(tarMap.begin(), tarMap.end()); @@ -1812,7 +1852,7 @@ void BackupExtExtension::IncrementalPacket(const vector & fileCount = 0; packFiles.clear(); tarInfos.clear(); - fdNum += FILE_AND_MANIFEST_FD_COUNT; + fdNum += BConstants::FILE_AND_MANIFEST_FD_COUNT; RefreshTimeInfo(startTime, fdNum); } } diff --git a/test/fuzztest/backupext_fuzzer/backupext_fuzzer.cpp b/test/fuzztest/backupext_fuzzer/backupext_fuzzer.cpp index 2528f0082b50f842a0afae2763c90b061983ce2b..d00abd06b85291998fde89fecb04c02d5267fd01 100644 --- a/test/fuzztest/backupext_fuzzer/backupext_fuzzer.cpp +++ b/test/fuzztest/backupext_fuzzer/backupext_fuzzer.cpp @@ -273,7 +273,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { /* Run your code on data */ auto extBackup = std::make_shared(); - auto extension = std::make_shared(extBackup); + auto extension = std::make_shared(extBackup, ""); OHOS::OnRemoteRequestFuzzTest(extension, data, size); OHOS::InitFuzzTest(extBackup, data, size); diff --git a/utils/include/b_filesystem/b_file.h b/utils/include/b_filesystem/b_file.h index 2d69e75fdf09cf070018f38bef7c84d6ee6fd9f7..c2cc3c8af2f59f3b8e04cde0c92bc8675fe93d94 100644 --- a/utils/include/b_filesystem/b_file.h +++ b/utils/include/b_filesystem/b_file.h @@ -82,6 +82,16 @@ public: * @return false some error occur */ static bool GetRealPath(const string &path, string &realPath); + + /** + * @brief check if string is endswith suffix + * + * @param str str + * @param suffix suffix str + * @return true str is endswith suffix + * @return false str is not endswith suffix + */ + static bool EndsWith(const string &str, const string &suffix); private: }; } // namespace OHOS::FileManagement::Backup diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 512526b5caba6b5f2a9c5910abd550148247f969..d77fbd7ebdb292d1512cd5a2fa1c86a62844cd5e 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -67,6 +67,11 @@ constexpr int IPC_MAX_WAIT_TIME = 3000; // IPC通讯最大等待时间(s) constexpr int MAX_PARCELABLE_VECTOR_NUM = 10000; constexpr char FILE_SEPARATOR_CHAR = '/'; +// 分片打包常量 +const int64_t DEFAULT_SLICE_SIZE = 100 * 1024 * 1024; // 分片文件大小为100M +const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 +const int FILE_AND_MANIFEST_FD_COUNT = 2; // 每组文件和简报数量统计 + constexpr int DEFAULT_VFS_CACHE_PRESSURE = 100; // 默认内存回收参数 constexpr int BACKUP_VFS_CACHE_PRESSURE = 10000; // 备份过程修改参数 @@ -105,6 +110,8 @@ static inline std::string_view SA_BUNDLE_BACKUP_TMP_DIR = "/tmp/"; static inline std::string_view BACKUP_TOOL_RECEIVE_DIR = "/data/backup/received/"; static inline std::string_view PATH_BUNDLE_BACKUP_HOME_EL1 = "/data/storage/el1/base/.backup"; static inline std::string_view PATH_BUNDLE_BACKUP_HOME = "/data/storage/el2/base/.backup"; +static inline std::string_view PATH_FILEMANAGE_BACKUP_HOME = "/storage/Users/currentUser/.backup"; +static inline std::string_view PATH_MEDIALDATA_BACKUP_HOME = "/storage/media/local/files/.backup"; static inline std::string_view BACKUP_TOOL_LINK_DIR = "/data/backup"; static inline std::string_view BACKUP_TOOL_INCREMENTAL_RECEIVE_DIR = "/data/backup/incrementalreceived/"; static inline std::string_view BACKUP_TOOL_MANIFEST = "/manifest"; @@ -112,6 +119,13 @@ static inline std::string_view BACKUP_TOOL_INCREMENTAL = "/incremental"; static inline std::string BACKUP_DIR_PRE = "/data/storage/"; static inline std::string CONTEXT_ELS[] = {"el1", "el2"}; static inline std::string BACKUP_DIR_END = "/base/.backup/"; + +// 文管bundleName +static inline std::string BUNDLE_FILE_MANAGER = "hmos.filemanager"; +// 文管bundleNameSize +constexpr size_t FM_LEN = 27; +// 媒体库数据bundleName +static inline std::string BUNDLE_MEDIAL_DATA = "com.ohos.medialibrary.medialibrarydata"; // SA Ext constexpr int BACKUP_DEFAULT_SA_ID = -1; constexpr int BACKUP_SA_RELOAD_MAX = 2; diff --git a/utils/src/b_filesystem/b_file.cpp b/utils/src/b_filesystem/b_file.cpp index 32e81090db86f40c43f7646d5dd5cd5a86f3fb6e..da11f4dba7c4361d8109f5ac20c067791c9a14cc 100644 --- a/utils/src/b_filesystem/b_file.cpp +++ b/utils/src/b_filesystem/b_file.cpp @@ -201,4 +201,12 @@ bool BFile::MoveFile(const string &from, const string &to) } return false; } + +bool BFile::EndsWith(const string &str, const string &suffix) +{ + if (suffix.length() > str.length()) { + return false; + } + return (str.rfind(suffix) == (str.length() - suffix.length())); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file