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 <sys/stat.h>
19 #include <sys/sysmacros.h>
20 #include <unistd.h>
21
22 #include "storage_service_errno.h"
23 #include "storage_service_log.h"
24 #include "utils/file_utils.h"
25 #include "utils/storage_radar.h"
26
27 using namespace std;
28 using namespace OHOS::StorageService;
29 namespace OHOS {
30 namespace StorageDaemon {
31 constexpr int32_t NODE_PERM = 0660;
32 constexpr const char *MMC_MAX_VOLUMES_PATH = "/sys/module/mmcblk/parameters/perdev_minors";
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 FILE *f = fopen(kPath, "r");
57 if (f == nullptr) {
58 LOGE("open %{private}s failed", path.c_str());
59 return E_ERR;
60 }
61 int fd = fileno(f);
62 if (fd < 0) {
63 LOGE("open %{private}s failed", path.c_str());
64 (void)fclose(f);
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::atoi(str.c_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_READMETADATA;
103 }
104 return E_OK;
105 }
106
ReadVolumeUuid(const std::string & devPath,std::string & uuid)107 int32_t ReadVolumeUuid(const std::string &devPath, std::string &uuid)
108 {
109 uuid = GetBlkidData(devPath, "UUID");
110 LOGI("ReadMetadata, fsUuid=%{public}s", GetAnonyString(uuid).c_str());
111 if (uuid.empty()) {
112 LOGE("External volume ReadMetadata error.");
113 return E_READMETADATA;
114 }
115 return E_OK;
116 }
117
GetBlkidData(const std::string & devPath,const std::string & type)118 std::string GetBlkidData(const std::string &devPath, const std::string &type)
119 {
120 std::vector<std::string> cmd;
121 cmd = {
122 "blkid",
123 "-n",
124 "mdraid",
125 "-s",
126 type,
127 "-o",
128 "value",
129 devPath
130 };
131 return GetBlkidDataByCmd(cmd);
132 }
133
GetBlkidDataByCmd(std::vector<std::string> & cmd)134 std::string GetBlkidDataByCmd(std::vector<std::string> &cmd)
135 {
136 std::vector<std::string> output;
137
138 int32_t err = ForkExec(cmd, &output);
139 if (err) {
140 StorageRadar::ReportVolumeOperation("ForkExec", err);
141 return "";
142 }
143
144 if (output.size() > 0) {
145 size_t sep = output[0].find_first_of("\n");
146 if (sep != string::npos)
147 output[0].resize(sep);
148 return output[0];
149 }
150 return "";
151 }
152
GetAnonyString(const std::string & value)153 std::string GetAnonyString(const std::string &value)
154 {
155 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
156 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
157 constexpr size_t INT32_MIN_ID_LENGTH = 3;
158 std::string res;
159 std::string tmpStr("******");
160 size_t strLen = value.length();
161 if (strLen < INT32_MIN_ID_LENGTH) {
162 return tmpStr;
163 }
164
165 if (strLen <= INT32_SHORT_ID_LENGTH) {
166 res += value[0];
167 res += tmpStr;
168 res += value[strLen - 1];
169 } else {
170 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
171 res += tmpStr;
172 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
173 }
174
175 return res;
176 }
177 } // namespace STORAGE_DAEMON
178 } // namespace OHOS
179