1 /*
2 * Copyright (C) 2022 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
16
17 #include "directory_ex.h"
18 #include <dirent.h>
19 #include <iostream>
20 #include <sys/stat.h>
21 #include "hilog/log.h"
22 #include "log_tags.h"
23 #include "unistd.h"
24
25 using namespace std;
26
27 namespace OHOS {
28 using namespace OHOS::HiviewDFX;
29 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "directory_ex_mock" };
30
ExtractFilePath(const string & fileFullName)31 string ExtractFilePath(const string& fileFullName)
32 {
33 return string(fileFullName).substr(0, fileFullName.rfind("/") + 1);
34 }
35
ExtractFileName(const std::string & fileFullName)36 std::string ExtractFileName(const std::string& fileFullName)
37 {
38 return string(fileFullName).substr(fileFullName.rfind("/") + 1, fileFullName.size());
39 }
40
ForceCreateDirectory(const string & path)41 bool ForceCreateDirectory(const string& path)
42 {
43 string::size_type index = 0;
44 do {
45 string subPath;
46 index = path.find('/', index + 1);
47 if (index == string::npos) {
48 subPath = path;
49 } else {
50 subPath = path.substr(0, index);
51 }
52
53 if (access(subPath.c_str(), F_OK) != 0) {
54 if (mkdir(subPath.c_str(), (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != 0 && errno != EEXIST) {
55 return false;
56 }
57 }
58 } while (index != string::npos);
59
60 return access(path.c_str(), F_OK) == 0;
61 }
62
ExtractFileExt(const string & fileName)63 string ExtractFileExt(const string& fileName)
64 {
65 string::size_type pos = fileName.rfind(".");
66 if (pos == string::npos) {
67 return "";
68 }
69 return string(fileName).substr(pos + 1, fileName.size());
70 }
71
TransformFileName(const string & fileName)72 string TransformFileName(const string& fileName)
73 {
74 string::size_type pos = fileName.find(".");
75 string transformfileName = "";
76 if (pos == string::npos) {
77 transformfileName = fileName;
78
79 #ifdef _WIN32
80 transformfileName = transformfileName.append(".dll");
81 #elif defined _APPLE
82 transformfileName = transformfileName.append(".dylib");
83 #endif
84
85 return transformfileName;
86 } else {
87 transformfileName = string(fileName).substr(0, pos + 1);
88
89 #ifdef _WIN32
90 transformfileName = transformfileName.append("dll");
91 #elif defined _APPLE
92 transformfileName = transformfileName.append("dylib");
93 #endif
94
95 return transformfileName;
96 }
97 }
98
IncludeTrailingPathDelimiter(const std::string & path)99 string IncludeTrailingPathDelimiter(const std::string& path)
100 {
101 if (path.rfind("/") != path.size() - 1) {
102 return path + "/";
103 }
104 return path;
105 }
106
GetDirFiles(const string & path,vector<string> & files)107 void GetDirFiles(const string& path, vector<string>& files)
108 {
109 struct stat info = { 0 };
110 string pathStringWithDelimiter;
111 DIR *dir = opendir(path.c_str());
112 if (dir == nullptr) {
113 return;
114 }
115
116 while (true) {
117 struct dirent *ptr = readdir(dir);
118 if (ptr == nullptr) {
119 break;
120 }
121
122 // current dir OR parrent dir
123 if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
124 continue;
125 } else if (S_ISDIR(info.st_mode)) {
126 pathStringWithDelimiter = IncludeTrailingPathDelimiter(path) + string(ptr->d_name);
127 GetDirFiles(pathStringWithDelimiter, files);
128 } else {
129 files.push_back(IncludeTrailingPathDelimiter(path) + string(ptr->d_name));
130 }
131 }
132 closedir(dir);
133 }
134
PathToRealPath(const string & path,string & realPath)135 bool PathToRealPath(const string& path, string& realPath)
136 {
137 #if !defined(_WIN32) && !defined(_APPLE)
138 if (path.empty()) {
139 HiLog::Error(LABEL, "path is empty!");
140 return false;
141 }
142 #endif
143
144 if ((path.length() >= PATH_MAX)) {
145 HiLog::Error(LABEL, "path len is error, the len is: [%{public}zu]", path.length());
146 return false;
147 }
148
149 char tmpPath[PATH_MAX] = {0};
150
151 #ifdef _WIN32
152 if (_fullpath(tmpPath, path.c_str(), PATH_MAX) == NULL) {
153 HiLog::Error(LABEL, "path to realpath error");
154 return false;
155 }
156 #else
157 if (realpath(path.c_str(), tmpPath) == nullptr) {
158 HiLog::Error(LABEL, "path to realpath error");
159 return false;
160 }
161 #endif
162
163 realPath = tmpPath;
164 if (access(realPath.c_str(), F_OK) != 0) {
165 HiLog::Error(LABEL, "check realpath (%{public}s) error", realPath.c_str());
166 return false;
167 }
168 return true;
169 }
170 } // namespace OHOS
171