From b59a79dfa29c16f6c4344c501f11776df6e5c6ec Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Mon, 17 Jul 2023 14:36:55 +0800 Subject: [PATCH] delete exist shared file when sharing twice Signed-off-by: zhangkaixiang Change-Id: I829bfb0871cbec559a8defbd265a7c48b87dd224 --- .../native/file_share/src/file_share.cpp | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 22b56a4fe..5cfea0e65 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -54,7 +54,6 @@ struct FileShareInfo { string providerLowerPath_; string providerSandboxPath_; vector sharePath_; - vector isExist_; string currentUid_; ShareFileType type_; }; @@ -112,11 +111,8 @@ static void GetSharePath(FileShareInfo &info, uint32_t flag) if ((flag & WRITE_URI_PERMISSION) == WRITE_URI_PERMISSION) { info.sharePath_.push_back(shareRWPath); info.sharePath_.push_back(shareRPath); - info.isExist_.push_back(false); - info.isExist_.push_back(false); } else if ((flag & READ_URI_PERMISSION) == READ_URI_PERMISSION) { info.sharePath_.push_back(shareRPath); - info.isExist_.push_back(false); } } @@ -182,6 +178,16 @@ static bool MakeDir(const string &path) return true; } +static void DeleteExistShareFile(const string &path) +{ + if (access(path.c_str(), F_OK) == 0) { + if (umount2(path.c_str(), MNT_DETACH) != 0) { + LOGE("Umount failed with %{public}d", errno); + } + remove(path.c_str()); + } +} + static int32_t PreparePreShareDir(FileShareInfo &info) { if (!CommonFunc::CheckValidPath(info.providerLowerPath_)) { @@ -199,8 +205,7 @@ static int32_t PreparePreShareDir(FileShareInfo &info) return -errno; } } else { - LOGE("File %{public}s already exists", info.sharePath_[i].c_str()); - info.isExist_[i] = true; + DeleteExistShareFile(info.sharePath_[i]); } } return 0; @@ -221,10 +226,6 @@ int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) } for (size_t i = 0; i < info.sharePath_.size(); i++) { - if (info.isExist_[i]) { - continue; - } - if (info.type_ == ShareFileType::FILE_TYPE) { if ((ret = creat(info.sharePath_[i].c_str(), FILE_MODE)) < 0) { LOGE("Create file failed with %{public}d", errno); -- Gitee