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 <unistd.h>
20 #include <unordered_map>
21 #include <fcntl.h>
22
23 #include <sys/stat.h>
24 #include <sys/sysmacros.h>
25
26 #include "storage_service_errno.h"
27 #include "storage_service_log.h"
28 #include "utils/file_utils.h"
29
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 close(fd);
65 return E_ERR;
66 }
67
68 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 } // namespace STORAGE_DAEMON
87 } // namespace OHOS
88