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

## Bug Fixes

<!-- Nothing yet! -->
* `winget export` now works when the destination path is a hidden file
18 changes: 16 additions & 2 deletions src/AppInstallerCLICore/Workflows/ImportExportFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,22 @@ 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);
DWORD bytesWritten = 0;
THROW_LAST_ERROR_IF(!WriteFile(fileHandle.get(), jsonContent.c_str(), static_cast<DWORD>(jsonContent.size()), &bytesWritten, nullptr));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential partial write is not handled. Would be better to have a string helper WriteStringToFile that does it.

}

void ReadImportFile(Execution::Context& context)
Expand Down
Loading