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

Unified Diff: runtime/bin/file_win.cc

Issue 2439173002: [windows] Make most file_win.cc functions use malloc for string conversions. (Closed)
Patch Set: Fix typo Created 4 years, 2 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 | « runtime/bin/file_system_watcher_win.cc ('k') | runtime/bin/socket_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/file_win.cc
diff --git a/runtime/bin/file_win.cc b/runtime/bin/file_win.cc
index d1e771948cfaefefbcee1834d455cc32c65270aa..fc72a78f3fdb62ffe521f994f75dcab216a4ec1a 100644
--- a/runtime/bin/file_win.cc
+++ b/runtime/bin/file_win.cc
@@ -233,21 +233,9 @@ File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) {
}
-File* File::ScopedOpen(const char* name, FileOpenMode mode) {
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- return FileOpenW(system_name, mode);
-}
-
-
File* File::Open(const char* path, FileOpenMode mode) {
- int path_len = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
- wchar_t* system_name = new wchar_t[path_len];
- if (system_name == NULL) {
- return NULL;
- }
- MultiByteToWideChar(CP_UTF8, 0, path, -1, system_name, path_len);
- File* file = FileOpenW(system_name, mode);
- delete[] system_name;
+ Utf8ToWideScope system_name(path);
+ File* file = FileOpenW(system_name.wide(), mode);
return file;
}
@@ -270,8 +258,8 @@ File* File::OpenStdio(int fd) {
bool File::Exists(const char* name) {
struct __stat64 st;
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- bool stat_status = _wstat64(system_name, &st);
+ Utf8ToWideScope system_name(name);
+ bool stat_status = _wstat64(system_name.wide(), &st);
if (stat_status == 0) {
return ((st.st_mode & S_IFMT) == S_IFREG);
} else {
@@ -281,8 +269,8 @@ bool File::Exists(const char* name) {
bool File::Create(const char* name) {
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- int fd = _wopen(system_name, O_RDONLY | O_CREAT, 0666);
+ Utf8ToWideScope system_name(name);
+ int fd = _wopen(system_name.wide(), O_RDONLY | O_CREAT, 0666);
if (fd < 0) {
return false;
}
@@ -326,17 +314,17 @@ static const int kMountPointHeaderSize = 4 * sizeof USHORT;
bool File::CreateLink(const char* utf8_name, const char* utf8_target) {
- const wchar_t* name = StringUtilsWin::Utf8ToWide(utf8_name);
- int create_status = CreateDirectoryW(name, NULL);
+ Utf8ToWideScope name(utf8_name);
+ int create_status = CreateDirectoryW(name.wide(), NULL);
// If the directory already existed, treat it as a success.
if ((create_status == 0) &&
((GetLastError() != ERROR_ALREADY_EXISTS) ||
- ((GetFileAttributesW(name) & FILE_ATTRIBUTE_DIRECTORY) != 0))) {
+ ((GetFileAttributesW(name.wide()) & FILE_ATTRIBUTE_DIRECTORY) != 0))) {
return false;
}
HANDLE dir_handle = CreateFileW(
- name,
+ name.wide(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
@@ -347,8 +335,8 @@ bool File::CreateLink(const char* utf8_name, const char* utf8_target) {
return false;
}
- const wchar_t* target = StringUtilsWin::Utf8ToWide(utf8_target);
- int target_len = wcslen(target);
+ Utf8ToWideScope target(utf8_target);
+ int target_len = wcslen(target.wide());
if (target_len > MAX_PATH - 1) {
CloseHandle(dir_handle);
return false;
@@ -357,13 +345,13 @@ bool File::CreateLink(const char* utf8_name, const char* utf8_target) {
int reparse_data_buffer_size =
sizeof REPARSE_DATA_BUFFER + 2 * MAX_PATH * sizeof WCHAR;
REPARSE_DATA_BUFFER* reparse_data_buffer =
- reinterpret_cast<REPARSE_DATA_BUFFER*>(Dart_ScopeAllocate(
- reparse_data_buffer_size));
+ reinterpret_cast<REPARSE_DATA_BUFFER*>(malloc(reparse_data_buffer_size));
reparse_data_buffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
- wcscpy(reparse_data_buffer->MountPointReparseBuffer.PathBuffer, target);
+ wcscpy(reparse_data_buffer->MountPointReparseBuffer.PathBuffer,
+ target.wide());
wcscpy(
reparse_data_buffer->MountPointReparseBuffer.PathBuffer + target_len + 1,
- target);
+ target.wide());
reparse_data_buffer->MountPointReparseBuffer.SubstituteNameOffset = 0;
reparse_data_buffer->MountPointReparseBuffer.SubstituteNameLength =
target_len * sizeof WCHAR;
@@ -383,6 +371,7 @@ bool File::CreateLink(const char* utf8_name, const char* utf8_target) {
0,
&dummy_received_bytes,
NULL);
+ free(reparse_data_buffer);
if (CloseHandle(dir_handle) == 0) {
return false;
}
@@ -391,20 +380,20 @@ bool File::CreateLink(const char* utf8_name, const char* utf8_target) {
bool File::Delete(const char* name) {
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- int status = _wremove(system_name);
+ Utf8ToWideScope system_name(name);
+ int status = _wremove(system_name.wide());
return status != -1;
}
bool File::DeleteLink(const char* name) {
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
+ Utf8ToWideScope system_name(name);
bool result = false;
- DWORD attributes = GetFileAttributesW(system_name);
+ DWORD attributes = GetFileAttributesW(system_name.wide());
if ((attributes != INVALID_FILE_ATTRIBUTES) &&
(attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
// It's a junction(link), delete it.
- result = (RemoveDirectoryW(system_name) != 0);
+ result = (RemoveDirectoryW(system_name.wide()) != 0);
} else {
SetLastError(ERROR_NOT_A_REPARSE_POINT);
}
@@ -415,11 +404,11 @@ bool File::DeleteLink(const char* name) {
bool File::Rename(const char* old_path, const char* new_path) {
File::Type type = GetType(old_path, false);
if (type == kIsFile) {
- const wchar_t* system_old_path = StringUtilsWin::Utf8ToWide(old_path);
- const wchar_t* system_new_path = StringUtilsWin::Utf8ToWide(new_path);
+ Utf8ToWideScope system_old_path(old_path);
+ Utf8ToWideScope system_new_path(new_path);
DWORD flags = MOVEFILE_WRITE_THROUGH | MOVEFILE_REPLACE_EXISTING;
int move_status =
- MoveFileExW(system_old_path, system_new_path, flags);
+ MoveFileExW(system_old_path.wide(), system_new_path.wide(), flags);
return (move_status != 0);
} else {
SetLastError(ERROR_FILE_NOT_FOUND);
@@ -431,11 +420,11 @@ bool File::Rename(const char* old_path, const char* new_path) {
bool File::RenameLink(const char* old_path, const char* new_path) {
File::Type type = GetType(old_path, false);
if (type == kIsLink) {
- const wchar_t* system_old_path = StringUtilsWin::Utf8ToWide(old_path);
- const wchar_t* system_new_path = StringUtilsWin::Utf8ToWide(new_path);
+ Utf8ToWideScope system_old_path(old_path);
+ Utf8ToWideScope system_new_path(new_path);
DWORD flags = MOVEFILE_WRITE_THROUGH | MOVEFILE_REPLACE_EXISTING;
int move_status =
- MoveFileExW(system_old_path, system_new_path, flags);
+ MoveFileExW(system_old_path.wide(), system_new_path.wide(), flags);
return (move_status != 0);
} else {
SetLastError(ERROR_FILE_NOT_FOUND);
@@ -447,10 +436,10 @@ bool File::RenameLink(const char* old_path, const char* new_path) {
bool File::Copy(const char* old_path, const char* new_path) {
File::Type type = GetType(old_path, false);
if (type == kIsFile) {
- const wchar_t* system_old_path = StringUtilsWin::Utf8ToWide(old_path);
- const wchar_t* system_new_path = StringUtilsWin::Utf8ToWide(new_path);
- bool success = CopyFileExW(system_old_path,
- system_new_path,
+ Utf8ToWideScope system_old_path(old_path);
+ Utf8ToWideScope system_new_path(new_path);
+ bool success = CopyFileExW(system_old_path.wide(),
+ system_new_path.wide(),
NULL,
NULL,
NULL,
@@ -465,8 +454,8 @@ bool File::Copy(const char* old_path, const char* new_path) {
int64_t File::LengthFromPath(const char* name) {
struct __stat64 st;
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- int stat_status = _wstat64(system_name, &st);
+ Utf8ToWideScope system_name(name);
+ int stat_status = _wstat64(system_name.wide(), &st);
if (stat_status == 0) {
return st.st_size;
}
@@ -565,8 +554,8 @@ void File::Stat(const char* name, int64_t* data) {
data[kType] = type;
if (type != kDoesNotExist) {
struct _stat64 st;
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- int stat_status = _wstat64(system_name, &st);
+ Utf8ToWideScope system_name(name);
+ int stat_status = _wstat64(system_name.wide(), &st);
if (stat_status == 0) {
data[kCreatedTime] = st.st_ctime * 1000;
data[kModifiedTime] = st.st_mtime * 1000;
@@ -582,8 +571,8 @@ void File::Stat(const char* name, int64_t* data) {
time_t File::LastModified(const char* name) {
struct __stat64 st;
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(name);
- int stat_status = _wstat64(system_name, &st);
+ Utf8ToWideScope system_name(name);
+ int stat_status = _wstat64(system_name.wide(), &st);
if (stat_status == 0) {
return st.st_mtime;
}
@@ -603,9 +592,9 @@ bool File::IsAbsolutePath(const char* pathname) {
const char* File::GetCanonicalPath(const char* pathname) {
- const wchar_t* system_name = StringUtilsWin::Utf8ToWide(pathname);
+ Utf8ToWideScope system_name(pathname);
HANDLE file_handle = CreateFileW(
- system_name,
+ system_name.wide(),
0,
FILE_SHARE_READ,
NULL,
@@ -639,7 +628,7 @@ const char* File::GetCanonicalPath(const char* pathname) {
if ((result_size < MAX_PATH - 1 + 4) &&
(result_size > 4) &&
(wcsncmp(path, L"\\\\?\\", 4) == 0) &&
- (wcsncmp(system_name, L"\\\\?\\", 4) != 0)) {
+ (wcsncmp(system_name.wide(), L"\\\\?\\", 4) != 0)) {
result = StringUtilsWin::WideToUtf8(path + 4);
} else {
result = StringUtilsWin::WideToUtf8(path);
@@ -670,19 +659,15 @@ File::StdioHandleType File::GetStdioHandleType(int fd) {
File::Type File::GetType(const char* pathname, bool follow_links) {
// Convert to wchar_t string.
- int name_len = MultiByteToWideChar(CP_UTF8, 0, pathname, -1, NULL, 0);
- wchar_t* name;
- name = new wchar_t[name_len];
- MultiByteToWideChar(CP_UTF8, 0, pathname, -1, name, name_len);
-
- DWORD attributes = GetFileAttributesW(name);
+ Utf8ToWideScope name(pathname);
+ DWORD attributes = GetFileAttributesW(name.wide());
File::Type result = kIsFile;
if (attributes == INVALID_FILE_ATTRIBUTES) {
result = kDoesNotExist;
} else if ((attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
if (follow_links) {
HANDLE dir_handle = CreateFileW(
- name,
+ name.wide(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
@@ -701,7 +686,6 @@ File::Type File::GetType(const char* pathname, bool follow_links) {
} else if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
result = kIsDirectory;
}
- delete[] name;
return result;
}
@@ -710,9 +694,9 @@ File::Identical File::AreIdentical(const char* file_1, const char* file_2) {
BY_HANDLE_FILE_INFORMATION file_info[2];
const char* file_names[2] = { file_1, file_2 };
for (int i = 0; i < 2; ++i) {
- const wchar_t* wide_name = StringUtilsWin::Utf8ToWide(file_names[i]);
+ Utf8ToWideScope wide_name(file_names[i]);
HANDLE file_handle = CreateFileW(
- wide_name,
+ wide_name.wide(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
« no previous file with comments | « runtime/bin/file_system_watcher_win.cc ('k') | runtime/bin/socket_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698