xref: /aosp_15_r20/external/pdfium/testing/utils/path_service.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2015 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/utils/path_service.h"
6 
7 #include <stddef.h>
8 
9 #ifdef _WIN32
10 #include <Windows.h>
11 #elif defined(__APPLE__)
12 #include <mach-o/dyld.h>
13 #include <sys/stat.h>
14 #elif defined(__Fuchsia__)
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #else  // Linux
18 #include <linux/limits.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #endif  // _WIN32
22 
23 #include <string>
24 
25 #include "core/fxcrt/fx_system.h"
26 #include "third_party/base/check.h"
27 
28 namespace {
29 
30 #if defined(__APPLE__) || defined(__Fuchsia__) || \
31     (defined(ANDROID) && __ANDROID_API__ < 21)
32 using stat_wrapper_t = struct stat;
33 
CallStat(const char * path,stat_wrapper_t * sb)34 int CallStat(const char* path, stat_wrapper_t* sb) {
35   return stat(path, sb);
36 }
37 #elif !_WIN32
38 using stat_wrapper_t = struct stat64;
39 
40 int CallStat(const char* path, stat_wrapper_t* sb) {
41   return stat64(path, sb);
42 }
43 #endif
44 
45 }  // namespace
46 
47 // static
DirectoryExists(const std::string & path)48 bool PathService::DirectoryExists(const std::string& path) {
49 #ifdef _WIN32
50   DWORD fileattr = GetFileAttributesA(path.c_str());
51   if (fileattr != INVALID_FILE_ATTRIBUTES)
52     return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0;
53   return false;
54 #else
55   stat_wrapper_t file_info;
56   if (CallStat(path.c_str(), &file_info) != 0)
57     return false;
58   return S_ISDIR(file_info.st_mode);
59 #endif
60 }
61 
62 // static
EndsWithSeparator(const std::string & path)63 bool PathService::EndsWithSeparator(const std::string& path) {
64   return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
65 }
66 
67 // static
GetExecutableDir(std::string * path)68 bool PathService::GetExecutableDir(std::string* path) {
69 // Get the current executable file path.
70 #ifdef _WIN32
71   char path_buffer[MAX_PATH];
72   path_buffer[0] = 0;
73 
74   if (GetModuleFileNameA(NULL, path_buffer, MAX_PATH) == 0)
75     return false;
76   *path = std::string(path_buffer);
77 #elif defined(__APPLE__)
78   DCHECK(path);
79   unsigned int path_length = 0;
80   _NSGetExecutablePath(NULL, &path_length);
81   if (path_length == 0)
82     return false;
83 
84   path->reserve(path_length);
85   path->resize(path_length - 1);
86   if (_NSGetExecutablePath(&((*path)[0]), &path_length))
87     return false;
88 #else   // Linux
89   static const char kProcSelfExe[] = "/proc/self/exe";
90   char buf[PATH_MAX];
91   ssize_t count = ::readlink(kProcSelfExe, buf, PATH_MAX);
92   if (count <= 0)
93     return false;
94 
95   *path = std::string(buf, count);
96 #endif  // _WIN32
97 
98   // Get the directory path.
99   size_t pos = path->size() - 1;
100   if (EndsWithSeparator(*path))
101     pos--;
102   size_t found = path->find_last_of(PATH_SEPARATOR, pos);
103   if (found == std::string::npos)
104     return false;
105   path->resize(found);
106   return true;
107 }
108 
109 // static
GetSourceDir(std::string * path)110 bool PathService::GetSourceDir(std::string* path) {
111   if (!GetExecutableDir(path))
112     return false;
113 
114   if (!EndsWithSeparator(*path))
115     path->push_back(PATH_SEPARATOR);
116   path->append("..");
117   path->push_back(PATH_SEPARATOR);
118 #if defined(ANDROID)
119   path->append("chromium_tests_root");
120 #else   // Non-Android
121   path->append("..");
122 #endif  // defined(ANDROID)
123   return true;
124 }
125 
126 // static
GetTestDataDir(std::string * path)127 bool PathService::GetTestDataDir(std::string* path) {
128   if (!GetSourceDir(path))
129     return false;
130 
131   if (!EndsWithSeparator(*path))
132     path->push_back(PATH_SEPARATOR);
133 
134   std::string potential_path = *path;
135   potential_path.append("testing");
136   potential_path.push_back(PATH_SEPARATOR);
137   potential_path.append("resources");
138   if (PathService::DirectoryExists(potential_path)) {
139     *path = potential_path;
140     return true;
141   }
142 
143   potential_path = *path;
144   potential_path.append("third_party");
145   potential_path.push_back(PATH_SEPARATOR);
146   potential_path.append("pdfium");
147   potential_path.push_back(PATH_SEPARATOR);
148   potential_path.append("testing");
149   potential_path.push_back(PATH_SEPARATOR);
150   potential_path.append("resources");
151   if (PathService::DirectoryExists(potential_path)) {
152     *path = potential_path;
153     return true;
154   }
155 
156   return false;
157 }
158 
159 // static
GetTestFilePath(const std::string & file_name,std::string * path)160 bool PathService::GetTestFilePath(const std::string& file_name,
161                                   std::string* path) {
162   if (!GetTestDataDir(path))
163     return false;
164 
165   if (!EndsWithSeparator(*path))
166     path->push_back(PATH_SEPARATOR);
167   path->append(file_name);
168   return true;
169 }
170 
171 // static
GetThirdPartyFilePath(const std::string & file_name,std::string * path)172 bool PathService::GetThirdPartyFilePath(const std::string& file_name,
173                                         std::string* path) {
174   if (!GetSourceDir(path))
175     return false;
176 
177   if (!EndsWithSeparator(*path))
178     path->push_back(PATH_SEPARATOR);
179 
180   std::string potential_path = *path;
181   potential_path.append("third_party");
182 
183   // Use third_party/bigint as a way to distinguish PDFium's vs. other's.
184   std::string bigint = potential_path + PATH_SEPARATOR + "bigint";
185   if (PathService::DirectoryExists(bigint)) {
186     *path = potential_path;
187     path->append(PATH_SEPARATOR + file_name);
188     return true;
189   }
190 
191   potential_path = *path;
192   potential_path.append("third_party");
193   potential_path.push_back(PATH_SEPARATOR);
194   potential_path.append("pdfium");
195   potential_path.push_back(PATH_SEPARATOR);
196   potential_path.append("third_party");
197   bigint = potential_path + PATH_SEPARATOR + "bigint";
198   if (PathService::DirectoryExists(potential_path)) {
199     *path = potential_path;
200     path->append(PATH_SEPARATOR + file_name);
201     return true;
202   }
203 
204   return false;
205 }
206