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
27namespace {
28constexpr size_t DEFAULT_READ_SIZE = 4096;
29}
30
31std::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
52std::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
70int FileUtils::WriteFile(const std::string& path, const std::string& content)
71{
72    return WriteFile(path, content, O_WRONLY);
73}
74
75int FileUtils::WriteFile(const std::string& path, const std::string& content, int flags)
76{
77    return WriteFile(path, content, flags, 0);
78}
79
80int 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        size_t index = path.length() > (pos + 1) ? path.length() - pos - 1 : 0;
91        std::string fileName = path.substr(pos+1, index);
92        CHECK_TRUE(!(std::regex_search(dirName, dirNameRegex) || std::regex_search(fileName, fileNameRegex)), -1,
93                   "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
94    } else {
95        CHECK_TRUE(!std::regex_search(path, fileNameRegex), -1,
96                   "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
97    }
98
99    int fd = open(path.c_str(), flags, mode);
100    CHECK_TRUE(fd >= 0, -1, "open %s failed, %d", path.c_str(), errno);
101
102    int retval = write(fd, content.data(), content.size());
103    CHECK_TRUE(close(fd) != -1, -1, "close %s failed, %d", path.c_str(), errno);
104    return retval;
105}
106
107std::vector<std::string> FileUtils::ListDir(const std::string& dirPath)
108{
109    std::vector<std::string> result;
110    DIR* dir = opendir(dirPath.c_str());
111    if (dir == nullptr) {
112        return result;
113    }
114
115    struct dirent* ent = nullptr;
116    while ((ent = readdir(dir)) != nullptr) {
117        std::string name = ent->d_name;
118        if (name == "." || name == "..") {
119            continue;
120        }
121        result.push_back(name);
122    }
123    closedir(dir);
124    return result;
125}
126