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 "dfsu_mount_argument_descriptors.h"
17 
18 #include <sstream>
19 #include <sys/mount.h>
20 
21 namespace OHOS {
22 namespace Storage {
23 namespace DistributedFile {
24 namespace Utils {
25 using namespace std;
26 namespace {
27     static const std::string DATA_POINT = "/data/service/el2/";
28     static const std::string BASE_MOUNT_POINT = "/mnt/hmdfs/";
29     static const std::string SYSFS_HMDFS_PATH = "/sys/fs/hmdfs/";
30 } // namespace
31 
GetFullSrc() const32 string MountArgument::GetFullSrc() const
33 {
34     stringstream ss;
35     ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_;
36     return ss.str();
37 }
38 
GetFullDst() const39 string MountArgument::GetFullDst() const
40 {
41     stringstream ss;
42     ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_;
43     return ss.str();
44 }
45 
GetCachePath() const46 string MountArgument::GetCachePath() const
47 {
48     stringstream ss;
49     ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_ << "/cache/";
50     return ss.str();
51 }
52 
MocklispHash(const string & str)53 static uint64_t MocklispHash(const string &str)
54 {
55     uint64_t res = 0;
56     constexpr int mocklispHashPos = 5;
57     /* Mocklisp hash function. */
58     for (auto ch : str) {
59         res = (res << mocklispHashPos) - res + (uint64_t)ch;
60     }
61     return res;
62 }
63 
GetCtrlPath() const64 string MountArgument::GetCtrlPath() const
65 {
66     auto dst = GetFullDst();
67     auto res = MocklispHash(dst);
68 
69     stringstream ss;
70     ss << SYSFS_HMDFS_PATH << res << "/cmd";
71     return ss.str();
72 }
73 
OptionsToString() const74 string MountArgument::OptionsToString() const
75 {
76     stringstream ss;
77     ss << "local_dst=" << GetFullDst();
78     if (useCache_) {
79         ss << ",cache_dir=" << GetCachePath();
80     }
81     if (caseSensitive_) {
82         ss << ",sensitive";
83     }
84     if (enableMergeView_) {
85         ss << ",merge";
86     }
87     if (enableFixupOwnerShip_) {
88         ss << ",fixupownership";
89     }
90     if (!enableOfflineStash_) {
91         ss << ",no_offline_stash";
92     }
93     if (externalFS_) {
94         ss << ",external_fs";
95     }
96     return ss.str();
97 }
98 
GetFlags() const99 unsigned long MountArgument::GetFlags() const
100 {
101     return MS_NODEV;
102 }
103 
Alpha(int userId,string relativePath)104 MountArgument DfsuMountArgumentDescriptors::Alpha(int userId, string relativePath)
105 {
106     MountArgument mountArgument = {
107         .userId_ = userId,
108         .needInitDir_ = true,
109         .useCache_ = true,
110         .enableMergeView_ = true,
111         .enableFixupOwnerShip_ = false,
112         .enableOfflineStash_ = true,
113         .externalFS_ = false,
114         .relativePath_ = relativePath,
115     };
116 
117     if (relativePath == "non_account") {
118         mountArgument.accountless_ = true;
119     }
120     return mountArgument;
121 };
122 } // namespace Utils
123 } // namespace DistributedFile
124 } // namespace Storage
125 } // namespace OHOS
126