1 /*
2  * Copyright (c) 2021 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 
16 #include "utils/mount_argument_utils.h"
17 
18 #include <sstream>
19 #include <sys/mount.h>
20 
21 namespace OHOS {
22 namespace StorageDaemon {
23 namespace Utils {
24 using namespace std;
25 namespace {
26     static const std::string DATA_POINT = "/data/service/el2/";
27     static const std::string BASE_MOUNT_POINT = "/mnt/hmdfs/";
28     static const std::string SYSFS_HMDFS_PATH = "/sys/fs/hmdfs/";
29     static const std::string COMM_DATA_POINT = "/storage/media/";
30     static const std::string COMM_CLOUD_POINT = "/storage/cloud/";
31     static const std::string RELATIVE_DOCS_PATH = "/files/Docs";
32     static const std::string SHAREFS_DATA_POINT = "/data/service/el2/";
33     static const std::string SHAREFS_BASE_MOUNT_POINT = "/mnt/share/";
34     static const std::string TMPFS_MNT_DATA = "/mnt/data/";
35     static const std::string HMDFS_DEVICE_VIEW_LOCAL_DOCS_PATH = "/device_view/local" + RELATIVE_DOCS_PATH;
36     static const std::string SANDBOX_PATH = "/mnt/sandbox/";
37     static const std::string MNT_USER_PATH = "/mnt/user/";
38     static const std::string NOSHAREFS_DOC_PATH = "/nosharefs/docs";
39     static const std::string SHAREFS_DOC_PATH = "/sharefs/docs";
40     static const std::string NOSHAREFS_DOC_CUR_PATH = "/nosharefs/docs/currentUser";
41     static const std::string SHAREFS_DOC_CUR_PATH = "/sharefs/docs/currentUser";
42     static const std::string LOCAL_FILE_DOCS_PATH = "/local/files/Docs";
43     static const std::string CUR_OTHER_PATH = "/currentUser/other";
44     static const std::string CUR_OTHER_APPDATA_PATH = "/currentUser/other/appdata";
45     static const std::string CUR_FILEMGR_PATH = "/currentUser/filemgr";
46     static const std::string CUR_FILEMGR_APPDATA_PATH = "/currentUser/filemgr/appdata";
47     static const std::string NOSHAREFS_APPDATA_PATH = "/nosharefs/appdata";
48 } // namespace
49 
GetFullSrc() const50 string MountArgument::GetFullSrc() const
51 {
52     stringstream ss;
53     ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_;
54 
55     return ss.str();
56 }
57 
GetFullDst() const58 string MountArgument::GetFullDst() const
59 {
60     stringstream ss;
61     ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_;
62 
63     return ss.str();
64 }
65 
GetFullMediaCloud() const66 string MountArgument::GetFullMediaCloud() const
67 {
68     stringstream ss;
69     ss << TMPFS_MNT_DATA << userId_ << "/" << "cloud";
70 
71     return ss.str();
72 }
73 
GetFullCloud() const74 string MountArgument::GetFullCloud() const
75 {
76     stringstream ss;
77     ss << TMPFS_MNT_DATA << userId_ << "/" << "cloud_fuse";
78 
79     return ss.str();
80 }
81 
GetShareSrc() const82 string MountArgument::GetShareSrc() const
83 {
84     stringstream ss;
85     ss << SHAREFS_DATA_POINT << userId_ << "/share";
86 
87     return ss.str();
88 }
89 
GetUserIdPara() const90 string MountArgument::GetUserIdPara() const
91 {
92     stringstream ss;
93     ss << "user_id=" << userId_;
94 
95     return ss.str();
96 }
97 
GetHmUserIdPara() const98 string MountArgument::GetHmUserIdPara() const
99 {
100     stringstream ss;
101     ss << "override_support_delete,user_id=" << userId_;
102     return ss.str();
103 }
104 
GetShareDst() const105 string MountArgument::GetShareDst() const
106 {
107     stringstream ss;
108     ss << SHAREFS_BASE_MOUNT_POINT << userId_;
109 
110     return ss.str();
111 }
112 
GetCommFullPath() const113 string MountArgument::GetCommFullPath() const
114 {
115     stringstream ss;
116     ss << COMM_DATA_POINT << userId_ << "/";
117 
118     return ss.str();
119 }
120 
GetCloudFullPath() const121 string MountArgument::GetCloudFullPath() const
122 {
123     stringstream ss;
124     ss << COMM_CLOUD_POINT << userId_ << "/";
125 
126     return ss.str();
127 }
128 
GetCloudDocsPath() const129 string MountArgument::GetCloudDocsPath() const
130 {
131     stringstream ss;
132     ss << COMM_CLOUD_POINT << userId_ << RELATIVE_DOCS_PATH;
133 
134     return ss.str();
135 }
136 
GetLocalDocsPath() const137 string MountArgument::GetLocalDocsPath() const
138 {
139     stringstream ss;
140     ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_ << HMDFS_DEVICE_VIEW_LOCAL_DOCS_PATH;
141 
142     return ss.str();
143 }
144 
GetCachePath() const145 string MountArgument::GetCachePath() const
146 {
147     stringstream ss;
148     if (enableCloudDisk_) {
149         ss << DATA_POINT << userId_ << "/hmdfs/cloud/";
150     } else {
151         ss << DATA_POINT << userId_ << "/hmdfs/cache/" << relativePath_ << "_cache/";
152     }
153     return ss.str();
154 }
155 
MocklispHash(const string & str)156 static uint64_t MocklispHash(const string &str)
157 {
158     uint64_t res = 0;
159     constexpr int mocklispHashPos = 5;
160     /* Mocklisp hash function. */
161     for (auto ch : str) {
162         res = (res << mocklispHashPos) - res + (uint64_t)ch;
163     }
164     return res;
165 }
166 
GetCtrlPath() const167 string MountArgument::GetCtrlPath() const
168 {
169     auto dst = GetFullDst();
170     auto res = MocklispHash(dst);
171 
172     stringstream ss;
173     ss << SYSFS_HMDFS_PATH << res << "/cmd";
174     return ss.str();
175 }
176 
GetMountPointPrefix() const177 string MountArgument::GetMountPointPrefix() const
178 {
179     stringstream ss;
180     ss << DATA_POINT << userId_ << "/hmdfs";
181     return ss.str();
182 }
183 
GetSandboxPath() const184 std::string MountArgument::GetSandboxPath() const
185 {
186     stringstream ss;
187     ss << SANDBOX_PATH << userId_;
188     return ss.str();
189 }
190 
OptionsToString() const191 string MountArgument::OptionsToString() const
192 {
193     stringstream ss;
194     ss << "local_dst=" << GetFullDst() << ",user_id=" << userId_;
195     ss << ",ra_pages=512";
196     if (useCache_) {
197         ss << ",cache_dir=" << GetCachePath();
198     }
199     if (useCloudDir_) {
200         ss << ",cloud_dir=" << GetFullMediaCloud();
201     }
202     if (caseSensitive_) {
203         ss << ",sensitive";
204     }
205     if (enableMergeView_) {
206         ss << ",merge";
207     }
208     if (enableCloudDisk_) {
209         ss << ",cloud_disk";
210     }
211     if (!enableOfflineStash_) {
212         ss << ",no_offline_stash";
213     }
214     return ss.str();
215 }
216 
GetMediaDocsPath() const217 string MountArgument::GetMediaDocsPath() const
218 {
219     stringstream ss;
220     ss << COMM_DATA_POINT << userId_ << LOCAL_FILE_DOCS_PATH;
221     return ss.str();
222 }
223 
GetNoSharefsAppdataPath() const224 string MountArgument::GetNoSharefsAppdataPath() const
225 {
226     stringstream ss;
227     ss << MNT_USER_PATH << userId_ << NOSHAREFS_APPDATA_PATH;
228     return ss.str();
229 }
230 
GetNoSharefsDocPath() const231 string MountArgument::GetNoSharefsDocPath() const
232 {
233     stringstream ss;
234     ss << MNT_USER_PATH << userId_ << NOSHAREFS_DOC_PATH;
235     return ss.str();
236 }
237 
GetNoSharefsDocCurPath() const238 string MountArgument::GetNoSharefsDocCurPath() const
239 {
240     stringstream ss;
241     ss << MNT_USER_PATH << userId_ << NOSHAREFS_DOC_CUR_PATH;
242     return ss.str();
243 }
244 
GetSharefsDocPath() const245 string MountArgument::GetSharefsDocPath() const
246 {
247     stringstream ss;
248     ss << MNT_USER_PATH << userId_ << SHAREFS_DOC_PATH;
249     return ss.str();
250 }
251 
GetSharefsDocCurPath() const252 string MountArgument::GetSharefsDocCurPath() const
253 {
254     stringstream ss;
255     ss << MNT_USER_PATH << userId_ << SHAREFS_DOC_CUR_PATH;
256     return ss.str();
257 }
258 
GetCurOtherAppdataPath() const259 string MountArgument::GetCurOtherAppdataPath() const
260 {
261     stringstream ss;
262     ss << MNT_USER_PATH << userId_ << CUR_OTHER_APPDATA_PATH;
263     return ss.str();
264 }
265 
GetMntUserPath() const266 string MountArgument::GetMntUserPath() const
267 {
268     stringstream ss;
269     ss << MNT_USER_PATH << userId_;
270     return ss.str();
271 }
272 
GetCurOtherPath() const273 string MountArgument::GetCurOtherPath() const
274 {
275     stringstream ss;
276     ss << MNT_USER_PATH << userId_ << CUR_OTHER_PATH;
277     return ss.str();
278 }
279 
GetCurFileMgrPath() const280 string MountArgument::GetCurFileMgrPath() const
281 {
282     stringstream ss;
283     ss << MNT_USER_PATH << userId_ << CUR_FILEMGR_PATH;
284     return ss.str();
285 }
286 
GetCurFileMgrAppdataPath() const287 string MountArgument::GetCurFileMgrAppdataPath() const
288 {
289     stringstream ss;
290     ss << MNT_USER_PATH << userId_ << CUR_FILEMGR_APPDATA_PATH;
291     return ss.str();
292 }
293 
GetFlags() const294 unsigned long MountArgument::GetFlags() const
295 {
296     return MS_NODEV;
297 }
298 
Alpha(int userId,string relativePath)299 MountArgument MountArgumentDescriptors::Alpha(int userId, string relativePath)
300 {
301     MountArgument mountArgument = {
302         .userId_ = userId,
303         .needInitDir_ = true,
304         .useCache_ = true,
305         .useCloudDir_ = true,
306         .enableMergeView_ = true,
307         .enableCloudDisk_ = false,
308         .enableFixupOwnerShip_ = false,
309         .enableOfflineStash_ = true,
310         .relativePath_ = relativePath,
311     };
312     return mountArgument;
313 }
314 } // namespace Utils
315 } // namespace StorageDaemon
316 } // namespace OHOS
317