1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 == -1 && errno == EAGAIN) {
41 continue;
42 }
43 if (nBytes <= 0) {
44 break;
45 }
46 count += nBytes;
47 }
48 content.resize(count);
49 return content;
50 }
51
ReadFile(const std::string & path)52 std::string FileUtils::ReadFile(const std::string& path)
53 {
54 char realPath[PATH_MAX + 1] = {0};
55 CHECK_TRUE((path.length() < PATH_MAX) && (realpath(path.c_str(), realPath) != nullptr), "",
56 "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
57 int fd = open(realPath, O_RDONLY);
58 if (fd == -1) {
59 const int bufSize = 256;
60 char buf[bufSize] = { 0 };
61 strerror_r(errno, buf, bufSize);
62 PROFILER_LOG_WARN(LOG_CORE, "open file %s FAILED: %s!", path.c_str(), buf);
63 return "";
64 }
65 std::string content = ReadFile(fd);
66 CHECK_TRUE(close(fd) != -1, content, "close %s failed, %d", path.c_str(), errno);
67 return content;
68 }
69
WriteFile(const std::string & path,const std::string & content)70 int FileUtils::WriteFile(const std::string& path, const std::string& content)
71 {
72 return WriteFile(path, content, O_WRONLY);
73 }
74
WriteFile(const std::string & path,const std::string & content,int flags)75 int FileUtils::WriteFile(const std::string& path, const std::string& content, int flags)
76 {
77 return WriteFile(path, content, flags, 0);
78 }
79
WriteFile(const std::string & path,const std::string & content,int flags,int mode)80 int FileUtils::WriteFile(const std::string& path, const std::string& content, int flags, int mode)
81 {
82 CHECK_TRUE(!path.empty() && (path.length() < PATH_MAX), -1,
83 "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
84
85 std::regex dirNameRegex("[.~-]");
86 std::regex fileNameRegex("[\\/:*?\"<>|]");
87 size_t pos = path.rfind("/");
88 if (pos != std::string::npos) {
89 std::string dirName = path.substr(0, pos+1);
90 std::string fileName = path.substr(pos+1, path.length()-pos-1);
91 CHECK_TRUE(!(std::regex_search(dirName, dirNameRegex) || std::regex_search(fileName, fileNameRegex)), -1,
92 "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
93 } else {
94 CHECK_TRUE(!std::regex_search(path, fileNameRegex), -1,
95 "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
96 }
97
98 int fd = open(path.c_str(), flags, mode);
99 CHECK_TRUE(fd >= 0, -1, "open %s failed, %d", path.c_str(), errno);
100
101 int retval = write(fd, content.data(), content.size());
102 CHECK_TRUE(close(fd) != -1, -1, "close %s failed, %d", path.c_str(), errno);
103 return retval;
104 }
105
ListDir(const std::string & dirPath)106 std::vector<std::string> FileUtils::ListDir(const std::string& dirPath)
107 {
108 std::vector<std::string> result;
109 DIR* dir = opendir(dirPath.c_str());
110 if (dir == nullptr) {
111 return result;
112 }
113
114 struct dirent* ent = nullptr;
115 while ((ent = readdir(dir)) != nullptr) {
116 std::string name = ent->d_name;
117 if (name == "." || name == "..") {
118 continue;
119 }
120 result.push_back(name);
121 }
122 closedir(dir);
123 return result;
124 }
125