Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use copy instead of rename for moving extracted archive directories #3003

Merged
merged 9 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/AppInstallerCLICore/PortableInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,17 @@ namespace AppInstaller::CLI::Portable
}
else if (fileType == PortableFileType::Directory)
{
AICLI_LOG(Core, Info, << "Moving directory to: " << filePath);
Filesystem::RenameFile(entry.CurrentPath, filePath);
if (Filesystem::IsSameVolume(entry.CurrentPath, filePath))
{
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
AICLI_LOG(Core, Info, << "Renaming directory to: " << filePath);
Filesystem::RenameFile(entry.CurrentPath, filePath);
}
else
{
// Copy directory instead of renaming as there is a known issue with renaming across drives.
AICLI_LOG(Core, Info, << "Copying directory to: " << filePath);
std::filesystem::copy(entry.CurrentPath, filePath, std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive);
}
}
else if (entry.FileType == PortableFileType::Symlink)
{
Expand Down
26 changes: 26 additions & 0 deletions src/AppInstallerCLITests/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,30 @@ TEST_CASE("VerifySymlink", "[filesystem]")
std::filesystem::remove(symlinkPath);

REQUIRE_FALSE(SymlinkExists(symlinkPath));
}

TEST_CASE("VerifyIsSameVolume", "[filesystem]")
{
// Note: Pipeline build machine uses 'D:\' as the volume.
std::filesystem::path path1 = L"C:\\Program Files\\WinGet\\Packages";
std::filesystem::path path2 = L"c:\\Users\\testUser\\AppData\\Local\\Microsoft\\WinGet\\Packages";
std::filesystem::path path3 = L"localPath\\test\\folder";
std::filesystem::path path4 = L"\\test\\folder";

std::filesystem::path path5 = L"D:\\test\\folder";
std::filesystem::path path6 = L"F:\\test\\folder";
std::filesystem::path path7 = L"d:\\randomFolder";

// Verify that a relative path points to the current volume.
REQUIRE(IsSameVolume(path1, path2));
REQUIRE(IsSameVolume(path5, path7));
REQUIRE(IsSameVolume(path3, path4));

REQUIRE_FALSE(IsSameVolume(path1, path5));
REQUIRE_FALSE(IsSameVolume(path1, path6));
REQUIRE_FALSE(IsSameVolume(path2, path5));
REQUIRE_FALSE(IsSameVolume(path2, path6));
REQUIRE_FALSE(IsSameVolume(path3, path6));
REQUIRE_FALSE(IsSameVolume(path5, path6));
REQUIRE_FALSE(IsSameVolume(path4, path6));
}
14 changes: 14 additions & 0 deletions src/AppInstallerCommonCore/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,18 @@ namespace AppInstaller::Filesystem
THROW_IF_FAILED(SHGetKnownFolderPath(id, KF_FLAG_NO_ALIAS | KF_FLAG_DONT_VERIFY | KF_FLAG_NO_PACKAGE_REDIRECTION, NULL, &knownFolder));
return knownFolder.get();
}

bool IsSameVolume(const std::filesystem::path& path1, const std::filesystem::path& path2)
{
std::wstring path1Volume;
std::wstring path2Volume;
DWORD path1Length = static_cast<DWORD>(path1.wstring().length() + 1);
DWORD path2Length = static_cast<DWORD>(path2.wstring().length() + 1);
path1Volume.resize(path1Length);
path2Volume.resize(path2Length);

GetVolumePathNameW(path1.c_str(), path1Volume.data(), path1Length);
GetVolumePathNameW(path2.c_str(), path2Volume.data(), path2Length);
return Utility::CaseInsensitiveEquals(Utility::ConvertToUTF8(path1Volume.data()), Utility::ConvertToUTF8(path2Volume.data()));
}
}
3 changes: 3 additions & 0 deletions src/AppInstallerCommonCore/Public/winget/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ namespace AppInstaller::Filesystem

// Gets the path of a known folder.
std::filesystem::path GetKnownFolderPath(const KNOWNFOLDERID& id);

// Verifies that the paths are on the same volume.
bool IsSameVolume(const std::filesystem::path& path1, const std::filesystem::path& path2);
}