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

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

Issue 2898753002: ReadDirectory() added in webrtc/test/testsupport/fileutils.h (Closed)
Patch Set: add trailing sep char to dir paths Created 3 years, 7 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
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 } 121 }
120 122
121 std::string OutputPath() { 123 std::string OutputPath() {
122 return kRootDirName; 124 return kRootDirName;
123 } 125 }
124 126
125 std::string WorkingDir() { 127 std::string WorkingDir() {
126 return kRootDirName; 128 return kRootDirName;
127 } 129 }
128 130
129 #else // WEBRTC_ANDROID 131 #else // WEBRTC_ANDROID
130 132
131 std::string ProjectRootPath() { 133 std::string ProjectRootPath() {
132 #if defined(WEBRTC_IOS) 134 #if defined(WEBRTC_IOS)
133 return IOSRootPath(); 135 return IOSRootPath();
134 #else 136 #else
135 std::string path = WorkingDir(); 137 std::string path = WorkingDir();
136 if (path == kFallbackPath) { 138 if (path == kFallbackPath) {
137 return kCannotFindProjectRootDir; 139 return kCannotFindProjectRootDir;
138 } 140 }
139 if (relative_dir_path_set) { 141 if (relative_dir_path_set) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 assert(false); 207 assert(false);
206 return ""; 208 return "";
207 } else { 209 } else {
208 ::close(fd); 210 ::close(fd);
209 } 211 }
210 std::string ret(tempname.get()); 212 std::string ret(tempname.get());
211 return ret; 213 return ret;
212 #endif 214 #endif
213 } 215 }
214 216
217 bool ReadDirectory(std::string path, std::vector<std::string>* output) {
218 RTC_DCHECK(output);
219 if (!DirExists(path)) { return false; }
nisse-webrtc 2017/05/29 07:20:40 I'm not sure what DirExists return for an empty st
AleBzk 2017/05/29 09:50:51 Done.
220 output->clear();
221 #if defined(WEBRTC_WIN)
222 // Append separator character if needed.
223 size_t path_len = path.length();
224 if (path[path_len - 1] != '\\') { path += '\\'; }
nisse-webrtc 2017/05/29 07:20:40 Use path.back() instead, and delete the |path_len|
AleBzk 2017/05/29 09:50:51 Done.
225
226 // Init.
227 WIN32_FIND_DATA data;
228 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
229 if (handle == INVALID_HANDLE_VALUE) { return false; }
230
231 // Populate output.
232 do {
233 const std::string name = rtc::ToUtf8(data.cFileName);
234 if (name != "." && name != "..") { output->emplace_back(path + name); }
235 } while (::FindNextFile(handle, &data) == TRUE);
236
237 // Release resources.
238 if (handle != INVALID_HANDLE_VALUE) { ::FindClose(handle); }
239 #else
240 // Append separator character if needed.
241 size_t path_len = path.length();
242 if (path[path_len - 1] != '/') { path += '/'; }
243
244 // Init.
245 DIR *dir = ::opendir(path.c_str());
246 if (dir == nullptr) { return false; }
247 struct dirent* dirent = readdir(dir);
248 if (dirent == nullptr) { return false; }
249 struct stat stat;
250 if (::stat(std::string(path + dirent->d_name).c_str(), &stat) != 0) {
nisse-webrtc 2017/05/29 07:20:40 Unclear to me why you call stat (it would make sen
AleBzk 2017/05/29 09:50:51 Right! I just copied and adapted the code from web
251 return false;
252 }
253
254 // Populate output.
255 do {
256 const std::string& name = dirent->d_name;
257 if (name != "." && name != "..") { output->emplace_back(path + name); }
258 dirent = ::readdir(dir);
259 if (dirent == nullptr) { break; }
260 } while (::stat(std::string(path + dirent->d_name).c_str(), &stat) == 0);
261
262 // Release resources.
263 closedir(dir);
264 #endif
265 return true;
266 }
267
215 bool CreateDir(const std::string& directory_name) { 268 bool CreateDir(const std::string& directory_name) {
216 struct stat path_info = {0}; 269 struct stat path_info = {0};
217 // Check if the path exists already: 270 // Check if the path exists already:
218 if (stat(directory_name.c_str(), &path_info) == 0) { 271 if (stat(directory_name.c_str(), &path_info) == 0) {
219 if (!S_ISDIR(path_info.st_mode)) { 272 if (!S_ISDIR(path_info.st_mode)) {
220 fprintf(stderr, "Path %s exists but is not a directory! Remove this " 273 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
221 "file and re-run to create the directory.\n", 274 "file and re-run to create the directory.\n",
222 directory_name.c_str()); 275 directory_name.c_str());
223 return false; 276 return false;
224 } 277 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 if (fseek(f, 0, SEEK_END) == 0) { 353 if (fseek(f, 0, SEEK_END) == 0) {
301 size = ftell(f); 354 size = ftell(f);
302 } 355 }
303 fclose(f); 356 fclose(f);
304 } 357 }
305 return size; 358 return size;
306 } 359 }
307 360
308 } // namespace test 361 } // namespace test
309 } // namespace webrtc 362 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698