Fix adding multiple files with the same name via AppxPackaging apis (#226)
This commit is contained in:
Родитель
b5fe50dfb3
Коммит
676288095f
|
@ -61,6 +61,7 @@ namespace MSIX {
|
|||
MissingAppxManifestXML = ERROR_FACILITY + 0x0034,
|
||||
DuplicateFootprintFile = ERROR_FACILITY + 0x0035,
|
||||
UnknownFileNameEncoding = ERROR_FACILITY + 0x0036,
|
||||
DuplicateFile = ERROR_FACILITY + 0x0037,
|
||||
|
||||
// Signature errors
|
||||
SignatureInvalid = ERROR_FACILITY + 0x0041,
|
||||
|
|
|
@ -164,6 +164,18 @@ namespace MSIX {
|
|||
void AppxPackageWriter::AddFileToPackage(const std::string& name, IStream* stream, bool toCompress,
|
||||
bool addToBlockMap, const char* contentType, bool forceContentTypeOverride)
|
||||
{
|
||||
std::string opcFileName;
|
||||
// Don't encode [Content Type].xml
|
||||
if (contentType != nullptr)
|
||||
{
|
||||
opcFileName = Encoding::EncodeFileName(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
opcFileName = name;
|
||||
}
|
||||
auto fileInfo = m_zipWriter->PrepareToAddFile(opcFileName, toCompress);
|
||||
|
||||
// Add content type to [Content Types].xml
|
||||
if (contentType != nullptr)
|
||||
{
|
||||
|
@ -177,18 +189,6 @@ namespace MSIX {
|
|||
ThrowHrIfFailed(stream->Seek(start, StreamBase::Reference::START, nullptr));
|
||||
std::uint64_t uncompressedSize = static_cast<std::uint64_t>(end.QuadPart);
|
||||
|
||||
std::string opcFileName;
|
||||
// Don't encode [Content Type].xml
|
||||
if (contentType != nullptr)
|
||||
{
|
||||
opcFileName = Encoding::EncodeFileName(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
opcFileName = name;
|
||||
}
|
||||
auto fileInfo = m_zipWriter->PrepareToAddFile(opcFileName, toCompress);
|
||||
|
||||
// Add file to block map.
|
||||
if (addToBlockMap)
|
||||
{
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "ZipFileStream.hpp"
|
||||
#include "DeflateStream.hpp"
|
||||
#include "StreamHelper.hpp"
|
||||
#include "Encoding.hpp"
|
||||
|
||||
namespace MSIX {
|
||||
|
||||
|
@ -68,6 +69,13 @@ namespace MSIX {
|
|||
{
|
||||
ThrowErrorIf(Error::InvalidState, m_state != ZipObjectWriter::State::ReadyForLfhOrClose, "Invalid zip writer state");
|
||||
|
||||
auto result = m_centralDirectories.find(name);
|
||||
if (result != m_centralDirectories.end())
|
||||
{
|
||||
auto message = "Adding duplicated file " + Encoding::DecodeFileName(name) + "to package";
|
||||
ThrowErrorAndLog(Error::DuplicateFile, message.c_str());
|
||||
}
|
||||
|
||||
// Get position were the lfh is going to be written
|
||||
ULARGE_INTEGER pos = {0};
|
||||
ThrowHrIfFailed(m_stream->Seek({0}, StreamBase::Reference::CURRENT, &pos));
|
||||
|
|
|
@ -352,13 +352,46 @@ TEST_CASE("Api_AppxPackageWriter_closed", "[api]")
|
|||
fileStream.Get()));
|
||||
}
|
||||
|
||||
TEST_CASE("Api_AppxPackageWriter_add_same_payload_file")
|
||||
{
|
||||
auto outputStream = MsixTest::StreamFile("test_package.msix", false, true);
|
||||
|
||||
MsixTest::ComPtr<IAppxPackageWriter> packageWriter;
|
||||
InitializePackageWriter(outputStream.Get(), &packageWriter);
|
||||
|
||||
auto contentStream = MsixTest::StreamFile("test_file.txt", false, true);
|
||||
WriteContentToStream(618963, contentStream.Get());
|
||||
|
||||
// Adding file same file twice
|
||||
REQUIRE_SUCCEEDED(packageWriter->AddPayloadFile(
|
||||
TestConstants::GoodFileNames[1].second.c_str(),
|
||||
TestConstants::ContentType.c_str(),
|
||||
APPX_COMPRESSION_OPTION_NORMAL,
|
||||
contentStream.Get()));
|
||||
|
||||
REQUIRE_HR(static_cast<HRESULT>(MSIX::Error::DuplicateFile),
|
||||
packageWriter->AddPayloadFile(
|
||||
TestConstants::GoodFileNames[1].second.c_str(),
|
||||
TestConstants::ContentType.c_str(),
|
||||
APPX_COMPRESSION_OPTION_NORMAL,
|
||||
contentStream.Get()));
|
||||
|
||||
// The package should be in an invalid state now.
|
||||
REQUIRE_HR(static_cast<HRESULT>(MSIX::Error::InvalidState),
|
||||
packageWriter->AddPayloadFile(
|
||||
TestConstants::GoodFileNames[0].second.c_str(),
|
||||
TestConstants::ContentType.c_str(),
|
||||
APPX_COMPRESSION_OPTION_NORMAL,
|
||||
contentStream.Get()));
|
||||
}
|
||||
|
||||
class GeneratedEasilyCompressedFileStream final : public MSIX::StreamBase
|
||||
{
|
||||
public:
|
||||
GeneratedEasilyCompressedFileStream(uint64_t size) : m_size(size) {}
|
||||
|
||||
// IStream
|
||||
HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER move, DWORD origin, ULARGE_INTEGER* newPosition) noexcept override try
|
||||
// IStream
|
||||
HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER move, DWORD origin, ULARGE_INTEGER* newPosition) noexcept override try
|
||||
{
|
||||
// Determine new range relative position
|
||||
LARGE_INTEGER newPos = { 0 };
|
||||
|
@ -386,7 +419,7 @@ public:
|
|||
}
|
||||
|
||||
if (newPosition) { newPosition->QuadPart = m_offset; }
|
||||
return static_cast<HRESULT>(MSIX::Error::OK);
|
||||
return static_cast<HRESULT>(MSIX::Error::OK);
|
||||
} CATCH_RETURN();
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Read(void* buffer, ULONG countBytes, ULONG* bytesRead) noexcept override try
|
||||
|
|
Загрузка…
Ссылка в новой задаче