blob: 61e4bcf7bc068b7896be6b46f1beff740e9b27af [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>
15#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016#else
17#include <unistd.h>
18#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000019#endif
20
21#include <sys/stat.h> // To check for directory existence.
22#ifndef S_ISDIR // Not defined in stat.h on Windows.
23#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000024#endif
25
26#include <cstdio>
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000027#include <cstring>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000028
pbos@webrtc.org34741c82013-05-27 08:02:22 +000029#include "webrtc/typedefs.h" // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000030
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000031namespace webrtc {
32namespace test {
33
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000034#ifdef WIN32
35static const char* kPathDelimiter = "\\";
36#else
37static const char* kPathDelimiter = "/";
38#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000039
40#ifdef WEBRTC_ANDROID
41static const char* kRootDirName = "/sdcard/";
42static const char* kResourcesDirName = "resources";
43#else
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000044// The file we're looking for to identify the project root dir.
45static const char* kProjectRootFileName = "DEPS";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000046static const char* kOutputDirName = "out";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000047static const char* kFallbackPath = "./";
48static const char* kResourcesDirName = "resources";
leozwang@webrtc.orgfb594422012-06-29 18:28:12 +000049#endif
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000050const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
51
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000052namespace {
53char relative_dir_path[FILENAME_MAX];
54bool relative_dir_path_set = false;
55}
56
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000057void SetExecutablePath(const std::string& path) {
58 std::string working_dir = WorkingDir();
59 std::string temp_path = path;
60
61 // Handle absolute paths; convert them to relative paths to the working dir.
62 if (path.find(working_dir) != std::string::npos) {
63 temp_path = path.substr(working_dir.length() + 1);
64 }
65 // Trim away the executable name; only store the relative dir path.
66 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000067 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
68 relative_dir_path_set = true;
69}
70
71bool FileExists(std::string& file_name) {
72 struct stat file_info = {0};
73 return stat(file_name.c_str(), &file_info) == 0;
74}
75
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000076#ifdef WEBRTC_ANDROID
77
78std::string ProjectRootPath() {
79 return kRootDirName;
80}
81
82std::string OutputPath() {
83 return kRootDirName;
84}
85
86std::string WorkingDir() {
87 return kRootDirName;
88}
89
90#else // WEBRTC_ANDROID
91
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000092std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000093 std::string path = WorkingDir();
94 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000095 return kCannotFindProjectRootDir;
96 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000097 if (relative_dir_path_set) {
98 path = path + kPathDelimiter + relative_dir_path;
99 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000100 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000101 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
102 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000103 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
104 if (FileExists(root_filename)) {
105 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000106 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000107 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000108 path = path.substr(0, path_delimiter_index);
109 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000110 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000111 // Reached the root directory.
112 fprintf(stderr, "Cannot find project root directory!\n");
113 return kCannotFindProjectRootDir;
114}
115
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000116std::string OutputPath() {
117 std::string path = ProjectRootPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000118 if (path == kCannotFindProjectRootDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000119 return kFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000120 }
121 path += kOutputDirName;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000122 if (!CreateDirectory(path)) {
123 return kFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000124 }
125 return path + kPathDelimiter;
126}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000127
128std::string WorkingDir() {
129 char path_buffer[FILENAME_MAX];
130 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
131 fprintf(stderr, "Cannot get current directory!\n");
132 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000133 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000134 return std::string(path_buffer);
135 }
136}
137
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000138#endif // !WEBRTC_ANDROID
139
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000140bool CreateDirectory(std::string directory_name) {
141 struct stat path_info = {0};
142 // Check if the path exists already:
143 if (stat(directory_name.c_str(), &path_info) == 0) {
144 if (!S_ISDIR(path_info.st_mode)) {
145 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
146 "file and re-run to create the directory.\n",
147 directory_name.c_str());
148 return false;
149 }
150 } else {
151#ifdef WIN32
152 return _mkdir(directory_name.c_str()) == 0;
153#else
154 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
155#endif
156 }
157 return true;
158}
159
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000160std::string ResourcePath(std::string name, std::string extension) {
161 std::string platform = "win";
162#ifdef WEBRTC_LINUX
163 platform = "linux";
164#endif // WEBRTC_LINUX
165#ifdef WEBRTC_MAC
166 platform = "mac";
167#endif // WEBRTC_MAC
168
169#ifdef WEBRTC_ARCH_64_BITS
170 std::string architecture = "64";
171#else
172 std::string architecture = "32";
173#endif // WEBRTC_ARCH_64_BITS
174
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000175 std::string resources_path = ProjectRootPath() + kResourcesDirName +
176 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000177 std::string resource_file = resources_path + name + "_" + platform + "_" +
178 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000179 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000180 return resource_file;
181 }
182 // Try without architecture.
183 resource_file = resources_path + name + "_" + platform + "." + extension;
184 if (FileExists(resource_file)) {
185 return resource_file;
186 }
187 // Try without platform.
188 resource_file = resources_path + name + "_" + architecture + "." + extension;
189 if (FileExists(resource_file)) {
190 return resource_file;
191 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000192
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000193 // Fall back on name without architecture or platform.
194 return resources_path + name + "." + extension;
195}
196
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000197size_t GetFileSize(std::string filename) {
198 FILE* f = fopen(filename.c_str(), "rb");
199 size_t size = 0;
200 if (f != NULL) {
201 if (fseek(f, 0, SEEK_END) == 0) {
202 size = ftell(f);
203 }
204 fclose(f);
205 }
206 return size;
207}
208
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000209} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000210} // namespace webrtc