Chromium Code Reviews| Index: runtime/bin/directory_win.cc |
| diff --git a/runtime/bin/directory_win.cc b/runtime/bin/directory_win.cc |
| index 436f9875b091c933e93d29a2f6442a000566d7eb..0c59d85bd5ef41b5a097542a8f90dc5aab27b124 100644 |
| --- a/runtime/bin/directory_win.cc |
| +++ b/runtime/bin/directory_win.cc |
| @@ -53,7 +53,9 @@ static bool HandleDir(char* dir_name, |
| if (written != strlen(dir_name)) { |
| return false; |
| } |
| - bool ok = listing->HandleDirectory(path); |
| + char* utf8_encoded_path = StringUtils::SystemStringToUtf8(path); |
|
Søren Gjesse
2012/11/13 12:48:45
Maybe remove "encoded" from utf8_encoded_path to s
Mads Ager (google)
2012/11/13 14:50:17
Done for all of them.
|
| + bool ok = listing->HandleDirectory(utf8_encoded_path); |
|
Søren Gjesse
2012/11/13 12:48:45
Maybe add ASSERT(utf8_encoded_path != path).
Mads Ager (google)
2012/11/13 14:50:17
I think I would rather go with your suggestion of
|
| + free(utf8_encoded_path); |
| if (!ok) return ok; |
| if (recursive) { |
| return ListRecursively(path, recursive, listing); |
| @@ -74,7 +76,10 @@ static bool HandleFile(char* file_name, |
| if (written != strlen(file_name)) { |
| return false; |
| }; |
| - return listing->HandleFile(path); |
| + char* utf8_encoded_path = StringUtils::SystemStringToUtf8(path); |
| + bool ok = listing->HandleFile(utf8_encoded_path); |
| + free(utf8_encoded_path); |
| + return ok; |
| } |
| @@ -125,7 +130,9 @@ static bool ComputeFullSearchPath(const char* dir_name, |
| static void PostError(DirectoryListing* listing, |
| const char* dir_name) { |
| - listing->HandleError(dir_name); |
| + const char* utf8_encoded_path = StringUtils::SystemStringToUtf8(dir_name); |
| + listing->HandleError(utf8_encoded_path); |
| + free(const_cast<char*>(utf8_encoded_path)); |
| } |
| @@ -314,44 +321,61 @@ static bool DeleteRecursively(const char* dir_name) { |
| bool Directory::List(const char* dir_name, |
| bool recursive, |
| DirectoryListing* listing) { |
| - bool completed = ListRecursively(dir_name, recursive, listing); |
| + const char* system_encoded_name = StringUtils::Utf8ToSystemString(dir_name); |
| + bool completed = ListRecursively(system_encoded_name, recursive, listing); |
| + free(const_cast<char*>(system_encoded_name)); |
| return completed; |
| } |
| -Directory::ExistsResult Directory::Exists(const char* dir_name) { |
| +static Directory::ExistsResult ExistsHelper(const char* dir_name) { |
| DWORD attributes = GetFileAttributes(dir_name); |
| if (attributes == INVALID_FILE_ATTRIBUTES) { |
| DWORD last_error = GetLastError(); |
| if (last_error == ERROR_FILE_NOT_FOUND || |
| last_error == ERROR_PATH_NOT_FOUND) { |
| - return DOES_NOT_EXIST; |
| + return Directory::DOES_NOT_EXIST; |
| } else { |
| // We might not be able to get the file attributes for other |
| // reasons such as lack of permissions. In that case we do |
| // not know if the directory exists. |
| - return UNKNOWN; |
| + return Directory::UNKNOWN; |
| } |
| } |
| bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| - return exists ? EXISTS : DOES_NOT_EXIST; |
| + return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; |
| +} |
| + |
| + |
| +Directory::ExistsResult Directory::Exists(const char* dir_name) { |
| + const char* system_encoded_name = StringUtils::Utf8ToSystemString(dir_name); |
| + Directory::ExistsResult result = ExistsHelper(system_encoded_name); |
| + free(const_cast<char*>(system_encoded_name)); |
| + return result; |
| } |
| char* Directory::Current() { |
| - char* result; |
| int length = GetCurrentDirectory(0, NULL); |
| - result = reinterpret_cast<char*>(malloc(length + 1)); |
| - GetCurrentDirectory(length + 1, result); |
| + char* current = reinterpret_cast<char*>(malloc(length + 1)); |
| + GetCurrentDirectory(length + 1, current); |
| + char* result = StringUtils::SystemStringToUtf8(current); |
| + free(current); |
| return result; |
| } |
| bool Directory::Create(const char* dir_name) { |
| + const char* system_encoded_name = StringUtils::Utf8ToSystemString(dir_name); |
| // If the directory already exists and is a directory do not |
| // attempt to create it again and treat it as a success. |
| - if (Exists(dir_name) == EXISTS) return true; |
| - return (CreateDirectory(dir_name, NULL) != 0); |
| + if (ExistsHelper(system_encoded_name) == EXISTS) { |
| + free(const_cast<char*>(system_encoded_name)); |
| + return true; |
| + } |
| + int create_status = CreateDirectory(system_encoded_name, NULL); |
| + free(const_cast<char*>(system_encoded_name)); |
| + return (create_status != 0); |
| } |
| @@ -369,7 +393,10 @@ char* Directory::CreateTemp(const char* const_template) { |
| return NULL; |
| } |
| } else { |
| - snprintf(path, MAX_PATH, "%s", const_template); |
| + const char* system_encoded_template = |
| + StringUtils::Utf8ToSystemString(const_template); |
| + snprintf(path, MAX_PATH, "%s", system_encoded_template); |
| + free(const_cast<char*>(system_encoded_template)); |
| path_length = strlen(path); |
| } |
| // Length of tempdir-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 44. |
| @@ -401,30 +428,44 @@ char* Directory::CreateTemp(const char* const_template) { |
| free(path); |
| return NULL; |
| } |
| - return path; |
| + char* result = StringUtils::SystemStringToUtf8(path); |
| + free(path); |
| + return result; |
| } |
| bool Directory::Delete(const char* dir_name, bool recursive) { |
| + bool result = false; |
| + const char* system_encoded_dir_name = |
| + StringUtils::Utf8ToSystemString(dir_name); |
| if (!recursive) { |
| - return (RemoveDirectory(dir_name) != 0); |
| + result = (RemoveDirectory(system_encoded_dir_name) != 0); |
| } else { |
| - return DeleteRecursively(dir_name); |
| + result = DeleteRecursively(system_encoded_dir_name); |
| } |
| + free(const_cast<char*>(system_encoded_dir_name)); |
| + return result; |
| } |
| bool Directory::Rename(const char* path, const char* new_path) { |
| - ExistsResult exists = Exists(path); |
| + const char* system_encoded_path = StringUtils::Utf8ToSystemString(path); |
| + const char* system_encoded_new_path = |
| + StringUtils::Utf8ToSystemString(new_path); |
| + ExistsResult exists = ExistsHelper(system_encoded_path); |
| if (exists != EXISTS) return false; |
| - ExistsResult new_exists = Exists(new_path); |
| + ExistsResult new_exists = ExistsHelper(system_encoded_new_path); |
| // MoveFile does not allow replacing exising directories. Therefore, |
| // if the new_path is currently a directory we need to delete it |
| // first. |
| if (new_exists == EXISTS) { |
| - bool success = DeleteRecursively(new_path); |
| + bool success = DeleteRecursively(system_encoded_new_path); |
| if (!success) return false; |
| } |
| DWORD flags = MOVEFILE_WRITE_THROUGH; |
| - return (MoveFileEx(path, new_path, flags) != 0); |
| + int move_status = |
| + MoveFileEx(system_encoded_path, system_encoded_new_path, flags); |
| + free(const_cast<char*>(system_encoded_path)); |
| + free(const_cast<char*>(system_encoded_new_path)); |
| + return (move_status != 0); |
| } |