• 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 
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 
ExtractFileExt(const string & fileName)31 string ExtractFileExt(const string& fileName)
32 {
33     string::size_type pos = fileName.rfind(".");
34     if (pos == string::npos) {
35         return "";
36     }
37     return string(fileName).substr(pos + 1, fileName.size());
38 }
39 
TransformFileName(const string & fileName)40 string TransformFileName(const string& fileName)
41 {
42     string::size_type pos = fileName.find(".");
43     if (pos == string::npos) {
44         string transformfileName = fileName;
45 
46 #ifdef _WIN32
47         transformfileName = transformfileName.append(".dll");
48 #elif defined _APPLE
49         transformfileName = transformfileName.append(".dylib");
50 #endif
51 
52         return transformfileName;
53     } else {
54         string transformfileName = string(fileName).substr(0, pos + 1);
55 
56 #ifdef _WIN32
57         transformfileName = transformfileName.append("dll");
58 #elif defined _APPLE
59         transformfileName = transformfileName.append("dylib");
60 #endif
61 
62         return transformfileName;
63     }
64 }
65 
IncludeTrailingPathDelimiter(const std::string & path)66 string IncludeTrailingPathDelimiter(const std::string& path)
67 {
68     if (path.rfind("/") != path.size() - 1) {
69         return path + "/";
70     }
71     return path;
72 }
73 
GetDirFiles(const string & path,vector<string> & files)74 void GetDirFiles(const string& path, vector<string>& files)
75 {
76     struct stat info = { 0 };
77     string pathStringWithDelimiter;
78     DIR *dir = opendir(path.c_str());
79     if (dir == nullptr) {
80         return;
81     }
82 
83     while (true) {
84         struct dirent *ptr = readdir(dir);
85         if (ptr == nullptr) {
86             break;
87         }
88 
89         // current dir OR parrent dir
90         if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
91             continue;
92         } else if (S_ISDIR(info.st_mode)) {
93             pathStringWithDelimiter = IncludeTrailingPathDelimiter(path) + string(ptr->d_name);
94             GetDirFiles(pathStringWithDelimiter, files);
95         } else {
96             files.push_back(IncludeTrailingPathDelimiter(path) + string(ptr->d_name));
97         }
98     }
99     closedir(dir);
100 }
101 
PathToRealPath(const string & path,string & realPath)102 bool PathToRealPath(const string& path, string& realPath)
103 {
104 #if !defined(_WIN32) && !defined(_APPLE)
105     if (path.empty()) {
106         HiLog::Error(LABEL, "path is empty!");
107         return false;
108     }
109 #endif
110 
111     if ((path.length() >= PATH_MAX)) {
112         HiLog::Error(LABEL, "path len is error, the len is: [%{public}zu]", path.length());
113         return false;
114     }
115 
116     char tmpPath[PATH_MAX] = {0};
117 
118 #ifdef _WIN32
119     if (_fullpath(tmpPath, path.c_str(), PATH_MAX) == NULL) {
120         HiLog::Error(LABEL, "path to realpath error");
121         return false;
122     }
123 #else
124     if (realpath(path.c_str(), tmpPath) == nullptr) {
125         HiLog::Error(LABEL, "path to realpath error");
126         return false;
127     }
128 #endif
129 
130     realPath = tmpPath;
131     if (access(realPath.c_str(), F_OK) != 0) {
132         HiLog::Error(LABEL, "check realpath (%{public}s) error", realPath.c_str());
133         return false;
134     }
135     return true;
136 }
137 } // namespace OHOS
138