• 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 SHAREFS_DATA_POINT = "/data/service/el2/";
31     static const std::string SHAREFS_BASE_MOUNT_POINT = "/mnt/share/";
32 } // namespace
33 
GetFullSrc() const34 string MountArgument::GetFullSrc() const
35 {
36     stringstream ss;
37     ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_;
38 
39     return ss.str();
40 }
41 
GetFullDst() const42 string MountArgument::GetFullDst() const
43 {
44     stringstream ss;
45     ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_;
46 
47     return ss.str();
48 }
49 
GetShareSrc() const50 string MountArgument::GetShareSrc() const
51 {
52     stringstream ss;
53     ss << SHAREFS_DATA_POINT << userId_ << "/share";
54 
55     return ss.str();
56 }
57 
GetUserIdPara() const58 string MountArgument::GetUserIdPara() const
59 {
60     stringstream ss;
61     ss << "user_id=" << userId_;
62 
63     return ss.str();
64 }
65 
GetShareDst() const66 string MountArgument::GetShareDst() const
67 {
68     stringstream ss;
69     ss << SHAREFS_BASE_MOUNT_POINT << userId_;
70 
71     return ss.str();
72 }
73 
GetCommFullPath() const74 string MountArgument::GetCommFullPath() const
75 {
76     stringstream ss;
77     ss << COMM_DATA_POINT << userId_ << "/";
78 
79     return ss.str();
80 }
81 
GetCachePath() const82 string MountArgument::GetCachePath() const
83 {
84     stringstream ss;
85     ss << DATA_POINT << userId_ << "/hmdfs/cache/" << relativePath_ << "_cache/";
86 
87     return ss.str();
88 }
89 
MocklispHash(const string & str)90 static uint64_t MocklispHash(const string &str)
91 {
92     uint64_t res = 0;
93     constexpr int mocklispHashPos = 5;
94     /* Mocklisp hash function. */
95     for (auto ch : str) {
96         res = (res << mocklispHashPos) - res + (uint64_t)ch;
97     }
98     return res;
99 }
100 
GetCtrlPath() const101 string MountArgument::GetCtrlPath() const
102 {
103     auto dst = GetFullDst();
104     auto res = MocklispHash(dst);
105 
106     stringstream ss;
107     ss << SYSFS_HMDFS_PATH << res << "/cmd";
108     return ss.str();
109 }
110 
OptionsToString() const111 string MountArgument::OptionsToString() const
112 {
113     stringstream ss;
114     ss << "local_dst=" << GetFullDst() << ",user_id=" << userId_;
115     if (useCache_) {
116         ss << ",cache_dir=" << GetCachePath();
117     }
118     if (caseSensitive_) {
119         ss << ",sensitive";
120     }
121     if (enableMergeView_) {
122         ss << ",merge";
123     }
124     if (!enableOfflineStash_) {
125         ss << ",no_offline_stash";
126     }
127     return ss.str();
128 }
129 
GetFlags() const130 unsigned long MountArgument::GetFlags() const
131 {
132     return MS_NODEV;
133 }
134 
Alpha(int userId,string relativePath)135 MountArgument MountArgumentDescriptors::Alpha(int userId, string relativePath)
136 {
137     MountArgument mountArgument = {
138         .userId_ = userId,
139         .needInitDir_ = true,
140         .useCache_ = true,
141         .enableMergeView_ = true,
142         .enableFixupOwnerShip_ = false,
143         .enableOfflineStash_ = true,
144         .relativePath_ = relativePath,
145     };
146     return mountArgument;
147 }
148 } // namespace Utils
149 } // namespace StorageDaemon
150 } // namespace OHOS
151