From c4728f14303f44e823c4fd6547234dab9c9567da Mon Sep 17 00:00:00 2001 From: hunili Date: Tue, 25 Feb 2025 17:40:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E7=9B=AE=E5=BD=95=E7=9B=91?= =?UTF-8?q?=E5=90=AC=20issue:https://gitee.com/openharmony/filemanagement?= =?UTF-8?q?=5Fapp=5Ffile=5Fservice/issues/IBOTC3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hunili --- .../native/backup_ext/src/ext_extension.cpp | 35 ++++++++++++------ .../backup_ext/src/sub_ext_extension.cpp | 4 ++- utils/include/b_filesystem/b_dir.h | 10 +++++- utils/src/b_filesystem/b_dir.cpp | 36 ++++++++++++++++++- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 1119285af..df1f654e6 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -228,6 +228,7 @@ static UniqueFd GetFileHandleForSpecialCloneCloud(const string &fileName) HiAudit::GetInstance(false).Write(auditLog); return UniqueFd(-1); } + BDir::RmDirMonitor(path); } UniqueFd fd(open(fileName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); if (fd < 0) { @@ -268,6 +269,7 @@ UniqueFd BackupExtExtension::GetFileHandle(const string &fileName, int32_t &errC string str = string("Failed to create restore folder. ").append(std::generic_category().message(errno)); throw BError(BError::Codes::EXT_INVAL_ARG, str); } + BDir::RmDirMonitor(path); string tarName = path + fileName; if (access(tarName.c_str(), F_OK) == 0) { @@ -306,12 +308,18 @@ static tuple GetIncreFileHandleForSpecialVersion(co } string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); - if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { - HILOGE("Failed to create restore folder : %{private}s, err = %{public}d", path.c_str(), errno); - errCode = errno; - AuditLog auditLog = {false, "mkdir failed", "ADD", "", 1, "FAILED", - "GetIncreFileHandleForSpecialVersion", "CommonFile", GetAnonyPath(path)}; - HiAudit::GetInstance(false).Write(auditLog); + if (access(path.c_str(), F_OK) == 0) { + HILOGW("The file already exists, path = %{public}s, err =%{public}d", + GetAnonyPath(path).c_str(), errno); + } else { + if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { + HILOGE("Failed to create restore folder : %{private}s, err = %{public}d", path.c_str(), errno); + errCode = errno; + AuditLog auditLog = {false, "mkdir failed", "ADD", "", 1, "FAILED", + "GetIncreFileHandleForSpecialVersion", "CommonFile", GetAnonyPath(path)}; + HiAudit::GetInstance(false).Write(auditLog); + } + BDir::RmDirMonitor(path); } string reportName = path + BConstants::BLANK_REPORT_NAME; UniqueFd reportFd(open(reportName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); @@ -335,10 +343,14 @@ static ErrCode GetIncrementalFileHandlePath(const string &fileName, const string } 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 errMsg = string("Failed to create restore folder. ").append(std::generic_category().message(errno)); - HILOGE("%{public}s, errno = %{public}d", errMsg.c_str(), errno); - return errno; + if (access(path.c_str(), F_OK) != 0) { + HILOGW("Failed to access restore folder : %{private}s, err = %{public}d", path.c_str(), errno); + if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { + string errMsg = string("Failed to create restore folder. ").append(std::generic_category().message(errno)); + HILOGE("%{public}s, errno = %{public}d", errMsg.c_str(), errno); + return errno; + } + BDir::RmDirMonitor(path); } tarName = path + fileName; return ERR_OK; @@ -815,6 +827,7 @@ tuple BackupExtExtension::CalculateDataSize(const B HILOGE("mkdir failed path :%{public}s, err = %{public}d", path.c_str(), errno); return {errno, 0, 0}; } + BDir::RmDirMonitor(path); vector includes = usrConfig.GetIncludes(); vector excludes = usrConfig.GetExcludes(); diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index 24e2a9c11..c924af18c 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -902,6 +902,7 @@ void BackupExtExtension::DoUser0Backup(const BJsonEntityExtensionConfig &usrConf if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { throw BError(errno); } + BDir::RmDirMonitor(path); vector includes = usrConfig.GetIncludes(); vector excludes = usrConfig.GetExcludes(); auto task = [obj {wptr(this)}, includes, excludes]() { @@ -1173,6 +1174,7 @@ int BackupExtExtension::DoIncrementalBackup(const vector if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { throw BError(errno); } + BDir::RmDirMonitor(path); auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { throw BError(BError::Codes::EXT_BROKEN_BACKUP_SA, std::generic_category().message(errno)); diff --git a/utils/include/b_filesystem/b_dir.h b/utils/include/b_filesystem/b_dir.h index d17452d5b..6904472ea 100644 --- a/utils/include/b_filesystem/b_dir.h +++ b/utils/include/b_filesystem/b_dir.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -108,6 +108,14 @@ public: * @return 是否存在软连接 */ static bool CheckAndRmSoftLink(const EndFileInfo &filePaths); + + /** + * @brief 删除文件监控 + * + * @param filePaths 待核实的路径 + * @return void + */ + static void RmDirMonitor(const std::string dirPath); }; } // namespace OHOS::FileManagement::Backup diff --git a/utils/src/b_filesystem/b_dir.cpp b/utils/src/b_filesystem/b_dir.cpp index 9b8f03dac..29f9bd38b 100644 --- a/utils/src/b_filesystem/b_dir.cpp +++ b/utils/src/b_filesystem/b_dir.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -550,4 +551,37 @@ bool BDir::CheckAndRmSoftLink(const EndFileInfo &filePaths) } return isSoftLink; } + +#define HMFS_MONITOR_FL 0x00000002 +#define HMF_IOCTL_HW_GET_FLAGS _IOR(0xf5, 70, unsigned int) +#define HMF_IOCTL_HW_SET_FLAGS _IOR(0xf5, 71, unsigned int) + +void BDir::RmDirMonitor(const std::string dirPath) +{ + int32_t fd = open(dirPath.c_str(), O_RDONLY); + if (fd < 0) { + HILOGE("open dfx dirPath %{public}s failed", dirPath.c_str()); + return; + } + unsigned int flags = 0; + int32_t ret = ioctl(fd, HMF_IOCTL_HW_GET_FLAGS, &flags); + if (ret < 0) { + HILOGE("check dfx flag dirPath %{public}s failed errno:%{public}d", dirPath.c_str(), errno); + close(fd); + return; + } + if (flags & HMFS_MONITOR_FL) { + // flag is already set + flags -= HMFS_MONITOR_FL; + ret = ioctl(fd, HMF_IOCTL_HW_SET_FLAGS, &flags); + if (ret < 0) { + HILOGE("Rmv dfx flag failed errno:%{public}d dirPath %{public}s", errno, dirPath.c_str()); + close(fd); + return; + } + HILOGE("Delete Control flag of %{public}s is Rmv succeed", dirPath.c_str()); + } + close(fd); + return; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee