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/disk_utils.h"
17
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <cstdio>
21 #include <sys/stat.h>
22 #include <sys/sysmacros.h>
23 #include <unistd.h>
24 #include <unordered_map>
25
26 #include "storage_service_errno.h"
27 #include "storage_service_log.h"
28 #include "utils/file_utils.h"
29
30 using namespace std;
31 namespace OHOS {
32 namespace StorageDaemon {
33 static constexpr int32_t NODE_PERM = 0660;
34
CreateDiskNode(const std::string &path, dev_t dev)35 int CreateDiskNode(const std::string &path, dev_t dev)
36 {
37 const char *kPath = path.c_str();
38 if (mknod(kPath, NODE_PERM | S_IFBLK, dev) < 0) {
39 LOGE("create disk node failed");
40 return E_ERR;
41 }
42 return E_OK;
43 }
44
DestroyDiskNode(const std::string &path)45 int DestroyDiskNode(const std::string &path)
46 {
47 const char *kPath = path.c_str();
48 if (TEMP_FAILURE_RETRY(unlink(kPath)) < 0) {
49 return E_ERR;
50 }
51 return E_OK;
52 }
53
GetDevSize(const std::string &path, uint64_t *size)54 int GetDevSize(const std::string &path, uint64_t *size)
55 {
56 const char *kPath = path.c_str();
57 FILE *f = fopen(kPath, "r");
58 if (f == nullptr) {
59 LOGE("open %{private}s failed", path.c_str());
60 return E_ERR;
61 }
62 int fd = fileno(f);
63 if (fd < 0) {
64 LOGE("open %{private}s failed", path.c_str());
65 return E_ERR;
66 }
67
68 if (ioctl(fd, BLKGETSIZE64, size)) {
69 LOGE("get device %{private}s size failed", path.c_str());
70 (void)fclose(f);
71 return E_ERR;
72 }
73
74 (void)fclose(f);
75 return E_OK;
76 }
77
GetMaxVolume(dev_t device)78 int GetMaxVolume(dev_t device)
79 {
80 unsigned int majorId = major(device);
81 if (majorId == DISK_MMC_MAJOR) {
82 std::string str;
83 if (!ReadFile(MMC_MAX_VOLUMES_PATH, &str)) {
84 LOGE("Get MmcMaxVolumes failed");
85 return E_ERR;
86 }
87 return std::stoi(str);
88 } else {
89 return MAX_SCSI_VOLUMES;
90 }
91 }
92
ReadMetadata(const std::string &devPath, std::string &uuid, std::string &type, std::string &label)93 int32_t ReadMetadata(const std::string &devPath, std::string &uuid, std::string &type, std::string &label)
94 {
95 uuid = GetBlkidData(devPath, "UUID");
96 type = GetBlkidData(devPath, "TYPE");
97 label = GetBlkidData(devPath, "LABEL");
98 LOGI("ReadMetadata, fsUuid=%{public}s, fsType=%{public}s, fsLabel=%{public}s.", GetAnonyString(uuid).c_str(),
99 type.c_str(), label.c_str());
100 if (uuid.empty() || type.empty()) {
101 LOGE("External volume ReadMetadata error.");
102 return E_ERR;
103 }
104 if (type == "hmfs" && label != "/data") {
105 LOGE("Failed to mount the partition: Unsupported");
106 return E_ERR;
107 }
108 return E_OK;
109 }
110
GetBlkidData(const std::string &devPath, const std::string &type)111 std::string GetBlkidData(const std::string &devPath, const std::string &type)
112 {
113 std::vector<std::string> cmd;
114 cmd = {
115 "blkid",
116 "-s",
117 type,
118 "-o",
119 "value",
120 devPath
121 };
122 return GetBlkidDataByCmd(cmd);
123 }
124
GetBlkidDataByCmd(std::vector<std::string> &cmd)125 std::string GetBlkidDataByCmd(std::vector<std::string> &cmd)
126 {
127 std::vector<std::string> output;
128
129 int32_t err = ForkExec(cmd, &output);
130 if (err) {
131 return "";
132 }
133
134 if (output.size() > 0) {
135 size_t sep = output[0].find_first_of("\n");
136 if (sep != string::npos)
137 output[0].resize(sep);
138 return output[0];
139 }
140 return "";
141 }
142
GetAnonyString(const std::string &value)143 std::string GetAnonyString(const std::string &value)
144 {
145 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
146 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
147 constexpr size_t INT32_MIN_ID_LENGTH = 3;
148 std::string res;
149 std::string tmpStr("******");
150 size_t strLen = value.length();
151 if (strLen < INT32_MIN_ID_LENGTH) {
152 return tmpStr;
153 }
154
155 if (strLen <= INT32_SHORT_ID_LENGTH) {
156 res += value[0];
157 res += tmpStr;
158 res += value[strLen - 1];
159 } else {
160 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
161 res += tmpStr;
162 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
163 }
164
165 return res;
166 }
167 } // namespace STORAGE_DAEMON
168 } // namespace OHOS
169