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