• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SHAREFS_DATA_POINT = "/data/service/el2/";
32     static const std::string SHAREFS_BASE_MOUNT_POINT = "/mnt/share/";
33 } // namespace
34 
GetFullSrc() const35 string MountArgument::GetFullSrc() const
36 {
37     stringstream ss;
38     ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_;
39 
40     return ss.str();
41 }
42 
GetFullDst() const43 string MountArgument::GetFullDst() const
44 {
45     stringstream ss;
46     ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_;
47 
48     return ss.str();
49 }
50 
GetFullCloud() const51 string MountArgument::GetFullCloud() const
52 {
53     stringstream ss;
54     ss << BASE_MOUNT_POINT << userId_ << "/" << "cloud";
55 
56     return ss.str();
57 }
58 
GetShareSrc() const59 string MountArgument::GetShareSrc() const
60 {
61     stringstream ss;
62     ss << SHAREFS_DATA_POINT << userId_ << "/share";
63 
64     return ss.str();
65 }
66 
GetUserIdPara() const67 string MountArgument::GetUserIdPara() const
68 {
69     stringstream ss;
70     ss << "user_id=" << userId_;
71 
72     return ss.str();
73 }
74 
GetShareDst() const75 string MountArgument::GetShareDst() const
76 {
77     stringstream ss;
78     ss << SHAREFS_BASE_MOUNT_POINT << userId_;
79 
80     return ss.str();
81 }
82 
GetCommFullPath() const83 string MountArgument::GetCommFullPath() const
84 {
85     stringstream ss;
86     ss << COMM_DATA_POINT << userId_ << "/";
87 
88     return ss.str();
89 }
90 
GetCloudFullPath() const91 string MountArgument::GetCloudFullPath() const
92 {
93     stringstream ss;
94     ss << COMM_CLOUD_POINT << userId_ << "/";
95 
96     return ss.str();
97 }
98 
GetCachePath() const99 string MountArgument::GetCachePath() const
100 {
101     stringstream ss;
102     ss << DATA_POINT << userId_ << "/hmdfs/cache/" << relativePath_ << "_cache/";
103 
104     return ss.str();
105 }
106 
MocklispHash(const string & str)107 static uint64_t MocklispHash(const string &str)
108 {
109     uint64_t res = 0;
110     constexpr int mocklispHashPos = 5;
111     /* Mocklisp hash function. */
112     for (auto ch : str) {
113         res = (res << mocklispHashPos) - res + (uint64_t)ch;
114     }
115     return res;
116 }
117 
GetCtrlPath() const118 string MountArgument::GetCtrlPath() const
119 {
120     auto dst = GetFullDst();
121     auto res = MocklispHash(dst);
122 
123     stringstream ss;
124     ss << SYSFS_HMDFS_PATH << res << "/cmd";
125     return ss.str();
126 }
127 
OptionsToString() const128 string MountArgument::OptionsToString() const
129 {
130     stringstream ss;
131     ss << "local_dst=" << GetFullDst() << ",user_id=" << userId_;
132     if (useCache_) {
133         ss << ",cache_dir=" << GetCachePath();
134     }
135     if (useCloudDir_) {
136         ss << ",cloud_dir=" << GetFullCloud();
137     }
138     if (caseSensitive_) {
139         ss << ",sensitive";
140     }
141     if (enableMergeView_) {
142         ss << ",merge";
143     }
144     if (!enableOfflineStash_) {
145         ss << ",no_offline_stash";
146     }
147     return ss.str();
148 }
149 
GetFlags() const150 unsigned long MountArgument::GetFlags() const
151 {
152     return MS_NODEV;
153 }
154 
Alpha(int userId,string relativePath)155 MountArgument MountArgumentDescriptors::Alpha(int userId, string relativePath)
156 {
157     MountArgument mountArgument = {
158         .userId_ = userId,
159         .needInitDir_ = true,
160         .useCache_ = true,
161         .useCloudDir_ = true,
162         .enableMergeView_ = true,
163         .enableFixupOwnerShip_ = false,
164         .enableOfflineStash_ = true,
165         .relativePath_ = relativePath,
166     };
167     return mountArgument;
168 }
169 } // namespace Utils
170 } // namespace StorageDaemon
171 } // namespace OHOS
172