kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 7a281a5 | 2012-06-27 03:22:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/testsupport/fileutils.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 12 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 13 | #include <assert.h> |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 14 | |
| 15 | #ifdef WIN32 |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 16 | #include <direct.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 17 | #include <tchar.h> |
| 18 | #include <windows.h> |
kjellander@webrtc.org | de49966 | 2013-08-29 11:26:41 +0000 | [diff] [blame] | 19 | #include <algorithm> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 20 | |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 21 | #include "Shlwapi.h" |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame] | 22 | #include "WinDef.h" |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 23 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/win32.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 25 | #define GET_CURRENT_DIR _getcwd |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 26 | #else |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 29 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 30 | #define GET_CURRENT_DIR getcwd |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 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) |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 40 | #include <string.h> |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 41 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 42 | #include <memory> |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 43 | #include <utility> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 44 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 45 | #include "rtc_base/checks.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 46 | #include "typedefs.h" // NOLINT(build/include) // For architecture defines |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 47 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 48 | namespace webrtc { |
| 49 | namespace test { |
| 50 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 51 | #if defined(WEBRTC_IOS) |
| 52 | // Defined in iosfileutils.mm. No header file to discourage use elsewhere. |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 53 | std::string IOSOutputPath(); |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 54 | std::string IOSRootPath(); |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 55 | std::string IOSResourcePath(std::string name, std::string extension); |
| 56 | #endif |
| 57 | |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 60 | #ifdef WIN32 |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 61 | const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 62 | #else |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 63 | const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 64 | #endif |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 65 | |
| 66 | #ifdef WEBRTC_ANDROID |
kjellander | 6820889 | 2016-06-16 23:29:30 -0700 | [diff] [blame] | 67 | const char* kRootDirName = "/sdcard/chromium_tests_root/"; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 68 | #else |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 69 | #if !defined(WEBRTC_IOS) |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 70 | const char* kOutputDirName = "out"; |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 71 | #endif |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 72 | const char* kFallbackPath = "./"; |
| 73 | #endif // !defined(WEBRTC_ANDROID) |
| 74 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 75 | #if !defined(WEBRTC_IOS) |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 76 | const char* kResourcesDirName = "resources"; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 77 | #endif |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 78 | |
pbos@webrtc.org | db7d82f | 2013-07-05 08:49:09 +0000 | [diff] [blame] | 79 | char relative_dir_path[FILENAME_MAX]; |
| 80 | bool relative_dir_path_set = false; |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; |
| 85 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 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 | } |
kjellander@webrtc.org | de49966 | 2013-08-29 11:26:41 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 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)); |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 103 | strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX); |
| 104 | relative_dir_path_set = true; |
| 105 | } |
| 106 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 107 | bool FileExists(const std::string& file_name) { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 108 | struct stat file_info = {0}; |
| 109 | return stat(file_name.c_str(), &file_info) == 0; |
| 110 | } |
| 111 | |
alessiob | e49fede | 2017-03-15 06:04:59 -0700 | [diff] [blame] | 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 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 118 | #ifdef WEBRTC_ANDROID |
| 119 | |
| 120 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 121 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | std::string OutputPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 125 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::string WorkingDir() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 129 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 130 | } |
| 131 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 132 | #else // WEBRTC_ANDROID |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 133 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 134 | std::string ProjectRootPath() { |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 135 | #if defined(WEBRTC_IOS) |
| 136 | return IOSRootPath(); |
| 137 | #else |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 138 | std::string path = WorkingDir(); |
| 139 | if (path == kFallbackPath) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 140 | return kCannotFindProjectRootDir; |
| 141 | } |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 142 | if (relative_dir_path_set) { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 143 | path = path + kPathDelimiter + relative_dir_path; |
| 144 | } |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 145 | path = path + kPathDelimiter + ".." + kPathDelimiter + ".."; |
| 146 | char canonical_path[FILENAME_MAX]; |
| 147 | #ifdef WIN32 |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame] | 148 | BOOL succeeded = PathCanonicalizeA(canonical_path, path.c_str()); |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 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; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 158 | } |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 159 | #endif |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 160 | } |
| 161 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 162 | std::string OutputPath() { |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 163 | #if defined(WEBRTC_IOS) |
| 164 | return IOSOutputPath(); |
| 165 | #else |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 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; |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 175 | #endif |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 176 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 177 | |
| 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; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 183 | } else { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 184 | return std::string(path_buffer); |
| 185 | } |
| 186 | } |
| 187 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 188 | #endif // !WEBRTC_ANDROID |
| 189 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 190 | // Generate a temporary filename in a safe way. |
| 191 | // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc. |
| 192 | std::string TempFilename(const std::string &dir, const std::string &prefix) { |
| 193 | #ifdef WIN32 |
| 194 | wchar_t filename[MAX_PATH]; |
nisse | 07b8388 | 2017-03-14 01:32:50 -0700 | [diff] [blame] | 195 | if (::GetTempFileName(rtc::ToUtf16(dir).c_str(), |
| 196 | rtc::ToUtf16(prefix).c_str(), 0, filename) != 0) |
| 197 | return rtc::ToUtf8(filename); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 198 | assert(false); |
| 199 | return ""; |
| 200 | #else |
| 201 | int len = dir.size() + prefix.size() + 2 + 6; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 202 | std::unique_ptr<char[]> tempname(new char[len]); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 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 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 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 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 269 | bool CreateDir(const std::string& directory_name) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 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 | |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 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 | |
| 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 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 305 | std::string ResourcePath(const std::string& name, |
| 306 | const std::string& extension) { |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 307 | #if defined(WEBRTC_IOS) |
| 308 | return IOSResourcePath(name, extension); |
| 309 | #else |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 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 |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 317 | #ifdef WEBRTC_ANDROID |
| 318 | platform = "android"; |
| 319 | #endif // WEBRTC_ANDROID |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 327 | std::string resources_path = ProjectRootPath() + kResourcesDirName + |
| 328 | kPathDelimiter; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 329 | std::string resource_file = resources_path + name + "_" + platform + "_" + |
| 330 | architecture + "." + extension; |
kjellander@webrtc.org | 80b2661 | 2011-12-07 18:50:17 +0000 | [diff] [blame] | 331 | if (FileExists(resource_file)) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 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 | } |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 344 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 345 | // Fall back on name without architecture or platform. |
| 346 | return resources_path + name + "." + extension; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 347 | #endif // defined (WEBRTC_IOS) |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 348 | } |
| 349 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 350 | size_t GetFileSize(const std::string& filename) { |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 362 | } // namespace test |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 363 | } // namespace webrtc |