blob: c89f9bd1a2f9bbf2479d86b14d3cb9c3e6742ad6 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
andrew@webrtc.org7a281a52012-06-27 03:22:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00003 *
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.org34741c82013-05-27 08:02:22 +000011#include "webrtc/test/testsupport/fileutils.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000012
13#ifdef WIN32
14#include <direct.h>
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000015#include <algorithm>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000017#else
18#include <unistd.h>
19#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000020#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.org7951e812011-10-13 12:24:41 +000025#endif
26
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000027#include <stdio.h>
28#include <string.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000029
pbos@webrtc.org34741c82013-05-27 08:02:22 +000030#include "webrtc/typedefs.h" // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000031
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000032namespace webrtc {
33namespace test {
34
henrike@webrtc.org34773d92013-07-08 14:55:23 +000035namespace {
36
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000037#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000038const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000039#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000040const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000041#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000042
43#ifdef WEBRTC_ANDROID
henrike@webrtc.org34773d92013-07-08 14:55:23 +000044const char* kResourcesDirName = "resources";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000045#else
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000046// The file we're looking for to identify the project root dir.
henrike@webrtc.org34773d92013-07-08 14:55:23 +000047const char* kProjectRootFileName = "DEPS";
48const char* kResourcesDirName = "resources";
leozwang@webrtc.orgfb594422012-06-29 18:28:12 +000049#endif
henrike@webrtc.orgcaf2fcc2013-07-05 04:15:38 +000050
henrike@webrtc.org34773d92013-07-08 14:55:23 +000051const char* kFallbackPath = "./";
52const char* kOutputDirName = "out";
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000053char relative_dir_path[FILENAME_MAX];
54bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000055
56} // namespace
57
58const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
59
60std::string OutputPathAndroid();
pbos@webrtc.orgfc496d92013-07-09 15:24:16 +000061std::string ProjectRootPathAndroid();
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000062
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000063void 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.orgde499662013-08-29 11:26:41 +000071 // 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.org193600b2012-10-17 04:39:44 +000078 // 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.org83b767b2012-10-15 18:14:12 +000080 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
81 relative_dir_path_set = true;
82}
83
84bool 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.org34773d92013-07-08 14:55:23 +000089std::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.org363efef2012-10-16 04:31:20 +0000101#ifdef WEBRTC_ANDROID
102
103std::string ProjectRootPath() {
pbos@webrtc.orgfc496d92013-07-09 15:24:16 +0000104 return ProjectRootPathAndroid();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000105}
106
107std::string OutputPath() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000108 return OutputPathAndroid();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000109}
110
111std::string WorkingDir() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000112 return ProjectRootPath();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000113}
114
115#else // WEBRTC_ANDROID
116
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000117std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000118 std::string path = WorkingDir();
119 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000120 return kCannotFindProjectRootDir;
121 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000122 if (relative_dir_path_set) {
123 path = path + kPathDelimiter + relative_dir_path;
124 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000125 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000126 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
127 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000128 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
129 if (FileExists(root_filename)) {
130 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000131 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000132 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000133 path = path.substr(0, path_delimiter_index);
134 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000135 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000136 // Reached the root directory.
137 fprintf(stderr, "Cannot find project root directory!\n");
138 return kCannotFindProjectRootDir;
139}
140
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000141std::string OutputPath() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000142 return OutputPathImpl();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000143}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000144
145std::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.org5b97b122011-12-08 07:42:18 +0000150 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000151 return std::string(path_buffer);
152 }
153}
154
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000155#endif // !WEBRTC_ANDROID
156
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000157bool 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.org4ed4f242011-12-05 16:31:12 +0000177std::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.org5b97b122011-12-08 07:42:18 +0000192 std::string resources_path = ProjectRootPath() + kResourcesDirName +
193 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000194 std::string resource_file = resources_path + name + "_" + platform + "_" +
195 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000196 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000197 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.org363efef2012-10-16 04:31:20 +0000209
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000210 // Fall back on name without architecture or platform.
211 return resources_path + name + "." + extension;
212}
213
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000214size_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.org7951e812011-10-13 12:24:41 +0000226} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000227} // namespace webrtc