From a70395e58e88a902264fe239f2ba5d58d9299f54 Mon Sep 17 00:00:00 2001 From: WDY Date: Tue, 8 Jul 2025 16:55:04 +0800 Subject: [PATCH] Fix session Signed-off-by: WDY --- .../cloud_asset_read_session.h | 2 ++ .../include/cloud_disk/cloud_disk_inode.h | 2 ++ .../src/cloud_disk/file_operations_cloud.cpp | 36 +++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/interfaces/inner_api/native/cloud_file_kit_inner/cloud_asset_read_session.h b/interfaces/inner_api/native/cloud_file_kit_inner/cloud_asset_read_session.h index dc6e13b61..b86f69574 100644 --- a/interfaces/inner_api/native/cloud_file_kit_inner/cloud_asset_read_session.h +++ b/interfaces/inner_api/native/cloud_file_kit_inner/cloud_asset_read_session.h @@ -17,6 +17,7 @@ #define OHOS_CLOUD_FILE_CLOUD_ASSET_READ_SESSION_H #include +#include #include #include "cloud_info.h" @@ -35,6 +36,7 @@ public: virtual void SentPrepareTraceId(std::string prepareTraceId); void SetPrepareTraceId(std::string prepareTraceId); std::string GetPrepareTraceId(); + int32_t sessionCount {0}; private: std::string traceId_; diff --git a/services/cloudfiledaemon/include/cloud_disk/cloud_disk_inode.h b/services/cloudfiledaemon/include/cloud_disk/cloud_disk_inode.h index dd18e84a2..b31132d25 100644 --- a/services/cloudfiledaemon/include/cloud_disk/cloud_disk_inode.h +++ b/services/cloudfiledaemon/include/cloud_disk/cloud_disk_inode.h @@ -90,6 +90,7 @@ struct CloudDiskFuseData { int64_t bundleNameId{1}; int64_t fileId{0}; std::shared_ptr rootNode{nullptr}; + std::unordered_map> readSessionCache; std::unordered_map> inodeCache; std::unordered_map> fileCache; std::unordered_map localIdCache; @@ -98,6 +99,7 @@ struct CloudDiskFuseData { std::shared_mutex fileLock; std::shared_mutex fileIdLock; std::shared_mutex localIdLock; + std::shared_mutex sessionLock; struct fuse_session *se; }; } // namespace CloudDisk diff --git a/services/cloudfiledaemon/src/cloud_disk/file_operations_cloud.cpp b/services/cloudfiledaemon/src/cloud_disk/file_operations_cloud.cpp index 824d6bae5..849b47d16 100644 --- a/services/cloudfiledaemon/src/cloud_disk/file_operations_cloud.cpp +++ b/services/cloudfiledaemon/src/cloud_disk/file_operations_cloud.cpp @@ -529,7 +529,15 @@ static int32_t GetNewSession(shared_ptr inoPtr, return ret; } LOGD("cloudId %s", cloudId.c_str()); + std::unique_lock lck(data->sessionLock); + if (data->readSessionCache.find(path) != data->readSessionCache.end()) { + data->readSessionCache[path]->sessionCount++; + filePtr->readSession = data->readSessionCache[path]; + return EOK; + } filePtr->readSession = database->NewAssetReadSession(data->userId, "file", cloudId, assets, path); + data->readSessionCache[path] = filePtr->readSession; + data->readSessionCache[path]->sessionCount++; return EOK; } @@ -573,6 +581,7 @@ static void CloudOpen(fuse_req_t req, shared_ptr inoPtr, filePtr->readSession = nullptr; FileOperationsHelper::PutCloudDiskFile(data, filePtr, fi->fh); fuse_inval(data->se, inoPtr->parent, ino, inoPtr->fileName); + data->readSessionCache.clear(); return (void) fuse_reply_err(req, ret); } } else { @@ -1850,6 +1859,24 @@ static int32_t HandleLocalClose(fuse_req_t req, shared_ptr inoPt return 0; } +static void ReleaseHandler(std::shared_ptr filePtr, + std::string cloudId, + std::string bundleName, + CloudDiskFuseData *data) +{ + + bool res = filePtr->readSession->Close(false); + if (!res) { + LOGE("close error"); + } + filePtr->readSession = nullptr; + std::string path = CloudFileUtils::GetLocalDKCachePath(cloudId, bundleName, data->userId); + if (data->readSessionCache.find(path) != data->readSessionCache.end()) { + data->readSessionCache.erase(path); + } + LOGD("readSession released"); +} + void FileOperationsCloud::Release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -1882,12 +1909,9 @@ void FileOperationsCloud::Release(fuse_req_t req, fuse_ino_t ino, struct fuse_fi return (void)fuse_reply_err(req, ret); } } else if (filePtr->type == CLOUD_DISK_FILE_TYPE_CLOUD && filePtr->readSession != nullptr) { - bool res = filePtr->readSession->Close(false); - if (!res) { - LOGE("close error"); - } - filePtr->readSession = nullptr; - LOGD("readSession released"); + std::unique_lock lck(data->sessionLock); + filePtr->readSession->sessionCount--; + ReleaseHandler(filePtr, inoPtr->cloudId, inoPtr->bundleName, data) } FileOperationsHelper::PutCloudDiskFile(data, filePtr, fi->fh); } -- Gitee