Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
path_service.cpp
Go to the documentation of this file.
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
28namespace {
29
30#if defined(__APPLE__) || defined(__Fuchsia__) ||
31 (defined(ANDROID) && __ANDROID_API__ < 21)
32using stat_wrapper_t = struct stat;
33
34int CallStat(const char* path, stat_wrapper_t* sb) {
35 return stat(path, sb);
36}
37#elif !_WIN32
38using stat_wrapper_t = struct stat64;
39
40int CallStat(const char* path, stat_wrapper_t* sb) {
41 return stat64(path, sb);
42}
43#endif
44
45} // namespace
46
47// static
48bool 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
63bool PathService::EndsWithSeparator(const std::string& path) {
64 return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
65}
66
67// static
68bool 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
110bool 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
127bool 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
160std::string PathService::GetTestFilePath(const std::string& file_name) {
161 std::string path;
162 if (!GetTestDataDir(&path)) {
163 return std::string();
164 }
165
166 if (!EndsWithSeparator(path)) {
167 path.push_back(PATH_SEPARATOR);
168 }
169 path.append(file_name);
170 return path;
171}
172
173// static
174bool PathService::GetThirdPartyFilePath(const std::string& file_name,
175 std::string* path) {
176 if (!GetSourceDir(path))
177 return false;
178
179 if (!EndsWithSeparator(*path))
180 path->push_back(PATH_SEPARATOR);
181
182 std::string potential_path = *path;
183 potential_path.append("third_party");
184
185 // Use third_party/bigint as a way to distinguish PDFium's vs. other's.
186 std::string bigint = potential_path + PATH_SEPARATOR + "bigint";
187 if (PathService::DirectoryExists(bigint)) {
188 *path = potential_path;
189 path->append(PATH_SEPARATOR + file_name);
190 return true;
191 }
192
193 potential_path = *path;
194 potential_path.append("third_party");
195 potential_path.push_back(PATH_SEPARATOR);
196 potential_path.append("pdfium");
197 potential_path.push_back(PATH_SEPARATOR);
198 potential_path.append("third_party");
199 bigint = potential_path + PATH_SEPARATOR + "bigint";
200 if (PathService::DirectoryExists(potential_path)) {
201 *path = potential_path;
202 path->append(PATH_SEPARATOR + file_name);
203 return true;
204 }
205
206 return false;
207}
#define PATH_SEPARATOR
#define PATH_MAX