• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "common_utils.h"
16 
17 #include <cinttypes>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 
21 #include "securec.h"
22 #include "sensors_errors.h"
23 
24 #undef LOG_TAG
25 #define LOG_TAG "CommonUtils"
26 
27 namespace OHOS {
28 namespace Sensors {
29 namespace {
30 const std::string CONFIG_DIR = "/data/test/vibrator/";
31 constexpr int32_t FILE_SIZE_MAX = 0x5000;
32 constexpr int32_t INVALID_FILE_SIZE = -1;
33 constexpr int32_t FILE_PATH_MAX = 1024;
34 } // namespace
35 
CheckFilePath(const std::string & filePath)36 bool CheckFilePath(const std::string &filePath)
37 {
38     if (filePath.empty()) {
39         MISC_HILOGE("Path is empty");
40         return false;
41     }
42     char realPath[PATH_MAX] = {};
43     if (realpath(filePath.c_str(), realPath) == nullptr) {
44         MISC_HILOGE("Path is error, %{public}d", errno);
45         return false;
46     }
47     if (!CheckFileDir(realPath, CONFIG_DIR)) {
48         MISC_HILOGE("File dir is invalid");
49         return false;
50     }
51     if (!CheckFileExtendName(realPath, "json")) {
52         MISC_HILOGE("Unable to parse files other than json format");
53         return false;
54     }
55     if (!IsFileExists(realPath)) {
56         MISC_HILOGE("File not exist");
57         return false;
58     }
59     if (!CheckFileSize(realPath)) {
60         MISC_HILOGE("File size out of read range");
61         return false;
62     }
63     return true;
64 }
65 
GetFileSize(const std::string & filePath)66 int32_t GetFileSize(const std::string &filePath)
67 {
68     struct stat statbuf = { 0 };
69     if (stat(filePath.c_str(), &statbuf) != 0) {
70         MISC_HILOGE("Get file size error");
71         return INVALID_FILE_SIZE;
72     }
73     return statbuf.st_size;
74 }
75 
GetFileSize(int32_t fd)76 int64_t GetFileSize(int32_t fd)
77 {
78     if (fd < 0) {
79         MISC_HILOGE("fd is invalid, fd:%{public}d", fd);
80         return INVALID_FILE_SIZE;
81     }
82     struct stat64 statbuf = { 0 };
83     if (fstat64(fd, &statbuf) != 0) {
84         MISC_HILOGE("fstat error, errno:%{public}d", errno);
85         return INVALID_FILE_SIZE;
86     }
87     return statbuf.st_size;
88 }
89 
GetFileName(const int32_t & fd,std::string & fileName)90 int32_t GetFileName(const int32_t &fd, std::string &fileName)
91 {
92     if (fd < 0) {
93         MISC_HILOGE("fd is invalid, fd:%{public}d", fd);
94         return ERROR;
95     }
96     char buf[FILE_PATH_MAX] = {'\0'};
97     char filePath[FILE_PATH_MAX] = {'\0'};
98 
99     int ret = snprintf_s(buf, sizeof(buf), (sizeof(buf) - 1), "/proc/self/fd/%d", fd);
100     if (ret < 0) {
101         MISC_HILOGE("snprintf failed with %{public}d", errno);
102         return ERROR;
103     }
104 
105     ret = readlink(buf, filePath, FILE_PATH_MAX);
106     if (ret < 0 || ret >= FILE_PATH_MAX) {
107         MISC_HILOGE("readlink failed with %{public}d", errno);
108         return ERROR;
109     }
110 
111     fileName = filePath;
112     std::size_t firstSlash = fileName.rfind("/");
113     if (firstSlash == fileName.npos) {
114         MISC_HILOGE("Get error path");
115         return ERROR;
116     }
117     fileName = fileName.substr(firstSlash + 1, fileName.size() - firstSlash);
118     return SUCCESS;
119 }
120 
GetFileExtName(const int32_t & fd,std::string & extName)121 int32_t GetFileExtName(const int32_t &fd, std::string &extName)
122 {
123     if (fd < 0) {
124         MISC_HILOGE("fd is invalid, fd:%{public}d", fd);
125         return ERROR;
126     }
127     std::string fileName = "";
128     if (GetFileName(fd, fileName) == ERROR) {
129         MISC_HILOGE("GetFileName failed");
130         return ERROR;
131     }
132     extName = fileName.substr(fileName.find_last_of(".") + 1);
133     return SUCCESS;
134 }
135 
CheckFileDir(const std::string & filePath,const std::string & dir)136 bool CheckFileDir(const std::string &filePath, const std::string &dir)
137 {
138     if (filePath.compare(0, CONFIG_DIR.size(), CONFIG_DIR) != 0) {
139         MISC_HILOGE("filePath dir is invalid");
140         return false;
141     }
142     return true;
143 }
144 
CheckFileSize(const std::string & filePath)145 bool CheckFileSize(const std::string &filePath)
146 {
147     int32_t fileSize = GetFileSize(filePath);
148     if ((fileSize <= 0) || (fileSize > FILE_SIZE_MAX)) {
149         MISC_HILOGE("File size out of read range");
150     }
151     return true;
152 }
153 
CheckFileExtendName(const std::string & filePath,const std::string & checkExtension)154 bool CheckFileExtendName(const std::string &filePath, const std::string &checkExtension)
155 {
156     std::string::size_type pos = filePath.find_last_of('.');
157     if (pos == std::string::npos) {
158         MISC_HILOGE("File is not find extension");
159         return false;
160     }
161     return (filePath.substr(pos + 1, filePath.npos) == checkExtension);
162 }
163 
IsFileExists(const std::string & fileName)164 bool IsFileExists(const std::string &fileName)
165 {
166     return (access(fileName.c_str(), F_OK) == 0);
167 }
168 } // namespace Sensors
169 } // namespace OHOS
170