BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / test / fileutils.cc

fileutils.cc

fileutils.cc 5.6 KiB Download Raw
1/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
11#include "fileutils.h"
12
13#ifdef WIN32
14#include <direct.h>
15#define GET_CURRENT_DIR _getcwd
16#else
17#include <unistd.h>
18#define GET_CURRENT_DIR getcwd
19#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)
24#endif
25
26#include <cstdio>
27#include <cstring>
28
29
30namespace webrtc {
31namespace test {
32
33#ifdef WIN32
34static const char* kPathDelimiter = "\\";
35#else
36static const char* kPathDelimiter = "/";
37#endif
38
39#ifdef WEBRTC_ANDROID
40static const char* kRootDirName = "/sdcard/";
41static const char* kResourcesDirName = "resources";
42#else
43// The file we're looking for to identify the project root dir.
44static const char* kProjectRootFileName = "h264_benchmark";
45static const char* kOutputDirName = "out";
46static const char* kFallbackPath = "./";
47static const char* kResourcesDirName = "resources";
48#endif
49const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
50
51namespace {
52char relative_dir_path[FILENAME_MAX];
53bool relative_dir_path_set = false;
54}
55
56void SetExecutablePath(const std::string& path) {
57 std::string working_dir = WorkingDir();
58 std::string temp_path = path;
59
60 // Handle absolute paths; convert them to relative paths to the working dir.
61 if (path.find(working_dir) != std::string::npos) {
62 temp_path = path.substr(working_dir.length() + 1);
63 }
64 // Trim away the executable name; only store the relative dir path.
65 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
66 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
67 relative_dir_path_set = true;
68}
69
70bool FileExists(std::string& file_name) {
71 struct stat file_info = {0};
72 return stat(file_name.c_str(), &file_info) == 0;
73}
74
75#ifdef WEBRTC_ANDROID
76
77std::string ProjectRootPath() {
78 return kRootDirName;
79}
80
81std::string OutputPath() {
82 return kRootDirName;
83}
84
85std::string WorkingDir() {
86 return kRootDirName;
87}
88
89#else // WEBRTC_ANDROID
90
91std::string ProjectRootPath() {
92 std::string path = WorkingDir();
93 if (path == kFallbackPath) {
94 return kCannotFindProjectRootDir;
95 }
96 if (relative_dir_path_set) {
97 path = path + kPathDelimiter + relative_dir_path;
98 }
99 // Check for our file that verifies the root dir.
100 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
101 while (path_delimiter_index != std::string::npos) {
102 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
103 if (FileExists(root_filename)) {
104 return path + kPathDelimiter;
105 }
106 // Move up one directory in the directory tree.
107 path = path.substr(0, path_delimiter_index);
108 path_delimiter_index = path.find_last_of(kPathDelimiter);
109 }
110 // Reached the root directory.
111 fprintf(stderr, "Cannot find project root directory!\n");
112 return kCannotFindProjectRootDir;
113}
114
115std::string OutputPath() {
116 std::string path = ProjectRootPath();
117 if (path == kCannotFindProjectRootDir) {
118 return kFallbackPath;
119 }
120 path += kOutputDirName;
121 if (!CreateDirectory(path)) {
122 return kFallbackPath;
123 }
124 return path + kPathDelimiter;
125}
126
127std::string WorkingDir() {
128 char path_buffer[FILENAME_MAX];
129 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
130 fprintf(stderr, "Cannot get current directory!\n");
131 return kFallbackPath;
132 } else {
133 return std::string(path_buffer);
134 }
135}
136
137#endif // !WEBRTC_ANDROID
138
139bool CreateDirectory(std::string directory_name) {
140 struct stat path_info = {0};
141 // Check if the path exists already:
142 if (stat(directory_name.c_str(), &path_info) == 0) {
143 if (!S_ISDIR(path_info.st_mode)) {
144 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
145 "file and re-run to create the directory.\n",
146 directory_name.c_str());
147 return false;
148 }
149 } else {
150#ifdef WIN32
151 return _mkdir(directory_name.c_str()) == 0;
152#else
153 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
154#endif
155 }
156 return true;
157}
158
159std::string ResourcePath(std::string name, std::string extension) {
160 std::string platform = "win";
161#ifdef WEBRTC_LINUX
162 platform = "linux";
163#endif // WEBRTC_LINUX
164#ifdef WEBRTC_MAC
165 platform = "mac";
166#endif // WEBRTC_MAC
167
168#ifdef WEBRTC_ARCH_64_BITS
169 std::string architecture = "64";
170#else
171 std::string architecture = "32";
172#endif // WEBRTC_ARCH_64_BITS
173
174 std::string resources_path = ProjectRootPath() + kResourcesDirName +
175 kPathDelimiter;
176 std::string resource_file = resources_path + name + "_" + platform + "_" +
177 architecture + "." + extension;
178 if (FileExists(resource_file)) {
179 return resource_file;
180 }
181 // Try without architecture.
182 resource_file = resources_path + name + "_" + platform + "." + extension;
183 if (FileExists(resource_file)) {
184 return resource_file;
185 }
186 // Try without platform.
187 resource_file = resources_path + name + "_" + architecture + "." + extension;
188 if (FileExists(resource_file)) {
189 return resource_file;
190 }
191
192 // Fall back on name without architecture or platform.
193 return resources_path + name + "." + extension;
194}
195
196size_t GetFileSize(std::string filename) {
197 FILE* f = fopen(filename.c_str(), "rb");
198 size_t size = 0;
199 if (f != NULL) {
200 if (fseek(f, 0, SEEK_END) == 0) {
201 size = ftell(f);
202 }
203 fclose(f);
204 }
205 return size;
206}
207
208} // namespace test
209} // namespace webrtc