From c2e045e1684e03ff3ffd510f7a799bad98ec61ff Mon Sep 17 00:00:00 2001 From: caochuan Date: Thu, 15 Jan 2026 13:42:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=93=83=E9=9F=B3=E5=BA=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E6=A3=80=E6=9F=A5=E8=AD=A6?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: caochuan --- services/utils/src/ringtone_file_utils.cpp | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/services/utils/src/ringtone_file_utils.cpp b/services/utils/src/ringtone_file_utils.cpp index 595061e..be0f10e 100644 --- a/services/utils/src/ringtone_file_utils.cpp +++ b/services/utils/src/ringtone_file_utils.cpp @@ -489,9 +489,27 @@ bool RingtoneFileUtils::CopyFileUtil(const string &filePath, const string &newPa int32_t RingtoneFileUtils::CopyFileFromFd(int32_t srcFd, const std::string &newPath) { - CHECK_AND_RETURN_RET_LOG(!RingtoneFileUtils::IsFileExists(newPath), E_HAS_FS_ERROR, - "error file exists:%{public}s", newPath.c_str()); - int32_t dest = open(newPath.c_str(), O_WRONLY | O_CREAT, MODE_RW_USR); + CHECK_AND_RETURN_RET_LOG(!newPath.empty(), E_HAS_FS_ERROR, + "file path is empty:%{private}s", newPath.c_str()); + std::string filename; + std::string dirPath; + size_t lastSlash = newPath.find_last_of('/'); + if (lastSlash != std::string::npos) { + dirPath = newPath.substr(0, lastSlash); + filename = newPath.substr(lastSlash + 1); + } else { + RINGTONE_ERR_LOG("file path is error:%{private}s", newPath.c_str()); + return E_HAS_FS_ERROR; + } + + char filePathTemp[PATH_MAX] = {0}; + bool bflag = realpath(dirPath.c_str(), filePathTemp) == nullptr; + CHECK_AND_RETURN_RET_LOG(!bflag, E_ERR, + "check newPath fail, dirPath = %{private}s", dirPath.c_str()); + std::string path = std::string(filePathTemp) + "/" + filename; + CHECK_AND_RETURN_RET_LOG(!RingtoneFileUtils::IsFileExists(path), E_HAS_FS_ERROR, + "error file exists:%{private}s", path.c_str()); + int32_t dest = open(path.c_str(), O_WRONLY | O_CREAT, MODE_RW_USR); CHECK_AND_RETURN_RET_LOG(dest > 0, E_ERR, "Open failed for destination file %{public}d", errno); OHOS::UniqueFd destFd(dest);