Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, 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" |
| 11 | 11 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 32 } | 32 } |
| 33 | 33 |
| 34 | 34 |
| 35 // Forward declaration. | 35 // Forward declaration. |
| 36 static bool ListRecursively(const char* dir_name, | 36 static bool ListRecursively(const char* dir_name, |
| 37 bool recursive, | 37 bool recursive, |
| 38 Dart_Port dir_port, | 38 Dart_Port dir_port, |
| 39 Dart_Port file_port, | 39 Dart_Port file_port, |
| 40 Dart_Port done_port, | 40 Dart_Port done_port, |
| 41 Dart_Port error_port); | 41 Dart_Port error_port); |
| 42 static bool DeleteRecursively(const char* dir_name); | |
| 42 | 43 |
| 43 | 44 |
| 44 static bool HandleDir(char* dir_name, | 45 static bool HandleDir(char* dir_name, |
| 45 char* path, | 46 char* path, |
| 46 int path_length, | 47 int path_length, |
| 47 bool recursive, | 48 bool recursive, |
| 48 Dart_Port dir_port, | 49 Dart_Port dir_port, |
| 49 Dart_Port file_port, | 50 Dart_Port file_port, |
| 50 Dart_Port done_port, | 51 Dart_Port done_port, |
| 51 Dart_Port error_port) { | 52 Dart_Port error_port) { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 | 204 |
| 204 if (FindClose(find_handle) == 0) { | 205 if (FindClose(find_handle) == 0) { |
| 205 PostError(error_port, "Failed to close directory", ""); | 206 PostError(error_port, "Failed to close directory", ""); |
| 206 } | 207 } |
| 207 free(path); | 208 free(path); |
| 208 | 209 |
| 209 return !listing_error; | 210 return !listing_error; |
| 210 } | 211 } |
| 211 | 212 |
| 212 | 213 |
| 214 static bool DeleteFile(char* file_name, | |
| 215 char* path, | |
| 216 int path_length) { | |
| 217 size_t written = snprintf(path + path_length, | |
| 218 MAX_PATH - path_length, | |
| 219 "%s", | |
| 220 file_name); | |
| 221 ASSERT(written == strlen(file_name)); | |
|
Bill Hesse
2012/02/02 17:24:39
Especially on Windows, I think this assert really
Mads Ager (google)
2012/02/03 11:48:54
Absolutely! Thanks!
| |
| 222 return (DeleteFile(path) != 0); | |
| 223 } | |
| 224 | |
| 225 | |
| 226 static bool DeleteDir(char* dir_name, | |
| 227 char* path, | |
| 228 int path_length) { | |
| 229 if (strcmp(dir_name, ".") != 0 && | |
| 230 strcmp(dir_name, "..") != 0) { | |
| 231 size_t written = snprintf(path + path_length, | |
| 232 MAX_PATH - path_length, | |
| 233 "%s", | |
| 234 dir_name); | |
| 235 ASSERT(written == strlen(dir_name)); | |
|
Bill Hesse
2012/02/02 17:24:39
These could all be made checks, rather than assert
Mads Ager (google)
2012/02/03 11:48:54
Done.
| |
| 236 return DeleteRecursively(path); | |
| 237 } | |
| 238 return true; | |
| 239 } | |
| 240 | |
| 241 | |
| 242 static bool DeleteEntry(LPWIN32_FIND_DATA find_file_data, | |
| 243 char* path, | |
| 244 int path_length) { | |
| 245 DWORD attributes = find_file_data->dwFileAttributes; | |
| 246 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | |
| 247 return DeleteDir(find_file_data->cFileName, path, path_length); | |
| 248 } else { | |
| 249 return DeleteFile(find_file_data->cFileName, path, path_length); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 | |
| 254 static bool DeleteRecursively(const char* dir_name) { | |
| 255 char* path = static_cast<char*>(malloc(MAX_PATH)); | |
| 256 int path_length = 0; | |
| 257 ComputeFullSearchPath(dir_name, path, &path_length); | |
| 258 | |
| 259 WIN32_FIND_DATA find_file_data; | |
| 260 HANDLE find_handle = FindFirstFile(path, &find_file_data); | |
| 261 | |
| 262 // Adjust the path by removing the '*' used for the search. | |
| 263 path_length -= 1; | |
| 264 path[path_length] = '\0'; | |
| 265 | |
| 266 if (find_handle == INVALID_HANDLE_VALUE) { | |
| 267 free(path); | |
| 268 return false; | |
| 269 } | |
| 270 | |
| 271 bool error = !DeleteEntry(&find_file_data, path, path_length); | |
| 272 | |
| 273 while ((FindNextFile(find_handle, &find_file_data) != 0) && !error) { | |
| 274 error = error || !DeleteEntry(&find_file_data, path, path_length); | |
| 275 } | |
| 276 | |
| 277 free(path); | |
| 278 | |
| 279 if ((GetLastError() != ERROR_NO_MORE_FILES) || | |
| 280 (FindClose(find_handle) == 0) || | |
| 281 (RemoveDirectory(dir_name) == 0)) { | |
| 282 return false; | |
| 283 } | |
| 284 | |
| 285 return !error; | |
| 286 } | |
| 287 | |
| 288 | |
| 213 void Directory::List(const char* dir_name, | 289 void Directory::List(const char* dir_name, |
| 214 bool recursive, | 290 bool recursive, |
| 215 Dart_Port dir_port, | 291 Dart_Port dir_port, |
| 216 Dart_Port file_port, | 292 Dart_Port file_port, |
| 217 Dart_Port done_port, | 293 Dart_Port done_port, |
| 218 Dart_Port error_port) { | 294 Dart_Port error_port) { |
| 219 bool result = ListRecursively(dir_name, | 295 bool result = ListRecursively(dir_name, |
| 220 recursive, | 296 recursive, |
| 221 dir_port, | 297 dir_port, |
| 222 file_port, | 298 file_port, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 free(*path); | 396 free(*path); |
| 321 *path = NULL; | 397 *path = NULL; |
| 322 int error_code = | 398 int error_code = |
| 323 SetOsErrorMessage(os_error_message, os_error_message_len); | 399 SetOsErrorMessage(os_error_message, os_error_message_len); |
| 324 return error_code; | 400 return error_code; |
| 325 } | 401 } |
| 326 return 0; | 402 return 0; |
| 327 } | 403 } |
| 328 | 404 |
| 329 | 405 |
| 330 bool Directory::Delete(const char* dir_name) { | 406 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 331 return (RemoveDirectory(dir_name) != 0); | 407 if (!recursive) { |
| 408 return (RemoveDirectory(dir_name) != 0); | |
| 409 } else { | |
| 410 return DeleteRecursively(dir_name); | |
| 411 } | |
| 332 } | 412 } |
| OLD | NEW |