blob: ebb64e21b93ed6f935f58c6c992490a3f1cd9071 [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
andrew@webrtc.org7a281a52012-06-27 03:22:37 +000011#include "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
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000029#include "typedefs.h" // For architecture defines
30
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
57void SetRelativeExecutablePath(const std::string& path) {
58 // Trim away the executable name; we only want to store the relative dir path.
59 std::string temp_path = path.substr(0, path.find_last_of(kPathDelimiter));
60 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
61 relative_dir_path_set = true;
62}
63
64bool FileExists(std::string& file_name) {
65 struct stat file_info = {0};
66 return stat(file_name.c_str(), &file_info) == 0;
67}
68
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000069#ifdef WEBRTC_ANDROID
70
71std::string ProjectRootPath() {
72 return kRootDirName;
73}
74
75std::string OutputPath() {
76 return kRootDirName;
77}
78
79std::string WorkingDir() {
80 return kRootDirName;
81}
82
83#else // WEBRTC_ANDROID
84
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000085std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000086 std::string path = WorkingDir();
87 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000088 return kCannotFindProjectRootDir;
89 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000090 if (relative_dir_path_set) {
91 path = path + kPathDelimiter + relative_dir_path;
92 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000093 // Check for our file that verifies the root dir.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000094 int path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000095 while (path_delimiter_index > -1) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000096 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
97 if (FileExists(root_filename)) {
98 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000099 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000100 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000101 path = path.substr(0, path_delimiter_index);
102 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000103 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000104 // Reached the root directory.
105 fprintf(stderr, "Cannot find project root directory!\n");
106 return kCannotFindProjectRootDir;
107}
108
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000109std::string OutputPath() {
110 std::string path = ProjectRootPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000111 if (path == kCannotFindProjectRootDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000112 return kFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000113 }
114 path += kOutputDirName;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000115 if (!CreateDirectory(path)) {
116 return kFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000117 }
118 return path + kPathDelimiter;
119}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000120
121std::string WorkingDir() {
122 char path_buffer[FILENAME_MAX];
123 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
124 fprintf(stderr, "Cannot get current directory!\n");
125 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000126 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000127 return std::string(path_buffer);
128 }
129}
130
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000131#endif // !WEBRTC_ANDROID
132
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000133bool CreateDirectory(std::string directory_name) {
134 struct stat path_info = {0};
135 // Check if the path exists already:
136 if (stat(directory_name.c_str(), &path_info) == 0) {
137 if (!S_ISDIR(path_info.st_mode)) {
138 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
139 "file and re-run to create the directory.\n",
140 directory_name.c_str());
141 return false;
142 }
143 } else {
144#ifdef WIN32
145 return _mkdir(directory_name.c_str()) == 0;
146#else
147 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
148#endif
149 }
150 return true;
151}
152
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000153std::string ResourcePath(std::string name, std::string extension) {
154 std::string platform = "win";
155#ifdef WEBRTC_LINUX
156 platform = "linux";
157#endif // WEBRTC_LINUX
158#ifdef WEBRTC_MAC
159 platform = "mac";
160#endif // WEBRTC_MAC
161
162#ifdef WEBRTC_ARCH_64_BITS
163 std::string architecture = "64";
164#else
165 std::string architecture = "32";
166#endif // WEBRTC_ARCH_64_BITS
167
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000168 std::string resources_path = ProjectRootPath() + kResourcesDirName +
169 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000170 std::string resource_file = resources_path + name + "_" + platform + "_" +
171 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000172 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000173 return resource_file;
174 }
175 // Try without architecture.
176 resource_file = resources_path + name + "_" + platform + "." + extension;
177 if (FileExists(resource_file)) {
178 return resource_file;
179 }
180 // Try without platform.
181 resource_file = resources_path + name + "_" + architecture + "." + extension;
182 if (FileExists(resource_file)) {
183 return resource_file;
184 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000185
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000186 // Fall back on name without architecture or platform.
187 return resources_path + name + "." + extension;
188}
189
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000190size_t GetFileSize(std::string filename) {
191 FILE* f = fopen(filename.c_str(), "rb");
192 size_t size = 0;
193 if (f != NULL) {
194 if (fseek(f, 0, SEEK_END) == 0) {
195 size = ftell(f);
196 }
197 fclose(f);
198 }
199 return size;
200}
201
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000202} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000203} // namespace webrtc