Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions src/windows/common/wslutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,20 @@ std::wstring wsl::windows::common::wslutil::DownloadFileImpl(
Filename = Url.substr(lastSlash + 1);
}

const auto downloadFolder =
winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync(std::filesystem::temp_directory_path().wstring()).get();
// GetFolderFromPathAsync does not accept hidden or system path.
// To avoid the temp path having those attributes (mainly hidden),
// we need to create a subfolder without those attributes.
auto downloadFolderPath = std::filesystem::temp_directory_path() / L"wsl-downloads";
std::filesystem::create_directories(downloadFolderPath);

const auto attributes = GetFileAttributesW(downloadFolderPath.c_str());
THROW_LAST_ERROR_IF(attributes == INVALID_FILE_ATTRIBUTES);
if (attributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
{
THROW_IF_WIN32_BOOL_FALSE(SetFileAttributesW(downloadFolderPath.c_str(), attributes & ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)));
}
Comment thread
chemwolf6922 marked this conversation as resolved.

const auto downloadFolder = winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync(downloadFolderPath.wstring()).get();

const auto file =
downloadFolder.CreateFileAsync(Filename, winrt::Windows::Storage::CreationCollisionOption::GenerateUniqueName).get();
Expand Down
22 changes: 20 additions & 2 deletions test/windows/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2474,14 +2474,32 @@ void Trim(std::wstring& string)
std::erase_if(string, [](auto c) { return !isalnum(c); });
}

ScopedEnvVariable::ScopedEnvVariable(const std::wstring& Name, const std::wstring& Value) : m_name(Name)
ScopedEnvVariable::ScopedEnvVariable(const std::wstring& Name, const std::wstring& Value, bool restore) : m_name(Name)
{
if (restore)
{
std::wstring value;
const auto result = wil::GetEnvironmentVariableW(Name.c_str(), value);
if (result != HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND))
{
VERIFY_SUCCEEDED(result);
m_originalValue = std::move(value);
}
}

VERIFY_IS_TRUE(SetEnvironmentVariable(Name.c_str(), Value.c_str()));
}

ScopedEnvVariable::~ScopedEnvVariable()
{
VERIFY_IS_TRUE(SetEnvironmentVariable(m_name.c_str(), nullptr));
if (m_originalValue.has_value())
{
VERIFY_IS_TRUE(SetEnvironmentVariable(m_name.c_str(), m_originalValue->c_str()));
}
else
{
VERIFY_IS_TRUE(SetEnvironmentVariable(m_name.c_str(), nullptr));
}
}

UniqueWebServer::UniqueWebServer(LPCWSTR Endpoint, LPCWSTR Content)
Expand Down
9 changes: 4 additions & 5 deletions test/windows/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,15 @@ class RegistryKeyChange
class ScopedEnvVariable
{
public:
ScopedEnvVariable(const std::wstring& Name, const std::wstring& Value);
ScopedEnvVariable(const std::wstring& Name, const std::wstring& Value, bool restore = false);
~ScopedEnvVariable();

ScopedEnvVariable(const WslConfigChange&) = delete;
ScopedEnvVariable(WslConfigChange&&) = delete;
const ScopedEnvVariable& operator=(ScopedEnvVariable&&) = delete;
const ScopedEnvVariable& operator=(ScopedEnvVariable&) = delete;
NON_COPYABLE(ScopedEnvVariable);
NON_MOVABLE(ScopedEnvVariable);

private:
std::wstring m_name;
std::optional<std::wstring> m_originalValue{std::nullopt};
};

class UniqueWebServer
Expand Down
27 changes: 26 additions & 1 deletion test/windows/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5143,7 +5143,7 @@ Error code: Wsl/Service/RegisterDistro/E_INVALIDARG\r\n";
wsl::shared::string::MultiByteToWide("01:23:45:67:89:AB"));
}

TEST_METHOD(ModernDistroInstall)
static void ModernDistroInstallImpl()
{
auto tarPath = "file://" + wsl::shared::string::WideToMultiByte(EscapePath(g_testDistroPath));

Expand Down Expand Up @@ -5779,6 +5779,31 @@ Error code: Wsl/InstallDistro/WSL_E_INVALID_JSON\r\n",
}
}

TEST_METHOD(ModernDistroInstall)
{
ModernDistroInstallImpl();
}

TEST_METHOD(ModernDistroInstallWithHiddenTempFolder)
{
// Avoid contaminating the real temp folder.
const auto testTempFolder = std::filesystem::temp_directory_path() / L"wsl-install-test";
std::filesystem::create_directories(testTempFolder);
Comment thread
chemwolf6922 marked this conversation as resolved.
auto cleanupTempFolder = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] {
std::error_code error;
std::filesystem::remove_all(testTempFolder, error);
});

const auto originalAttributes = GetFileAttributesW(testTempFolder.c_str());
VERIFY_IS_TRUE(originalAttributes != INVALID_FILE_ATTRIBUTES);
VERIFY_IS_TRUE(SetFileAttributesW(testTempFolder.c_str(), originalAttributes | FILE_ATTRIBUTE_HIDDEN));
Comment thread
chemwolf6922 marked this conversation as resolved.

ScopedEnvVariable temp(L"TEMP", testTempFolder.wstring(), true);
ScopedEnvVariable tmp(L"TMP", testTempFolder.wstring(), true);

ModernDistroInstallImpl();
}
Comment thread
chemwolf6922 marked this conversation as resolved.

TEST_METHOD(ModernInstallEndToEnd)
{
constexpr auto tarName = L"end2end.tar";
Expand Down