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