From 2c2a864692f08273801a3922bd2e9ac8e8ec618b Mon Sep 17 00:00:00 2001 From: hunili Date: Thu, 15 Aug 2024 15:25:32 +0800 Subject: [PATCH] Rename err to copy(5.0release) issue: https://gitee.com/openharmony/filemanagement_app_file_service/issues/IAK9N2 Signed-off-by: hunili --- utils/src/b_filesystem/b_file.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/utils/src/b_filesystem/b_file.cpp b/utils/src/b_filesystem/b_file.cpp index f5828df41..097125673 100644 --- a/utils/src/b_filesystem/b_file.cpp +++ b/utils/src/b_filesystem/b_file.cpp @@ -186,10 +186,19 @@ bool BFile::MoveFile(const string &from, const string &to) throw BError(errno); } newPath.append("/").append(basename(name.get())); - if (rename(oldPath.c_str(), newPath.c_str())) { - HILOGE("failed to rename path, oldPath:%{public}s ,newPath: %{public}s", - GetAnonyPath(oldPath).c_str(), GetAnonyPath(newPath).c_str()); - throw BError(errno); + if (rename(oldPath.c_str(), newPath.c_str()) != 0) { + HILOGI("rename err,try copy errno: %{public}d", errno); + UniqueFd fdFrom(open(oldPath.data(), O_RDONLY)); + if (fdFrom == -1) { // -1: fd error code + HILOGE("failed to open the file %{public}s", GetAnonyPath(from).c_str()); + throw BError(errno); + } + UniqueFd fdTo(open(newPath.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); + if (fdTo == -1) { // -1: fd error code + HILOGE("failed to open the file %{public}s", GetAnonyPath(to).c_str()); + throw BError(errno); + } + SendFile(fdTo, fdFrom); } return true; } catch (const BError &e) { -- Gitee