1 /*
2 * Copyright (c) 2021-2025 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 #include <sys/stat.h>
21
22 #include "dfs_error.h"
23 #include "utils_log.h"
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 namespace Utils {
28 using namespace std;
29 namespace {
30 static const std::string DATA_POINT = "/data/service/el2/";
31 static const std::string BASE_MOUNT_POINT = "/mnt/hmdfs/";
32 static const std::string SYSFS_HMDFS_PATH = "/sys/fs/hmdfs/";
33 } // namespace
34
GetFullSrc() const35 string MountArgument::GetFullSrc() const
36 {
37 stringstream ss;
38 ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_;
39 return ss.str();
40 }
41
GetFullDst() const42 string MountArgument::GetFullDst() const
43 {
44 stringstream ss;
45 ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_;
46 return ss.str();
47 }
48
GetCachePath() const49 string MountArgument::GetCachePath() const
50 {
51 stringstream ss;
52 ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_ << "/cache/";
53 return ss.str();
54 }
55
MocklispHash(const string & str)56 static uint64_t MocklispHash(const string &str)
57 {
58 struct stat statBuf;
59 auto err = stat(str.c_str(), &statBuf);
60 if (err != 0) {
61 LOGE("stat failed %{public}s error, err: %{public}d", GetAnonyString(str).c_str(), err);
62 return static_cast<uint64_t>(FileManagement::ERR_BAD_VALUE);
63 }
64 LOGI("statBuf dev id: %{public}lu", static_cast<unsigned long>(statBuf.st_dev));
65 return statBuf.st_dev;
66 }
67
GetCtrlPath() const68 string MountArgument::GetCtrlPath() const
69 {
70 auto dst = GetFullDst();
71 auto res = MocklispHash(dst);
72 stringstream ss;
73 ss << SYSFS_HMDFS_PATH << res << "/cmd";
74 return ss.str();
75 }
76
OptionsToString() const77 string MountArgument::OptionsToString() const
78 {
79 stringstream ss;
80 ss << "local_dst=" << GetFullDst();
81 if (useCache_) {
82 ss << ",cache_dir=" << GetCachePath();
83 }
84 if (caseSensitive_) {
85 ss << ",sensitive";
86 }
87 if (enableMergeView_) {
88 ss << ",merge";
89 }
90 if (enableFixupOwnerShip_) {
91 ss << ",fixupownership";
92 }
93 if (!enableOfflineStash_) {
94 ss << ",no_offline_stash";
95 }
96 if (externalFS_) {
97 ss << ",external_fs";
98 }
99 return ss.str();
100 }
101
GetFlags() const102 unsigned long MountArgument::GetFlags() const
103 {
104 return MS_NODEV;
105 }
106
Alpha(int userId,string relativePath)107 MountArgument DfsuMountArgumentDescriptors::Alpha(int userId, string relativePath)
108 {
109 MountArgument mountArgument = {
110 .userId_ = userId,
111 .needInitDir_ = true,
112 .useCache_ = true,
113 .enableMergeView_ = true,
114 .enableFixupOwnerShip_ = false,
115 .enableOfflineStash_ = true,
116 .externalFS_ = false,
117 .relativePath_ = relativePath,
118 };
119
120 if (relativePath == "non_account") {
121 mountArgument.accountless_ = true;
122 }
123 return mountArgument;
124 };
125 } // namespace Utils
126 } // namespace DistributedFile
127 } // namespace Storage
128 } // namespace OHOS
129