1 /*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "clouddisk_notify_utils_mock.h"
16
17 #include "clouddisk_notify_utils.h"
18 #include "clouddisk_rdb_utils.h"
19 #include "file_column.h"
20 #include "cloud_pref_impl.h"
21 #include "dfs_error.h"
22 #include "utils_log.h"
23
24 namespace OHOS::FileManagement::CloudDisk {
25 const uint32_t MAX_QUERY_TIMES = 1024;
26 const string BUNDLENAME_FLAG = "<BundleName>";
27 const string CLOUDDISK_URI_PREFIX = "file://<BundleName>/data/storage/el2/cloud";
28 const string BACKFLASH = "/";
29 const int32_t MOCKUSERID = 10;
30 const int32_t CloudDiskNotifyUtils::maxCacheCnt_;
31 list<pair<string, CacheNode>> CloudDiskNotifyUtils::cacheList_;
32 unordered_map<string, list<pair<string, CacheNode>>::iterator> CloudDiskNotifyUtils::cacheMap_;
33 mutex CloudDiskNotifyUtils::cacheMutex_;
34
isRoot(const fuse_ino_t & ino)35 static bool isRoot(const fuse_ino_t &ino)
36 {
37 return ino == FUSE_ROOT_ID;
38 }
39
GetUriRecursively(CloudDiskFuseData * data,FindCloudDiskInodeFunc func,shared_ptr<CloudDiskInode> inoPtr,string & uri)40 static int32_t GetUriRecursively(CloudDiskFuseData* data, FindCloudDiskInodeFunc func,
41 shared_ptr<CloudDiskInode> inoPtr, string &uri)
42 {
43 string bundleName = inoPtr->bundleName;
44 string realPrefix = CLOUDDISK_URI_PREFIX;
45 realPrefix.replace(realPrefix.find(BUNDLENAME_FLAG), BUNDLENAME_FLAG.length(), bundleName);
46 uint32_t queryTimes = 0;
47 while (!isRoot(inoPtr->parent)) {
48 inoPtr = func(data, inoPtr->parent);
49 if (!inoPtr || inoPtr->fileName.empty()) {
50 break;
51 }
52 uri = inoPtr->fileName + BACKFLASH + uri;
53 queryTimes++;
54 if (uri.length() > PATH_MAX || queryTimes > MAX_QUERY_TIMES) {
55 return E_INVAL_ARG;
56 }
57 }
58 uri = realPrefix + BACKFLASH + uri;
59 LOGD("GetUriRecursively uri: %{public}s", GetAnonyString(uri).c_str());
60 return E_OK;
61 }
62
GetNotifyData(CloudDiskFuseData * data,FindCloudDiskInodeFunc func,const fuse_ino_t & ino,NotifyData & notifyData)63 int32_t CloudDiskNotifyUtils::GetNotifyData(CloudDiskFuseData* data, FindCloudDiskInodeFunc func,
64 const fuse_ino_t &ino, NotifyData ¬ifyData)
65 {
66 if (data->userId == MOCKUSERID) {
67 LOGI("AAA");
68 return E_INVAL_ARG;
69 }
70 return E_OK;
71 }
72
GetNotifyData(CloudDiskFuseData * data,FindCloudDiskInodeFunc func,const fuse_ino_t & parent,const string & name,NotifyData & notifyData)73 int32_t CloudDiskNotifyUtils::GetNotifyData(CloudDiskFuseData* data, FindCloudDiskInodeFunc func,
74 const fuse_ino_t &parent, const string &name, NotifyData ¬ifyData)
75 {
76 if (data->userId == MOCKUSERID || name == "mock") {
77 return E_INVAL_ARG;
78 }
79 return E_OK;
80 }
81
GetNotifyData(CloudDiskFuseData * data,FindCloudDiskInodeFunc func,shared_ptr<CloudDiskInode> inoPtr,NotifyData & notifyData)82 int32_t CloudDiskNotifyUtils::GetNotifyData(CloudDiskFuseData* data, FindCloudDiskInodeFunc func,
83 shared_ptr<CloudDiskInode> inoPtr, NotifyData ¬ifyData)
84 {
85 if (data->userId == MOCKUSERID) {
86 return E_INVAL_ARG;
87 }
88 return E_OK;
89 }
90
GetNotifyData(CloudDiskFuseData * data,FindCloudDiskInodeFunc func,shared_ptr<CloudDiskInode> pInoPtr,const string & name,NotifyData & notifyData)91 int32_t CloudDiskNotifyUtils::GetNotifyData(CloudDiskFuseData* data, FindCloudDiskInodeFunc func,
92 shared_ptr<CloudDiskInode> pInoPtr, const string &name, NotifyData ¬ifyData)
93 {
94 if (data->userId == MOCKUSERID || name == "mock") {
95 return E_INVAL_ARG;
96 }
97 return E_OK;
98 }
99
GetCacheNode(const string & cloudId,CacheNode & cacheNode)100 int32_t CloudDiskNotifyUtils::GetCacheNode(const string &cloudId, CacheNode &cacheNode)
101 {
102 lock_guard<mutex> lock(cacheMutex_);
103 auto it = cacheMap_.find(cloudId);
104 if (it == cacheMap_.end()) {
105 LOGI("Not fount in cache, id: %{public}s", cloudId.c_str());
106 return E_INVAL_ARG;
107 }
108 cacheList_.splice(cacheList_.begin(), cacheList_, it->second);
109 cacheNode = it->second->second;
110 return E_OK;
111 }
112
PutCacheNode(const string & cloudId,const CacheNode & cacheNode)113 void CloudDiskNotifyUtils::PutCacheNode(const string &cloudId, const CacheNode &cacheNode)
114 {
115 if (cacheNode.isDir != TYPE_DIR_STR) {
116 return;
117 }
118 lock_guard<mutex> lock(cacheMutex_);
119 auto it = cacheMap_.find(cloudId);
120 if (it != cacheMap_.end()) {
121 LOGD("update cache name: %{public}s", GetAnonyString(cacheNode.fileName).c_str());
122 it->second->second = cacheNode;
123 cacheList_.splice(cacheList_.begin(), cacheList_, it->second);
124 return;
125 }
126 if (cacheMap_.size() == maxCacheCnt_) {
127 LOGI("upto max, delete last one");
128 string deleteCloudId = cacheList_.back().first;
129 cacheList_.pop_back();
130 cacheMap_.erase(deleteCloudId);
131 }
132 LOGD("insert to cache name: %{public}s", GetAnonyString(cacheNode.fileName).c_str());
133 cacheList_.emplace_front(cloudId, cacheNode);
134 cacheMap_[cloudId] = cacheList_.begin();
135 }
136
GetUriFromCache(const string & bundleName,const string & rootId,const CacheNode & cacheNode,string & uri)137 int32_t CloudDiskNotifyUtils::GetUriFromCache(const string &bundleName,
138 const string &rootId,
139 const CacheNode &cacheNode,
140 string &uri)
141 {
142 if (uri == "mock") {
143 return E_RDB;
144 }
145 uri = ICloudDiskNotifyUtils::ins == nullptr ? uri : ICloudDiskNotifyUtils::ins->GetUriFromCache();
146 return E_OK;
147 }
148 } // namespace OHOS::FileManagement::CloudDisk