From 3459964c672aa101a509cf84826e2f5359ed0f1e Mon Sep 17 00:00:00 2001 From: Dwayne Slater Date: Sun, 15 Apr 2018 14:25:58 -0400 Subject: [PATCH] Fix opening assets on Windows Two bugs: To open a directory with CreateFile, FILE_FLAG_BACKUP_SEMANTICS needs to be specified. GetFinalPathNameByHandle returns a UNC path, so forward slashes need to be converted to backslashes. --- fml/platform/win/file_win.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/fml/platform/win/file_win.cc b/fml/platform/win/file_win.cc index 3f5e90494b013..c07c7d5e5a5c0 100644 --- a/fml/platform/win/file_win.cc +++ b/fml/platform/win/file_win.cc @@ -6,6 +6,7 @@ #include +#include #include #include "flutter/fml/platform/win/wstring_conversion.h" @@ -36,13 +37,19 @@ fml::UniqueFD OpenFile(const std::wstring& path, break; } + DWORD flags = FILE_ATTRIBUTE_NORMAL; + + if (is_directory) { + flags |= FILE_FLAG_BACKUP_SEMANTICS; + } + return fml::UniqueFD{::CreateFile( path.c_str(), // lpFileName desired_access, // dwDesiredAccess FILE_SHARE_READ, // dwShareMode 0, // lpSecurityAttributes OPEN_EXISTING, // dwCreationDisposition - FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes + flags, // dwFlagsAndAttributes 0 // hTemplateFile )}; } @@ -83,7 +90,10 @@ fml::UniqueFD OpenFile(const fml::UniqueFD& base_directory, std::wstringstream stream; stream << GetFullHandlePath(base_directory) << "\\" << path; - return OpenFile(stream.str(), permission, is_directory); + std::wstring native_path = stream.str(); + // native_path is a UNC path, so transform any forward slashes into back slashes + std::replace(native_path.begin(), native_path.end(), '/', '\\'); + return OpenFile(native_path, permission, is_directory); } fml::UniqueFD Duplicate(fml::UniqueFD::element_type descriptor) {