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

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

Issue 2898753002: ReadDirectory() added in webrtc/test/testsupport/fileutils.h (Closed)
Patch Set: comments from Karl addressed 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 if (path.length() == 0)
84 return rtc::Optional<std::vector<std::string>>();
kwiberg-webrtc 2017/05/30 08:28:12 This check is the same in both versions, so move t
AleBzk 2017/05/30 11:09:16 Done.
85
86 // Append separator character if needed.
87 if (path.back() != '\\')
88 path += '\\';
89
90 // Init.
91 WIN32_FIND_DATA data;
92 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
93 if (handle == INVALID_HANDLE_VALUE)
94 return rtc::Optional<std::vector<std::string>>();
95
96 // Populate output.
97 std::vector<std::string> found_entries;
98 do {
99 const std::string name = rtc::ToUtf8(data.cFileName);
100 if (name != "." && name != "..")
101 found_entries.emplace_back(path + name);
102 } while (::FindNextFile(handle, &data) == TRUE);
103
104 // Release resources.
105 if (handle != INVALID_HANDLE_VALUE)
106 ::FindClose(handle);
107
108 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
109 }
110 #else
111 rtc::Optional<std::vector<std::string>> ReadDirectoryNotWin(std::string path) {
112 if (path.length() == 0)
113 return rtc::Optional<std::vector<std::string>>();
114
115 // Append separator character if needed.
116 if (path.back() != '/')
117 path += '/';
118
119 // Init.
120 DIR* dir = ::opendir(path.c_str());
121 if (dir == nullptr)
122 return rtc::Optional<std::vector<std::string>>();
123
124 // Populate output.
125 std::vector<std::string> found_entries;
126 while (dirent* dirent = readdir(dir)) {
127 const std::string& name = dirent->d_name;
128 if (name != "." && name != "..")
129 found_entries.emplace_back(path + name);
130 }
131
132 // Release resources.
133 closedir(dir);
134
135 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
136 }
137 #endif
138
79 } // namespace 139 } // namespace
80 140
81 const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; 141 const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
82 142
83 void SetExecutablePath(const std::string& path) { 143 void SetExecutablePath(const std::string& path) {
84 std::string working_dir = WorkingDir(); 144 std::string working_dir = WorkingDir();
85 std::string temp_path = path; 145 std::string temp_path = path;
86 146
87 // Handle absolute paths; convert them to relative paths to the working dir. 147 // Handle absolute paths; convert them to relative paths to the working dir.
88 if (path.find(working_dir) != std::string::npos) { 148 if (path.find(working_dir) != std::string::npos) {
(...skipping 30 matching lines...) Expand all
119 } 179 }
120 180
121 std::string OutputPath() { 181 std::string OutputPath() {
122 return kRootDirName; 182 return kRootDirName;
123 } 183 }
124 184
125 std::string WorkingDir() { 185 std::string WorkingDir() {
126 return kRootDirName; 186 return kRootDirName;
127 } 187 }
128 188
129 #else // WEBRTC_ANDROID 189 #else // WEBRTC_ANDROID
130 190
131 std::string ProjectRootPath() { 191 std::string ProjectRootPath() {
132 #if defined(WEBRTC_IOS) 192 #if defined(WEBRTC_IOS)
133 return IOSRootPath(); 193 return IOSRootPath();
134 #else 194 #else
135 std::string path = WorkingDir(); 195 std::string path = WorkingDir();
136 if (path == kFallbackPath) { 196 if (path == kFallbackPath) {
137 return kCannotFindProjectRootDir; 197 return kCannotFindProjectRootDir;
138 } 198 }
139 if (relative_dir_path_set) { 199 if (relative_dir_path_set) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 assert(false); 265 assert(false);
206 return ""; 266 return "";
207 } else { 267 } else {
208 ::close(fd); 268 ::close(fd);
209 } 269 }
210 std::string ret(tempname.get()); 270 std::string ret(tempname.get());
211 return ret; 271 return ret;
212 #endif 272 #endif
213 } 273 }
214 274
275 rtc::Optional<std::vector<std::string>> ReadDirectory(const std::string& path) {
kwiberg-webrtc 2017/05/30 08:28:12 Wait, didn't you just explain to me why path shoul
AleBzk 2017/05/30 11:09:16 Just wanted to avoid an unnecessary extra copy. Th
276 #if defined(WEBRTC_WIN)
277 return ReadDirectoryWin(path);
278 #else
279 return ReadDirectoryNotWin(path);
280 #endif
281 }
282
215 bool CreateDir(const std::string& directory_name) { 283 bool CreateDir(const std::string& directory_name) {
216 struct stat path_info = {0}; 284 struct stat path_info = {0};
217 // Check if the path exists already: 285 // Check if the path exists already:
218 if (stat(directory_name.c_str(), &path_info) == 0) { 286 if (stat(directory_name.c_str(), &path_info) == 0) {
219 if (!S_ISDIR(path_info.st_mode)) { 287 if (!S_ISDIR(path_info.st_mode)) {
220 fprintf(stderr, "Path %s exists but is not a directory! Remove this " 288 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
221 "file and re-run to create the directory.\n", 289 "file and re-run to create the directory.\n",
222 directory_name.c_str()); 290 directory_name.c_str());
223 return false; 291 return false;
224 } 292 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 if (fseek(f, 0, SEEK_END) == 0) { 368 if (fseek(f, 0, SEEK_END) == 0) {
301 size = ftell(f); 369 size = ftell(f);
302 } 370 }
303 fclose(f); 371 fclose(f);
304 } 372 }
305 return size; 373 return size;
306 } 374 }
307 375
308 } // namespace test 376 } // namespace test
309 } // namespace webrtc 377 } // 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