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