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

Side by Side Diff: runtime/bin/directory_win.cc

Issue 8511001: Reapply directory error handling change. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Cut down on includes in posix version. Created 9 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « runtime/bin/directory_posix.cc ('k') | runtime/bin/platform.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/directory.h"
6
5 #include <errno.h> 7 #include <errno.h>
6 #include <sys/stat.h> 8 #include <sys/stat.h>
7 9
8 #include "bin/directory.h" 10 #include "bin/platform.h"
9 11
10 // Forward declaration. 12 // Forward declaration.
11 static bool ListRecursively(const char* dir_name, 13 static bool ListRecursively(const char* dir_name,
12 bool recursive, 14 bool recursive,
13 Dart_Port dir_port, 15 Dart_Port dir_port,
14 Dart_Port file_port, 16 Dart_Port file_port,
15 Dart_Port done_port, 17 Dart_Port done_port,
16 Dart_Port error_port); 18 Dart_Port error_port);
17 19
18 20
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 90 }
89 } 91 }
90 92
91 93
92 static void ComputeFullSearchPath(const char* dir_name, 94 static void ComputeFullSearchPath(const char* dir_name,
93 char* path, 95 char* path,
94 int* path_length) { 96 int* path_length) {
95 // GetFullPathName only works in a multi-threaded environment if 97 // GetFullPathName only works in a multi-threaded environment if
96 // SetCurrentDirectory is not used. We currently have no plan for 98 // SetCurrentDirectory is not used. We currently have no plan for
97 // exposing SetCurrentDirectory. 99 // exposing SetCurrentDirectory.
98 int written = 100 size_t written =
99 GetFullPathName(dir_name, MAX_PATH - *path_length, path, NULL); 101 GetFullPathName(dir_name, MAX_PATH - *path_length, path, NULL);
100 *path_length += written; 102 *path_length += written;
101 written = snprintf(path + *path_length, 103 written = snprintf(path + *path_length,
102 MAX_PATH - *path_length, 104 MAX_PATH - *path_length,
103 "%s", 105 "%s",
104 "\\*"); 106 "\\*");
105 ASSERT(written == 2); 107 ASSERT(written == 2);
106 *path_length += written; 108 *path_length += written;
107 } 109 }
108 110
111 static void PostError(Dart_Port error_port,
112 const char* prefix,
113 const char* suffix) {
114 if (error_port != 0) {
115 char* error_str = Platform::StrError(GetLastError());
116 int error_message_size =
117 strlen(prefix) + strlen(suffix) + strlen(error_str) + 3;
118 char* message = static_cast<char*>(malloc(error_message_size + 1));
119 size_t written = snprintf(message,
120 error_message_size + 1,
121 "%s%s (%s)",
122 prefix,
123 suffix,
124 error_str);
125 ASSERT(written == error_message_size);
126 free(error_str);
127 Dart_Post(error_port, Dart_NewString(message));
128 free(message);
129 }
130 }
131
132
109 static bool ListRecursively(const char* dir_name, 133 static bool ListRecursively(const char* dir_name,
110 bool recursive, 134 bool recursive,
111 Dart_Port dir_port, 135 Dart_Port dir_port,
112 Dart_Port file_port, 136 Dart_Port file_port,
113 Dart_Port done_port, 137 Dart_Port done_port,
114 Dart_Port error_port) { 138 Dart_Port error_port) {
115 char* path = static_cast<char*>(malloc(MAX_PATH)); 139 char* path = static_cast<char*>(malloc(MAX_PATH));
116 int path_length = 0; 140 int path_length = 0;
117 ComputeFullSearchPath(dir_name, path, &path_length); 141 ComputeFullSearchPath(dir_name, path, &path_length);
118 142
119 WIN32_FIND_DATA find_file_data; 143 WIN32_FIND_DATA find_file_data;
120 HANDLE find_handle = FindFirstFile(path, &find_file_data); 144 HANDLE find_handle = FindFirstFile(path, &find_file_data);
121 145
122 // Adjust the path by removing the '*' used for the search. 146 // Adjust the path by removing the '*' used for the search.
123 path_length -= 1; 147 path_length -= 1;
124 path[path_length] = '\0'; 148 path[path_length] = '\0';
125 149
126 if (find_handle == INVALID_HANDLE_VALUE) { 150 if (find_handle == INVALID_HANDLE_VALUE) {
127 // TODO(ager): Post on error port. 151 PostError(error_port, "Directory listing failed for: ", path);
128 free(path); 152 free(path);
129 return false; 153 return false;
130 } 154 }
131 155
132 bool completed = HandleEntry(&find_file_data, 156 bool listing_error = !HandleEntry(&find_file_data,
133 path, 157 path,
134 path_length, 158 path_length,
135 recursive, 159 recursive,
136 dir_port, 160 dir_port,
137 file_port, 161 file_port,
138 done_port, 162 done_port,
139 error_port); 163 error_port);
140 164
141 while (FindNextFile(find_handle, &find_file_data) != 0) { 165 while ((FindNextFile(find_handle, &find_file_data) != 0) && !listing_error) {
142 completed = completed && HandleEntry(&find_file_data, 166 listing_error = listing_error || !HandleEntry(&find_file_data,
143 path, 167 path,
144 path_length, 168 path_length,
145 recursive, 169 recursive,
146 dir_port, 170 dir_port,
147 file_port, 171 file_port,
148 done_port, 172 done_port,
149 error_port); 173 error_port);
150 } 174 }
151 175
152 completed = completed && (GetLastError() == ERROR_NO_MORE_FILES); 176 if (GetLastError() != ERROR_NO_MORE_FILES) {
177 listing_error = true;
178 PostError(error_port, "Directory listing failed", "");
179 }
153 180
154 // TODO(ager): Post on error port if close fails. 181 if (FindClose(find_handle) == 0) {
155 FindClose(find_handle); 182 PostError(error_port, "Failed to close directory", "");
183 }
156 free(path); 184 free(path);
157 185
158 return completed; 186 return !listing_error;
159 } 187 }
160 188
161 189
162 void Directory::List(const char* dir_name, 190 void Directory::List(const char* dir_name,
163 bool recursive, 191 bool recursive,
164 Dart_Port dir_port, 192 Dart_Port dir_port,
165 Dart_Port file_port, 193 Dart_Port file_port,
166 Dart_Port done_port, 194 Dart_Port done_port,
167 Dart_Port error_port) { 195 Dart_Port error_port) {
168 bool result = ListRecursively(dir_name, 196 bool result = ListRecursively(dir_name,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 234
207 235
208 bool Directory::Create(const char* dir_name) { 236 bool Directory::Create(const char* dir_name) {
209 return (CreateDirectory(dir_name, NULL) != 0); 237 return (CreateDirectory(dir_name, NULL) != 0);
210 } 238 }
211 239
212 240
213 bool Directory::Delete(const char* dir_name) { 241 bool Directory::Delete(const char* dir_name) {
214 return (RemoveDirectory(dir_name) != 0); 242 return (RemoveDirectory(dir_name) != 0);
215 } 243 }
OLDNEW
« no previous file with comments | « runtime/bin/directory_posix.cc ('k') | runtime/bin/platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698