blob: d99b990918cab464879f313b165f68d9070b447a [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
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000026#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000027#endif
28
29#include <sys/stat.h> // To check for directory existence.
30#ifndef S_ISDIR // Not defined in stat.h on Windows.
31#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000032#endif
33
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000034#include <stdio.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000035#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000036#include <string.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000037
kwibergbfefb032016-05-01 14:53:46 -070038#include <memory>
39
pbos@webrtc.org34741c82013-05-27 08:02:22 +000040#include "webrtc/typedefs.h" // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000041
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000042namespace webrtc {
43namespace test {
44
henrika1d34fe92015-06-16 10:04:20 +020045#if defined(WEBRTC_IOS)
46// Defined in iosfileutils.mm. No header file to discourage use elsewhere.
kjellander02060002016-02-16 22:06:12 -080047std::string IOSOutputPath();
henrika1d34fe92015-06-16 10:04:20 +020048std::string IOSResourcePath(std::string name, std::string extension);
49#endif
50
henrike@webrtc.org34773d92013-07-08 14:55:23 +000051namespace {
52
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000053#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000054const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000055#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000056const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000057#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000058
59#ifdef WEBRTC_ANDROID
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000060const char* kRootDirName = "/sdcard/";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000061#else
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000062// The file we're looking for to identify the project root dir.
henrike@webrtc.org34773d92013-07-08 14:55:23 +000063const char* kProjectRootFileName = "DEPS";
kjellander02060002016-02-16 22:06:12 -080064#if !defined(WEBRTC_IOS)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000065const char* kOutputDirName = "out";
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000066#endif
kjellander02060002016-02-16 22:06:12 -080067const char* kFallbackPath = "./";
68#endif // !defined(WEBRTC_ANDROID)
69
henrika1d34fe92015-06-16 10:04:20 +020070#if !defined(WEBRTC_IOS)
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000071const char* kResourcesDirName = "resources";
henrika1d34fe92015-06-16 10:04:20 +020072#endif
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000073
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000074char relative_dir_path[FILENAME_MAX];
75bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000076
77} // namespace
78
79const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
80
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000081void SetExecutablePath(const std::string& path) {
82 std::string working_dir = WorkingDir();
83 std::string temp_path = path;
84
85 // Handle absolute paths; convert them to relative paths to the working dir.
86 if (path.find(working_dir) != std::string::npos) {
87 temp_path = path.substr(working_dir.length() + 1);
88 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000089 // On Windows, when tests are run under memory tools like DrMemory and TSan,
90 // slashes occur in the path as directory separators. Make sure we replace
91 // such cases with backslashes in order for the paths to be correct.
92#ifdef WIN32
93 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
94#endif
95
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000096 // Trim away the executable name; only store the relative dir path.
97 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000098 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
99 relative_dir_path_set = true;
100}
101
102bool FileExists(std::string& file_name) {
103 struct stat file_info = {0};
104 return stat(file_name.c_str(), &file_info) == 0;
105}
106
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000107#ifdef WEBRTC_ANDROID
108
109std::string ProjectRootPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000110 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000111}
112
113std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000114 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000115}
116
117std::string WorkingDir() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000118 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000119}
120
121#else // WEBRTC_ANDROID
122
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000123std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000124 std::string path = WorkingDir();
125 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000126 return kCannotFindProjectRootDir;
127 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000128 if (relative_dir_path_set) {
129 path = path + kPathDelimiter + relative_dir_path;
130 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000131 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000132 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
133 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000134 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
135 if (FileExists(root_filename)) {
136 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000137 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000138 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000139 path = path.substr(0, path_delimiter_index);
140 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000141 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000142 // Reached the root directory.
143 fprintf(stderr, "Cannot find project root directory!\n");
144 return kCannotFindProjectRootDir;
145}
146
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000147std::string OutputPath() {
kjellander02060002016-02-16 22:06:12 -0800148#if defined(WEBRTC_IOS)
149 return IOSOutputPath();
150#else
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000151 std::string path = ProjectRootPath();
152 if (path == kCannotFindProjectRootDir) {
153 return kFallbackPath;
154 }
155 path += kOutputDirName;
156 if (!CreateDir(path)) {
157 return kFallbackPath;
158 }
159 return path + kPathDelimiter;
kjellander02060002016-02-16 22:06:12 -0800160#endif
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000161}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000162
163std::string WorkingDir() {
164 char path_buffer[FILENAME_MAX];
165 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
166 fprintf(stderr, "Cannot get current directory!\n");
167 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000168 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000169 return std::string(path_buffer);
170 }
171}
172
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000173#endif // !WEBRTC_ANDROID
174
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000175// Generate a temporary filename in a safe way.
176// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
177std::string TempFilename(const std::string &dir, const std::string &prefix) {
178#ifdef WIN32
179 wchar_t filename[MAX_PATH];
180 if (::GetTempFileName(ToUtf16(dir).c_str(),
181 ToUtf16(prefix).c_str(), 0, filename) != 0)
182 return ToUtf8(filename);
183 assert(false);
184 return "";
185#else
186 int len = dir.size() + prefix.size() + 2 + 6;
kwibergbfefb032016-05-01 14:53:46 -0700187 std::unique_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000188
189 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
190 prefix.c_str());
191 int fd = ::mkstemp(tempname.get());
192 if (fd == -1) {
193 assert(false);
194 return "";
195 } else {
196 ::close(fd);
197 }
198 std::string ret(tempname.get());
199 return ret;
200#endif
201}
202
203bool CreateDir(std::string directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000204 struct stat path_info = {0};
205 // Check if the path exists already:
206 if (stat(directory_name.c_str(), &path_info) == 0) {
207 if (!S_ISDIR(path_info.st_mode)) {
208 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
209 "file and re-run to create the directory.\n",
210 directory_name.c_str());
211 return false;
212 }
213 } else {
214#ifdef WIN32
215 return _mkdir(directory_name.c_str()) == 0;
216#else
217 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
218#endif
219 }
220 return true;
221}
222
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000223std::string ResourcePath(std::string name, std::string extension) {
henrika1d34fe92015-06-16 10:04:20 +0200224#if defined(WEBRTC_IOS)
225 return IOSResourcePath(name, extension);
226#else
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000227 std::string platform = "win";
228#ifdef WEBRTC_LINUX
229 platform = "linux";
230#endif // WEBRTC_LINUX
231#ifdef WEBRTC_MAC
232 platform = "mac";
233#endif // WEBRTC_MAC
ivoc72c08ed2016-01-20 07:26:24 -0800234#ifdef WEBRTC_ANDROID
235 platform = "android";
236#endif // WEBRTC_ANDROID
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000237
238#ifdef WEBRTC_ARCH_64_BITS
239 std::string architecture = "64";
240#else
241 std::string architecture = "32";
242#endif // WEBRTC_ARCH_64_BITS
243
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000244 std::string resources_path = ProjectRootPath() + kResourcesDirName +
245 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000246 std::string resource_file = resources_path + name + "_" + platform + "_" +
247 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000248 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000249 return resource_file;
250 }
251 // Try without architecture.
252 resource_file = resources_path + name + "_" + platform + "." + extension;
253 if (FileExists(resource_file)) {
254 return resource_file;
255 }
256 // Try without platform.
257 resource_file = resources_path + name + "_" + architecture + "." + extension;
258 if (FileExists(resource_file)) {
259 return resource_file;
260 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000261
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000262 // Fall back on name without architecture or platform.
263 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200264#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000265}
266
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000267size_t GetFileSize(std::string filename) {
268 FILE* f = fopen(filename.c_str(), "rb");
269 size_t size = 0;
270 if (f != NULL) {
271 if (fseek(f, 0, SEEK_END) == 0) {
272 size = ftell(f);
273 }
274 fclose(f);
275 }
276 return size;
277}
278
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000279} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000280} // namespace webrtc