| Index: runtime/bin/directory_win.cc
|
| diff --git a/runtime/bin/directory_win.cc b/runtime/bin/directory_win.cc
|
| index 9d8f6f31935d30691c4344acc8354d300dfef835..2079f831d4ab6c3f43aa9d5ea771b8fba77772ad 100644
|
| --- a/runtime/bin/directory_win.cc
|
| +++ b/runtime/bin/directory_win.cc
|
| @@ -9,7 +9,35 @@
|
|
|
| #include "bin/log.h"
|
|
|
| -// Forward declaration.
|
| +class PathBuffer {
|
| + public:
|
| + PathBuffer() : length(0) { }
|
| +
|
| + wchar_t data[MAX_PATH + 1];
|
| + int length;
|
| +
|
| + bool Add(const wchar_t* name) {
|
| + size_t written = _snprintf(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 {
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + void Reset(int new_length) {
|
| + length = new_length;
|
| + data[length] = L'\0';
|
| + }
|
| +};
|
| +
|
| +
|
| +// Forward declarations.
|
| static bool ListRecursively(const wchar_t* dir_name,
|
| bool recursive,
|
| DirectoryListing* listing);
|
| @@ -17,43 +45,29 @@ static bool DeleteRecursively(const wchar_t* dir_name);
|
|
|
|
|
| static bool HandleDir(wchar_t* dir_name,
|
| - wchar_t* path,
|
| - int path_length,
|
| + PathBuffer* path,
|
| bool recursive,
|
| DirectoryListing* 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);
|
| - }
|
| + if (wcscmp(dir_name, L".") == 0) return true;
|
| + if (wcscmp(dir_name, L"..") == 0) return true;
|
| + if (!path->Add(dir_name)) 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);
|
| }
|
| - return true;
|
| }
|
|
|
|
|
| static bool HandleFile(wchar_t* file_name,
|
| - wchar_t* path,
|
| - int path_length,
|
| + PathBuffer* path,
|
| DirectoryListing* listing) {
|
| - size_t written = _snwprintf(path + path_length,
|
| - MAX_PATH - path_length,
|
| - L"%s",
|
| - file_name);
|
| - if (written != wcslen(file_name)) {
|
| + if (!path->Add(file_name)) {
|
| return false;
|
| - };
|
| - char* utf8_path = StringUtils::WideToUtf8(path);
|
| + }
|
| + char* utf8_path = StringUtils::WideToUtf8(path->data);
|
| bool ok = listing->HandleFile(utf8_path);
|
| free(utf8_path);
|
| return ok;
|
| @@ -61,48 +75,42 @@ static bool HandleFile(wchar_t* file_name,
|
|
|
|
|
| static bool HandleEntry(LPWIN32_FIND_DATAW find_file_data,
|
| - wchar_t* path,
|
| - int path_length,
|
| + PathBuffer* path,
|
| 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, path_length, listing);
|
| + return HandleFile(find_file_data->cFileName, path, listing);
|
| }
|
| }
|
|
|
|
|
| -// 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) {
|
| +static PathBuffer* ComputeFullSearchPath(const wchar_t* dir_name) {
|
| // GetFullPathName only works in a multi-threaded environment if
|
| // SetCurrentDirectory is not used. We currently have no plan for
|
| // exposing SetCurrentDirectory.
|
| - size_t written = GetFullPathNameW(dir_name, MAX_PATH, path, NULL);
|
| + PathBuffer* path = new PathBuffer();
|
| +
|
| + size_t written = GetFullPathNameW(dir_name, MAX_PATH + 1, path->data, 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) {
|
| - return false;
|
| + 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;
|
| + if (path->Add(L"\\*")) {
|
| + return path;
|
| + } else {
|
| + delete path;
|
| + return NULL;
|
| }
|
| - *path_length += written;
|
| - return true;
|
| }
|
|
|
| static void PostError(DirectoryListing* listing,
|
| @@ -121,12 +129,10 @@ 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.
|
| - 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) {
|
| + PathBuffer* path = ComputeFullSearchPath(dir_name, path, &path_length);
|
| + if (path == NULL) {
|
| PostError(listing, dir_name);
|
| - free(path);
|
| + delete path;
|
| return false;
|
| }
|
|
|
| @@ -134,25 +140,24 @@ static bool ListRecursively(const wchar_t* dir_name,
|
| HANDLE find_handle = FindFirstFileW(path, &find_file_data);
|
|
|
| // Adjust the path by removing the '*' used for the search.
|
| - path_length -= 1;
|
| - path[path_length] = '\0';
|
| + path->Reset(path->length - 1);
|
|
|
| if (find_handle == INVALID_HANDLE_VALUE) {
|
| PostError(listing, path);
|
| - free(path);
|
| + delete 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;
|
| }
|
| @@ -166,12 +171,12 @@ static bool ListRecursively(const wchar_t* dir_name,
|
| success = false;
|
| PostError(listing, dir_name);
|
| }
|
| - free(path);
|
| + delete path;
|
|
|
| return success;
|
| }
|
|
|
| -
|
| +// TODO(whesse): Finish adding PathBuffer, starting here.
|
| static bool DeleteFile(wchar_t* file_name,
|
| wchar_t* path,
|
| int path_length) {
|
|
|