• 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 "logging.h"
25 
26 namespace {
27 constexpr size_t DEFAULT_READ_SIZE = 4096;
28 }
29 
ReadFile(int fd)30 std::string FileUtils::ReadFile(int fd)
31 {
32     std::string content;
33     size_t count = 0;
34     while (true) {
35         if (content.size() - count < DEFAULT_READ_SIZE) {
36             content.resize(content.size() + DEFAULT_READ_SIZE);
37         }
38         ssize_t nBytes = TEMP_FAILURE_RETRY(read(fd, &content[count], content.size() - count));
39         if (nBytes <= 0) {
40             break;
41         }
42         count += nBytes;
43     }
44     content.resize(count);
45     return content;
46 }
47 
ReadFile(const std::string & path)48 std::string FileUtils::ReadFile(const std::string& path)
49 {
50     char realPath[PATH_MAX + 1] = {0};
51 
52     if ((path.length() > PATH_MAX) || (realpath(path.c_str(), realPath) == nullptr)) {
53         return "";
54     }
55     int fd = open(realPath, O_RDONLY);
56     if (fd < 0) {
57         const int bufSize = 1024;
58         char buf[bufSize] = { 0 };
59         strerror_r(errno, buf, bufSize);
60         HILOG_WARN(LOG_CORE, "open file %s FAILED: %s!", path.c_str(), buf);
61         return "";
62     }
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) || (path.find("..") != std::string::npos)) {
82         HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
83         return -1;
84     }
85     int fd = open(path.c_str(), flags, mode);
86     CHECK_TRUE(fd >= 0, -1, "open %s failed, %d", path.c_str(), errno);
87 
88     int retval = write(fd, content.data(), content.size());
89     CHECK_TRUE(close(fd) != -1, -1, "close %s failed, %d", path.c_str(), errno);
90     HILOG_DEBUG(LOG_CORE, "write(%s) with '%s' done!", path.c_str(), content.c_str());
91     return retval;
92 }
93 
ListDir(const std::string & dirPath)94 std::vector<std::string> FileUtils::ListDir(const std::string& dirPath)
95 {
96     std::vector<std::string> result;
97     DIR* dir = opendir(dirPath.c_str());
98     if (dir == nullptr) {
99         return result;
100     }
101 
102     struct dirent* ent = nullptr;
103     while ((ent = readdir(dir)) != nullptr) {
104         std::string name = ent->d_name;
105         if (name == "." || name == "..") {
106             continue;
107         }
108         result.push_back(name);
109     }
110     closedir(dir);
111     return result;
112 }
113