1 /*
2 * Copyright (c) 2022 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 "file_utils.h"
16
17 #include <cerrno>
18 #include <sys/stat.h>
19 #include <unistd.h>
20
21 #include "sensors_errors.h"
22
23 namespace OHOS {
24 namespace Sensors {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceFileUtils" };
27 const std::string CONFIG_DIR = "/vendor/etc/vibrator/";
28 constexpr int32_t FILE_SIZE_MAX = 0x5000;
29 constexpr int32_t READ_DATA_BUFF_SIZE = 256;
30 constexpr int32_t INVALID_FILE_SIZE = -1;
31 } // namespace
32
ReadJsonFile(const std::string & filePath)33 std::string ReadJsonFile(const std::string &filePath)
34 {
35 if (filePath.empty()) {
36 MISC_HILOGE("Path is empty");
37 return {};
38 }
39 char realPath[PATH_MAX] = {};
40 if (realpath(filePath.c_str(), realPath) == nullptr) {
41 MISC_HILOGE("Path is error, %{public}d", errno);
42 return {};
43 }
44 if (!CheckFileDir(realPath, CONFIG_DIR)) {
45 MISC_HILOGE("File dir is invalid");
46 return {};
47 }
48 if (!CheckFileExtendName(realPath, "json")) {
49 MISC_HILOGE("Unable to parse files other than json format");
50 return {};
51 }
52 if (!IsFileExists(filePath)) {
53 MISC_HILOGE("File not exist");
54 return {};
55 }
56 if (!CheckFileSize(filePath)) {
57 MISC_HILOGE("File size out of read range");
58 return {};
59 }
60 return ReadFile(realPath);
61 }
62
GetFileSize(const std::string & filePath)63 int32_t GetFileSize(const std::string& filePath)
64 {
65 struct stat statbuf = {0};
66 if (stat(filePath.c_str(), &statbuf) != 0) {
67 MISC_HILOGE("Get file size error");
68 return INVALID_FILE_SIZE;
69 }
70 return statbuf.st_size;
71 }
72
CheckFileDir(const std::string & filePath,const std::string & dir)73 bool CheckFileDir(const std::string& filePath, const std::string& dir)
74 {
75 if (filePath.compare(0, CONFIG_DIR.size(), CONFIG_DIR) != 0) {
76 MISC_HILOGE("FilePath dir is invalid");
77 return false;
78 }
79 return true;
80 }
81
CheckFileSize(const std::string & filePath)82 bool CheckFileSize(const std::string& filePath)
83 {
84 int32_t fileSize = GetFileSize(filePath);
85 if ((fileSize <= 0) || (fileSize > FILE_SIZE_MAX)) {
86 MISC_HILOGE("File size out of read range");
87 return false;
88 }
89 return true;
90 }
91
CheckFileExtendName(const std::string & filePath,const std::string & checkExtension)92 bool CheckFileExtendName(const std::string& filePath, const std::string& checkExtension)
93 {
94 std::string::size_type pos = filePath.find_last_of('.');
95 if (pos == std::string::npos) {
96 MISC_HILOGE("File is not find extension");
97 return false;
98 }
99 return (filePath.substr(pos + 1, filePath.npos) == checkExtension);
100 }
101
IsFileExists(const std::string & fileName)102 bool IsFileExists(const std::string& fileName)
103 {
104 return (access(fileName.c_str(), F_OK) == 0);
105 }
106
ReadFile(const std::string & filePath)107 std::string ReadFile(const std::string &filePath)
108 {
109 FILE* fp = fopen(filePath.c_str(), "r");
110 CHKPS(fp);
111 std::string dataStr;
112 char buf[READ_DATA_BUFF_SIZE] = {};
113 while (fgets(buf, sizeof(buf), fp) != nullptr) {
114 dataStr += buf;
115 }
116 if (fclose(fp) != 0) {
117 MISC_HILOGW("Close file failed");
118 }
119 return dataStr;
120 }
121 } // namespace Sensors
122 } // namespace OHOS