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::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_READMETADATA;
103 }
104 return E_OK;
105 }
106
GetBlkidData(const std::string & devPath,const std::string & type)107 std::string GetBlkidData(const std::string &devPath, const std::string &type)
108 {
109 std::vector<std::string> cmd;
110 cmd = {
111 "blkid",
112 "-s",
113 type,
114 "-o",
115 "value",
116 devPath
117 };
118 return GetBlkidDataByCmd(cmd);
119 }
120
GetBlkidDataByCmd(std::vector<std::string> & cmd)121 std::string GetBlkidDataByCmd(std::vector<std::string> &cmd)
122 {
123 std::vector<std::string> output;
124
125 int32_t err = ForkExec(cmd, &output);
126 if (err) {
127 StorageRadar::ReportVolumeOperation("ForkExec", err);
128 return "";
129 }
130
131 if (output.size() > 0) {
132 size_t sep = output[0].find_first_of("\n");
133 if (sep != string::npos)
134 output[0].resize(sep);
135 return output[0];
136 }
137 return "";
138 }
139
GetAnonyString(const std::string & value)140 std::string GetAnonyString(const std::string &value)
141 {
142 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
143 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
144 constexpr size_t INT32_MIN_ID_LENGTH = 3;
145 std::string res;
146 std::string tmpStr("******");
147 size_t strLen = value.length();
148 if (strLen < INT32_MIN_ID_LENGTH) {
149 return tmpStr;
150 }
151
152 if (strLen <= INT32_SHORT_ID_LENGTH) {
153 res += value[0];
154 res += tmpStr;
155 res += value[strLen - 1];
156 } else {
157 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
158 res += tmpStr;
159 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
160 }
161
162 return res;
163 }
164 } // namespace STORAGE_DAEMON
165 } // namespace OHOS
166