2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2007-09-17 03:48:03 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsZipWriter.h"
|
2014-01-21 11:10:33 +04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "StreamFunctions.h"
|
2007-09-17 03:48:03 +04:00
|
|
|
#include "nsZipDataStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIAsyncStreamCopier.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInputStreamPump.h"
|
2015-09-03 23:27:46 +03:00
|
|
|
#include "nsILoadInfo.h"
|
2007-09-17 03:48:03 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsMemory.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2007-09-17 03:48:03 +04:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIFile.h"
|
2007-09-17 03:48:03 +04:00
|
|
|
#include "prio.h"
|
|
|
|
|
|
|
|
#define ZIP_EOCDR_HEADER_SIZE 22
|
|
|
|
#define ZIP_EOCDR_HEADER_SIGNATURE 0x06054b50
|
|
|
|
|
2015-11-04 00:36:32 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
/**
|
|
|
|
* nsZipWriter is used to create and add to zip files.
|
|
|
|
* It is based on the spec available at
|
|
|
|
* http://www.pkware.com/documents/casestudies/APPNOTE.TXT.
|
|
|
|
*
|
|
|
|
* The basic structure of a zip file created is slightly simpler than that
|
|
|
|
* illustrated in the spec because certain features of the zip format are
|
|
|
|
* unsupported:
|
|
|
|
*
|
|
|
|
* [local file header 1]
|
|
|
|
* [file data 1]
|
|
|
|
* .
|
|
|
|
* .
|
|
|
|
* .
|
|
|
|
* [local file header n]
|
|
|
|
* [file data n]
|
|
|
|
* [central directory]
|
|
|
|
* [end of central directory record]
|
|
|
|
*/
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsZipWriter, nsIZipWriter,
|
|
|
|
nsIRequestObserver)
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
nsZipWriter::nsZipWriter()
|
2016-01-12 21:16:59 +03:00
|
|
|
: mCDSOffset(0)
|
|
|
|
, mCDSDirty(false)
|
|
|
|
, mInQueue(false)
|
|
|
|
{}
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
nsZipWriter::~nsZipWriter()
|
|
|
|
{
|
|
|
|
if (mStream && !mInQueue)
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::GetComment(nsACString & aComment)
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
aComment = mComment;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::SetComment(const nsACString & aComment)
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
mComment = aComment;
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsZipWriter::GetInQueue(bool *aInQueue)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
*aInQueue = mInQueue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::GetFile(nsIFile **aFile)
|
|
|
|
{
|
2008-06-03 14:42:16 +04:00
|
|
|
if (!mFile)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = mFile->Clone(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ADDREF(*aFile = file);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads file entries out of an existing zip file.
|
|
|
|
*/
|
|
|
|
nsresult nsZipWriter::ReadFile(nsIFile *aFile)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t size;
|
2007-09-17 03:48:03 +04:00
|
|
|
nsresult rv = aFile->GetFileSize(&size);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-03-18 17:19:59 +03:00
|
|
|
// If the file is too short, it cannot be a valid archive, thus we fail
|
|
|
|
// without even attempting to open it
|
|
|
|
NS_ENSURE_TRUE(size > ZIP_EOCDR_HEADER_SIZE, NS_ERROR_FILE_CORRUPTED);
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), aFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t buf[1024];
|
|
|
|
int64_t seek = size - 1024;
|
|
|
|
uint32_t length = 1024;
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(inputStream);
|
|
|
|
|
|
|
|
while (true) {
|
2009-03-18 17:19:59 +03:00
|
|
|
if (seek < 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
length += (int32_t)seek;
|
2009-03-18 17:19:59 +03:00
|
|
|
seek = 0;
|
|
|
|
}
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, seek);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
2009-12-16 02:01:08 +03:00
|
|
|
rv = ZW_ReadData(inputStream, (char *)buf, length);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to backtrack from the end of the file until we find the
|
|
|
|
* CDS signature
|
|
|
|
*/
|
|
|
|
// We know it's at least this far from the end
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t pos = length - ZIP_EOCDR_HEADER_SIZE;
|
|
|
|
(int32_t)pos >= 0; pos--) {
|
|
|
|
uint32_t sig = PEEK32(buf + pos);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (sig == ZIP_EOCDR_HEADER_SIGNATURE) {
|
|
|
|
// Skip down to entry count
|
|
|
|
pos += 10;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t entries = READ16(buf, &pos);
|
2007-09-17 03:48:03 +04:00
|
|
|
// Skip past CDS size
|
|
|
|
pos += 4;
|
|
|
|
mCDSOffset = READ32(buf, &pos);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t commentlen = READ16(buf, &pos);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
if (commentlen == 0)
|
|
|
|
mComment.Truncate();
|
|
|
|
else if (pos + commentlen <= length)
|
2009-12-16 02:01:08 +03:00
|
|
|
mComment.Assign((const char *)buf + pos, commentlen);
|
2007-09-17 03:48:03 +04:00
|
|
|
else {
|
|
|
|
if ((seek + pos + commentlen) > size) {
|
|
|
|
inputStream->Close();
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
2015-11-04 00:36:32 +03:00
|
|
|
auto field = MakeUnique<char[]>(commentlen);
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
seek + pos);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = ZW_ReadData(inputStream, field.get(), length);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mComment.Assign(field.get(), commentlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
mCDSOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t entry = 0; entry < entries; entry++) {
|
2007-09-17 03:48:03 +04:00
|
|
|
nsZipHeader* header = new nsZipHeader();
|
|
|
|
if (!header) {
|
|
|
|
inputStream->Close();
|
|
|
|
mEntryHash.Clear();
|
|
|
|
mHeaders.Clear();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
rv = header->ReadCDSHeader(inputStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
mEntryHash.Clear();
|
|
|
|
mHeaders.Clear();
|
|
|
|
return rv;
|
|
|
|
}
|
2012-05-18 21:30:49 +04:00
|
|
|
mEntryHash.Put(header->mName, mHeaders.Count());
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!mHeaders.AppendObject(header))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputStream->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seek == 0) {
|
|
|
|
// We've reached the start with no signature found. Corrupt.
|
|
|
|
inputStream->Close();
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overlap by the size of the end of cdr
|
|
|
|
seek -= (1024 - ZIP_EOCDR_HEADER_SIZE);
|
|
|
|
}
|
|
|
|
// Will never reach here in reality
|
|
|
|
NS_NOTREACHED("Loop should never complete");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsZipWriter::Open(nsIFile *aFile, int32_t aIoFlags)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
if (mStream)
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
|
|
|
|
|
|
|
// Need to be able to write to the file
|
|
|
|
if (aIoFlags & PR_RDONLY)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-09-17 19:33:04 +04:00
|
|
|
|
|
|
|
nsresult rv = aFile->Clone(getter_AddRefs(mFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2007-09-17 19:33:04 +04:00
|
|
|
rv = mFile->Exists(&exists);
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!exists && !(aIoFlags & PR_CREATE_FILE))
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
|
|
|
|
if (exists && !(aIoFlags & (PR_TRUNCATE | PR_WRONLY))) {
|
2007-09-17 19:33:04 +04:00
|
|
|
rv = ReadFile(mFile);
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = false;
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mCDSOffset = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
mComment.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Silently drop PR_APPEND
|
|
|
|
aIoFlags &= 0xef;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> stream;
|
2007-09-17 19:33:04 +04:00
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(stream), mFile, aIoFlags);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mHeaders.Clear();
|
|
|
|
mEntryHash.Clear();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-11-09 13:12:46 +03:00
|
|
|
rv = NS_NewBufferedOutputStream(getter_AddRefs(mStream), stream, 64 * 1024);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
stream->Close();
|
|
|
|
mHeaders.Clear();
|
|
|
|
mEntryHash.Clear();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCDSOffset > 0) {
|
|
|
|
rv = SeekCDS();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::GetEntry(const nsACString & aZipEntry,
|
|
|
|
nsIZipEntry **_retval)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos;
|
2007-09-17 03:48:03 +04:00
|
|
|
if (mEntryHash.Get(aZipEntry, &pos))
|
|
|
|
NS_ADDREF(*_retval = mHeaders[pos]);
|
|
|
|
else
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = nullptr;
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::HasEntry(const nsACString & aZipEntry,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *_retval)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = mEntryHash.Get(aZipEntry, nullptr);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::AddEntryDirectory(const nsACString & aZipEntry,
|
2011-09-29 10:19:26 +04:00
|
|
|
PRTime aModTime, bool aQueue)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (aQueue) {
|
|
|
|
nsZipQueueItem item;
|
|
|
|
item.mOperation = OPERATION_ADD;
|
|
|
|
item.mZipEntry = aZipEntry;
|
|
|
|
item.mModTime = aModTime;
|
2008-10-31 23:10:46 +03:00
|
|
|
item.mPermissions = PERMISSIONS_DIR;
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!mQueue.AppendElement(item))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
2008-10-31 23:10:46 +03:00
|
|
|
return InternalAddEntryDirectory(aZipEntry, aModTime, PERMISSIONS_DIR);
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::AddEntryFile(const nsACString & aZipEntry,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aCompression, nsIFile *aFile,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aQueue)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2007-09-17 19:33:04 +04:00
|
|
|
nsresult rv;
|
2007-09-17 03:48:03 +04:00
|
|
|
if (aQueue) {
|
|
|
|
nsZipQueueItem item;
|
|
|
|
item.mOperation = OPERATION_ADD;
|
|
|
|
item.mZipEntry = aZipEntry;
|
|
|
|
item.mCompression = aCompression;
|
2007-09-17 19:33:04 +04:00
|
|
|
rv = aFile->Clone(getter_AddRefs(item.mFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!mQueue.AppendElement(item))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2007-09-17 19:33:04 +04:00
|
|
|
rv = aFile->Exists(&exists);
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!exists)
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isdir;
|
2007-09-17 03:48:03 +04:00
|
|
|
rv = aFile->IsDirectory(&isdir);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-30 11:10:35 +04:00
|
|
|
PRTime modtime;
|
2007-09-17 03:48:03 +04:00
|
|
|
rv = aFile->GetLastModifiedTime(&modtime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
modtime *= PR_USEC_PER_MSEC;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t permissions;
|
2008-10-31 23:10:46 +03:00
|
|
|
rv = aFile->GetPermissions(&permissions);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
if (isdir)
|
2008-10-31 23:10:46 +03:00
|
|
|
return InternalAddEntryDirectory(aZipEntry, modtime, permissions);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mEntryHash.Get(aZipEntry, nullptr))
|
2007-09-17 03:48:03 +04:00
|
|
|
return NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream),
|
|
|
|
aFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = AddEntryStream(aZipEntry, modtime, aCompression, inputStream,
|
2011-10-17 18:59:28 +04:00
|
|
|
false, permissions);
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return inputStream->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::AddEntryChannel(const nsACString & aZipEntry,
|
|
|
|
PRTime aModTime,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aCompression,
|
2007-09-17 03:48:03 +04:00
|
|
|
nsIChannel *aChannel,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aQueue)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aChannel);
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (aQueue) {
|
|
|
|
nsZipQueueItem item;
|
|
|
|
item.mOperation = OPERATION_ADD;
|
|
|
|
item.mZipEntry = aZipEntry;
|
|
|
|
item.mModTime = aModTime;
|
|
|
|
item.mCompression = aCompression;
|
2008-10-31 23:10:46 +03:00
|
|
|
item.mPermissions = PERMISSIONS_FILE;
|
2007-09-17 03:48:03 +04:00
|
|
|
item.mChannel = aChannel;
|
|
|
|
if (!mQueue.AppendElement(item))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mEntryHash.Get(aZipEntry, nullptr))
|
2007-09-17 03:48:03 +04:00
|
|
|
return NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
2016-06-28 20:03:56 +03:00
|
|
|
nsresult rv = NS_MaybeOpenChannelUsingOpen2(aChannel,
|
|
|
|
getter_AddRefs(inputStream));
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = AddEntryStream(aZipEntry, aModTime, aCompression, inputStream,
|
2011-10-17 18:59:28 +04:00
|
|
|
false, PERMISSIONS_FILE);
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return inputStream->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::AddEntryStream(const nsACString & aZipEntry,
|
|
|
|
PRTime aModTime,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aCompression,
|
2007-09-17 03:48:03 +04:00
|
|
|
nsIInputStream *aStream,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aQueue)
|
2008-10-31 23:10:46 +03:00
|
|
|
{
|
|
|
|
return AddEntryStream(aZipEntry, aModTime, aCompression, aStream, aQueue,
|
|
|
|
PERMISSIONS_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsZipWriter::AddEntryStream(const nsACString & aZipEntry,
|
|
|
|
PRTime aModTime,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aCompression,
|
2008-10-31 23:10:46 +03:00
|
|
|
nsIInputStream *aStream,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aQueue,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aPermissions)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aStream);
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (aQueue) {
|
|
|
|
nsZipQueueItem item;
|
|
|
|
item.mOperation = OPERATION_ADD;
|
|
|
|
item.mZipEntry = aZipEntry;
|
|
|
|
item.mModTime = aModTime;
|
|
|
|
item.mCompression = aCompression;
|
2008-10-31 23:10:46 +03:00
|
|
|
item.mPermissions = aPermissions;
|
2007-09-17 03:48:03 +04:00
|
|
|
item.mStream = aStream;
|
|
|
|
if (!mQueue.AppendElement(item))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mEntryHash.Get(aZipEntry, nullptr))
|
2007-09-17 03:48:03 +04:00
|
|
|
return NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHeader> header = new nsZipHeader();
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_TRUE(header, NS_ERROR_OUT_OF_MEMORY);
|
2008-10-31 23:10:46 +03:00
|
|
|
header->Init(aZipEntry, aModTime, ZIP_ATTRS(aPermissions, ZIP_ATTRS_FILE),
|
|
|
|
mCDSOffset);
|
2007-09-17 03:48:03 +04:00
|
|
|
nsresult rv = header->WriteFileHeader(mStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
SeekCDS();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipDataStream> stream = new nsZipDataStream();
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!stream) {
|
|
|
|
SeekCDS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
rv = stream->Init(this, mStream, header, aCompression);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
SeekCDS();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = stream->ReadStream(aStream);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
SeekCDS();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::RemoveEntry(const nsACString & aZipEntry,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aQueue)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (aQueue) {
|
|
|
|
nsZipQueueItem item;
|
|
|
|
item.mOperation = OPERATION_REMOVE;
|
|
|
|
item.mZipEntry = aZipEntry;
|
|
|
|
if (!mQueue.AppendElement(item))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos;
|
2007-09-17 03:48:03 +04:00
|
|
|
if (mEntryHash.Get(aZipEntry, &pos)) {
|
|
|
|
// Flush any remaining data before we seek.
|
|
|
|
nsresult rv = mStream->Flush();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (pos < mHeaders.Count() - 1) {
|
|
|
|
// This is not the last entry, pull back the data.
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mStream);
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
mHeaders[pos]->mOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream),
|
|
|
|
mFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
seekable = do_QueryInterface(inputStream);
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
mHeaders[pos + 1]->mOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = mCDSOffset - mHeaders[pos + 1]->mOffset;
|
|
|
|
uint32_t read = 0;
|
2007-09-17 03:48:03 +04:00
|
|
|
char buf[4096];
|
|
|
|
while (count > 0) {
|
2014-01-21 11:10:33 +04:00
|
|
|
read = std::min(count, (uint32_t) sizeof(buf));
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
rv = inputStream->Read(buf, read, &read);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ZW_WriteData(mStream, buf, read);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= read;
|
|
|
|
}
|
|
|
|
inputStream->Close();
|
|
|
|
|
|
|
|
// Rewrite header offsets and update hash
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t shift = (mHeaders[pos + 1]->mOffset -
|
2007-09-17 03:48:03 +04:00
|
|
|
mHeaders[pos]->mOffset);
|
|
|
|
mCDSOffset -= shift;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos2 = pos + 1;
|
2007-09-17 03:48:03 +04:00
|
|
|
while (pos2 < mHeaders.Count()) {
|
2012-05-18 21:30:49 +04:00
|
|
|
mEntryHash.Put(mHeaders[pos2]->mName, pos2-1);
|
2007-09-17 03:48:03 +04:00
|
|
|
mHeaders[pos2]->mOffset -= shift;
|
|
|
|
pos2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Remove the last entry is just a case of moving the CDS
|
|
|
|
mCDSOffset = mHeaders[pos]->mOffset;
|
|
|
|
rv = SeekCDS();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mEntryHash.Remove(mHeaders[pos]->mName);
|
|
|
|
mHeaders.RemoveObjectAt(pos);
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::ProcessQueue(nsIRequestObserver *aObserver,
|
|
|
|
nsISupports *aContext)
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
|
|
|
|
mProcessObserver = aObserver;
|
|
|
|
mProcessContext = aContext;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInQueue = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
if (mProcessObserver)
|
2012-07-30 18:20:58 +04:00
|
|
|
mProcessObserver->OnStartRequest(nullptr, mProcessContext);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
BeginProcessingNextItem();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::Close()
|
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (mInQueue)
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
|
|
|
|
if (mCDSDirty) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = 0;
|
|
|
|
for (int32_t i = 0; i < mHeaders.Count(); i++) {
|
2007-09-17 03:48:03 +04:00
|
|
|
nsresult rv = mHeaders[i]->WriteCDSHeader(mStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
size += mHeaders[i]->GetCDSHeaderLength();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t buf[ZIP_EOCDR_HEADER_SIZE];
|
|
|
|
uint32_t pos = 0;
|
2007-09-17 03:48:03 +04:00
|
|
|
WRITE32(buf, &pos, ZIP_EOCDR_HEADER_SIGNATURE);
|
|
|
|
WRITE16(buf, &pos, 0);
|
|
|
|
WRITE16(buf, &pos, 0);
|
|
|
|
WRITE16(buf, &pos, mHeaders.Count());
|
|
|
|
WRITE16(buf, &pos, mHeaders.Count());
|
|
|
|
WRITE32(buf, &pos, size);
|
|
|
|
WRITE32(buf, &pos, mCDSOffset);
|
|
|
|
WRITE16(buf, &pos, mComment.Length());
|
|
|
|
|
2009-12-16 02:01:08 +03:00
|
|
|
nsresult rv = ZW_WriteData(mStream, (const char *)buf, pos);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ZW_WriteData(mStream, mComment.get(), mComment.Length());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mStream);
|
|
|
|
rv = seekable->SetEOF();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
2010-09-14 23:47:47 +04:00
|
|
|
|
|
|
|
// Go back and rewrite the file headers
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mHeaders.Count(); i++) {
|
2010-09-14 23:47:47 +04:00
|
|
|
nsZipHeader *header = mHeaders[i];
|
|
|
|
if (!header->mWriteOnClose)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, header->mOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = header->WriteFileHeader(mStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mStream->Close();
|
2012-07-30 18:20:58 +04:00
|
|
|
mStream = nullptr;
|
2007-09-17 03:48:03 +04:00
|
|
|
mHeaders.Clear();
|
|
|
|
mEntryHash.Clear();
|
|
|
|
mQueue.Clear();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our nsIRequestObserver monitors removal operations performed on the queue
|
|
|
|
NS_IMETHODIMP nsZipWriter::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsZipWriter::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(aStatusCode)) {
|
|
|
|
FinishQueue(aStatusCode);
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mStream->Flush();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FinishQueue(rv);
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = SeekCDS();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FinishQueue(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
BeginProcessingNextItem();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-21 11:10:33 +04:00
|
|
|
/*
|
|
|
|
* Make all stored(uncompressed) files align to given alignment size.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP nsZipWriter::AlignStoredFiles(uint16_t aAlignSize)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Check for range and power of 2.
|
|
|
|
if (aAlignSize < 2 || aAlignSize > 32768 ||
|
|
|
|
(aAlignSize & (aAlignSize - 1)) != 0) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mHeaders.Count(); i++) {
|
|
|
|
nsZipHeader *header = mHeaders[i];
|
|
|
|
|
|
|
|
// Check whether this entry is file and compression method is stored.
|
|
|
|
bool isdir;
|
|
|
|
rv = header->GetIsDirectory(&isdir);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (isdir || header->mMethod != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Pad extra field to align data starting position to specified size.
|
|
|
|
uint32_t old_len = header->mLocalFieldLength;
|
|
|
|
rv = header->PadExtraField(header->mOffset, aAlignSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// No padding means data already aligned.
|
|
|
|
uint32_t shift = header->mLocalFieldLength - old_len;
|
|
|
|
if (shift == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush any remaining data before we start.
|
|
|
|
rv = mStream->Flush();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open zip file for reading.
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), mFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> in_seekable = do_QueryInterface(inputStream);
|
|
|
|
nsCOMPtr<nsISeekableStream> out_seekable = do_QueryInterface(mStream);
|
|
|
|
|
|
|
|
uint32_t data_offset = header->mOffset + header->GetFileHeaderLength() - shift;
|
|
|
|
uint32_t count = mCDSOffset - data_offset;
|
|
|
|
uint32_t read;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
// Shift data to aligned postion.
|
|
|
|
while (count > 0) {
|
|
|
|
read = std::min(count, (uint32_t) sizeof(buf));
|
|
|
|
|
|
|
|
rv = in_seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
data_offset + count - read);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = inputStream->Read(buf, read, &read);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = out_seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
data_offset + count - read + shift);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ZW_WriteData(mStream, buf, read);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= read;
|
|
|
|
}
|
|
|
|
inputStream->Close();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update current header
|
|
|
|
rv = out_seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
header->mOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = header->WriteFileHeader(mStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update offset of all other headers
|
|
|
|
int pos = i + 1;
|
|
|
|
while (pos < mHeaders.Count()) {
|
|
|
|
mHeaders[pos]->mOffset += shift;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
mCDSOffset += shift;
|
|
|
|
rv = SeekCDS();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mCDSDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
nsresult nsZipWriter::InternalAddEntryDirectory(const nsACString & aZipEntry,
|
2008-10-31 23:10:46 +03:00
|
|
|
PRTime aModTime,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aPermissions)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHeader> header = new nsZipHeader();
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_TRUE(header, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t zipAttributes = ZIP_ATTRS(aPermissions, ZIP_ATTRS_DIRECTORY);
|
2008-10-31 23:10:46 +03:00
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
if (aZipEntry.Last() != '/') {
|
|
|
|
nsCString dirPath;
|
|
|
|
dirPath.Assign(aZipEntry + NS_LITERAL_CSTRING("/"));
|
2008-10-31 23:10:46 +03:00
|
|
|
header->Init(dirPath, aModTime, zipAttributes, mCDSOffset);
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|
|
|
|
else
|
2008-10-31 23:10:46 +03:00
|
|
|
header->Init(aZipEntry, aModTime, zipAttributes, mCDSOffset);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mEntryHash.Get(header->mName, nullptr))
|
2007-09-17 03:48:03 +04:00
|
|
|
return NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
|
|
|
|
nsresult rv = header->WriteFileHeader(mStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
mCDSOffset += header->GetFileHeaderLength();
|
2012-05-18 21:30:49 +04:00
|
|
|
mEntryHash.Put(header->mName, mHeaders.Count());
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!mHeaders.AppendObject(header)) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recovering from an error while adding a new entry is simply a case of
|
|
|
|
* seeking back to the CDS. If we fail trying to do that though then cleanup
|
|
|
|
* and bail out.
|
|
|
|
*/
|
|
|
|
nsresult nsZipWriter::SeekCDS()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mStream, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, mCDSOffset);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In a bad error condition this essentially closes down the component as best
|
|
|
|
* it can.
|
|
|
|
*/
|
|
|
|
void nsZipWriter::Cleanup()
|
|
|
|
{
|
|
|
|
mHeaders.Clear();
|
|
|
|
mEntryHash.Clear();
|
|
|
|
if (mStream)
|
|
|
|
mStream->Close();
|
2012-07-30 18:20:58 +04:00
|
|
|
mStream = nullptr;
|
|
|
|
mFile = nullptr;
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when writing a file to the zip is complete.
|
|
|
|
*/
|
|
|
|
nsresult nsZipWriter::EntryCompleteCallback(nsZipHeader* aHeader,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2012-05-18 21:30:49 +04:00
|
|
|
mEntryHash.Put(aHeader->mName, mHeaders.Count());
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!mHeaders.AppendObject(aHeader)) {
|
|
|
|
mEntryHash.Remove(aHeader->mName);
|
|
|
|
SeekCDS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
mCDSOffset += aHeader->mCSize + aHeader->GetFileHeaderLength();
|
|
|
|
|
|
|
|
if (mInQueue)
|
|
|
|
BeginProcessingNextItem();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = SeekCDS();
|
|
|
|
if (mInQueue)
|
|
|
|
FinishQueue(aStatus);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult nsZipWriter::BeginProcessingAddition(nsZipQueueItem* aItem,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* complete)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
if (aItem->mFile) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2007-09-17 03:48:03 +04:00
|
|
|
nsresult rv = aItem->mFile->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!exists) return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isdir;
|
2007-09-17 03:48:03 +04:00
|
|
|
rv = aItem->mFile->IsDirectory(&isdir);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aItem->mFile->GetLastModifiedTime(&aItem->mModTime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aItem->mModTime *= PR_USEC_PER_MSEC;
|
|
|
|
|
2008-10-31 23:10:46 +03:00
|
|
|
rv = aItem->mFile->GetPermissions(&aItem->mPermissions);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-09-17 03:48:03 +04:00
|
|
|
if (!isdir) {
|
|
|
|
// Set up for fall through to stream reader
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(aItem->mStream),
|
|
|
|
aItem->mFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// If a dir then this will fall through to the plain dir addition
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t zipAttributes = ZIP_ATTRS(aItem->mPermissions, ZIP_ATTRS_FILE);
|
2008-10-31 23:10:46 +03:00
|
|
|
|
2011-10-26 04:30:59 +04:00
|
|
|
if (aItem->mStream || aItem->mChannel) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHeader> header = new nsZipHeader();
|
2007-09-17 03:48:03 +04:00
|
|
|
NS_ENSURE_TRUE(header, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2008-10-31 23:10:46 +03:00
|
|
|
header->Init(aItem->mZipEntry, aItem->mModTime, zipAttributes,
|
2007-09-17 03:48:03 +04:00
|
|
|
mCDSOffset);
|
|
|
|
nsresult rv = header->WriteFileHeader(mStream);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipDataStream> stream = new nsZipDataStream();
|
2011-10-26 04:30:59 +04:00
|
|
|
NS_ENSURE_TRUE(stream, NS_ERROR_OUT_OF_MEMORY);
|
2007-09-17 03:48:03 +04:00
|
|
|
rv = stream->Init(this, mStream, header, aItem->mCompression);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-26 04:30:59 +04:00
|
|
|
if (aItem->mStream) {
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump;
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(pump), aItem->mStream,
|
|
|
|
-1, -1, 0, 0, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 03:48:03 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = pump->AsyncRead(stream, nullptr);
|
2011-10-26 04:30:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
2016-06-28 20:03:56 +03:00
|
|
|
rv = NS_MaybeOpenChannelUsingAsyncOpen2(aItem->mChannel, stream);
|
2011-10-26 04:30:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be plain directory addition
|
2011-10-17 18:59:28 +04:00
|
|
|
*complete = true;
|
2008-10-31 23:10:46 +03:00
|
|
|
return InternalAddEntryDirectory(aItem->mZipEntry, aItem->mModTime,
|
|
|
|
aItem->mPermissions);
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
inline nsresult nsZipWriter::BeginProcessingRemoval(int32_t aPos)
|
2007-09-17 03:48:03 +04:00
|
|
|
{
|
|
|
|
// Open the zip file for reading
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream),
|
|
|
|
mFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump;
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(pump), inputStream, -1, -1, 0,
|
2011-10-17 18:59:28 +04:00
|
|
|
0, true);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
rv = NS_NewSimpleStreamListener(getter_AddRefs(listener), mStream, this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mStream);
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET,
|
|
|
|
mHeaders[aPos]->mOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t shift = (mHeaders[aPos + 1]->mOffset -
|
2007-09-17 03:48:03 +04:00
|
|
|
mHeaders[aPos]->mOffset);
|
|
|
|
mCDSOffset -= shift;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos2 = aPos + 1;
|
2007-09-17 03:48:03 +04:00
|
|
|
while (pos2 < mHeaders.Count()) {
|
|
|
|
mEntryHash.Put(mHeaders[pos2]->mName, pos2 - 1);
|
|
|
|
mHeaders[pos2]->mOffset -= shift;
|
|
|
|
pos2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEntryHash.Remove(mHeaders[aPos]->mName);
|
|
|
|
mHeaders.RemoveObjectAt(aPos);
|
2011-10-17 18:59:28 +04:00
|
|
|
mCDSDirty = true;
|
2007-09-17 03:48:03 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = pump->AsyncRead(listener, nullptr);
|
2007-09-17 03:48:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
inputStream->Close();
|
|
|
|
Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Starts processing on the next item in the queue.
|
|
|
|
*/
|
|
|
|
void nsZipWriter::BeginProcessingNextItem()
|
|
|
|
{
|
|
|
|
while (!mQueue.IsEmpty()) {
|
|
|
|
|
|
|
|
nsZipQueueItem next = mQueue[0];
|
|
|
|
mQueue.RemoveElementAt(0);
|
|
|
|
|
|
|
|
if (next.mOperation == OPERATION_REMOVE) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos = -1;
|
2007-09-17 03:48:03 +04:00
|
|
|
if (mEntryHash.Get(next.mZipEntry, &pos)) {
|
|
|
|
if (pos < mHeaders.Count() - 1) {
|
|
|
|
nsresult rv = BeginProcessingRemoval(pos);
|
|
|
|
if (NS_FAILED(rv)) FinishQueue(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCDSOffset = mHeaders[pos]->mOffset;
|
|
|
|
nsresult rv = SeekCDS();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FinishQueue(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mEntryHash.Remove(mHeaders[pos]->mName);
|
|
|
|
mHeaders.RemoveObjectAt(pos);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FinishQueue(NS_ERROR_FILE_NOT_FOUND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (next.mOperation == OPERATION_ADD) {
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mEntryHash.Get(next.mZipEntry, nullptr)) {
|
2007-09-17 03:48:03 +04:00
|
|
|
FinishQueue(NS_ERROR_FILE_ALREADY_EXISTS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool complete = false;
|
2007-09-17 03:48:03 +04:00
|
|
|
nsresult rv = BeginProcessingAddition(&next, &complete);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
SeekCDS();
|
|
|
|
FinishQueue(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!complete)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FinishQueue(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ends processing with the given status.
|
|
|
|
*/
|
|
|
|
void nsZipWriter::FinishQueue(nsresult aStatus)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRequestObserver> observer = mProcessObserver;
|
|
|
|
nsCOMPtr<nsISupports> context = mProcessContext;
|
|
|
|
// Clean up everything first in case the observer decides to queue more
|
|
|
|
// things
|
2012-07-30 18:20:58 +04:00
|
|
|
mProcessObserver = nullptr;
|
|
|
|
mProcessContext = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInQueue = false;
|
2007-09-17 03:48:03 +04:00
|
|
|
|
|
|
|
if (observer)
|
2012-07-30 18:20:58 +04:00
|
|
|
observer->OnStopRequest(nullptr, context, aStatus);
|
2007-09-17 03:48:03 +04:00
|
|
|
}
|