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 } // namespace
38
GetFullSrc() const39 string MountArgument::GetFullSrc() const
40 {
41 stringstream ss;
42 ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_;
43
44 return ss.str();
45 }
46
GetFullDst() const47 string MountArgument::GetFullDst() const
48 {
49 stringstream ss;
50 ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_;
51
52 return ss.str();
53 }
54
GetFullMediaCloud() const55 string MountArgument::GetFullMediaCloud() const
56 {
57 stringstream ss;
58 ss << TMPFS_MNT_DATA << userId_ << "/" << "cloud";
59
60 return ss.str();
61 }
62
GetFullCloud() const63 string MountArgument::GetFullCloud() const
64 {
65 stringstream ss;
66 ss << TMPFS_MNT_DATA << userId_ << "/" << "cloud_fuse";
67
68 return ss.str();
69 }
70
GetShareSrc() const71 string MountArgument::GetShareSrc() const
72 {
73 stringstream ss;
74 ss << SHAREFS_DATA_POINT << userId_ << "/share";
75
76 return ss.str();
77 }
78
GetUserIdPara() const79 string MountArgument::GetUserIdPara() const
80 {
81 stringstream ss;
82 ss << "user_id=" << userId_;
83
84 return ss.str();
85 }
86
GetShareDst() const87 string MountArgument::GetShareDst() const
88 {
89 stringstream ss;
90 ss << SHAREFS_BASE_MOUNT_POINT << userId_;
91
92 return ss.str();
93 }
94
GetCommFullPath() const95 string MountArgument::GetCommFullPath() const
96 {
97 stringstream ss;
98 ss << COMM_DATA_POINT << userId_ << "/";
99
100 return ss.str();
101 }
102
GetCloudFullPath() const103 string MountArgument::GetCloudFullPath() const
104 {
105 stringstream ss;
106 ss << COMM_CLOUD_POINT << userId_ << "/";
107
108 return ss.str();
109 }
110
GetCloudDocsPath() const111 string MountArgument::GetCloudDocsPath() const
112 {
113 stringstream ss;
114 ss << COMM_CLOUD_POINT << userId_ << RELATIVE_DOCS_PATH;
115
116 return ss.str();
117 }
118
GetLocalDocsPath() const119 string MountArgument::GetLocalDocsPath() const
120 {
121 stringstream ss;
122 ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_ << HMDFS_DEVICE_VIEW_LOCAL_DOCS_PATH;
123
124 return ss.str();
125 }
126
GetCachePath() const127 string MountArgument::GetCachePath() const
128 {
129 stringstream ss;
130 if (enableCloudDisk_) {
131 ss << DATA_POINT << userId_ << "/hmdfs/cloud/";
132 } else {
133 ss << DATA_POINT << userId_ << "/hmdfs/cache/" << relativePath_ << "_cache/";
134 }
135 return ss.str();
136 }
137
MocklispHash(const string & str)138 static uint64_t MocklispHash(const string &str)
139 {
140 uint64_t res = 0;
141 constexpr int mocklispHashPos = 5;
142 /* Mocklisp hash function. */
143 for (auto ch : str) {
144 res = (res << mocklispHashPos) - res + (uint64_t)ch;
145 }
146 return res;
147 }
148
GetCtrlPath() const149 string MountArgument::GetCtrlPath() const
150 {
151 auto dst = GetFullDst();
152 auto res = MocklispHash(dst);
153
154 stringstream ss;
155 ss << SYSFS_HMDFS_PATH << res << "/cmd";
156 return ss.str();
157 }
158
GetMountPointPrefix() const159 string MountArgument::GetMountPointPrefix() const
160 {
161 stringstream ss;
162 ss << DATA_POINT << userId_ << "/hmdfs";
163 return ss.str();
164 }
165
GetSandboxPath() const166 std::string MountArgument::GetSandboxPath() const
167 {
168 stringstream ss;
169 ss << SANDBOX_PATH << userId_;
170 return ss.str();
171 }
172
OptionsToString() const173 string MountArgument::OptionsToString() const
174 {
175 stringstream ss;
176 ss << "local_dst=" << GetFullDst() << ",user_id=" << userId_;
177 ss << ",ra_pages=512";
178 if (useCache_) {
179 ss << ",cache_dir=" << GetCachePath();
180 }
181 if (useCloudDir_) {
182 ss << ",cloud_dir=" << GetFullMediaCloud();
183 }
184 if (caseSensitive_) {
185 ss << ",sensitive";
186 }
187 if (enableMergeView_) {
188 ss << ",merge";
189 }
190 if (enableCloudDisk_) {
191 ss << ",cloud_disk";
192 }
193 if (!enableOfflineStash_) {
194 ss << ",no_offline_stash";
195 }
196 return ss.str();
197 }
198
GetFlags() const199 unsigned long MountArgument::GetFlags() const
200 {
201 return MS_NODEV;
202 }
203
Alpha(int userId,string relativePath)204 MountArgument MountArgumentDescriptors::Alpha(int userId, string relativePath)
205 {
206 MountArgument mountArgument = {
207 .userId_ = userId,
208 .needInitDir_ = true,
209 .useCache_ = true,
210 .useCloudDir_ = true,
211 .enableMergeView_ = true,
212 .enableCloudDisk_ = false,
213 .enableFixupOwnerShip_ = false,
214 .enableOfflineStash_ = true,
215 .relativePath_ = relativePath,
216 };
217 return mountArgument;
218 }
219 } // namespace Utils
220 } // namespace StorageDaemon
221 } // namespace OHOS
222