• 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 #include <chrono>
16 #include <filesystem>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include "directory_ex.h"
20 #include "hilog_wrapper.h"
21 #include "util/file_utils.h"
22 namespace fs = std::filesystem;
23 using namespace std;
24 namespace OHOS {
25 namespace HiviewDFX {
26 static const std::string UNKNOWN = "unknown";
27 constexpr int VALUES_MIN_LEN = 1;
FileUtils()28 FileUtils::FileUtils()
29 {
30 }
~FileUtils()31 FileUtils::~FileUtils()
32 {
33 }
34 
CreateFolder(const string & path)35 bool FileUtils::CreateFolder(const string &path)
36 {
37     if (!access(path.c_str(), F_OK) || path == "") {
38         return true;
39     }
40 
41     size_t pos = path.rfind("/");
42     if (pos == string::npos) {
43         return false;
44     }
45 
46     string upperPath = path.substr(0, pos);
47     if (CreateFolder(upperPath)) {
48         if (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO)) {
49             if (errno != EEXIST) {
50                 return false;
51             }
52         }
53         return true;
54     }
55     return false;
56 }
57 
LoadStringFromProcCb(const std::string & path,bool oneLine,bool endWithoutN,const DataHandler & func)58 bool FileUtils::LoadStringFromProcCb(const std::string& path, bool oneLine, bool endWithoutN, const DataHandler& func)
59 {
60     char canonicalPath[PATH_MAX] = {0};
61     if (realpath(path.c_str(), canonicalPath) == nullptr) {
62         DUMPER_HILOGE(MODULE_COMMON, "realpath failed, errno=%{public}d, path=%{public}s", errno, path.c_str());
63         return false;
64     }
65     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(canonicalPath, "re"), fclose};
66     if (fp == nullptr) {
67         DUMPER_HILOGE(MODULE_COMMON, "fopen failed, errno=%{public}d, path=%{public}s", errno, path.c_str());
68         return false;
69     }
70     char *lineBuf = nullptr;
71     ssize_t lineLen;
72     size_t lineAlloc = 0;
73     while ((lineLen = getline(&lineBuf, &lineAlloc, fp.get())) > 0) {
74         lineBuf[lineLen] = '\0';
75         if (endWithoutN && lineBuf[lineLen-1] == '\n') {
76             lineBuf[lineLen-1] = '\0';
77         }
78         const string content = lineBuf;
79         func(content);
80         if (oneLine) {
81             break;
82         }
83     }
84     if (lineBuf != nullptr) {
85         free(lineBuf);
86         lineBuf = nullptr;
87     }
88     return true;
89 }
90 
GetProcValue(const int32_t & pid,const string & path,const string & key)91 string FileUtils::GetProcValue(const int32_t &pid, const string& path, const string& key)
92 {
93     if (!DumpUtils::PathIsValid(path)) {
94         DUMPER_HILOGE(MODULE_COMMON, "path is valid");
95         return UNKNOWN;
96     }
97     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "rd"), fclose};
98     if (fp == nullptr) {
99         DUMPER_HILOGE(MODULE_COMMON, "fopen failed");
100         return UNKNOWN;
101     }
102     char *lineBuf = nullptr;
103     ssize_t lineLen;
104     size_t lineAlloc = 0;
105     string content;
106     while ((lineLen = getline(&lineBuf, &lineAlloc, fp.get())) > 0) {
107         lineBuf[lineLen] = '\0';
108         if (lineBuf[lineLen - 1] == '\n') {
109             lineBuf[lineLen - 1] = '\0';
110         }
111         content = lineBuf;
112         if (content.find(key) != std::string::npos) {
113             break;
114         }
115         content = "";
116     }
117     if (lineBuf != nullptr) {
118         free(lineBuf);
119         lineBuf = nullptr;
120     }
121     if (!content.empty()) {
122         vector<string> values;
123         StringUtils::GetInstance().StringSplit(content, ":", values);
124         if (values.size() <= VALUES_MIN_LEN || values[VALUES_MIN_LEN].size() <= VALUES_MIN_LEN) {
125             DUMPER_HILOGE(MODULE_SERVICE, "values is invalid");
126             return UNKNOWN;
127         } else {
128             return values[VALUES_MIN_LEN].substr(VALUES_MIN_LEN);
129         }
130     } else {
131         DUMPER_HILOGE(MODULE_SERVICE, "content is empty");
132         return UNKNOWN;
133     }
134 }
135 
GetLastWriteTime(const std::string & path,time_t & lastWriteTime)136 bool FileUtils::GetLastWriteTime(const std::string &path, time_t& lastWriteTime)
137 {
138     std::string realPath;
139     if (!OHOS::PathToRealPath(path, realPath)) {
140         DUMPER_HILOGE(MODULE_COMMON, "path is invalid");
141         return false;
142     }
143     fs::path filePath(realPath);
144     auto fsLastWriteTime = fs::last_write_time(filePath);
145     auto convertStandardTime = std::chrono::time_point_cast<std::chrono::system_clock::duration>(fsLastWriteTime -
146         fs::file_time_type::clock::now() + std::chrono::system_clock::now());
147     lastWriteTime = std::chrono::system_clock::to_time_t(convertStandardTime);
148     return true;
149 }
150 } // namespace HiviewDFX
151 } // namespace OHOS