[fml] Handle absolute paths on Windows (flutter/engine#33029)

This commit is contained in:
Zachary Anderson
2022-04-30 10:39:07 -07:00
committed by GitHub
parent 4c5ee01e55
commit 511245230b
2 changed files with 49 additions and 4 deletions

View File

@@ -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<fml::DataMapping>(
std::vector<uint8_t>{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;

View File

@@ -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());
}