1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "test/testsupport/fileutils.h"
12
13 #include <assert.h>
14
15 #ifdef WIN32
16 #include <direct.h>
17 #include <tchar.h>
18 #include <windows.h>
19 #include <algorithm>
20
21 #include "Shlwapi.h"
22 #include "WinDef.h"
23
24 #include "rtc_base/win32.h"
25 #define GET_CURRENT_DIR _getcwd
26 #else
27 #include <dirent.h>
28 #include <unistd.h>
29
30 #define GET_CURRENT_DIR getcwd
31 #endif
32
33 #include <sys/stat.h> // To check for directory existence.
34 #ifndef S_ISDIR // Not defined in stat.h on Windows.
35 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <memory>
43 #include <utility>
44
45 #include "rtc_base/checks.h"
46 #include "typedefs.h" // NOLINT(build/include) // For architecture defines
47
48 namespace webrtc {
49 namespace test {
50
51 #if defined(WEBRTC_IOS)
52 // Defined in iosfileutils.mm. No header file to discourage use elsewhere.
53 std::string IOSOutputPath();
54 std::string IOSRootPath();
55 std::string IOSResourcePath(std::string name, std::string extension);
56 #endif
57
58 namespace {
59
60 #ifdef WIN32
61 const char* kPathDelimiter = "\\";
62 #else
63 const char* kPathDelimiter = "/";
64 #endif
65
66 #ifdef WEBRTC_ANDROID
67 const char* kRootDirName = "/sdcard/chromium_tests_root/";
68 #else
69 #if !defined(WEBRTC_IOS)
70 const char* kOutputDirName = "out";
71 #endif
72 const char* kFallbackPath = "./";
73 #endif // !defined(WEBRTC_ANDROID)
74
75 #if !defined(WEBRTC_IOS)
76 const char* kResourcesDirName = "resources";
77 #endif
78
79 char relative_dir_path[FILENAME_MAX];
80 bool relative_dir_path_set = false;
81
82 } // namespace
83
84 const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
85
SetExecutablePath(const std::string & path)86 void SetExecutablePath(const std::string& path) {
87 std::string working_dir = WorkingDir();
88 std::string temp_path = path;
89
90 // Handle absolute paths; convert them to relative paths to the working dir.
91 if (path.find(working_dir) != std::string::npos) {
92 temp_path = path.substr(working_dir.length() + 1);
93 }
94 // On Windows, when tests are run under memory tools like DrMemory and TSan,
95 // slashes occur in the path as directory separators. Make sure we replace
96 // such cases with backslashes in order for the paths to be correct.
97 #ifdef WIN32
98 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
99 #endif
100
101 // Trim away the executable name; only store the relative dir path.
102 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
103 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
104 relative_dir_path_set = true;
105 }
106
FileExists(const std::string & file_name)107 bool FileExists(const std::string& file_name) {
108 struct stat file_info = {0};
109 return stat(file_name.c_str(), &file_info) == 0;
110 }
111
DirExists(const std::string & directory_name)112 bool DirExists(const std::string& directory_name) {
113 struct stat directory_info = {0};
114 return stat(directory_name.c_str(), &directory_info) == 0 && S_ISDIR(
115 directory_info.st_mode);
116 }
117
118 #ifdef WEBRTC_ANDROID
119
ProjectRootPath()120 std::string ProjectRootPath() {
121 return kRootDirName;
122 }
123
OutputPath()124 std::string OutputPath() {
125 return kRootDirName;
126 }
127
WorkingDir()128 std::string WorkingDir() {
129 return kRootDirName;
130 }
131
132 #else // WEBRTC_ANDROID
133
ProjectRootPath()134 std::string ProjectRootPath() {
135 #if defined(WEBRTC_IOS)
136 return IOSRootPath();
137 #else
138 std::string path = WorkingDir();
139 if (path == kFallbackPath) {
140 return kCannotFindProjectRootDir;
141 }
142 if (relative_dir_path_set) {
143 path = path + kPathDelimiter + relative_dir_path;
144 }
145 path = path + kPathDelimiter + ".." + kPathDelimiter + "..";
146 char canonical_path[FILENAME_MAX];
147 #ifdef WIN32
148 BOOL succeeded = PathCanonicalizeA(canonical_path, path.c_str());
149 #else
150 bool succeeded = realpath(path.c_str(), canonical_path) != NULL;
151 #endif
152 if (succeeded) {
153 path = std::string(canonical_path) + kPathDelimiter;
154 return path;
155 } else {
156 fprintf(stderr, "Cannot find project root directory!\n");
157 return kCannotFindProjectRootDir;
158 }
159 #endif
160 }
161
OutputPath()162 std::string OutputPath() {
163 #if defined(WEBRTC_IOS)
164 return IOSOutputPath();
165 #else
166 std::string path = ProjectRootPath();
167 if (path == kCannotFindProjectRootDir) {
168 return kFallbackPath;
169 }
170 path += kOutputDirName;
171 if (!CreateDir(path)) {
172 return kFallbackPath;
173 }
174 return path + kPathDelimiter;
175 #endif
176 }
177
WorkingDir()178 std::string WorkingDir() {
179 char path_buffer[FILENAME_MAX];
180 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
181 fprintf(stderr, "Cannot get current directory!\n");
182 return kFallbackPath;
183 } else {
184 return std::string(path_buffer);
185 }
186 }
187
188 #endif // !WEBRTC_ANDROID
189
190 // Generate a temporary filename in a safe way.
191 // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
TempFilename(const std::string & dir,const std::string & prefix)192 std::string TempFilename(const std::string &dir, const std::string &prefix) {
193 #ifdef WIN32
194 wchar_t filename[MAX_PATH];
195 if (::GetTempFileName(rtc::ToUtf16(dir).c_str(),
196 rtc::ToUtf16(prefix).c_str(), 0, filename) != 0)
197 return rtc::ToUtf8(filename);
198 assert(false);
199 return "";
200 #else
201 int len = dir.size() + prefix.size() + 2 + 6;
202 std::unique_ptr<char[]> tempname(new char[len]);
203
204 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
205 prefix.c_str());
206 int fd = ::mkstemp(tempname.get());
207 if (fd == -1) {
208 assert(false);
209 return "";
210 } else {
211 ::close(fd);
212 }
213 std::string ret(tempname.get());
214 return ret;
215 #endif
216 }
217
ReadDirectory(std::string path)218 rtc::Optional<std::vector<std::string>> ReadDirectory(std::string path) {
219 if (path.length() == 0)
220 return rtc::Optional<std::vector<std::string>>();
221
222 #if defined(WEBRTC_WIN)
223 // Append separator character if needed.
224 if (path.back() != '\\')
225 path += '\\';
226
227 // Init.
228 WIN32_FIND_DATA data;
229 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
230 if (handle == INVALID_HANDLE_VALUE)
231 return rtc::Optional<std::vector<std::string>>();
232
233 // Populate output.
234 std::vector<std::string> found_entries;
235 do {
236 const std::string name = rtc::ToUtf8(data.cFileName);
237 if (name != "." && name != "..")
238 found_entries.emplace_back(path + name);
239 } while (::FindNextFile(handle, &data) == TRUE);
240
241 // Release resources.
242 if (handle != INVALID_HANDLE_VALUE)
243 ::FindClose(handle);
244 #else
245 // Append separator character if needed.
246 if (path.back() != '/')
247 path += '/';
248
249 // Init.
250 DIR* dir = ::opendir(path.c_str());
251 if (dir == nullptr)
252 return rtc::Optional<std::vector<std::string>>();
253
254 // Populate output.
255 std::vector<std::string> found_entries;
256 while (dirent* dirent = readdir(dir)) {
257 const std::string& name = dirent->d_name;
258 if (name != "." && name != "..")
259 found_entries.emplace_back(path + name);
260 }
261
262 // Release resources.
263 closedir(dir);
264 #endif
265
266 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
267 }
268
CreateDir(const std::string & directory_name)269 bool CreateDir(const std::string& directory_name) {
270 struct stat path_info = {0};
271 // Check if the path exists already:
272 if (stat(directory_name.c_str(), &path_info) == 0) {
273 if (!S_ISDIR(path_info.st_mode)) {
274 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
275 "file and re-run to create the directory.\n",
276 directory_name.c_str());
277 return false;
278 }
279 } else {
280 #ifdef WIN32
281 return _mkdir(directory_name.c_str()) == 0;
282 #else
283 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
284 #endif
285 }
286 return true;
287 }
288
RemoveDir(const std::string & directory_name)289 bool RemoveDir(const std::string& directory_name) {
290 #ifdef WIN32
291 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
292 #else
293 return rmdir(directory_name.c_str()) == 0;
294 #endif
295 }
296
RemoveFile(const std::string & file_name)297 bool RemoveFile(const std::string& file_name) {
298 #ifdef WIN32
299 return DeleteFileA(file_name.c_str()) != FALSE;
300 #else
301 return unlink(file_name.c_str()) == 0;
302 #endif
303 }
304
ResourcePath(const std::string & name,const std::string & extension)305 std::string ResourcePath(const std::string& name,
306 const std::string& extension) {
307 #if defined(WEBRTC_IOS)
308 return IOSResourcePath(name, extension);
309 #else
310 std::string platform = "win";
311 #ifdef WEBRTC_LINUX
312 platform = "linux";
313 #endif // WEBRTC_LINUX
314 #ifdef WEBRTC_MAC
315 platform = "mac";
316 #endif // WEBRTC_MAC
317 #ifdef WEBRTC_ANDROID
318 platform = "android";
319 #endif // WEBRTC_ANDROID
320
321 #ifdef WEBRTC_ARCH_64_BITS
322 std::string architecture = "64";
323 #else
324 std::string architecture = "32";
325 #endif // WEBRTC_ARCH_64_BITS
326
327 std::string resources_path = ProjectRootPath() + kResourcesDirName +
328 kPathDelimiter;
329 std::string resource_file = resources_path + name + "_" + platform + "_" +
330 architecture + "." + extension;
331 if (FileExists(resource_file)) {
332 return resource_file;
333 }
334 // Try without architecture.
335 resource_file = resources_path + name + "_" + platform + "." + extension;
336 if (FileExists(resource_file)) {
337 return resource_file;
338 }
339 // Try without platform.
340 resource_file = resources_path + name + "_" + architecture + "." + extension;
341 if (FileExists(resource_file)) {
342 return resource_file;
343 }
344
345 // Fall back on name without architecture or platform.
346 return resources_path + name + "." + extension;
347 #endif // defined (WEBRTC_IOS)
348 }
349
GetFileSize(const std::string & filename)350 size_t GetFileSize(const std::string& filename) {
351 FILE* f = fopen(filename.c_str(), "rb");
352 size_t size = 0;
353 if (f != NULL) {
354 if (fseek(f, 0, SEEK_END) == 0) {
355 size = ftell(f);
356 }
357 fclose(f);
358 }
359 return size;
360 }
361
362 } // namespace test
363 } // namespace webrtc
364