• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
30 namespace StorageDaemon {
31 static constexpr int32_t NODE_PERM = 0660;
32 
CreateDiskNode(const std::string & path,dev_t dev)33 int CreateDiskNode(const std::string &path, dev_t dev)
34 {
35     const char *kPath = path.c_str();
36     if (mknod(kPath, NODE_PERM | S_IFBLK, dev) < 0) {
37         LOGE("create disk node failed");
38         return E_ERR;
39     }
40     return E_OK;
41 }
42 
DestroyDiskNode(const std::string & path)43 int DestroyDiskNode(const std::string &path)
44 {
45     const char *kPath = path.c_str();
46     if (TEMP_FAILURE_RETRY(unlink(kPath)) < 0) {
47         return E_ERR;
48     }
49     return E_OK;
50 }
51 
GetDevSize(std::string path,uint64_t * size)52 int GetDevSize(std::string path, uint64_t *size)
53 {
54     const char *kPath = path.c_str();
55     int fd = open(kPath, O_RDONLY);
56     if (fd < 0) {
57         LOGE("open %s{private}s failed", path.c_str());
58         return E_ERR;
59     }
60 
61     if (ioctl(fd, BLKGETSIZE64, size)) {
62         LOGE("get device %s{private}s size failed", path.c_str());
63         (void)close(fd);
64         return E_ERR;
65     }
66 
67     (void)close(fd);
68     return E_OK;
69 }
70 
GetMaxVolume(dev_t device)71 int GetMaxVolume(dev_t device)
72 {
73     unsigned int majorId = major(device);
74     if (majorId == DISK_MMC_MAJOR) {
75         std::string str;
76         if (!ReadFile(MMC_MAX_VOLUMES_PATH, &str)) {
77             LOGE("Get MmcMaxVolumes failed");
78             return E_ERR;
79         }
80         return std::stoi(str);
81     } else {
82         return MAX_SCSI_VOLUMES;
83     }
84 }
85 } // namespace STORAGE_DAEMON
86 } // namespace OHOS
87