Fix adding multiple files with the same name via AppxPackaging apis (#226)

This commit is contained in:
Ruben Guerrero 2019-10-01 17:02:38 -07:00 коммит произвёл GitHub
Родитель b5fe50dfb3
Коммит 676288095f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 57 добавлений и 15 удалений

Просмотреть файл

@ -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