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 | |
pbos@webrtc.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 11 | #include "webrtc/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 | |
| 21 | #include "webrtc/system_wrappers/interface/utf_util_win.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 22 | #define GET_CURRENT_DIR _getcwd |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 23 | #else |
| 24 | #include <unistd.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 25 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 26 | #include "webrtc/base/scoped_ptr.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 27 | #define GET_CURRENT_DIR getcwd |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | #include <sys/stat.h> // To check for directory existence. |
| 31 | #ifndef S_ISDIR // Not defined in stat.h on Windows. |
| 32 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 37 | #include <string.h> |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 38 | |
pbos@webrtc.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 39 | #include "webrtc/typedefs.h" // For architecture defines |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 40 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 41 | namespace webrtc { |
| 42 | namespace test { |
| 43 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 44 | #if defined(WEBRTC_IOS) |
| 45 | // Defined in iosfileutils.mm. No header file to discourage use elsewhere. |
| 46 | std::string IOSResourcePath(std::string name, std::string extension); |
| 47 | #endif |
| 48 | |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 51 | #ifdef WIN32 |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 52 | const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 53 | #else |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 54 | const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 55 | #endif |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 56 | |
| 57 | #ifdef WEBRTC_ANDROID |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 58 | const char* kRootDirName = "/sdcard/"; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 59 | #else |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 60 | // The file we're looking for to identify the project root dir. |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 61 | const char* kProjectRootFileName = "DEPS"; |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 62 | const char* kOutputDirName = "out"; |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 63 | const char* kFallbackPath = "./"; |
| 64 | #endif |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 65 | #if !defined(WEBRTC_IOS) |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 66 | const char* kResourcesDirName = "resources"; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 67 | #endif |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 68 | |
pbos@webrtc.org | db7d82f | 2013-07-05 08:49:09 +0000 | [diff] [blame] | 69 | char relative_dir_path[FILENAME_MAX]; |
| 70 | bool relative_dir_path_set = false; |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; |
| 75 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 76 | void SetExecutablePath(const std::string& path) { |
| 77 | std::string working_dir = WorkingDir(); |
| 78 | std::string temp_path = path; |
| 79 | |
| 80 | // Handle absolute paths; convert them to relative paths to the working dir. |
| 81 | if (path.find(working_dir) != std::string::npos) { |
| 82 | temp_path = path.substr(working_dir.length() + 1); |
| 83 | } |
kjellander@webrtc.org | de49966 | 2013-08-29 11:26:41 +0000 | [diff] [blame] | 84 | // On Windows, when tests are run under memory tools like DrMemory and TSan, |
| 85 | // slashes occur in the path as directory separators. Make sure we replace |
| 86 | // such cases with backslashes in order for the paths to be correct. |
| 87 | #ifdef WIN32 |
| 88 | std::replace(temp_path.begin(), temp_path.end(), '/', '\\'); |
| 89 | #endif |
| 90 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 91 | // Trim away the executable name; only store the relative dir path. |
| 92 | 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] | 93 | strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX); |
| 94 | relative_dir_path_set = true; |
| 95 | } |
| 96 | |
| 97 | bool FileExists(std::string& file_name) { |
| 98 | struct stat file_info = {0}; |
| 99 | return stat(file_name.c_str(), &file_info) == 0; |
| 100 | } |
| 101 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 102 | #ifdef WEBRTC_ANDROID |
| 103 | |
| 104 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 105 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | std::string OutputPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 109 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::string WorkingDir() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 113 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #else // WEBRTC_ANDROID |
| 117 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 118 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 119 | std::string path = WorkingDir(); |
| 120 | if (path == kFallbackPath) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 121 | return kCannotFindProjectRootDir; |
| 122 | } |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 123 | if (relative_dir_path_set) { |
| 124 | path = path + kPathDelimiter + relative_dir_path; |
| 125 | } |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 126 | // Check for our file that verifies the root dir. |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 127 | size_t path_delimiter_index = path.find_last_of(kPathDelimiter); |
| 128 | while (path_delimiter_index != std::string::npos) { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 129 | std::string root_filename = path + kPathDelimiter + kProjectRootFileName; |
| 130 | if (FileExists(root_filename)) { |
| 131 | return path + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 132 | } |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 133 | // Move up one directory in the directory tree. |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 134 | path = path.substr(0, path_delimiter_index); |
| 135 | path_delimiter_index = path.find_last_of(kPathDelimiter); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 136 | } |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 137 | // Reached the root directory. |
| 138 | fprintf(stderr, "Cannot find project root directory!\n"); |
| 139 | return kCannotFindProjectRootDir; |
| 140 | } |
| 141 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 142 | std::string OutputPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 143 | std::string path = ProjectRootPath(); |
| 144 | if (path == kCannotFindProjectRootDir) { |
| 145 | return kFallbackPath; |
| 146 | } |
| 147 | path += kOutputDirName; |
| 148 | if (!CreateDir(path)) { |
| 149 | return kFallbackPath; |
| 150 | } |
| 151 | return path + kPathDelimiter; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 152 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 153 | |
| 154 | std::string WorkingDir() { |
| 155 | char path_buffer[FILENAME_MAX]; |
| 156 | if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) { |
| 157 | fprintf(stderr, "Cannot get current directory!\n"); |
| 158 | return kFallbackPath; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 159 | } else { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 160 | return std::string(path_buffer); |
| 161 | } |
| 162 | } |
| 163 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 164 | #endif // !WEBRTC_ANDROID |
| 165 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 166 | // Generate a temporary filename in a safe way. |
| 167 | // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc. |
| 168 | std::string TempFilename(const std::string &dir, const std::string &prefix) { |
| 169 | #ifdef WIN32 |
| 170 | wchar_t filename[MAX_PATH]; |
| 171 | if (::GetTempFileName(ToUtf16(dir).c_str(), |
| 172 | ToUtf16(prefix).c_str(), 0, filename) != 0) |
| 173 | return ToUtf8(filename); |
| 174 | assert(false); |
| 175 | return ""; |
| 176 | #else |
| 177 | int len = dir.size() + prefix.size() + 2 + 6; |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 178 | rtc::scoped_ptr<char[]> tempname(new char[len]); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 179 | |
| 180 | snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), |
| 181 | prefix.c_str()); |
| 182 | int fd = ::mkstemp(tempname.get()); |
| 183 | if (fd == -1) { |
| 184 | assert(false); |
| 185 | return ""; |
| 186 | } else { |
| 187 | ::close(fd); |
| 188 | } |
| 189 | std::string ret(tempname.get()); |
| 190 | return ret; |
| 191 | #endif |
| 192 | } |
| 193 | |
| 194 | bool CreateDir(std::string directory_name) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 195 | struct stat path_info = {0}; |
| 196 | // Check if the path exists already: |
| 197 | if (stat(directory_name.c_str(), &path_info) == 0) { |
| 198 | if (!S_ISDIR(path_info.st_mode)) { |
| 199 | fprintf(stderr, "Path %s exists but is not a directory! Remove this " |
| 200 | "file and re-run to create the directory.\n", |
| 201 | directory_name.c_str()); |
| 202 | return false; |
| 203 | } |
| 204 | } else { |
| 205 | #ifdef WIN32 |
| 206 | return _mkdir(directory_name.c_str()) == 0; |
| 207 | #else |
| 208 | return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0; |
| 209 | #endif |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 214 | std::string ResourcePath(std::string name, std::string extension) { |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 215 | #if defined(WEBRTC_IOS) |
| 216 | return IOSResourcePath(name, extension); |
| 217 | #else |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 218 | std::string platform = "win"; |
| 219 | #ifdef WEBRTC_LINUX |
| 220 | platform = "linux"; |
| 221 | #endif // WEBRTC_LINUX |
| 222 | #ifdef WEBRTC_MAC |
| 223 | platform = "mac"; |
| 224 | #endif // WEBRTC_MAC |
| 225 | |
| 226 | #ifdef WEBRTC_ARCH_64_BITS |
| 227 | std::string architecture = "64"; |
| 228 | #else |
| 229 | std::string architecture = "32"; |
| 230 | #endif // WEBRTC_ARCH_64_BITS |
| 231 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 232 | std::string resources_path = ProjectRootPath() + kResourcesDirName + |
| 233 | kPathDelimiter; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 234 | std::string resource_file = resources_path + name + "_" + platform + "_" + |
| 235 | architecture + "." + extension; |
kjellander@webrtc.org | 80b2661 | 2011-12-07 18:50:17 +0000 | [diff] [blame] | 236 | if (FileExists(resource_file)) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 237 | return resource_file; |
| 238 | } |
| 239 | // Try without architecture. |
| 240 | resource_file = resources_path + name + "_" + platform + "." + extension; |
| 241 | if (FileExists(resource_file)) { |
| 242 | return resource_file; |
| 243 | } |
| 244 | // Try without platform. |
| 245 | resource_file = resources_path + name + "_" + architecture + "." + extension; |
| 246 | if (FileExists(resource_file)) { |
| 247 | return resource_file; |
| 248 | } |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 249 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 250 | // Fall back on name without architecture or platform. |
| 251 | return resources_path + name + "." + extension; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 252 | #endif // defined (WEBRTC_IOS) |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 253 | } |
| 254 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 255 | size_t GetFileSize(std::string filename) { |
| 256 | FILE* f = fopen(filename.c_str(), "rb"); |
| 257 | size_t size = 0; |
| 258 | if (f != NULL) { |
| 259 | if (fseek(f, 0, SEEK_END) == 0) { |
| 260 | size = ftell(f); |
| 261 | } |
| 262 | fclose(f); |
| 263 | } |
| 264 | return size; |
| 265 | } |
| 266 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 267 | } // namespace test |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 268 | } // namespace webrtc |