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