| OLD | NEW |
| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <sys/stat.h> | 6 #include <sys/stat.h> |
| 7 | 7 |
| 8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
| 9 | 9 |
| 10 // Forward declaration. | 10 // Forward declaration. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 GetFullPathName(dir_name, MAX_PATH - *path_length, path, NULL); | 99 GetFullPathName(dir_name, MAX_PATH - *path_length, path, NULL); |
| 100 *path_length += written; | 100 *path_length += written; |
| 101 written = snprintf(path + *path_length, | 101 written = snprintf(path + *path_length, |
| 102 MAX_PATH - *path_length, | 102 MAX_PATH - *path_length, |
| 103 "%s", | 103 "%s", |
| 104 "\\*"); | 104 "\\*"); |
| 105 ASSERT(written == 2); | 105 ASSERT(written == 2); |
| 106 *path_length += written; | 106 *path_length += written; |
| 107 } | 107 } |
| 108 | 108 |
| 109 static void PostError(Dart_Port error_port, |
| 110 const char* prefix, |
| 111 const char* suffix) { |
| 112 if (error_port != 0) { |
| 113 static const int kBufferSize = 10; |
| 114 char error_str[kBufferSize]; |
| 115 int written = snprintf(error_str, kBufferSize, "%d", GetLastError()); |
| 116 ASSERT(written < kBufferSize); |
| 117 int error_message_size = |
| 118 strlen(prefix) + strlen(suffix) + strlen(error_str) + 3; |
| 119 char* message = static_cast<char*>(malloc(error_message_size + 1)); |
| 120 written = snprintf(message, |
| 121 error_message_size + 1, |
| 122 "%s%s (%s)", |
| 123 prefix, |
| 124 suffix, |
| 125 error_str); |
| 126 ASSERT(written == error_message_size); |
| 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 Loading... |
| 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 } |
| OLD | NEW |