Skip to content

Commit

Permalink
[fml] Handle absolute paths on Windows (flutter#33029)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderso authored Apr 30, 2022
1 parent 77c6a02 commit b8d80cd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
27 changes: 27 additions & 0 deletions fml/file_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
26 changes: 22 additions & 4 deletions fml/platform/win/file_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit b8d80cd

Please sign in to comment.