blob: a3e66201fa5097d05a08514036a9530fadb6c107 [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
henrike@webrtc.org34773d92013-07-08 14:55:23 +000053const char* kResourcesDirName = "resources";
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";
57const char* kResourcesDirName = "resources";
leozwang@webrtc.orgfb594422012-06-29 18:28:12 +000058#endif
henrike@webrtc.orgcaf2fcc2013-07-05 04:15:38 +000059
henrike@webrtc.org34773d92013-07-08 14:55:23 +000060const char* kFallbackPath = "./";
61const char* kOutputDirName = "out";
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
69std::string OutputPathAndroid();
pbos@webrtc.orgfc496d92013-07-09 15:24:16 +000070std::string ProjectRootPathAndroid();
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000071
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000072void SetExecutablePath(const std::string& path) {
73 std::string working_dir = WorkingDir();
74 std::string temp_path = path;
75
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);
79 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000080 // 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.
83#ifdef WIN32
84 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
85#endif
86
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000087 // Trim away the executable name; only store the relative dir path.
88 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000089 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
90 relative_dir_path_set = true;
91}
92
93bool FileExists(std::string& file_name) {
94 struct stat file_info = {0};
95 return stat(file_name.c_str(), &file_info) == 0;
96}
97
henrike@webrtc.org34773d92013-07-08 14:55:23 +000098std::string OutputPathImpl() {
99 std::string path = ProjectRootPath();
100 if (path == kCannotFindProjectRootDir) {
101 return kFallbackPath;
102 }
103 path += kOutputDirName;
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000104 if (!CreateDir(path)) {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000105 return kFallbackPath;
106 }
107 return path + kPathDelimiter;
108}
109
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000110#ifdef WEBRTC_ANDROID
111
112std::string ProjectRootPath() {
pbos@webrtc.orgfc496d92013-07-09 15:24:16 +0000113 return ProjectRootPathAndroid();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000114}
115
116std::string OutputPath() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000117 return OutputPathAndroid();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000118}
119
120std::string WorkingDir() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000121 return ProjectRootPath();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000122}
123
124#else // WEBRTC_ANDROID
125
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000126std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000127 std::string path = WorkingDir();
128 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000129 return kCannotFindProjectRootDir;
130 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000131 if (relative_dir_path_set) {
132 path = path + kPathDelimiter + relative_dir_path;
133 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000134 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000135 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
136 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000137 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
138 if (FileExists(root_filename)) {
139 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000140 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000141 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000142 path = path.substr(0, path_delimiter_index);
143 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000144 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000145 // Reached the root directory.
146 fprintf(stderr, "Cannot find project root directory!\n");
147 return kCannotFindProjectRootDir;
148}
149
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000150std::string OutputPath() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000151 return OutputPathImpl();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000152}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000153
154std::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;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000159 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000160 return std::string(path_buffer);
161 }
162}
163
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000164#endif // !WEBRTC_ANDROID
165
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000166// Generate a temporary filename in a safe way.
167// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
168std::string TempFilename(const std::string &dir, const std::string &prefix) {
169#ifdef WIN32
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);
174 assert(false);
175 return "";
176#else
177 int len = dir.size() + prefix.size() + 2 + 6;
178 scoped_ptr<char[]> tempname(new char[len]);
179
180 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
181 prefix.c_str());
182 int fd = ::mkstemp(tempname.get());
183 if (fd == -1) {
184 assert(false);
185 return "";
186 } else {
187 ::close(fd);
188 }
189 std::string ret(tempname.get());
190 return ret;
191#endif
192}
193
194bool CreateDir(std::string directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000195 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());
202 return false;
203 }
204 } else {
205#ifdef WIN32
206 return _mkdir(directory_name.c_str()) == 0;
207#else
208 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
209#endif
210 }
211 return true;
212}
213
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000214std::string ResourcePath(std::string name, std::string extension) {
215 std::string platform = "win";
216#ifdef WEBRTC_LINUX
217 platform = "linux";
218#endif // WEBRTC_LINUX
219#ifdef WEBRTC_MAC
220 platform = "mac";
221#endif // WEBRTC_MAC
222
223#ifdef WEBRTC_ARCH_64_BITS
224 std::string architecture = "64";
225#else
226 std::string architecture = "32";
227#endif // WEBRTC_ARCH_64_BITS
228
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000229 std::string resources_path = ProjectRootPath() + kResourcesDirName +
230 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000231 std::string resource_file = resources_path + name + "_" + platform + "_" +
232 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000233 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000234 return resource_file;
235 }
236 // Try without architecture.
237 resource_file = resources_path + name + "_" + platform + "." + extension;
238 if (FileExists(resource_file)) {
239 return resource_file;
240 }
241 // Try without platform.
242 resource_file = resources_path + name + "_" + architecture + "." + extension;
243 if (FileExists(resource_file)) {
244 return resource_file;
245 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000246
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000247 // Fall back on name without architecture or platform.
248 return resources_path + name + "." + extension;
249}
250
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000251size_t GetFileSize(std::string filename) {
252 FILE* f = fopen(filename.c_str(), "rb");
253 size_t size = 0;
254 if (f != NULL) {
255 if (fseek(f, 0, SEEK_END) == 0) {
256 size = ftell(f);
257 }
258 fclose(f);
259 }
260 return size;
261}
262
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000263} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000264} // namespace webrtc