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