kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
| 11 | #include "fileutils.h" |
| 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> |
| 27 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 28 | #include "typedefs.h" // For architecture defines |
| 29 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 30 | namespace webrtc { |
| 31 | namespace test { |
| 32 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 33 | #ifdef WIN32 |
| 34 | static const char* kPathDelimiter = "\\"; |
| 35 | #else |
| 36 | static const char* kPathDelimiter = "/"; |
| 37 | #endif |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 38 | // The file we're looking for to identify the project root dir. |
| 39 | static const char* kProjectRootFileName = "DEPS"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 40 | static const char* kOutputDirName = "out"; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 41 | static const char* kFallbackPath = "./"; |
| 42 | static const char* kResourcesDirName = "resources"; |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 43 | const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; |
| 44 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 45 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 46 | std::string working_dir = WorkingDir(); |
| 47 | if (working_dir == kFallbackPath) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 48 | return kCannotFindProjectRootDir; |
| 49 | } |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 50 | // Check for our file that verifies the root dir. |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 51 | std::string current_path(working_dir); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 52 | FILE* file = NULL; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 53 | int path_delimiter_index = current_path.find_last_of(kPathDelimiter); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 54 | while (path_delimiter_index > -1) { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 55 | std::string root_filename = current_path + kPathDelimiter + |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 56 | kProjectRootFileName; |
| 57 | file = fopen(root_filename.c_str(), "r"); |
| 58 | if (file != NULL) { |
kjellander@webrtc.org | 5483210 | 2011-11-25 09:33:41 +0000 | [diff] [blame] | 59 | fclose(file); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 60 | return current_path + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 61 | } |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 62 | // Move up one directory in the directory tree. |
| 63 | current_path = current_path.substr(0, path_delimiter_index); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 64 | path_delimiter_index = current_path.find_last_of(kPathDelimiter); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 65 | } |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 66 | // Reached the root directory. |
| 67 | fprintf(stderr, "Cannot find project root directory!\n"); |
| 68 | return kCannotFindProjectRootDir; |
| 69 | } |
| 70 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 71 | std::string OutputPath() { |
| 72 | std::string path = ProjectRootPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 73 | if (path == kCannotFindProjectRootDir) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 74 | return kFallbackPath; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 75 | } |
| 76 | path += kOutputDirName; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 77 | if (!CreateDirectory(path)) { |
| 78 | return kFallbackPath; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 79 | } |
| 80 | return path + kPathDelimiter; |
| 81 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 82 | |
| 83 | std::string WorkingDir() { |
| 84 | char path_buffer[FILENAME_MAX]; |
| 85 | if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) { |
| 86 | fprintf(stderr, "Cannot get current directory!\n"); |
| 87 | return kFallbackPath; |
| 88 | } |
| 89 | else { |
| 90 | return std::string(path_buffer); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | bool CreateDirectory(std::string directory_name) { |
| 95 | struct stat path_info = {0}; |
| 96 | // Check if the path exists already: |
| 97 | if (stat(directory_name.c_str(), &path_info) == 0) { |
| 98 | if (!S_ISDIR(path_info.st_mode)) { |
| 99 | fprintf(stderr, "Path %s exists but is not a directory! Remove this " |
| 100 | "file and re-run to create the directory.\n", |
| 101 | directory_name.c_str()); |
| 102 | return false; |
| 103 | } |
| 104 | } else { |
| 105 | #ifdef WIN32 |
| 106 | return _mkdir(directory_name.c_str()) == 0; |
| 107 | #else |
| 108 | return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0; |
| 109 | #endif |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool FileExists(std::string file_name) { |
| 115 | struct stat file_info = {0}; |
| 116 | return stat(file_name.c_str(), &file_info) == 0; |
| 117 | } |
| 118 | |
| 119 | std::string ResourcePath(std::string name, std::string extension) { |
| 120 | std::string platform = "win"; |
| 121 | #ifdef WEBRTC_LINUX |
| 122 | platform = "linux"; |
| 123 | #endif // WEBRTC_LINUX |
| 124 | #ifdef WEBRTC_MAC |
| 125 | platform = "mac"; |
| 126 | #endif // WEBRTC_MAC |
| 127 | |
| 128 | #ifdef WEBRTC_ARCH_64_BITS |
| 129 | std::string architecture = "64"; |
| 130 | #else |
| 131 | std::string architecture = "32"; |
| 132 | #endif // WEBRTC_ARCH_64_BITS |
| 133 | |
| 134 | std::string resources_path = ProjectRootPath() + kResourcesDirName + kPathDelimiter; |
| 135 | std::string resource_file = resources_path + name + "_" + platform + "_" + |
| 136 | architecture + "." + extension; |
kjellander@webrtc.org | 80b2661 | 2011-12-07 18:50:17 +0000 | [diff] [blame^] | 137 | if (FileExists(resource_file)) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 138 | return resource_file; |
| 139 | } |
| 140 | // Try without architecture. |
| 141 | resource_file = resources_path + name + "_" + platform + "." + extension; |
| 142 | if (FileExists(resource_file)) { |
| 143 | return resource_file; |
| 144 | } |
| 145 | // Try without platform. |
| 146 | resource_file = resources_path + name + "_" + architecture + "." + extension; |
| 147 | if (FileExists(resource_file)) { |
| 148 | return resource_file; |
| 149 | } |
| 150 | // Fall back on name without architecture or platform. |
| 151 | return resources_path + name + "." + extension; |
| 152 | } |
| 153 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 154 | } // namespace test |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 155 | } // namespace webrtc |