diff --git a/engine/src/flutter/fml/file_unittest.cc b/engine/src/flutter/fml/file_unittest.cc index 9074da4b25..56eb43ff2d 100644 --- a/engine/src/flutter/fml/file_unittest.cc +++ b/engine/src/flutter/fml/file_unittest.cc @@ -248,6 +248,33 @@ TEST(FileTest, AtomicWriteTest) { ASSERT_TRUE(fml::UnlinkFile(dir.fd(), "precious_data")); } +TEST(FileTest, IgnoreBaseDirWhenPathIsAbsolute) { + fml::ScopedTemporaryDirectory dir; + + // Make an absolute path. + std::string filename = "filename.txt"; + std::string full_path = + fml::paths::AbsolutePath(fml::paths::JoinPaths({dir.path(), filename})); + + const std::string contents = "These are my contents."; + auto data = std::make_unique( + std::vector{contents.begin(), contents.end()}); + + // Write. + ASSERT_TRUE(fml::WriteAtomically(dir.fd(), full_path.c_str(), *data)); + + // Test existence. + ASSERT_TRUE(fml::FileExists(dir.fd(), full_path.c_str())); + + // Read and verify. + ASSERT_EQ(contents, + ReadStringFromFile(fml::OpenFile(dir.fd(), full_path.c_str(), false, + fml::FilePermission::kRead))); + + // Cleanup. + ASSERT_TRUE(fml::UnlinkFile(dir.fd(), full_path.c_str())); +} + TEST(FileTest, EmptyMappingTest) { fml::ScopedTemporaryDirectory dir; diff --git a/engine/src/flutter/fml/platform/win/file_win.cc b/engine/src/flutter/fml/platform/win/file_win.cc index cc45e5af3c..17b6cbdf6f 100644 --- a/engine/src/flutter/fml/platform/win/file_win.cc +++ b/engine/src/flutter/fml/platform/win/file_win.cc @@ -34,11 +34,29 @@ static std::string GetFullHandlePath(const fml::UniqueFD& handle) { return WideStringToUtf8({buffer, buffer_size}); } +static bool IsAbsolutePath(const char* path) { + if (path == nullptr || strlen(path) == 0) { + return false; + } + + auto wpath = Utf8ToWideString({path}); + if (wpath.size() == 0) { + return false; + } + + return ::PathIsRelative(wpath.c_str()) == FALSE; +} + static std::string GetAbsolutePath(const fml::UniqueFD& base_directory, const char* subpath) { - std::stringstream stream; - stream << GetFullHandlePath(base_directory) << "\\" << subpath; - auto path = stream.str(); + std::string path; + if (IsAbsolutePath(subpath)) { + path = subpath; + } else { + std::stringstream stream; + stream << GetFullHandlePath(base_directory) << "\\" << subpath; + path = stream.str(); + } std::replace(path.begin(), path.end(), '/', '\\'); return path; } @@ -82,7 +100,7 @@ static DWORD GetFileAttributesForUtf8Path(const char* absolute_path) { static DWORD GetFileAttributesForUtf8Path(const fml::UniqueFD& base_directory, const char* path) { - std::string full_path = GetFullHandlePath(base_directory) + "\\" + path; + std::string full_path = GetAbsolutePath(base_directory, path); return GetFileAttributesForUtf8Path(full_path.c_str()); }