2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/test/testsupport/fileutils.h"
21 #include "webrtc/system_wrappers/interface/utf_util_win.h"
22 #define GET_CURRENT_DIR _getcwd
26 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
27 #define GET_CURRENT_DIR getcwd
30 #include <sys/stat.h> // To check for directory existence.
31 #ifndef S_ISDIR // Not defined in stat.h on Windows.
32 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
39 #include "webrtc/typedefs.h" // For architecture defines
47 const char* kPathDelimiter = "\\";
49 const char* kPathDelimiter = "/";
53 const char* kResourcesDirName = "resources";
55 // The file we're looking for to identify the project root dir.
56 const char* kProjectRootFileName = "DEPS";
57 const char* kResourcesDirName = "resources";
60 const char* kFallbackPath = "./";
61 const char* kOutputDirName = "out";
62 char relative_dir_path[FILENAME_MAX];
63 bool relative_dir_path_set = false;
67 const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
69 std::string OutputPathAndroid();
70 std::string ProjectRootPathAndroid();
72 void SetExecutablePath(const std::string& path) {
73 std::string working_dir = WorkingDir();
74 std::string temp_path = path;
76 // Handle absolute paths; convert them to relative paths to the working dir.
77 if (path.find(working_dir) != std::string::npos) {
78 temp_path = path.substr(working_dir.length() + 1);
80 // On Windows, when tests are run under memory tools like DrMemory and TSan,
81 // slashes occur in the path as directory separators. Make sure we replace
82 // such cases with backslashes in order for the paths to be correct.
84 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
87 // Trim away the executable name; only store the relative dir path.
88 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
89 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
90 relative_dir_path_set = true;
93 bool FileExists(std::string& file_name) {
94 struct stat file_info = {0};
95 return stat(file_name.c_str(), &file_info) == 0;
98 std::string OutputPathImpl() {
99 std::string path = ProjectRootPath();
100 if (path == kCannotFindProjectRootDir) {
101 return kFallbackPath;
103 path += kOutputDirName;
104 if (!CreateDir(path)) {
105 return kFallbackPath;
107 return path + kPathDelimiter;
110 #ifdef WEBRTC_ANDROID
112 std::string ProjectRootPath() {
113 return ProjectRootPathAndroid();
116 std::string OutputPath() {
117 return OutputPathAndroid();
120 std::string WorkingDir() {
121 return ProjectRootPath();
124 #else // WEBRTC_ANDROID
126 std::string ProjectRootPath() {
127 std::string path = WorkingDir();
128 if (path == kFallbackPath) {
129 return kCannotFindProjectRootDir;
131 if (relative_dir_path_set) {
132 path = path + kPathDelimiter + relative_dir_path;
134 // Check for our file that verifies the root dir.
135 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
136 while (path_delimiter_index != std::string::npos) {
137 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
138 if (FileExists(root_filename)) {
139 return path + kPathDelimiter;
141 // Move up one directory in the directory tree.
142 path = path.substr(0, path_delimiter_index);
143 path_delimiter_index = path.find_last_of(kPathDelimiter);
145 // Reached the root directory.
146 fprintf(stderr, "Cannot find project root directory!\n");
147 return kCannotFindProjectRootDir;
150 std::string OutputPath() {
151 return OutputPathImpl();
154 std::string WorkingDir() {
155 char path_buffer[FILENAME_MAX];
156 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
157 fprintf(stderr, "Cannot get current directory!\n");
158 return kFallbackPath;
160 return std::string(path_buffer);
164 #endif // !WEBRTC_ANDROID
166 // Generate a temporary filename in a safe way.
167 // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
168 std::string TempFilename(const std::string &dir, const std::string &prefix) {
170 wchar_t filename[MAX_PATH];
171 if (::GetTempFileName(ToUtf16(dir).c_str(),
172 ToUtf16(prefix).c_str(), 0, filename) != 0)
173 return ToUtf8(filename);
177 int len = dir.size() + prefix.size() + 2 + 6;
178 scoped_ptr<char[]> tempname(new char[len]);
180 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
182 int fd = ::mkstemp(tempname.get());
189 std::string ret(tempname.get());
194 bool CreateDir(std::string directory_name) {
195 struct stat path_info = {0};
196 // Check if the path exists already:
197 if (stat(directory_name.c_str(), &path_info) == 0) {
198 if (!S_ISDIR(path_info.st_mode)) {
199 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
200 "file and re-run to create the directory.\n",
201 directory_name.c_str());
206 return _mkdir(directory_name.c_str()) == 0;
208 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
214 std::string ResourcePath(std::string name, std::string extension) {
215 std::string platform = "win";
218 #endif // WEBRTC_LINUX
223 #ifdef WEBRTC_ARCH_64_BITS
224 std::string architecture = "64";
226 std::string architecture = "32";
227 #endif // WEBRTC_ARCH_64_BITS
229 std::string resources_path = ProjectRootPath() + kResourcesDirName +
231 std::string resource_file = resources_path + name + "_" + platform + "_" +
232 architecture + "." + extension;
233 if (FileExists(resource_file)) {
234 return resource_file;
236 // Try without architecture.
237 resource_file = resources_path + name + "_" + platform + "." + extension;
238 if (FileExists(resource_file)) {
239 return resource_file;
241 // Try without platform.
242 resource_file = resources_path + name + "_" + architecture + "." + extension;
243 if (FileExists(resource_file)) {
244 return resource_file;
247 // Fall back on name without architecture or platform.
248 return resources_path + name + "." + extension;
251 size_t GetFileSize(std::string filename) {
252 FILE* f = fopen(filename.c_str(), "rb");
255 if (fseek(f, 0, SEEK_END) == 0) {
264 } // namespace webrtc