blob: 36ca391c4135c36657d3d81468edd623f09313fa [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
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000013#include <assert.h>
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000014
15#ifdef WIN32
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016#include <direct.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000017#include <tchar.h>
18#include <windows.h>
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000019#include <algorithm>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000020
21#include "webrtc/system_wrappers/interface/utf_util_win.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000022#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000023#else
24#include <unistd.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000025
26#include "webrtc/system_wrappers/interface/scoped_ptr.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000027#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000028#endif
29
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)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000033#endif
34
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000035#include <stdio.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000036#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000037#include <string.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000038
pbos@webrtc.org34741c82013-05-27 08:02:22 +000039#include "webrtc/typedefs.h" // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000040
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000041namespace webrtc {
42namespace test {
43
henrike@webrtc.org34773d92013-07-08 14:55:23 +000044namespace {
45
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000046#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000047const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000048#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000049const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000050#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000051
52#ifdef WEBRTC_ANDROID
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000053const char* kRootDirName = "/sdcard/";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000054#else
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000055// The file we're looking for to identify the project root dir.
henrike@webrtc.org34773d92013-07-08 14:55:23 +000056const char* kProjectRootFileName = "DEPS";
henrike@webrtc.org34773d92013-07-08 14:55:23 +000057const char* kOutputDirName = "out";
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000058const char* kFallbackPath = "./";
59#endif
60const char* kResourcesDirName = "resources";
61
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000062char relative_dir_path[FILENAME_MAX];
63bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000064
65} // namespace
66
67const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
68
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000069void SetExecutablePath(const std::string& path) {
70 std::string working_dir = WorkingDir();
71 std::string temp_path = path;
72
73 // Handle absolute paths; convert them to relative paths to the working dir.
74 if (path.find(working_dir) != std::string::npos) {
75 temp_path = path.substr(working_dir.length() + 1);
76 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000077 // On Windows, when tests are run under memory tools like DrMemory and TSan,
78 // slashes occur in the path as directory separators. Make sure we replace
79 // such cases with backslashes in order for the paths to be correct.
80#ifdef WIN32
81 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
82#endif
83
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000084 // Trim away the executable name; only store the relative dir path.
85 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000086 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
87 relative_dir_path_set = true;
88}
89
90bool FileExists(std::string& file_name) {
91 struct stat file_info = {0};
92 return stat(file_name.c_str(), &file_info) == 0;
93}
94
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000095#ifdef WEBRTC_ANDROID
96
97std::string ProjectRootPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000098 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000099}
100
101std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000102 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000103}
104
105std::string WorkingDir() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000106 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000107}
108
109#else // WEBRTC_ANDROID
110
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000111std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000112 std::string path = WorkingDir();
113 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000114 return kCannotFindProjectRootDir;
115 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000116 if (relative_dir_path_set) {
117 path = path + kPathDelimiter + relative_dir_path;
118 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000119 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000120 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
121 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000122 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
123 if (FileExists(root_filename)) {
124 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000125 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000126 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000127 path = path.substr(0, path_delimiter_index);
128 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000129 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000130 // Reached the root directory.
131 fprintf(stderr, "Cannot find project root directory!\n");
132 return kCannotFindProjectRootDir;
133}
134
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000135std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000136 std::string path = ProjectRootPath();
137 if (path == kCannotFindProjectRootDir) {
138 return kFallbackPath;
139 }
140 path += kOutputDirName;
141 if (!CreateDir(path)) {
142 return kFallbackPath;
143 }
144 return path + kPathDelimiter;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000145}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000146
147std::string WorkingDir() {
148 char path_buffer[FILENAME_MAX];
149 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
150 fprintf(stderr, "Cannot get current directory!\n");
151 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000152 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000153 return std::string(path_buffer);
154 }
155}
156
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000157#endif // !WEBRTC_ANDROID
158
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000159// Generate a temporary filename in a safe way.
160// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
161std::string TempFilename(const std::string &dir, const std::string &prefix) {
162#ifdef WIN32
163 wchar_t filename[MAX_PATH];
164 if (::GetTempFileName(ToUtf16(dir).c_str(),
165 ToUtf16(prefix).c_str(), 0, filename) != 0)
166 return ToUtf8(filename);
167 assert(false);
168 return "";
169#else
170 int len = dir.size() + prefix.size() + 2 + 6;
171 scoped_ptr<char[]> tempname(new char[len]);
172
173 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
174 prefix.c_str());
175 int fd = ::mkstemp(tempname.get());
176 if (fd == -1) {
177 assert(false);
178 return "";
179 } else {
180 ::close(fd);
181 }
182 std::string ret(tempname.get());
183 return ret;
184#endif
185}
186
187bool CreateDir(std::string directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000188 struct stat path_info = {0};
189 // Check if the path exists already:
190 if (stat(directory_name.c_str(), &path_info) == 0) {
191 if (!S_ISDIR(path_info.st_mode)) {
192 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
193 "file and re-run to create the directory.\n",
194 directory_name.c_str());
195 return false;
196 }
197 } else {
198#ifdef WIN32
199 return _mkdir(directory_name.c_str()) == 0;
200#else
201 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
202#endif
203 }
204 return true;
205}
206
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000207std::string ResourcePath(std::string name, std::string extension) {
208 std::string platform = "win";
209#ifdef WEBRTC_LINUX
210 platform = "linux";
211#endif // WEBRTC_LINUX
212#ifdef WEBRTC_MAC
213 platform = "mac";
214#endif // WEBRTC_MAC
215
216#ifdef WEBRTC_ARCH_64_BITS
217 std::string architecture = "64";
218#else
219 std::string architecture = "32";
220#endif // WEBRTC_ARCH_64_BITS
221
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000222 std::string resources_path = ProjectRootPath() + kResourcesDirName +
223 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000224 std::string resource_file = resources_path + name + "_" + platform + "_" +
225 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000226 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000227 return resource_file;
228 }
229 // Try without architecture.
230 resource_file = resources_path + name + "_" + platform + "." + extension;
231 if (FileExists(resource_file)) {
232 return resource_file;
233 }
234 // Try without platform.
235 resource_file = resources_path + name + "_" + architecture + "." + extension;
236 if (FileExists(resource_file)) {
237 return resource_file;
238 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000239
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000240 // Fall back on name without architecture or platform.
241 return resources_path + name + "." + extension;
242}
243
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000244size_t GetFileSize(std::string filename) {
245 FILE* f = fopen(filename.c_str(), "rb");
246 size_t size = 0;
247 if (f != NULL) {
248 if (fseek(f, 0, SEEK_END) == 0) {
249 size = ftell(f);
250 }
251 fclose(f);
252 }
253 return size;
254}
255
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000256} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000257} // namespace webrtc