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
1 change: 1 addition & 0 deletions doc/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The PowerShell module now automatically uses `GH_TOKEN` or `GITHUB_TOKEN` enviro

## Bug Fixes

* `winget export` now works when the destination path is a hidden file
* Fixed the `useLatest` property in the DSC v3 `Microsoft.WinGet/Package` resource schema to emit a boolean default (`false`) instead of the incorrect string `"false"`.
* `SignFile` in `WinGetSourceCreator` now supports an optional RFC 3161 timestamp server via the new `TimestampServer` property on the `Signature` model. When set, `signtool.exe` is called with `/tr <url> /td sha256`, embedding a countersignature timestamp so that signed packages remain valid after the signing certificate expires.
* File and directory paths passed to `signtool.exe` and `makeappx.exe` are now quoted, fixing failures when paths contain spaces.
Expand Down
18 changes: 16 additions & 2 deletions src/AppInstallerCLICore/Workflows/ImportExportFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "PackageCollection.h"
#include "DependenciesFlow.h"
#include "WorkflowBase.h"
#include <winget/Filesystem.h>
#include <winget/RepositorySearch.h>
#include <winget/Runtime.h>
#include <winget/PackageVersionSelection.h>
Expand Down Expand Up @@ -177,8 +178,21 @@ namespace AppInstaller::CLI::Workflow
auto packages = PackagesJson::CreateJson(context.Get<Execution::Data::PackageCollection>());

std::filesystem::path outputFilePath{ context.Args.GetArg(Execution::Args::Type::OutputFile) };
std::ofstream outputFileStream{ outputFilePath };
outputFileStream << packages;

// GetFileAttributesW returns INVALID_FILE_ATTRIBUTES for nonexistent files, so no separate exists() check is needed.
DWORD attrs = GetFileAttributesW(outputFilePath.c_str());
bool isHidden = (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_HIDDEN));

// Open the file directly without changing its attributes:
// - For an existing hidden file, use TRUNCATE_EXISTING to clear its content while preserving its attributes.
// - Otherwise, use CREATE_ALWAYS to create a new file or overwrite an existing one.
DWORD creationDisposition = isHidden ? TRUNCATE_EXISTING : CREATE_ALWAYS;
wil::unique_hfile fileHandle{ CreateFileW(outputFilePath.c_str(), GENERIC_WRITE, 0, nullptr, creationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr) };
THROW_LAST_ERROR_IF(!fileHandle);

Json::StreamWriterBuilder writerBuilder;
std::string jsonContent = Json::writeString(writerBuilder, packages);
Filesystem::WriteStringToFile(fileHandle.get(), jsonContent);
}

void ReadImportFile(Execution::Context& context)
Expand Down
50 changes: 50 additions & 0 deletions src/AppInstallerCLITests/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,56 @@ TEST_CASE("PathTree_VisitIf_Correct", "[filesystem][pathtree]")
pathTree.VisitIf(L"C:", check_input, if_input);
}

TEST_CASE("WriteStringToFile", "[filesystem]")
{
SECTION("Basic content")
{
TestCommon::TempDirectory tempDirectory{ "WriteStringToFile" };
auto tempFile = tempDirectory.CreateTempFile("output", ".txt");
wil::unique_hfile fileHandle{ CreateFileW(tempFile.GetPath().c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr) };
REQUIRE(fileHandle);

std::string content = "Hello, WinGet!";
REQUIRE_NOTHROW(WriteStringToFile(fileHandle.get(), content));
fileHandle.reset();

std::ifstream readBack{ tempFile.GetPath() };
std::string result{ std::istreambuf_iterator<char>(readBack), std::istreambuf_iterator<char>() };
REQUIRE(result == content);
}

SECTION("Empty content")
{
TestCommon::TempDirectory tempDirectory{ "WriteStringToFile" };
auto tempFile = tempDirectory.CreateTempFile("empty", ".txt");
wil::unique_hfile fileHandle{ CreateFileW(tempFile.GetPath().c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr) };
REQUIRE(fileHandle);

REQUIRE_NOTHROW(WriteStringToFile(fileHandle.get(), ""));
fileHandle.reset();

std::ifstream readBack{ tempFile.GetPath() };
std::string result{ std::istreambuf_iterator<char>(readBack), std::istreambuf_iterator<char>() };
REQUIRE(result.empty());
}

SECTION("Large content")
{
TestCommon::TempDirectory tempDirectory{ "WriteStringToFile" };
auto tempFile = tempDirectory.CreateTempFile("large", ".txt");
wil::unique_hfile fileHandle{ CreateFileW(tempFile.GetPath().c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr) };
REQUIRE(fileHandle);

std::string content(1 << 20, 'x'); // 1 MiB of 'x'
REQUIRE_NOTHROW(WriteStringToFile(fileHandle.get(), content));
fileHandle.reset();

std::ifstream readBack{ tempFile.GetPath() };
std::string result{ std::istreambuf_iterator<char>(readBack), std::istreambuf_iterator<char>() };
REQUIRE(result == content);
}
}

TEST_CASE("GetFileInfoFor", "[filesystem]")
{
TestCommon::TempDirectory tempDirectory{ "GetFileInfoFor" };
Expand Down
16 changes: 16 additions & 0 deletions src/AppInstallerSharedLib/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,20 @@ namespace AppInstaller::Filesystem

files.resize(i);
}

void WriteStringToFile(HANDLE fileHandle, std::string_view content)
{
size_t totalBytesWritten = 0;
while (totalBytesWritten < content.size())
{
DWORD bytesWritten = 0;
THROW_LAST_ERROR_IF(!WriteFile(
fileHandle,
content.data() + totalBytesWritten,
static_cast<DWORD>(content.size() - totalBytesWritten),
&bytesWritten,
nullptr));
totalBytesWritten += bytesWritten;
}
}
}
4 changes: 4 additions & 0 deletions src/AppInstallerSharedLib/Public/winget/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <filesystem>
#include <map>
#include <optional>
#include <string_view>
#include <vector>
#include <shtypes.h>

Expand Down Expand Up @@ -148,4 +149,7 @@ namespace AppInstaller::Filesystem

// Modifies the given files to only include those that exceed the limits that are provided.
void FilterToFilesExceedingLimits(std::vector<FileInfo>& files, const FileLimits& limits);

// Writes the given string to the file handle, handling partial writes.
void WriteStringToFile(HANDLE fileHandle, std::string_view content);
}