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 * Description: FileUtils implements
16 */
17 #include "file_utils.h"
18
19 #include <cerrno>
20 #include <cstring>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <regex>
25 #include "logging.h"
26
27 namespace {
28 constexpr size_t DEFAULT_READ_SIZE = 4096;
29 }
30
ReadFile(int fd)31 std::string FileUtils::ReadFile(int fd)
32 {
33 std::string content;
34 size_t count = 0;
35 while (true) {
36 if (content.size() - count < DEFAULT_READ_SIZE) {
37 content.resize(content.size() + DEFAULT_READ_SIZE);
38 }
39 ssize_t nBytes = TEMP_FAILURE_RETRY(read(fd, &content[count], content.size() - count));
40 if (nBytes <= 0) {
41 break;
42 }
43 count += nBytes;
44 }
45 content.resize(count);
46 return content;
47 }
48
ReadFile(const std::string & path)49 std::string FileUtils::ReadFile(const std::string& path)
50 {
51 char realPath[PATH_MAX + 1] = {0};
52 if ((path.length() >= PATH_MAX) || (realpath(path.c_str(), realPath) == nullptr)) {
53 HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
54 return "";
55 }
56 int fd = open(realPath, O_RDONLY);
57 if (fd == -1) {
58 const int bufSize = 256;
59 char buf[bufSize] = { 0 };
60 strerror_r(errno, buf, bufSize);
61 HILOG_WARN(LOG_CORE, "open file %s FAILED: %s!", path.c_str(), buf);
62 return "";
63 }
64 std::string content = ReadFile(fd);
65 CHECK_TRUE(close(fd) != -1, content, "close %s failed, %d", path.c_str(), errno);
66 return content;
67 }
68
WriteFile(const std::string & path,const std::string & content)69 int FileUtils::WriteFile(const std::string& path, const std::string& content)
70 {
71 return WriteFile(path, content, O_WRONLY);
72 }
73
WriteFile(const std::string & path,const std::string & content,int flags)74 int FileUtils::WriteFile(const std::string& path, const std::string& content, int flags)
75 {
76 return WriteFile(path, content, flags, 0);
77 }
78
WriteFile(const std::string & path,const std::string & content,int flags,int mode)79 int FileUtils::WriteFile(const std::string& path, const std::string& content, int flags, int mode)
80 {
81 if (path.empty() || (path.length() >= PATH_MAX)) {
82 HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
83 return -1;
84 }
85
86 std::regex dirNameRegex("[.~-]");
87 std::regex fileNameRegex("[\\/:*?\"<>|]");
88 size_t pos = path.rfind("/");
89 if (pos != std::string::npos) {
90 std::string dirName = path.substr(0, pos+1);
91 std::string fileName = path.substr(pos+1, path.length()-pos-1);
92 if (std::regex_search(dirName, dirNameRegex) || std::regex_search(fileName, fileNameRegex)) {
93 HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
94 return -1;
95 }
96 } else {
97 if (std::regex_search(path, fileNameRegex)) {
98 HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
99 return -1;
100 }
101 }
102
103 int fd = open(path.c_str(), flags, mode);
104 CHECK_TRUE(fd >= 0, -1, "open %s failed, %d", path.c_str(), errno);
105
106 int retval = write(fd, content.data(), content.size());
107 CHECK_TRUE(close(fd) != -1, -1, "close %s failed, %d", path.c_str(), errno);
108 return retval;
109 }
110
ListDir(const std::string & dirPath)111 std::vector<std::string> FileUtils::ListDir(const std::string& dirPath)
112 {
113 std::vector<std::string> result;
114 DIR* dir = opendir(dirPath.c_str());
115 if (dir == nullptr) {
116 return result;
117 }
118
119 struct dirent* ent = nullptr;
120 while ((ent = readdir(dir)) != nullptr) {
121 std::string name = ent->d_name;
122 if (name == "." || name == "..") {
123 continue;
124 }
125 result.push_back(name);
126 }
127 closedir(dir);
128 return result;
129 }
130