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