OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/test/testsupport/fileutils.h" | 11 #include "webrtc/test/testsupport/fileutils.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 #ifdef WIN32 | 15 #ifdef WIN32 |
16 #include <direct.h> | 16 #include <direct.h> |
17 #include <tchar.h> | 17 #include <tchar.h> |
18 #include <windows.h> | 18 #include <windows.h> |
19 #include <algorithm> | 19 #include <algorithm> |
20 | 20 |
21 #include "Shlwapi.h" | 21 #include "Shlwapi.h" |
22 #include "WinDef.h" | 22 #include "WinDef.h" |
23 | 23 |
24 #include "webrtc/base/win32.h" | 24 #include "webrtc/base/win32.h" |
25 #define GET_CURRENT_DIR _getcwd | 25 #define GET_CURRENT_DIR _getcwd |
26 #else | 26 #else |
| 27 #include <dirent.h> |
27 #include <unistd.h> | 28 #include <unistd.h> |
28 | 29 |
29 #define GET_CURRENT_DIR getcwd | 30 #define GET_CURRENT_DIR getcwd |
30 #endif | 31 #endif |
31 | 32 |
32 #include <sys/stat.h> // To check for directory existence. | 33 #include <sys/stat.h> // To check for directory existence. |
33 #ifndef S_ISDIR // Not defined in stat.h on Windows. | 34 #ifndef S_ISDIR // Not defined in stat.h on Windows. |
34 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) | 35 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
35 #endif | 36 #endif |
36 | 37 |
37 #include <stdio.h> | 38 #include <stdio.h> |
38 #include <stdlib.h> | 39 #include <stdlib.h> |
39 #include <string.h> | 40 #include <string.h> |
40 | 41 |
41 #include <memory> | 42 #include <memory> |
| 43 #include <utility> |
42 | 44 |
| 45 #include "webrtc/base/checks.h" |
43 #include "webrtc/typedefs.h" // For architecture defines | 46 #include "webrtc/typedefs.h" // For architecture defines |
44 | 47 |
45 namespace webrtc { | 48 namespace webrtc { |
46 namespace test { | 49 namespace test { |
47 | 50 |
48 #if defined(WEBRTC_IOS) | 51 #if defined(WEBRTC_IOS) |
49 // Defined in iosfileutils.mm. No header file to discourage use elsewhere. | 52 // Defined in iosfileutils.mm. No header file to discourage use elsewhere. |
50 std::string IOSOutputPath(); | 53 std::string IOSOutputPath(); |
51 std::string IOSRootPath(); | 54 std::string IOSRootPath(); |
52 std::string IOSResourcePath(std::string name, std::string extension); | 55 std::string IOSResourcePath(std::string name, std::string extension); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 122 } |
120 | 123 |
121 std::string OutputPath() { | 124 std::string OutputPath() { |
122 return kRootDirName; | 125 return kRootDirName; |
123 } | 126 } |
124 | 127 |
125 std::string WorkingDir() { | 128 std::string WorkingDir() { |
126 return kRootDirName; | 129 return kRootDirName; |
127 } | 130 } |
128 | 131 |
129 #else // WEBRTC_ANDROID | 132 #else // WEBRTC_ANDROID |
130 | 133 |
131 std::string ProjectRootPath() { | 134 std::string ProjectRootPath() { |
132 #if defined(WEBRTC_IOS) | 135 #if defined(WEBRTC_IOS) |
133 return IOSRootPath(); | 136 return IOSRootPath(); |
134 #else | 137 #else |
135 std::string path = WorkingDir(); | 138 std::string path = WorkingDir(); |
136 if (path == kFallbackPath) { | 139 if (path == kFallbackPath) { |
137 return kCannotFindProjectRootDir; | 140 return kCannotFindProjectRootDir; |
138 } | 141 } |
139 if (relative_dir_path_set) { | 142 if (relative_dir_path_set) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 assert(false); | 208 assert(false); |
206 return ""; | 209 return ""; |
207 } else { | 210 } else { |
208 ::close(fd); | 211 ::close(fd); |
209 } | 212 } |
210 std::string ret(tempname.get()); | 213 std::string ret(tempname.get()); |
211 return ret; | 214 return ret; |
212 #endif | 215 #endif |
213 } | 216 } |
214 | 217 |
| 218 rtc::Optional<std::vector<std::string>> ReadDirectory(std::string path) { |
| 219 if (path.length() == 0) |
| 220 return rtc::Optional<std::vector<std::string>>(); |
| 221 |
| 222 #if defined(WEBRTC_WIN) |
| 223 // Append separator character if needed. |
| 224 if (path.back() != '\\') |
| 225 path += '\\'; |
| 226 |
| 227 // Init. |
| 228 WIN32_FIND_DATA data; |
| 229 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data); |
| 230 if (handle == INVALID_HANDLE_VALUE) |
| 231 return rtc::Optional<std::vector<std::string>>(); |
| 232 |
| 233 // Populate output. |
| 234 std::vector<std::string> found_entries; |
| 235 do { |
| 236 const std::string name = rtc::ToUtf8(data.cFileName); |
| 237 if (name != "." && name != "..") |
| 238 found_entries.emplace_back(path + name); |
| 239 } while (::FindNextFile(handle, &data) == TRUE); |
| 240 |
| 241 // Release resources. |
| 242 if (handle != INVALID_HANDLE_VALUE) |
| 243 ::FindClose(handle); |
| 244 #else |
| 245 // Append separator character if needed. |
| 246 if (path.back() != '/') |
| 247 path += '/'; |
| 248 |
| 249 // Init. |
| 250 DIR* dir = ::opendir(path.c_str()); |
| 251 if (dir == nullptr) |
| 252 return rtc::Optional<std::vector<std::string>>(); |
| 253 |
| 254 // Populate output. |
| 255 std::vector<std::string> found_entries; |
| 256 while (dirent* dirent = readdir(dir)) { |
| 257 const std::string& name = dirent->d_name; |
| 258 if (name != "." && name != "..") |
| 259 found_entries.emplace_back(path + name); |
| 260 } |
| 261 |
| 262 // Release resources. |
| 263 closedir(dir); |
| 264 #endif |
| 265 |
| 266 return rtc::Optional<std::vector<std::string>>(std::move(found_entries)); |
| 267 } |
| 268 |
215 bool CreateDir(const std::string& directory_name) { | 269 bool CreateDir(const std::string& directory_name) { |
216 struct stat path_info = {0}; | 270 struct stat path_info = {0}; |
217 // Check if the path exists already: | 271 // Check if the path exists already: |
218 if (stat(directory_name.c_str(), &path_info) == 0) { | 272 if (stat(directory_name.c_str(), &path_info) == 0) { |
219 if (!S_ISDIR(path_info.st_mode)) { | 273 if (!S_ISDIR(path_info.st_mode)) { |
220 fprintf(stderr, "Path %s exists but is not a directory! Remove this " | 274 fprintf(stderr, "Path %s exists but is not a directory! Remove this " |
221 "file and re-run to create the directory.\n", | 275 "file and re-run to create the directory.\n", |
222 directory_name.c_str()); | 276 directory_name.c_str()); |
223 return false; | 277 return false; |
224 } | 278 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 if (fseek(f, 0, SEEK_END) == 0) { | 354 if (fseek(f, 0, SEEK_END) == 0) { |
301 size = ftell(f); | 355 size = ftell(f); |
302 } | 356 } |
303 fclose(f); | 357 fclose(f); |
304 } | 358 } |
305 return size; | 359 return size; |
306 } | 360 } |
307 | 361 |
308 } // namespace test | 362 } // namespace test |
309 } // namespace webrtc | 363 } // namespace webrtc |
OLD | NEW |