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(std::string path,uint64_t * size)53 int GetDevSize(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 %s{private}s failed", path.c_str());
59 return E_ERR;
60 }
61
62 if (ioctl(fd, BLKGETSIZE64, size)) {
63 LOGE("get device %s{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.", uuid.c_str(), type.c_str(),
98 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 = string::npos;
127 sep = output[0].find_first_of("\n");
128 if (sep != string::npos)
129 output[0].resize(sep);
130 return output[0];
131 }
132 return "";
133 }
134 } // namespace STORAGE_DAEMON
135 } // namespace OHOS
136