Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: webrtc/test/testsupport/fileutils.cc

Issue 2898753002: ReadDirectory() added in webrtc/test/testsupport/fileutils.h (Closed)
Patch Set: nits Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/test/testsupport/fileutils.h ('k') | webrtc/test/testsupport/fileutils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
42 43
44 #include "webrtc/base/checks.h"
43 #include "webrtc/typedefs.h" // For architecture defines 45 #include "webrtc/typedefs.h" // For architecture defines
44 46
45 namespace webrtc { 47 namespace webrtc {
46 namespace test { 48 namespace test {
47 49
48 #if defined(WEBRTC_IOS) 50 #if defined(WEBRTC_IOS)
49 // Defined in iosfileutils.mm. No header file to discourage use elsewhere. 51 // Defined in iosfileutils.mm. No header file to discourage use elsewhere.
50 std::string IOSOutputPath(); 52 std::string IOSOutputPath();
51 std::string IOSRootPath(); 53 std::string IOSRootPath();
52 std::string IOSResourcePath(std::string name, std::string extension); 54 std::string IOSResourcePath(std::string name, std::string extension);
(...skipping 16 matching lines...) Expand all
69 const char* kFallbackPath = "./"; 71 const char* kFallbackPath = "./";
70 #endif // !defined(WEBRTC_ANDROID) 72 #endif // !defined(WEBRTC_ANDROID)
71 73
72 #if !defined(WEBRTC_IOS) 74 #if !defined(WEBRTC_IOS)
73 const char* kResourcesDirName = "resources"; 75 const char* kResourcesDirName = "resources";
74 #endif 76 #endif
75 77
76 char relative_dir_path[FILENAME_MAX]; 78 char relative_dir_path[FILENAME_MAX];
77 bool relative_dir_path_set = false; 79 bool relative_dir_path_set = false;
78 80
81 #ifdef WIN32
82 rtc::Optional<std::vector<std::string>> ReadDirectoryWin(std::string path) {
83 // Append separator character if needed.
84 if (path.back() != '\\')
85 path += '\\';
86
87 // Init.
88 WIN32_FIND_DATA data;
89 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
90 if (handle == INVALID_HANDLE_VALUE)
91 return rtc::Optional<std::vector<std::string>>();
92
93 // Populate output.
94 std::vector<std::string> found_entries;
95 do {
96 const std::string name = rtc::ToUtf8(data.cFileName);
97 if (name != "." && name != "..")
98 found_entries.emplace_back(path + name);
99 } while (::FindNextFile(handle, &data) == TRUE);
100
101 // Release resources.
102 if (handle != INVALID_HANDLE_VALUE)
103 ::FindClose(handle);
104
105 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
106 }
107 #else
108 rtc::Optional<std::vector<std::string>> ReadDirectoryNotWin(std::string path) {
109 // Append separator character if needed.
110 if (path.back() != '/')
111 path += '/';
112
113 // Init.
114 DIR* dir = ::opendir(path.c_str());
115 if (dir == nullptr)
116 return rtc::Optional<std::vector<std::string>>();
117
118 // Populate output.
119 std::vector<std::string> found_entries;
120 while (dirent* dirent = readdir(dir)) {
121 const std::string& name = dirent->d_name;
122 if (name != "." && name != "..")
123 found_entries.emplace_back(path + name);
124 }
125
126 // Release resources.
127 closedir(dir);
128
129 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
130 }
131 #endif
132
79 } // namespace 133 } // namespace
80 134
81 const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; 135 const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
82 136
83 void SetExecutablePath(const std::string& path) { 137 void SetExecutablePath(const std::string& path) {
84 std::string working_dir = WorkingDir(); 138 std::string working_dir = WorkingDir();
85 std::string temp_path = path; 139 std::string temp_path = path;
86 140
87 // Handle absolute paths; convert them to relative paths to the working dir. 141 // Handle absolute paths; convert them to relative paths to the working dir.
88 if (path.find(working_dir) != std::string::npos) { 142 if (path.find(working_dir) != std::string::npos) {
(...skipping 30 matching lines...) Expand all
119 } 173 }
120 174
121 std::string OutputPath() { 175 std::string OutputPath() {
122 return kRootDirName; 176 return kRootDirName;
123 } 177 }
124 178
125 std::string WorkingDir() { 179 std::string WorkingDir() {
126 return kRootDirName; 180 return kRootDirName;
127 } 181 }
128 182
129 #else // WEBRTC_ANDROID 183 #else // WEBRTC_ANDROID
130 184
131 std::string ProjectRootPath() { 185 std::string ProjectRootPath() {
132 #if defined(WEBRTC_IOS) 186 #if defined(WEBRTC_IOS)
133 return IOSRootPath(); 187 return IOSRootPath();
134 #else 188 #else
135 std::string path = WorkingDir(); 189 std::string path = WorkingDir();
136 if (path == kFallbackPath) { 190 if (path == kFallbackPath) {
137 return kCannotFindProjectRootDir; 191 return kCannotFindProjectRootDir;
138 } 192 }
139 if (relative_dir_path_set) { 193 if (relative_dir_path_set) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 assert(false); 259 assert(false);
206 return ""; 260 return "";
207 } else { 261 } else {
208 ::close(fd); 262 ::close(fd);
209 } 263 }
210 std::string ret(tempname.get()); 264 std::string ret(tempname.get());
211 return ret; 265 return ret;
212 #endif 266 #endif
213 } 267 }
214 268
269 rtc::Optional<std::vector<std::string>> ReadDirectory(const std::string& path) {
270 if (path.length() == 0)
271 return rtc::Optional<std::vector<std::string>>();
272 #if defined(WEBRTC_WIN)
273 return ReadDirectoryWin(path);
nisse-webrtc 2017/05/30 12:18:53 To me, it was more readable with the windows and n
kwiberg-webrtc 2017/05/30 12:37:00 Really? I generally find that readability is hurt
nisse-webrtc 2017/05/30 12:55:25 At least in this case, when there's clearly two se
274 #else
275 return ReadDirectoryNotWin(path);
276 #endif
277 }
278
215 bool CreateDir(const std::string& directory_name) { 279 bool CreateDir(const std::string& directory_name) {
216 struct stat path_info = {0}; 280 struct stat path_info = {0};
217 // Check if the path exists already: 281 // Check if the path exists already:
218 if (stat(directory_name.c_str(), &path_info) == 0) { 282 if (stat(directory_name.c_str(), &path_info) == 0) {
219 if (!S_ISDIR(path_info.st_mode)) { 283 if (!S_ISDIR(path_info.st_mode)) {
220 fprintf(stderr, "Path %s exists but is not a directory! Remove this " 284 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
221 "file and re-run to create the directory.\n", 285 "file and re-run to create the directory.\n",
222 directory_name.c_str()); 286 directory_name.c_str());
223 return false; 287 return false;
224 } 288 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 if (fseek(f, 0, SEEK_END) == 0) { 364 if (fseek(f, 0, SEEK_END) == 0) {
301 size = ftell(f); 365 size = ftell(f);
302 } 366 }
303 fclose(f); 367 fclose(f);
304 } 368 }
305 return size; 369 return size;
306 } 370 }
307 371
308 } // namespace test 372 } // namespace test
309 } // namespace webrtc 373 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/test/testsupport/fileutils.h ('k') | webrtc/test/testsupport/fileutils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698