• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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/set_flag_utils.h"
17 #include "utils/file_utils.h"
18 
19 #include <fcntl.h>
20 #include <filesystem>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 #include "storage_service_log.h"
26 
27 namespace OHOS {
28 namespace StorageService {
29 #define HMFS_MONITOR_FL 0x00000002
30 #define HMFS_IOCTL_HW_GET_FLAGS _IOR(0XF5, 70, unsigned int)
31 #define HMFS_IOCTL_HW_SET_FLAGS _IOR(0XF5, 71, unsigned int)
32 const std::string PATH_EL0 = "/data/service/el0/storage_daemon/sd";
33 const std::string PATH_EL1 = "/data/service/el1/public/storage_daemon/sd";
34 
ParseDirAllPath()35 void SetFlagUtils::ParseDirAllPath()
36 {
37     ParseDirPath(PATH_EL0);
38     ParseDirPath(PATH_EL1);
39 }
40 
ParseDirPath(const std::string & path)41 void SetFlagUtils::ParseDirPath(const std::string &path)
42 {
43     if (!std::filesystem::exists(path)) {
44         LOGE("Invalid file path.");
45         return;
46     }
47     if (!StorageDaemon::IsDir(path)) {
48         LOGE("Input path is not a directory.");
49         return;
50     }
51     if (SetDirDelFlags(path) == false) {
52         LOGE("path not exist.");
53         return;
54     }
55     std::filesystem::directory_iterator pathList(path);
56     for (const auto& resPath : pathList) {
57         if (StorageDaemon::IsDir(resPath.path())) {
58             ParseDirPath(resPath.path().c_str());
59         } else if (StorageDaemon::IsFile(resPath.path())) {
60             SetFileDelFlags(resPath.path().c_str());
61         } else {
62             LOGE("Invalid file path.");
63         }
64     }
65 }
66 
SetFileDelFlags(const std::string & filepath)67 bool SetFlagUtils::SetFileDelFlags(const std::string &filepath)
68 {
69     LOGI("SetFlagUtils SetFileDelFlags for filepath=%{public}s start.", filepath.c_str());
70     char absPath[PATH_MAX] = {0};
71     if (realpath(filepath.c_str(), absPath) == nullptr) {
72         LOGE("SetFlagUtils Failed to get file realpath");
73         return false;
74     }
75     int32_t fd = open(absPath, O_RDWR);
76     if (fd < 0) {
77         LOGE("SetFlagUtils Failed to open file, errno: %{public}d", errno);
78         return false;
79     }
80     unsigned int flags = 0;
81     int32_t ret = ioctl(fd, HMFS_IOCTL_HW_GET_FLAGS, &flags);
82     if (ret < 0) {
83         LOGE("SetFlagUtils Failed to get file flags, errno: %{public}d", errno);
84         close(fd);
85         return false;
86     }
87     if (flags & HMFS_MONITOR_FL) {
88         LOGE("SetFlagUtils Delete control flag ia already set");
89         close(fd);
90         return true;
91     }
92     flags |= HMFS_MONITOR_FL;
93     ret  = ioctl(fd, HMFS_IOCTL_HW_SET_FLAGS, &flags);
94     if (ret < 0) {
95         LOGE("SetFlagUtils Failed to set file flags, errno: %{public}d", errno);
96     }
97     close(fd);
98     return true;
99 }
100 
SetDirDelFlags(const std::string & dirpath)101 bool SetFlagUtils::SetDirDelFlags(const std::string &dirpath)
102 {
103     LOGI("SetFlagUtils SetDirDelFlags for dirpath=%{public}s start.", dirpath.c_str());
104     char absPath[PATH_MAX] = {0};
105     if (realpath(dirpath.c_str(), absPath) == nullptr) {
106         LOGE("SetFlagUtils Failed to get realpath");
107         return false;
108     }
109     int32_t fd = open(absPath, O_DIRECTORY);
110     if (fd < 0) {
111         LOGE("SetFlagUtils Failed to open dir, errno: %{public}d", errno);
112         return false;
113     }
114     unsigned int flags = 0;
115     int32_t ret = ioctl(fd, HMFS_IOCTL_HW_GET_FLAGS, &flags);
116     if (ret < 0) {
117         LOGE("SetFlagUtils Failed to get flags, errno: %{public}d", errno);
118         close(fd);
119         return false;
120     }
121     if (flags & HMFS_MONITOR_FL) {
122         LOGE("SetFlagUtils Delete control flag ia already set");
123         close(fd);
124         return true;
125     }
126     flags |= HMFS_MONITOR_FL;
127     ret  = ioctl(fd, HMFS_IOCTL_HW_SET_FLAGS, &flags);
128     if (ret < 0) {
129         LOGE("SetFlagUtils Failed to set flags, errno: %{public}d", errno);
130     }
131     close(fd);
132     return true;
133 }
134 }
135 }