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