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