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" |
Niels Möller | d7b9131 | 2018-05-24 11:21:34 +0200 | [diff] [blame] | 46 | #include "rtc_base/stringutils.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 47 | #include "typedefs.h" // NOLINT(build/include) // For architecture defines |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 48 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 49 | namespace webrtc { |
| 50 | namespace test { |
| 51 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 52 | #if defined(WEBRTC_IOS) |
| 53 | // Defined in iosfileutils.mm. No header file to discourage use elsewhere. |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 54 | std::string IOSOutputPath(); |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 55 | std::string IOSRootPath(); |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 56 | std::string IOSResourcePath(std::string name, std::string extension); |
| 57 | #endif |
| 58 | |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 61 | #ifdef WIN32 |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 62 | const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 63 | #else |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 64 | const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 65 | #endif |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 66 | |
| 67 | #ifdef WEBRTC_ANDROID |
kjellander | 6820889 | 2016-06-16 23:29:30 -0700 | [diff] [blame] | 68 | const char* kRootDirName = "/sdcard/chromium_tests_root/"; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 69 | #else |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 70 | #if !defined(WEBRTC_IOS) |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 71 | const char* kOutputDirName = "out"; |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 72 | #endif |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 73 | const char* kFallbackPath = "./"; |
| 74 | #endif // !defined(WEBRTC_ANDROID) |
| 75 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 76 | #if !defined(WEBRTC_IOS) |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 77 | const char* kResourcesDirName = "resources"; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 78 | #endif |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 79 | |
pbos@webrtc.org | db7d82f | 2013-07-05 08:49:09 +0000 | [diff] [blame] | 80 | char relative_dir_path[FILENAME_MAX]; |
| 81 | bool relative_dir_path_set = false; |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; |
| 86 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 87 | void SetExecutablePath(const std::string& path) { |
| 88 | std::string working_dir = WorkingDir(); |
| 89 | std::string temp_path = path; |
| 90 | |
| 91 | // Handle absolute paths; convert them to relative paths to the working dir. |
| 92 | if (path.find(working_dir) != std::string::npos) { |
| 93 | temp_path = path.substr(working_dir.length() + 1); |
| 94 | } |
kjellander@webrtc.org | de49966 | 2013-08-29 11:26:41 +0000 | [diff] [blame] | 95 | // On Windows, when tests are run under memory tools like DrMemory and TSan, |
| 96 | // slashes occur in the path as directory separators. Make sure we replace |
| 97 | // such cases with backslashes in order for the paths to be correct. |
| 98 | #ifdef WIN32 |
| 99 | std::replace(temp_path.begin(), temp_path.end(), '/', '\\'); |
| 100 | #endif |
| 101 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 102 | // Trim away the executable name; only store the relative dir path. |
| 103 | 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] | 104 | strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX); |
| 105 | relative_dir_path_set = true; |
| 106 | } |
| 107 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 108 | bool FileExists(const std::string& file_name) { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 109 | struct stat file_info = {0}; |
| 110 | return stat(file_name.c_str(), &file_info) == 0; |
| 111 | } |
| 112 | |
alessiob | e49fede | 2017-03-15 06:04:59 -0700 | [diff] [blame] | 113 | bool DirExists(const std::string& directory_name) { |
| 114 | struct stat directory_info = {0}; |
| 115 | return stat(directory_name.c_str(), &directory_info) == 0 && S_ISDIR( |
| 116 | directory_info.st_mode); |
| 117 | } |
| 118 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 119 | #ifdef WEBRTC_ANDROID |
| 120 | |
| 121 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 122 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | std::string OutputPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 126 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | std::string WorkingDir() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 130 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 131 | } |
| 132 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 133 | #else // WEBRTC_ANDROID |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 134 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 135 | std::string ProjectRootPath() { |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 136 | #if defined(WEBRTC_IOS) |
| 137 | return IOSRootPath(); |
| 138 | #else |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 139 | std::string path = WorkingDir(); |
| 140 | if (path == kFallbackPath) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 141 | return kCannotFindProjectRootDir; |
| 142 | } |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 143 | if (relative_dir_path_set) { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 144 | path = path + kPathDelimiter + relative_dir_path; |
| 145 | } |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 146 | path = path + kPathDelimiter + ".." + kPathDelimiter + ".."; |
| 147 | char canonical_path[FILENAME_MAX]; |
| 148 | #ifdef WIN32 |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame] | 149 | BOOL succeeded = PathCanonicalizeA(canonical_path, path.c_str()); |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 150 | #else |
| 151 | bool succeeded = realpath(path.c_str(), canonical_path) != NULL; |
| 152 | #endif |
| 153 | if (succeeded) { |
| 154 | path = std::string(canonical_path) + kPathDelimiter; |
| 155 | return path; |
| 156 | } else { |
| 157 | fprintf(stderr, "Cannot find project root directory!\n"); |
| 158 | return kCannotFindProjectRootDir; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 159 | } |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 160 | #endif |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 161 | } |
| 162 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 163 | std::string OutputPath() { |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 164 | #if defined(WEBRTC_IOS) |
| 165 | return IOSOutputPath(); |
| 166 | #else |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 167 | std::string path = ProjectRootPath(); |
| 168 | if (path == kCannotFindProjectRootDir) { |
| 169 | return kFallbackPath; |
| 170 | } |
| 171 | path += kOutputDirName; |
| 172 | if (!CreateDir(path)) { |
| 173 | return kFallbackPath; |
| 174 | } |
| 175 | return path + kPathDelimiter; |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 176 | #endif |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 177 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 178 | |
| 179 | std::string WorkingDir() { |
| 180 | char path_buffer[FILENAME_MAX]; |
| 181 | if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) { |
| 182 | fprintf(stderr, "Cannot get current directory!\n"); |
| 183 | return kFallbackPath; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 184 | } else { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 185 | return std::string(path_buffer); |
| 186 | } |
| 187 | } |
| 188 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 189 | #endif // !WEBRTC_ANDROID |
| 190 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 191 | // Generate a temporary filename in a safe way. |
| 192 | // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc. |
| 193 | std::string TempFilename(const std::string &dir, const std::string &prefix) { |
| 194 | #ifdef WIN32 |
| 195 | wchar_t filename[MAX_PATH]; |
nisse | 07b8388 | 2017-03-14 01:32:50 -0700 | [diff] [blame] | 196 | if (::GetTempFileName(rtc::ToUtf16(dir).c_str(), |
| 197 | rtc::ToUtf16(prefix).c_str(), 0, filename) != 0) |
| 198 | return rtc::ToUtf8(filename); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 199 | assert(false); |
| 200 | return ""; |
| 201 | #else |
| 202 | int len = dir.size() + prefix.size() + 2 + 6; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 203 | std::unique_ptr<char[]> tempname(new char[len]); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 204 | |
| 205 | snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), |
| 206 | prefix.c_str()); |
| 207 | int fd = ::mkstemp(tempname.get()); |
| 208 | if (fd == -1) { |
| 209 | assert(false); |
| 210 | return ""; |
| 211 | } else { |
| 212 | ::close(fd); |
| 213 | } |
| 214 | std::string ret(tempname.get()); |
| 215 | return ret; |
| 216 | #endif |
| 217 | } |
| 218 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 219 | std::string GenerateTempFilename(const std::string& dir, |
| 220 | const std::string& prefix) { |
| 221 | std::string filename = TempFilename(dir, prefix); |
| 222 | RemoveFile(filename); |
| 223 | return filename; |
| 224 | } |
| 225 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 226 | absl::optional<std::vector<std::string>> ReadDirectory(std::string path) { |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 227 | if (path.length() == 0) |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 228 | return absl::optional<std::vector<std::string>>(); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 229 | |
| 230 | #if defined(WEBRTC_WIN) |
| 231 | // Append separator character if needed. |
| 232 | if (path.back() != '\\') |
| 233 | path += '\\'; |
| 234 | |
| 235 | // Init. |
| 236 | WIN32_FIND_DATA data; |
| 237 | HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data); |
| 238 | if (handle == INVALID_HANDLE_VALUE) |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 239 | return absl::optional<std::vector<std::string>>(); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 240 | |
| 241 | // Populate output. |
| 242 | std::vector<std::string> found_entries; |
| 243 | do { |
| 244 | const std::string name = rtc::ToUtf8(data.cFileName); |
| 245 | if (name != "." && name != "..") |
| 246 | found_entries.emplace_back(path + name); |
| 247 | } while (::FindNextFile(handle, &data) == TRUE); |
| 248 | |
| 249 | // Release resources. |
| 250 | if (handle != INVALID_HANDLE_VALUE) |
| 251 | ::FindClose(handle); |
| 252 | #else |
| 253 | // Append separator character if needed. |
| 254 | if (path.back() != '/') |
| 255 | path += '/'; |
| 256 | |
| 257 | // Init. |
| 258 | DIR* dir = ::opendir(path.c_str()); |
| 259 | if (dir == nullptr) |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 260 | return absl::optional<std::vector<std::string>>(); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 261 | |
| 262 | // Populate output. |
| 263 | std::vector<std::string> found_entries; |
| 264 | while (dirent* dirent = readdir(dir)) { |
| 265 | const std::string& name = dirent->d_name; |
| 266 | if (name != "." && name != "..") |
| 267 | found_entries.emplace_back(path + name); |
| 268 | } |
| 269 | |
| 270 | // Release resources. |
| 271 | closedir(dir); |
| 272 | #endif |
| 273 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 274 | return absl::optional<std::vector<std::string>>(std::move(found_entries)); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 275 | } |
| 276 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 277 | bool CreateDir(const std::string& directory_name) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 278 | struct stat path_info = {0}; |
| 279 | // Check if the path exists already: |
| 280 | if (stat(directory_name.c_str(), &path_info) == 0) { |
| 281 | if (!S_ISDIR(path_info.st_mode)) { |
| 282 | fprintf(stderr, "Path %s exists but is not a directory! Remove this " |
| 283 | "file and re-run to create the directory.\n", |
| 284 | directory_name.c_str()); |
| 285 | return false; |
| 286 | } |
| 287 | } else { |
| 288 | #ifdef WIN32 |
| 289 | return _mkdir(directory_name.c_str()) == 0; |
| 290 | #else |
| 291 | return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0; |
| 292 | #endif |
| 293 | } |
| 294 | return true; |
| 295 | } |
| 296 | |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 297 | bool RemoveDir(const std::string& directory_name) { |
| 298 | #ifdef WIN32 |
| 299 | return RemoveDirectoryA(directory_name.c_str()) != FALSE; |
| 300 | #else |
| 301 | return rmdir(directory_name.c_str()) == 0; |
| 302 | #endif |
| 303 | } |
| 304 | |
| 305 | bool RemoveFile(const std::string& file_name) { |
| 306 | #ifdef WIN32 |
| 307 | return DeleteFileA(file_name.c_str()) != FALSE; |
| 308 | #else |
| 309 | return unlink(file_name.c_str()) == 0; |
| 310 | #endif |
| 311 | } |
| 312 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 313 | std::string ResourcePath(const std::string& name, |
| 314 | const std::string& extension) { |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 315 | #if defined(WEBRTC_IOS) |
| 316 | return IOSResourcePath(name, extension); |
| 317 | #else |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 318 | std::string platform = "win"; |
| 319 | #ifdef WEBRTC_LINUX |
| 320 | platform = "linux"; |
| 321 | #endif // WEBRTC_LINUX |
| 322 | #ifdef WEBRTC_MAC |
| 323 | platform = "mac"; |
| 324 | #endif // WEBRTC_MAC |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 325 | #ifdef WEBRTC_ANDROID |
| 326 | platform = "android"; |
| 327 | #endif // WEBRTC_ANDROID |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 328 | |
| 329 | #ifdef WEBRTC_ARCH_64_BITS |
| 330 | std::string architecture = "64"; |
| 331 | #else |
| 332 | std::string architecture = "32"; |
| 333 | #endif // WEBRTC_ARCH_64_BITS |
| 334 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 335 | std::string resources_path = ProjectRootPath() + kResourcesDirName + |
| 336 | kPathDelimiter; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 337 | std::string resource_file = resources_path + name + "_" + platform + "_" + |
| 338 | architecture + "." + extension; |
kjellander@webrtc.org | 80b2661 | 2011-12-07 18:50:17 +0000 | [diff] [blame] | 339 | if (FileExists(resource_file)) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 340 | return resource_file; |
| 341 | } |
| 342 | // Try without architecture. |
| 343 | resource_file = resources_path + name + "_" + platform + "." + extension; |
| 344 | if (FileExists(resource_file)) { |
| 345 | return resource_file; |
| 346 | } |
| 347 | // Try without platform. |
| 348 | resource_file = resources_path + name + "_" + architecture + "." + extension; |
| 349 | if (FileExists(resource_file)) { |
| 350 | return resource_file; |
| 351 | } |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 352 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 353 | // Fall back on name without architecture or platform. |
| 354 | return resources_path + name + "." + extension; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 355 | #endif // defined (WEBRTC_IOS) |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Niels Möller | 392f8d0 | 2018-06-01 10:14:44 +0200 | [diff] [blame] | 358 | std::string JoinFilename(const std::string& dir, const std::string& name) { |
| 359 | RTC_CHECK(!dir.empty()) << "Special cases not implemented."; |
| 360 | return dir + kPathDelimiter + name; |
| 361 | } |
| 362 | |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 363 | size_t GetFileSize(const std::string& filename) { |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 364 | FILE* f = fopen(filename.c_str(), "rb"); |
| 365 | size_t size = 0; |
| 366 | if (f != NULL) { |
| 367 | if (fseek(f, 0, SEEK_END) == 0) { |
| 368 | size = ftell(f); |
| 369 | } |
| 370 | fclose(f); |
| 371 | } |
| 372 | return size; |
| 373 | } |
| 374 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 375 | } // namespace test |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 376 | } // namespace webrtc |