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

Unified Diff: runtime/bin/directory_win.cc

Issue 9316066: Implement recursive directory deletion. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixing comment. Created 8 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: runtime/bin/directory_win.cc
diff --git a/runtime/bin/directory_win.cc b/runtime/bin/directory_win.cc
index 5070c9c39c1dff61178a8d38cc0ec4848654199f..16eeb93d0b73743e617ad079962ccfc18b68881e 100644
--- a/runtime/bin/directory_win.cc
+++ b/runtime/bin/directory_win.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@@ -39,6 +39,7 @@ static bool ListRecursively(const char* dir_name,
Dart_Port file_port,
Dart_Port done_port,
Dart_Port error_port);
+static bool DeleteRecursively(const char* dir_name);
static bool HandleDir(char* dir_name,
@@ -210,6 +211,81 @@ static bool ListRecursively(const char* dir_name,
}
+static bool DeleteFile(char* file_name,
+ char* path,
+ int path_length) {
+ size_t written = snprintf(path + path_length,
+ MAX_PATH - path_length,
+ "%s",
+ file_name);
+ 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!
+ return (DeleteFile(path) != 0);
+}
+
+
+static bool DeleteDir(char* dir_name,
+ char* path,
+ int path_length) {
+ if (strcmp(dir_name, ".") != 0 &&
+ strcmp(dir_name, "..") != 0) {
+ size_t written = snprintf(path + path_length,
+ MAX_PATH - path_length,
+ "%s",
+ dir_name);
+ 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.
+ return DeleteRecursively(path);
+ }
+ return true;
+}
+
+
+static bool DeleteEntry(LPWIN32_FIND_DATA find_file_data,
+ char* path,
+ int path_length) {
+ DWORD attributes = find_file_data->dwFileAttributes;
+ if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
+ return DeleteDir(find_file_data->cFileName, path, path_length);
+ } else {
+ return DeleteFile(find_file_data->cFileName, path, path_length);
+ }
+}
+
+
+static bool DeleteRecursively(const char* dir_name) {
+ char* path = static_cast<char*>(malloc(MAX_PATH));
+ int path_length = 0;
+ ComputeFullSearchPath(dir_name, path, &path_length);
+
+ WIN32_FIND_DATA find_file_data;
+ HANDLE find_handle = FindFirstFile(path, &find_file_data);
+
+ // Adjust the path by removing the '*' used for the search.
+ path_length -= 1;
+ path[path_length] = '\0';
+
+ if (find_handle == INVALID_HANDLE_VALUE) {
+ free(path);
+ return false;
+ }
+
+ bool error = !DeleteEntry(&find_file_data, path, path_length);
+
+ while ((FindNextFile(find_handle, &find_file_data) != 0) && !error) {
+ error = error || !DeleteEntry(&find_file_data, path, path_length);
+ }
+
+ free(path);
+
+ if ((GetLastError() != ERROR_NO_MORE_FILES) ||
+ (FindClose(find_handle) == 0) ||
+ (RemoveDirectory(dir_name) == 0)) {
+ return false;
+ }
+
+ return !error;
+}
+
+
void Directory::List(const char* dir_name,
bool recursive,
Dart_Port dir_port,
@@ -327,6 +403,10 @@ int Directory::CreateTemp(const char* const_template,
}
-bool Directory::Delete(const char* dir_name) {
- return (RemoveDirectory(dir_name) != 0);
+bool Directory::Delete(const char* dir_name, bool recursive) {
+ if (!recursive) {
+ return (RemoveDirectory(dir_name) != 0);
+ } else {
+ return DeleteRecursively(dir_name);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698