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

Unified Diff: runtime/bin/directory_win.cc

Issue 12210086: Revert changes to directory listing on Windows, to diagnose pub problems. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/directory_win.cc
diff --git a/runtime/bin/directory_win.cc b/runtime/bin/directory_win.cc
index 24d98f70eaf18e54b5caec609cf54ef518a28570..9d8f6f31935d30691c4344acc8354d300dfef835 100644
--- a/runtime/bin/directory_win.cc
+++ b/runtime/bin/directory_win.cc
@@ -9,78 +9,51 @@
#include "bin/log.h"
-class PathBuffer {
- public:
- PathBuffer() : length(0) { }
-
- wchar_t data[MAX_PATH + 1];
- int length;
-
- bool Add(const wchar_t* name) {
- size_t written = _snwprintf(data + length,
- MAX_PATH - length,
- L"%s",
- name);
- data[MAX_PATH] = L'\0';
- if (written == wcsnlen(name, MAX_PATH + 1)) {
- length += written;
- return true;
- } else {
- SetLastError(ERROR_BUFFER_OVERFLOW);
- return false;
- }
- }
-
- void Reset(int new_length) {
- length = new_length;
- data[length] = L'\0';
- }
-};
-
-
-// Forward declarations.
+// Forward declaration.
static bool ListRecursively(const wchar_t* dir_name,
bool recursive,
DirectoryListing* listing);
static bool DeleteRecursively(const wchar_t* dir_name);
-static void PostError(DirectoryListing* listing,
- const wchar_t* dir_name) {
- const char* utf8_path = StringUtils::WideToUtf8(dir_name);
- listing->HandleError(utf8_path);
- free(const_cast<char*>(utf8_path));
-}
-
-
static bool HandleDir(wchar_t* dir_name,
- PathBuffer* path,
+ wchar_t* path,
+ int path_length,
bool recursive,
DirectoryListing* listing) {
- if (wcscmp(dir_name, L".") == 0) return true;
- if (wcscmp(dir_name, L"..") == 0) return true;
- if (!path->Add(dir_name)) {
- PostError(listing, path->data);
- return false;
- }
- char* utf8_path = StringUtils::WideToUtf8(path->data);
- bool ok = listing->HandleDirectory(utf8_path);
- free(utf8_path);
- if (!ok) return ok;
- if (recursive) {
- return ListRecursively(path->data, recursive, listing);
+ if (wcscmp(dir_name, L".") != 0 &&
+ wcscmp(dir_name, L"..") != 0) {
+ size_t written = _snwprintf(path + path_length,
+ MAX_PATH - path_length,
+ L"%s",
+ dir_name);
+ if (written != wcslen(dir_name)) {
+ return false;
+ }
+ char* utf8_path = StringUtils::WideToUtf8(path);
+ bool ok = listing->HandleDirectory(utf8_path);
+ free(utf8_path);
+ if (!ok) return ok;
+ if (recursive) {
+ return ListRecursively(path, recursive, listing);
+ }
}
+ return true;
}
static bool HandleFile(wchar_t* file_name,
- PathBuffer* path,
+ wchar_t* path,
+ int path_length,
DirectoryListing* listing) {
- if (!path->Add(file_name)) {
- PostError(listing, path->data);
+ size_t written = _snwprintf(path + path_length,
+ MAX_PATH - path_length,
+ L"%s",
+ file_name);
+ if (written != wcslen(file_name)) {
return false;
- }
- char* utf8_path = StringUtils::WideToUtf8(path->data);
+ };
+ char* utf8_path = StringUtils::WideToUtf8(path);
bool ok = listing->HandleFile(utf8_path);
free(utf8_path);
return ok;
@@ -88,42 +61,55 @@ static bool HandleFile(wchar_t* file_name,
static bool HandleEntry(LPWIN32_FIND_DATAW find_file_data,
- PathBuffer* path,
+ wchar_t* path,
+ int path_length,
bool recursive,
DirectoryListing* listing) {
DWORD attributes = find_file_data->dwFileAttributes;
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
return HandleDir(find_file_data->cFileName,
path,
+ path_length,
recursive,
listing);
} else {
- return HandleFile(find_file_data->cFileName, path, listing);
+ return HandleFile(find_file_data->cFileName, path, path_length, listing);
}
}
-static PathBuffer* ComputeFullSearchPath(const wchar_t* dir_name) {
+// ComputeFullSearchPath must be called with a path array of size at
+// least MAX_PATH.
+static bool ComputeFullSearchPath(const wchar_t* dir_name,
+ wchar_t* path,
+ int* path_length) {
// GetFullPathName only works in a multi-threaded environment if
// SetCurrentDirectory is not used. We currently have no plan for
// exposing SetCurrentDirectory.
- PathBuffer* path = new PathBuffer();
-
- size_t written = GetFullPathNameW(dir_name, MAX_PATH + 1, path->data, NULL);
+ size_t written = GetFullPathNameW(dir_name, MAX_PATH, path, NULL);
// GetFullPathName only accepts input strings of size less than
// MAX_PATH and returns 0 to indicate failure for paths longer than
// that. Therefore the path buffer is always big enough.
if (written == 0 || written > MAX_PATH) {
- delete path;
- return NULL;
+ return false;
}
- path->length = written;
- if (path->Add(L"\\*")) {
- return path;
- } else {
- delete path;
- return NULL;
+ *path_length = written;
+ written = _snwprintf(path + *path_length,
+ MAX_PATH - *path_length,
+ L"%s",
+ L"\\*");
+ if (written != 2) {
+ return false;
}
+ *path_length += written;
+ return true;
+}
+
+static void PostError(DirectoryListing* listing,
+ const wchar_t* dir_name) {
+ const char* utf8_path = StringUtils::WideToUtf8(dir_name);
+ listing->HandleError(utf8_path);
+ free(const_cast<char*>(utf8_path));
}
@@ -135,35 +121,38 @@ static bool ListRecursively(const wchar_t* dir_name,
// recursive traversal. path_length does not always equal
// strlen(path) but indicates the current prefix of path that is the
// path of the current directory in the traversal.
- PathBuffer* path = ComputeFullSearchPath(dir_name);
- if (path == NULL) {
+ wchar_t* path = static_cast<wchar_t*>(malloc(MAX_PATH * sizeof(wchar_t)));
+ int path_length = 0;
+ bool valid = ComputeFullSearchPath(dir_name, path, &path_length);
+ if (!valid) {
PostError(listing, dir_name);
- delete path;
+ free(path);
return false;
}
WIN32_FIND_DATAW find_file_data;
- HANDLE find_handle = FindFirstFileW(path->data, &find_file_data);
+ HANDLE find_handle = FindFirstFileW(path, &find_file_data);
// Adjust the path by removing the '*' used for the search.
- path->Reset(path->length - 1);
+ path_length -= 1;
+ path[path_length] = '\0';
if (find_handle == INVALID_HANDLE_VALUE) {
- PostError(listing, path->data);
- delete path;
+ PostError(listing, path);
+ free(path);
return false;
}
- int path_length = path->length;
bool success = HandleEntry(&find_file_data,
path,
+ path_length,
recursive,
listing);
while ((FindNextFileW(find_handle, &find_file_data) != 0)) {
- path->Reset(path_length); // HandleEntry adds the entry name to path.
success = HandleEntry(&find_file_data,
path,
+ path_length,
recursive,
listing) && success;
}
@@ -177,16 +166,24 @@ static bool ListRecursively(const wchar_t* dir_name,
success = false;
PostError(listing, dir_name);
}
- delete path;
+ free(path);
return success;
}
-static bool DeleteFile(wchar_t* file_name, PathBuffer* path) {
- if (!path->Add(file_name)) return false;
+static bool DeleteFile(wchar_t* file_name,
+ wchar_t* path,
+ int path_length) {
+ size_t written = _snwprintf(path + path_length,
+ MAX_PATH - path_length,
+ L"%s",
+ file_name);
+ if (written != wcslen(file_name)) {
+ return false;
+ }
- if (DeleteFileW(path->data) != 0) {
+ if (DeleteFileW(path) != 0) {
return true;
}
@@ -194,7 +191,7 @@ static bool DeleteFile(wchar_t* file_name, PathBuffer* path) {
// again. This mirrors Linux/Mac where a directory containing read-only files
// can still be recursively deleted.
if (GetLastError() == ERROR_ACCESS_DENIED) {
- DWORD attributes = GetFileAttributesW(path->data);
+ DWORD attributes = GetFileAttributesW(path);
if (attributes == INVALID_FILE_ATTRIBUTES) {
return false;
}
@@ -202,11 +199,11 @@ static bool DeleteFile(wchar_t* file_name, PathBuffer* path) {
if ((attributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) {
attributes &= ~FILE_ATTRIBUTE_READONLY;
- if (SetFileAttributesW(path->data, attributes) == 0) {
+ if (SetFileAttributesW(path, attributes) == 0) {
return false;
}
- return DeleteFileW(path->data) != 0;
+ return DeleteFileW(path) != 0;
}
}
@@ -214,20 +211,33 @@ static bool DeleteFile(wchar_t* file_name, PathBuffer* path) {
}
-static bool DeleteDir(wchar_t* dir_name, PathBuffer* path) {
- if (wcscmp(dir_name, L".") == 0) return true;
- if (wcscmp(dir_name, L"..") == 0) return true;
- return path->Add(dir_name) && DeleteRecursively(path->data);
+static bool DeleteDir(wchar_t* dir_name,
+ wchar_t* path,
+ int path_length) {
+ if (wcscmp(dir_name, L".") != 0 &&
+ wcscmp(dir_name, L"..") != 0) {
+ size_t written = _snwprintf(path + path_length,
+ MAX_PATH - path_length,
+ L"%s",
+ dir_name);
+ if (written != wcslen(dir_name)) {
+ return false;
+ }
+ return DeleteRecursively(path);
+ }
+ return true;
}
-static bool DeleteEntry(LPWIN32_FIND_DATAW find_file_data, PathBuffer* path) {
+static bool DeleteEntry(LPWIN32_FIND_DATAW find_file_data,
+ wchar_t* path,
+ int path_length) {
DWORD attributes = find_file_data->dwFileAttributes;
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
- return DeleteDir(find_file_data->cFileName, path);
+ return DeleteDir(find_file_data->cFileName, path, path_length);
} else {
- return DeleteFile(find_file_data->cFileName, path);
+ return DeleteFile(find_file_data->cFileName, path, path_length);
}
}
@@ -247,29 +257,33 @@ static bool DeleteRecursively(const wchar_t* dir_name) {
// recursive traversal. path_length does not always equal
// strlen(path) but indicates the current prefix of path that is the
// path of the current directory in the traversal.
- PathBuffer* path = ComputeFullSearchPath(dir_name);
- if (path == NULL) return false;
+ wchar_t* path = static_cast<wchar_t*>(malloc(MAX_PATH * sizeof(wchar_t)));
+ int path_length = 0;
+ bool valid = ComputeFullSearchPath(dir_name, path, &path_length);
+ if (!valid) {
+ free(path);
+ return false;
+ }
WIN32_FIND_DATAW find_file_data;
- HANDLE find_handle = FindFirstFileW(path->data, &find_file_data);
+ HANDLE find_handle = FindFirstFileW(path, &find_file_data);
// Adjust the path by removing the '*' used for the search.
- int path_length = path->length - 1;
- path->Reset(path_length);
+ path_length -= 1;
+ path[path_length] = '\0';
if (find_handle == INVALID_HANDLE_VALUE) {
- delete path;
+ free(path);
return false;
}
- bool success = DeleteEntry(&find_file_data, path);
+ bool success = DeleteEntry(&find_file_data, path, path_length);
while ((FindNextFileW(find_handle, &find_file_data) != 0) && success) {
- path->Reset(path_length); // DeleteEntry adds to the path.
- success = success && DeleteEntry(&find_file_data, path);
+ success = success && DeleteEntry(&find_file_data, path, path_length);
}
- delete path;
+ free(path);
if ((GetLastError() != ERROR_NO_MORE_FILES) ||
(FindClose(find_handle) == 0) ||
@@ -348,51 +362,52 @@ char* Directory::CreateTemp(const char* const_template) {
// dir_template. Creates this directory, with a default security
// descriptor inherited from its parent directory.
// The return value must be freed by the caller.
- PathBuffer* path = new PathBuffer();
+ wchar_t* path = static_cast<wchar_t*>(malloc(MAX_PATH * sizeof(wchar_t)));
+ int path_length;
if (0 == strncmp(const_template, "", 1)) {
- path->length = GetTempPathW(MAX_PATH, path->data);
- if (path->length == 0) {
- delete path;
+ path_length = GetTempPathW(MAX_PATH, path);
+ if (path_length == 0) {
+ free(path);
return NULL;
}
} else {
const wchar_t* system_template = StringUtils::Utf8ToWide(const_template);
- path->Add(system_template);
+ _snwprintf(path, MAX_PATH, L"%s", system_template);
free(const_cast<wchar_t*>(system_template));
+ path_length = wcslen(path);
}
// Length of tempdir-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 44.
- if (path->length > MAX_PATH - 44) {
- delete path;
+ if (path_length > MAX_PATH - 44) {
+ free(path);
return NULL;
}
- if ((path->data)[path->length - 1] == L'\\') {
+ if ((path)[path_length - 1] == L'\\') {
// No base name for the directory - use "tempdir".
- path->Add(L"tempdir");
+ _snwprintf(path + path_length, MAX_PATH - path_length, L"tempdir");
+ path_length = wcslen(path);
}
UUID uuid;
RPC_STATUS status = UuidCreateSequential(&uuid);
if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) {
- delete path;
+ free(path);
return NULL;
}
RPC_WSTR uuid_string;
status = UuidToStringW(&uuid, &uuid_string);
if (status != RPC_S_OK) {
- delete path;
+ free(path);
return NULL;
}
- path->Add(L"-");
- // RPC_WSTR is an unsigned short*, so we cast to wchar_t*.
- path->Add(reinterpret_cast<wchar_t*>(uuid_string));
+ _snwprintf(path + path_length, MAX_PATH - path_length, L"-%s", uuid_string);
RpcStringFreeW(&uuid_string);
- if (!CreateDirectoryW(path->data, NULL)) {
- delete path;
+ if (!CreateDirectoryW(path, NULL)) {
+ free(path);
return NULL;
}
- char* result = StringUtils::WideToUtf8(path->data);
- delete path;
+ char* result = StringUtils::WideToUtf8(path);
+ free(path);
return result;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698