[fml] Handle absolute paths on Windows (flutter/engine#33029)
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user