blob: 2fab425a3166430ca76a1d8fc98bdbd11ce3c147 [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
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/system_wrappers/include/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
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000026#include "webrtc/base/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
henrika1d34fe92015-06-16 10:04:20 +020044#if defined(WEBRTC_IOS)
45// Defined in iosfileutils.mm. No header file to discourage use elsewhere.
kjellander02060002016-02-16 22:06:12 -080046std::string IOSOutputPath();
henrika1d34fe92015-06-16 10:04:20 +020047std::string IOSResourcePath(std::string name, std::string extension);
48#endif
49
henrike@webrtc.org34773d92013-07-08 14:55:23 +000050namespace {
51
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000052#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000053const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000054#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000055const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000056#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000057
58#ifdef WEBRTC_ANDROID
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000059const char* kRootDirName = "/sdcard/";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000060#else
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000061// The file we're looking for to identify the project root dir.
henrike@webrtc.org34773d92013-07-08 14:55:23 +000062const char* kProjectRootFileName = "DEPS";
kjellander02060002016-02-16 22:06:12 -080063#if !defined(WEBRTC_IOS)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000064const char* kOutputDirName = "out";
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000065#endif
kjellander02060002016-02-16 22:06:12 -080066const char* kFallbackPath = "./";
67#endif // !defined(WEBRTC_ANDROID)
68
henrika1d34fe92015-06-16 10:04:20 +020069#if !defined(WEBRTC_IOS)
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000070const char* kResourcesDirName = "resources";
henrika1d34fe92015-06-16 10:04:20 +020071#endif
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000072
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000073char relative_dir_path[FILENAME_MAX];
74bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000075
76} // namespace
77
78const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
79
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000080void SetExecutablePath(const std::string& path) {
81 std::string working_dir = WorkingDir();
82 std::string temp_path = path;
83
84 // Handle absolute paths; convert them to relative paths to the working dir.
85 if (path.find(working_dir) != std::string::npos) {
86 temp_path = path.substr(working_dir.length() + 1);
87 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000088 // On Windows, when tests are run under memory tools like DrMemory and TSan,
89 // slashes occur in the path as directory separators. Make sure we replace
90 // such cases with backslashes in order for the paths to be correct.
91#ifdef WIN32
92 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
93#endif
94
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000095 // Trim away the executable name; only store the relative dir path.
96 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000097 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
98 relative_dir_path_set = true;
99}
100
101bool FileExists(std::string& file_name) {
102 struct stat file_info = {0};
103 return stat(file_name.c_str(), &file_info) == 0;
104}
105
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000106#ifdef WEBRTC_ANDROID
107
108std::string ProjectRootPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000109 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000110}
111
112std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000113 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000114}
115
116std::string WorkingDir() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000117 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000118}
119
120#else // WEBRTC_ANDROID
121
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000122std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000123 std::string path = WorkingDir();
124 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000125 return kCannotFindProjectRootDir;
126 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000127 if (relative_dir_path_set) {
128 path = path + kPathDelimiter + relative_dir_path;
129 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000130 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000131 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
132 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000133 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
134 if (FileExists(root_filename)) {
135 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000136 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000137 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000138 path = path.substr(0, path_delimiter_index);
139 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000140 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000141 // Reached the root directory.
142 fprintf(stderr, "Cannot find project root directory!\n");
143 return kCannotFindProjectRootDir;
144}
145
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000146std::string OutputPath() {
kjellander02060002016-02-16 22:06:12 -0800147#if defined(WEBRTC_IOS)
148 return IOSOutputPath();
149#else
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000150 std::string path = ProjectRootPath();
151 if (path == kCannotFindProjectRootDir) {
152 return kFallbackPath;
153 }
154 path += kOutputDirName;
155 if (!CreateDir(path)) {
156 return kFallbackPath;
157 }
158 return path + kPathDelimiter;
kjellander02060002016-02-16 22:06:12 -0800159#endif
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000160}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000161
162std::string WorkingDir() {
163 char path_buffer[FILENAME_MAX];
164 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
165 fprintf(stderr, "Cannot get current directory!\n");
166 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000167 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000168 return std::string(path_buffer);
169 }
170}
171
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000172#endif // !WEBRTC_ANDROID
173
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000174// Generate a temporary filename in a safe way.
175// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
176std::string TempFilename(const std::string &dir, const std::string &prefix) {
177#ifdef WIN32
178 wchar_t filename[MAX_PATH];
179 if (::GetTempFileName(ToUtf16(dir).c_str(),
180 ToUtf16(prefix).c_str(), 0, filename) != 0)
181 return ToUtf8(filename);
182 assert(false);
183 return "";
184#else
185 int len = dir.size() + prefix.size() + 2 + 6;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000186 rtc::scoped_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000187
188 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
189 prefix.c_str());
190 int fd = ::mkstemp(tempname.get());
191 if (fd == -1) {
192 assert(false);
193 return "";
194 } else {
195 ::close(fd);
196 }
197 std::string ret(tempname.get());
198 return ret;
199#endif
200}
201
202bool CreateDir(std::string directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000203 struct stat path_info = {0};
204 // Check if the path exists already:
205 if (stat(directory_name.c_str(), &path_info) == 0) {
206 if (!S_ISDIR(path_info.st_mode)) {
207 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
208 "file and re-run to create the directory.\n",
209 directory_name.c_str());
210 return false;
211 }
212 } else {
213#ifdef WIN32
214 return _mkdir(directory_name.c_str()) == 0;
215#else
216 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
217#endif
218 }
219 return true;
220}
221
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000222std::string ResourcePath(std::string name, std::string extension) {
henrika1d34fe92015-06-16 10:04:20 +0200223#if defined(WEBRTC_IOS)
224 return IOSResourcePath(name, extension);
225#else
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000226 std::string platform = "win";
227#ifdef WEBRTC_LINUX
228 platform = "linux";
229#endif // WEBRTC_LINUX
230#ifdef WEBRTC_MAC
231 platform = "mac";
232#endif // WEBRTC_MAC
ivoc72c08ed2016-01-20 07:26:24 -0800233#ifdef WEBRTC_ANDROID
234 platform = "android";
235#endif // WEBRTC_ANDROID
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000236
237#ifdef WEBRTC_ARCH_64_BITS
238 std::string architecture = "64";
239#else
240 std::string architecture = "32";
241#endif // WEBRTC_ARCH_64_BITS
242
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000243 std::string resources_path = ProjectRootPath() + kResourcesDirName +
244 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000245 std::string resource_file = resources_path + name + "_" + platform + "_" +
246 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000247 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000248 return resource_file;
249 }
250 // Try without architecture.
251 resource_file = resources_path + name + "_" + platform + "." + extension;
252 if (FileExists(resource_file)) {
253 return resource_file;
254 }
255 // Try without platform.
256 resource_file = resources_path + name + "_" + architecture + "." + extension;
257 if (FileExists(resource_file)) {
258 return resource_file;
259 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000260
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000261 // Fall back on name without architecture or platform.
262 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200263#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000264}
265
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000266size_t GetFileSize(std::string filename) {
267 FILE* f = fopen(filename.c_str(), "rb");
268 size_t size = 0;
269 if (f != NULL) {
270 if (fseek(f, 0, SEEK_END) == 0) {
271 size = ftell(f);
272 }
273 fclose(f);
274 }
275 return size;
276}
277
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000278} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000279} // namespace webrtc