| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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" | 5 #include "bin/directory.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include "bin/platform.h" | 10 #include "bin/platform.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 "%s", | 117 "%s", |
| 118 "\\*"); | 118 "\\*"); |
| 119 if (written != 2) { | 119 if (written != 2) { |
| 120 return false; | 120 return false; |
| 121 } | 121 } |
| 122 *path_length += written; | 122 *path_length += written; |
| 123 return true; | 123 return true; |
| 124 } | 124 } |
| 125 | 125 |
| 126 static void PostError(DirectoryListing* listing, | 126 static void PostError(DirectoryListing* listing, |
| 127 const char* prefix, | 127 const char* dir_name) { |
| 128 const char* suffix) { | 128 listing->HandleError(dir_name); |
| 129 char* error_str = Platform::StrError(GetLastError()); | |
| 130 int error_message_size = | |
| 131 strlen(prefix) + strlen(suffix) + strlen(error_str) + 3; | |
| 132 char* message = static_cast<char*>(malloc(error_message_size + 1)); | |
| 133 size_t written = snprintf(message, | |
| 134 error_message_size + 1, | |
| 135 "%s%s (%s)", | |
| 136 prefix, | |
| 137 suffix, | |
| 138 error_str); | |
| 139 ASSERT(written == error_message_size); | |
| 140 free(error_str); | |
| 141 listing->HandleError(message); | |
| 142 free(message); | |
| 143 } | 129 } |
| 144 | 130 |
| 145 | 131 |
| 146 static bool ListRecursively(const char* dir_name, | 132 static bool ListRecursively(const char* dir_name, |
| 147 bool recursive, | 133 bool recursive, |
| 148 DirectoryListing* listing) { | 134 DirectoryListing* listing) { |
| 149 // Compute full path for the directory currently being listed. The | 135 // Compute full path for the directory currently being listed. The |
| 150 // path buffer will be used to construct the current path in the | 136 // path buffer will be used to construct the current path in the |
| 151 // recursive traversal. path_length does not always equal | 137 // recursive traversal. path_length does not always equal |
| 152 // strlen(path) but indicates the current prefix of path that is the | 138 // strlen(path) but indicates the current prefix of path that is the |
| 153 // path of the current directory in the traversal. | 139 // path of the current directory in the traversal. |
| 154 char* path = static_cast<char*>(malloc(MAX_PATH)); | 140 char* path = static_cast<char*>(malloc(MAX_PATH)); |
| 155 int path_length = 0; | 141 int path_length = 0; |
| 156 bool valid = ComputeFullSearchPath(dir_name, path, &path_length); | 142 bool valid = ComputeFullSearchPath(dir_name, path, &path_length); |
| 157 if (!valid) { | 143 if (!valid) { |
| 158 PostError(listing, "Directory listing failed for: ", dir_name); | 144 PostError(listing, dir_name); |
| 159 free(path); | 145 free(path); |
| 160 return false; | 146 return false; |
| 161 } | 147 } |
| 162 | 148 |
| 163 WIN32_FIND_DATA find_file_data; | 149 WIN32_FIND_DATA find_file_data; |
| 164 HANDLE find_handle = FindFirstFile(path, &find_file_data); | 150 HANDLE find_handle = FindFirstFile(path, &find_file_data); |
| 165 | 151 |
| 166 // Adjust the path by removing the '*' used for the search. | 152 // Adjust the path by removing the '*' used for the search. |
| 167 path_length -= 1; | 153 path_length -= 1; |
| 168 path[path_length] = '\0'; | 154 path[path_length] = '\0'; |
| 169 | 155 |
| 170 if (find_handle == INVALID_HANDLE_VALUE) { | 156 if (find_handle == INVALID_HANDLE_VALUE) { |
| 171 PostError(listing, "Directory listing failed for: ", path); | 157 PostError(listing, path); |
| 172 free(path); | 158 free(path); |
| 173 return false; | 159 return false; |
| 174 } | 160 } |
| 175 | 161 |
| 176 bool success = HandleEntry(&find_file_data, | 162 bool success = HandleEntry(&find_file_data, |
| 177 path, | 163 path, |
| 178 path_length, | 164 path_length, |
| 179 recursive, | 165 recursive, |
| 180 listing); | 166 listing); |
| 181 | 167 |
| 182 while ((FindNextFile(find_handle, &find_file_data) != 0) && success) { | 168 while ((FindNextFile(find_handle, &find_file_data) != 0) && success) { |
| 183 success = success && HandleEntry(&find_file_data, | 169 success = success && HandleEntry(&find_file_data, |
| 184 path, | 170 path, |
| 185 path_length, | 171 path_length, |
| 186 recursive, | 172 recursive, |
| 187 listing); | 173 listing); |
| 188 } | 174 } |
| 189 | 175 |
| 190 if (GetLastError() != ERROR_NO_MORE_FILES) { | 176 if (GetLastError() != ERROR_NO_MORE_FILES) { |
| 191 success = false; | 177 success = false; |
| 192 PostError(listing, "Directory listing failed", ""); | 178 PostError(listing, dir_name); |
| 193 } | 179 } |
| 194 | 180 |
| 195 if (FindClose(find_handle) == 0) { | 181 if (FindClose(find_handle) == 0) { |
| 196 PostError(listing, "Failed to close directory", ""); | 182 success = false; |
| 183 PostError(listing, dir_name); |
| 197 } | 184 } |
| 198 free(path); | 185 free(path); |
| 199 | 186 |
| 200 return success; | 187 return success; |
| 201 } | 188 } |
| 202 | 189 |
| 203 | 190 |
| 204 static bool DeleteFile(char* file_name, | 191 static bool DeleteFile(char* file_name, |
| 205 char* path, | 192 char* path, |
| 206 int path_length) { | 193 int path_length) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 } | 375 } |
| 389 | 376 |
| 390 | 377 |
| 391 bool Directory::Delete(const char* dir_name, bool recursive) { | 378 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 392 if (!recursive) { | 379 if (!recursive) { |
| 393 return (RemoveDirectory(dir_name) != 0); | 380 return (RemoveDirectory(dir_name) != 0); |
| 394 } else { | 381 } else { |
| 395 return DeleteRecursively(dir_name); | 382 return DeleteRecursively(dir_name); |
| 396 } | 383 } |
| 397 } | 384 } |
| OLD | NEW |