Backed out "changeset: ff9eba3f8224" of

Bug 504864 - mmap io for JARs; r=benjamin
which does not compile on Windows
This commit is contained in:
Serge Gautherie 2009-08-08 12:40:50 +02:00
parent b71d0123c6
commit 10269466ce
7 changed files with 156 additions and 310 deletions

View File

@ -122,6 +122,7 @@ nsJAR::nsJAR(): mManifestData(nsnull, nsnull, DeleteManifestEntry, nsnull, 10),
mReleaseTime(PR_INTERVAL_NO_TIMEOUT),
mCache(nsnull),
mLock(nsnull),
mMtime(0),
mTotalItemsInManifest(0)
{
}
@ -166,6 +167,8 @@ nsJAR::Open(nsIFile* zipFile)
if (mLock) return NS_ERROR_FAILURE; // Already open!
mZipFile = zipFile;
nsresult rv = zipFile->GetLastModifiedTime(&mMtime);
if (NS_FAILED(rv)) return rv;
mLock = PR_NewLock();
NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
@ -173,7 +176,7 @@ nsJAR::Open(nsIFile* zipFile)
PRFileDesc *fd = OpenFile();
NS_ENSURE_TRUE(fd, NS_ERROR_FAILURE);
nsresult rv = mZip.OpenArchive(fd);
rv = mZip.OpenArchive(fd);
if (NS_FAILED(rv)) Close();
return rv;
@ -337,9 +340,19 @@ nsJAR::GetInputStreamWithSpec(const nsACString& aJarDirSpec,
nsresult rv = NS_OK;
if (!item || item->isDirectory) {
rv = jis->InitDirectory(this, aJarDirSpec, aEntryName);
rv = jis->InitDirectory(&mZip, aJarDirSpec, aEntryName);
} else {
rv = jis->InitFile(mZip.GetFD(item), item);
// Open jarfile, to get its own filedescriptor for the stream
// XXX The file may have been overwritten, so |item| might not be
// valid. We really want to work from inode rather than file name.
PRFileDesc *fd = nsnull;
fd = OpenFile();
if (fd) {
rv = jis->InitFile(&mZip, item, fd);
// |jis| now owns |fd|
} else {
rv = NS_ERROR_FAILURE;
}
}
if (NS_FAILED(rv)) {
NS_RELEASE(*result);
@ -1100,9 +1113,13 @@ nsZipReaderCache::GetZip(nsIFile* zipFile, nsIZipReader* *result)
rv = zipFile->GetNativePath(path);
if (NS_FAILED(rv)) return rv;
PRInt64 Mtime;
rv = zipFile->GetLastModifiedTime(&Mtime);
if (NS_FAILED(rv)) return rv;
nsCStringKey key(path);
nsJAR* zip = static_cast<nsJAR*>(static_cast<nsIZipReader*>(mZips.Get(&key))); // AddRefs
if (zip) {
if (zip && Mtime == zip->GetMtime()) {
#ifdef ZIP_CACHE_HIT_RATE
mZipCacheHits++;
#endif

View File

@ -133,6 +133,10 @@ class nsJAR : public nsIZipReader, public nsIJAR
mCache = cache;
}
PRInt64 GetMtime() {
return mMtime;
}
protected:
//-- Private data members
nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk

View File

@ -23,7 +23,6 @@
*
* Contributor(s):
* Mitch Stoltz <mstoltz@netscape.com>
* Taras Glek <tglek@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -46,7 +45,6 @@
#include "nsNetUtil.h"
#include "nsEscape.h"
#include "nsIFile.h"
#include "nsDebug.h"
/*---------------------------------------------
* nsISupports implementation
@ -59,17 +57,23 @@ NS_IMPL_THREADSAFE_ISUPPORTS1(nsJARInputStream, nsIInputStream)
*--------------------------------------------------------*/
nsresult
nsJARInputStream::InitFile(nsZipHandle *aFd, nsZipItem *item)
nsJARInputStream::InitFile(nsZipArchive* aZip, nsZipItem *item, PRFileDesc *fd)
{
nsresult rv;
NS_ABORT_IF_FALSE(aFd, "Argument may not be null");
NS_ABORT_IF_FALSE(item, "Argument may not be null");
// Keep the file handle, even on failure
mFd = fd;
NS_ENSURE_ARG_POINTER(aZip);
NS_ENSURE_ARG_POINTER(item);
NS_ENSURE_ARG_POINTER(fd);
// Mark it as closed, in case something fails in initialisation
mClosed = PR_TRUE;
// Keep the important bits of nsZipItem only
mInSize = item->size;
//-- prepare for the compression type
switch (item->compression) {
case STORED:
@ -92,7 +96,8 @@ nsJARInputStream::InitFile(nsZipHandle *aFd, nsZipItem *item)
}
//-- Set filepointer to start of item
mFd.Open(aFd, item->dataOffset, item->size);
rv = aZip->SeekToItem(item, mFd);
NS_ENSURE_SUCCESS(rv, NS_ERROR_FILE_CORRUPTED);
// Open for reading
mClosed = PR_FALSE;
@ -101,19 +106,19 @@ nsJARInputStream::InitFile(nsZipHandle *aFd, nsZipItem *item)
}
nsresult
nsJARInputStream::InitDirectory(nsJAR* aJar,
nsJARInputStream::InitDirectory(nsZipArchive* aZip,
const nsACString& aJarDirSpec,
const char* aDir)
{
NS_ABORT_IF_FALSE(aJar, "Argument may not be null");
NS_ABORT_IF_FALSE(aDir, "Argument may not be null");
NS_ENSURE_ARG_POINTER(aZip);
NS_ENSURE_ARG_POINTER(aDir);
// Mark it as closed, in case something fails in initialisation
mClosed = PR_TRUE;
mDirectory = PR_TRUE;
// Keep the zipReader for getting the actual zipItems
mJar = aJar;
mZip = aZip;
nsZipFind *find;
nsresult rv;
// We can get aDir's contents as strings via FindEntries
@ -151,7 +156,7 @@ nsJARInputStream::InitDirectory(nsJAR* aJar,
}
nsCAutoString pattern = escDirName + NS_LITERAL_CSTRING("?*~") +
escDirName + NS_LITERAL_CSTRING("?*/?*");
rv = mJar->mZip.FindInit(pattern.get(), &find);
rv = aZip->FindInit(pattern.get(), &find);
if (NS_FAILED(rv)) return rv;
const char *name;
@ -215,25 +220,27 @@ nsJARInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aBytesRead)
PRInt32 bytesRead = 0;
aCount = PR_MIN(aCount, mInSize - mCurPos);
if (aCount) {
bytesRead = mFd.Read(aBuffer, aCount);
bytesRead = PR_Read(mFd, aBuffer, aCount);
if (bytesRead < 0)
return NS_ERROR_FILE_CORRUPTED;
mCurPos += bytesRead;
if ((PRUint32)bytesRead != aCount) {
if (bytesRead != aCount) {
// file is truncated or was lying about size, we're done
Close();
PR_Close(mFd);
mFd = nsnull;
return NS_ERROR_FILE_CORRUPTED;
}
}
*aBytesRead = bytesRead;
}
// be aggressive about closing!
// note that sometimes, we will close mFd before we've finished
// deflating - this is because zlib buffers the input
// So, don't free the ReadBuf/InflateStruct yet.
// It is ok to close the fd multiple times (also in nsJARInputStream::Close())
if (mCurPos >= mInSize) {
mFd.Close();
if (mCurPos >= mInSize && mFd) {
PR_Close(mFd);
mFd = nsnull;
}
}
return rv;
@ -258,8 +265,11 @@ NS_IMETHODIMP
nsJARInputStream::Close()
{
PR_FREEIF(mInflate);
if (mFd) {
PR_Close(mFd);
mFd = nsnull;
}
mClosed = PR_TRUE;
mFd.Close();
return NS_OK;
}
@ -287,7 +297,8 @@ nsJARInputStream::ContinueInflate(char* aBuffer, PRUint32 aCount,
// time to fill the buffer!
PRUint32 bytesToRead = PR_MIN(mInSize - mCurPos, ZIP_BUFLEN);
PRInt32 bytesRead = mFd.Read(mInflate->mReadBuf, bytesToRead);
NS_ASSERTION(mFd, "File handle missing");
PRInt32 bytesRead = PR_Read(mFd, mInflate->mReadBuf, bytesToRead);
if (bytesRead < 0) {
zerr = Z_ERRNO;
break;
@ -351,7 +362,7 @@ nsJARInputStream::ReadDirectory(char* aBuffer, PRUint32 aCount, PRUint32 *aBytes
const char * entryName = mArray[mArrPos].get();
PRUint32 entryNameLen = mArray[mArrPos].Length();
nsZipItem* ze = mJar->mZip.GetItem(entryName);
nsZipItem* ze = mZip->GetItem(entryName);
NS_ENSURE_TRUE(ze, NS_ERROR_FILE_TARGET_DOES_NOT_EXIST);
// Last Modified Time

View File

@ -53,24 +53,23 @@ class nsJARInputStream : public nsIInputStream
{
public:
nsJARInputStream() :
mInSize(0), mCurPos(0), mInflate(nsnull), mDirectory(0), mClosed(PR_FALSE)
{ }
~nsJARInputStream() {
Close();
}
mFd(nsnull), mInSize(0), mCurPos(0),
mInflate(nsnull), mDirectory(0), mClosed(PR_FALSE) { }
~nsJARInputStream() { Close(); }
NS_DECL_ISUPPORTS
NS_DECL_NSIINPUTSTREAM
// takes ownership of |fd|, even on failure
nsresult InitFile(nsZipHandle *aFd, nsZipItem *item);
nsresult InitFile(nsZipArchive* aZip, nsZipItem *item, PRFileDesc *fd);
nsresult InitDirectory(nsJAR *aJar,
nsresult InitDirectory(nsZipArchive* aZip,
const nsACString& aJarDirSpec,
const char* aDir);
private:
PRFileDesc* mFd; // My own file handle, for reading
PRUint32 mInSize; // Size in original file
PRUint32 mCurPos; // Current position in input
@ -84,14 +83,14 @@ class nsJARInputStream : public nsIInputStream
struct InflateStruct * mInflate;
/* For directory reading */
nsRefPtr<nsJAR> mJar; // string reference to zipreader
nsZipArchive* mZip; // the zipReader
PRUint32 mNameLen; // length of dirname
nsCAutoString mBuffer; // storage for generated text of stream
PRUint32 mArrPos; // current position within mArray
nsTArray<nsCString> mArray; // array of names in (zip) directory
PRPackedBool mDirectory; // is this a directory?
PRPackedBool mClosed; // Whether the stream is closed
nsSeekableZipHandle mFd; // handle for reading
PRPackedBool mDirectory;
PRPackedBool mClosed; // Whether the stream is closed
nsresult ContinueInflate(char* aBuf, PRUint32 aCount, PRUint32* aBytesRead);
nsresult ReadDirectory(char* aBuf, PRUint32 aCount, PRUint32* aBytesRead);

View File

@ -26,7 +26,6 @@
* Mitch Stoltz <mstoltz@netscape.com>
* Jeroen Dobbelaere <jeroen.dobbelaere@acunia.com>
* Jeff Walden <jwalden+code@mit.edu>
* Taras Glek <tglek@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -50,19 +49,17 @@
*/
#include "nsWildCard.h"
#include "nscore.h"
#include "prmem.h"
#include "prio.h"
#include "plstr.h"
#include "prlog.h"
#define ZFILE_CREATE PR_WRONLY | PR_CREATE_FILE
#define READTYPE PRInt32
#include "zlib.h"
#include "nsISupportsUtils.h"
#include "nsRecyclingAllocator.h"
#include "prio.h"
#include "plstr.h"
#include "prlog.h"
#include "stdlib.h"
#include "nsWildCard.h"
#include "zipfile.h"
#include "zipstruct.h"
#include "nsZipArchive.h"
/**
* Globals
@ -208,60 +205,6 @@ nsresult gZlibInit(z_stream *zs)
return ZIP_OK;
}
nsZipHandle::nsZipHandle()
: mFd(nsnull)
, mFileData(nsnull)
, mLen(0)
, mMap(nsnull)
, mRefCnt(1)
{
}
NS_IMPL_THREADSAFE_ADDREF(nsZipHandle)
NS_IMPL_THREADSAFE_RELEASE(nsZipHandle)
nsresult nsZipHandle::Init(PRFileDesc *fd, nsZipHandle **ret)
{
PRInt64 size = PR_Available64(fd);
if (size >= PR_INT32_MAX)
return NS_ERROR_FILE_TOO_BIG;
PRFileMap *map = PR_CreateFileMap(fd, size, PR_PROT_READONLY);
if (!map)
return NS_ERROR_FAILURE;
nsZipHandle *handle = new nsZipHandle();
if (!handle)
return NS_ERROR_OUT_OF_MEMORY;
handle->mFd = fd;
handle->mMap = map;
handle->mLen = (PRUint32) size;
handle->mFileData = (PRUint8*) PR_MemMap(map, 0, handle->mLen);
*ret = handle;
return NS_OK;
}
PRInt32 nsZipHandle::Read(PRUint32 aPosition, void *aBuffer, PRUint32 aCount)
{
if (mLen < 0 || aPosition+aCount > (PRUint32) mLen)
return -1;
PRInt32 count = PR_MIN(aCount, mLen - aPosition);
memcpy(aBuffer, mFileData + aPosition, count);
return count;
}
nsZipHandle::~nsZipHandle()
{
if (mFd) {
PR_MemUnmap(mFileData, mLen);
PR_CloseFileMap(mMap);
PR_Close(mFd);
mFd = 0;
}
}
//***********************************************************
// nsZipArchive -- public methods
//***********************************************************
@ -272,13 +215,15 @@ nsZipHandle::~nsZipHandle()
//---------------------------------------------
nsresult nsZipArchive::OpenArchive(PRFileDesc * fd)
{
nsresult rv = nsZipHandle::Init(fd, getter_AddRefs(mFd));
if (NS_FAILED(rv))
return rv;
if (!fd)
return ZIP_ERR_PARAM;
// Initialize our arena
PL_INIT_ARENA_POOL(&mArena, "ZipArena", ZIP_ARENABLOCKSIZE);
//-- Keep the filedescriptor for further reading...
mFd = fd;
//-- get table of contents for archive
return BuildFileList();
}
@ -334,8 +279,11 @@ nsresult nsZipArchive::CloseArchive()
// Let us also cleanup the mFiles table for re-use on the next 'open' call
for (int i = 0; i < ZIP_TABSIZE; i++) {
mFiles[i] = 0;
}
if (mFd) {
PR_Close(mFd);
mFd = 0;
}
mFd = NULL;
mBuiltSynthetics = PR_FALSE;
return ZIP_OK;
}
@ -386,11 +334,7 @@ nsresult nsZipArchive::ExtractFile(nsZipItem *item, const char *outname,
PR_ASSERT(!item->isDirectory);
//-- move to the start of file's data
if (MaybeReadItem(item) != ZIP_OK)
return ZIP_ERR_CORRUPT;
nsSeekableZipHandle fd;
if (!fd.Open(mFd.get(), item->headerOffset, item->size))
if (SeekToItem(item, mFd) != ZIP_OK)
return ZIP_ERR_CORRUPT;
nsresult rv;
@ -399,11 +343,11 @@ nsresult nsZipArchive::ExtractFile(nsZipItem *item, const char *outname,
switch(item->compression)
{
case STORED:
rv = CopyItemToDisk(item->size, item->crc32, fd, aFd);
rv = CopyItemToDisk(item->size, item->crc32, aFd);
break;
case DEFLATED:
rv = InflateItem(item, fd, aFd);
rv = InflateItem(item, aFd);
break;
default:
@ -565,22 +509,29 @@ nsZipItem* nsZipArchive::CreateZipItem(PRUint16 namelen)
//---------------------------------------------
nsresult nsZipArchive::BuildFileList()
{
PRUint8 *buf;
PRUint8 buf[4*BR_BUF_SIZE];
//-----------------------------------------------------------------------
// locate the central directory via the End record
//-----------------------------------------------------------------------
//-- get archive size using end pos
PRInt32 pos = (PRInt32) mFd->mLen;
PRInt32 pos = PR_Seek(mFd, 0, PR_SEEK_END);
if (pos <= 0)
return ZIP_ERR_CORRUPT;
PRInt32 central = -1;
while (central == -1)
PRBool bEndsigFound = PR_FALSE;
while (!bEndsigFound)
{
//-- read backwards in 1K-sized chunks (unless file is less than 1K)
PRInt32 bufsize = pos > BR_BUF_SIZE ? BR_BUF_SIZE : pos;
pos -= bufsize;
buf = mFd->mFileData + pos;
if (!ZIP_Seek(mFd, pos, PR_SEEK_SET))
return ZIP_ERR_CORRUPT;
if (PR_Read(mFd, buf, bufsize) != (READTYPE)bufsize)
return ZIP_ERR_CORRUPT;
//-- scan for ENDSIG
PRUint8 *endp = buf + bufsize;
@ -589,12 +540,16 @@ nsresult nsZipArchive::BuildFileList()
if (xtolong(endp) == ENDSIG)
{
//-- Seek to start of central directory
central = xtolong(((ZipEnd *) endp)->offset_central_dir);
PRInt32 central = xtolong(((ZipEnd *) endp)->offset_central_dir);
if (!ZIP_Seek(mFd, central, PR_SEEK_SET))
return ZIP_ERR_CORRUPT;
bEndsigFound = PR_TRUE;
break;
}
}
if (central != -1)
if (bEndsigFound)
break;
if (pos <= 0)
@ -611,8 +566,7 @@ nsresult nsZipArchive::BuildFileList()
//-------------------------------------------------------
// read the central directory headers
//-------------------------------------------------------
PRInt32 byteCount = mFd->mLen - central;
buf = mFd->mFileData + central;
PRInt32 byteCount = PR_Read(mFd, &buf, sizeof(buf));
pos = 0;
PRUint32 sig = xtolong(buf);
while (sig == CENTRALSIG) {
@ -659,11 +613,31 @@ nsresult nsZipArchive::BuildFileList()
#endif
pos += ZIPCENTRAL_SIZE;
//-------------------------------------------------------
// Make sure that remainder of this record (name, comments, extra)
// and the next ZipCentral is all in the buffer
//-------------------------------------------------------
PRInt32 leftover = byteCount - pos;
if (leftover < (namelen + extralen + commentlen + ZIPCENTRAL_SIZE)) {
//-- not enough data left to process at top of loop.
//-- move leftover and read more
memcpy(buf, buf+pos, leftover);
byteCount = leftover + PR_Read(mFd, buf+leftover, sizeof(buf)-leftover);
pos = 0;
if (byteCount < (namelen + extralen + commentlen + sizeof(sig))) {
// truncated file
return ZIP_ERR_CORRUPT;
}
}
//-------------------------------------------------------
// get the item name
//-------------------------------------------------------
memcpy(item->name, buf+pos, namelen);
item->name[namelen] = 0;
//-- an item whose name ends with '/' is a directory
item->isDirectory = ('/' == item->name[namelen - 1]);
@ -784,17 +758,11 @@ nsresult nsZipArchive::BuildSynthetics()
return ZIP_OK;
}
nsZipHandle* nsZipArchive::GetFD(nsZipItem* aItem)
{
if (!mFd || !MaybeReadItem(aItem))
return NULL;
return mFd.get();
}
//---------------------------------------------
// nsZipArchive::MaybeReadItem
// nsZipArchive::SeekToItem
//---------------------------------------------
bool nsZipArchive::MaybeReadItem(nsZipItem* aItem)
nsresult nsZipArchive::SeekToItem(nsZipItem* aItem, PRFileDesc* aFd)
{
PR_ASSERT (aItem);
@ -807,33 +775,36 @@ bool nsZipArchive::MaybeReadItem(nsZipItem* aItem)
//-- NOTE: extralen is different in central header and local header
//-- for archives created using the Unix "zip" utility. To set
//-- the offset accurately we need the _local_ extralen.
if (!mFd || !mFd->mLen > aItem->headerOffset + ZIPLOCAL_SIZE)
return false;
if (!ZIP_Seek(aFd, aItem->headerOffset, PR_SEEK_SET))
return ZIP_ERR_CORRUPT;
ZipLocal *Local = (ZipLocal*)(mFd->mFileData + aItem->headerOffset);
//check limits here
if ((xtolong(Local->signature) != LOCALSIG))
ZipLocal Local;
if ((PR_Read(aFd, (char*)&Local, ZIPLOCAL_SIZE) != (READTYPE) ZIPLOCAL_SIZE) ||
(xtolong(Local.signature) != LOCALSIG))
{
//-- read error or local header not found
return false;
return ZIP_ERR_CORRUPT;
}
aItem->dataOffset = aItem->headerOffset +
ZIPLOCAL_SIZE +
xtoint(Local->filename_len) +
xtoint(Local->extrafield_len);
xtoint(Local.filename_len) +
xtoint(Local.extrafield_len);
aItem->hasDataOffset = PR_TRUE;
}
return true;
//-- move to start of file in archive
if (!ZIP_Seek(aFd, aItem->dataOffset, PR_SEEK_SET))
return ZIP_ERR_CORRUPT;
return ZIP_OK;
}
//---------------------------------------------
// nsZipArchive::CopyItemToDisk
//---------------------------------------------
nsresult
nsZipArchive::CopyItemToDisk(PRUint32 itemSize, PRUint32 itemCrc,
nsSeekableZipHandle &fd, PRFileDesc* outFD)
nsZipArchive::CopyItemToDisk(PRUint32 itemSize, PRUint32 itemCrc, PRFileDesc* outFD)
/*
* This function copies an archive item to disk, to the
* file specified by outFD. If outFD is zero, the extracted data is
@ -851,7 +822,7 @@ nsZipArchive::CopyItemToDisk(PRUint32 itemSize, PRUint32 itemCrc,
{
chunk = (itemSize - pos < ZIP_BUFLEN) ? (itemSize - pos) : ZIP_BUFLEN;
if (fd.Read(buf, chunk) != (READTYPE)chunk)
if (PR_Read(mFd, buf, chunk) != (READTYPE)chunk)
{
//-- unexpected end of data in archive
return ZIP_ERR_CORRUPT;
@ -878,7 +849,7 @@ nsZipArchive::CopyItemToDisk(PRUint32 itemSize, PRUint32 itemCrc,
//---------------------------------------------
// nsZipArchive::InflateItem
//---------------------------------------------
nsresult nsZipArchive::InflateItem(const nsZipItem* aItem, nsSeekableZipHandle &fd, PRFileDesc* outFD)
nsresult nsZipArchive::InflateItem(const nsZipItem* aItem, PRFileDesc* outFD)
/*
* This function inflates an archive item to disk, to the
* file specified by outFD. If outFD is zero, the extracted data is
@ -916,7 +887,7 @@ nsresult nsZipArchive::InflateItem(const nsZipItem* aItem, nsSeekableZipHandle &
//-- read another chunk of compressed data
PRUint32 chunk = (size-zs.total_in < ZIP_BUFLEN) ? size-zs.total_in : ZIP_BUFLEN;
if (fd.Read(inbuf, chunk) != (READTYPE)chunk)
if (PR_Read(mFd, inbuf, chunk) != (READTYPE)chunk)
{
//-- unexpected end of data
status = ZIP_ERR_CORRUPT;
@ -995,7 +966,8 @@ cleanup:
//------------------------------------------
nsZipArchive::nsZipArchive() :
mBuiltSynthetics(PR_FALSE)
mFd(0),
mBuiltSynthetics(PR_FALSE)
{
MOZ_COUNT_CTOR(nsZipArchive);
@ -1103,3 +1075,4 @@ static PRBool IsSymlink(unsigned char *ll)
return ((xtoint(ll+2) & S_IFMT) == S_IFLNK);
}
#endif

View File

@ -24,7 +24,6 @@
* Daniel Veditz <dveditz@netscape.com>
* Samir Gehani <sgehani@netscape.com>
* Mitch Stoltz <mstoltz@netscape.com>
* Taras Glek <tglek@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -54,7 +53,6 @@
#define ZIP_Seek(fd,p,m) (PR_Seek((fd),((PROffset32)p),(m))==((PROffset32)p))
#include "zlib.h"
#include "nsAutoPtr.h"
class nsZipFind;
class nsZipReadState;
@ -113,8 +111,6 @@ struct nsZipItem
char name[1]; // actually, bigger than 1
};
class nsZipHandle;
class nsSeekableZipHandle;
/**
* nsZipArchive -- a class for reading the PKZIP file format.
*
@ -190,10 +186,12 @@ public:
*/
PRInt32 FindInit(const char * aPattern, nsZipFind** aFind);
/* Gets an undependent handle to the jar
* Also ensures that aItem is fully filled
/**
* Moves the filepointer aFd to the start of data of the aItem.
* @param aItem Pointer to nsZipItem
* @param aFd The filepointer to move
*/
nsZipHandle* GetFD(nsZipItem* aItem);
nsresult SeekToItem(nsZipItem* aItem, PRFileDesc* aFd);
private:
//--- private members ---
@ -201,18 +199,12 @@ private:
nsZipItem* mFiles[ZIP_TABSIZE];
PLArenaPool mArena;
/**
* Fills in nsZipItem fields that were not filled in by BuildFileList
* @param aItem Pointer to nsZipItem
* returns true if the item was filled in successfully
*/
bool MaybeReadItem(nsZipItem* aItem);
// Used for central directory reading, and for Test and Extract
PRFileDesc *mFd;
// Whether we synthesized the directory entries
PRPackedBool mBuiltSynthetics;
// file handle
nsRefPtr<nsZipHandle> mFd;
//--- private methods ---
nsZipArchive& operator=(const nsZipArchive& rhs); // prevent assignments
@ -222,92 +214,8 @@ private:
nsresult BuildFileList();
nsresult BuildSynthetics();
nsresult CopyItemToDisk(PRUint32 size, PRUint32 crc, nsSeekableZipHandle &fd, PRFileDesc* outFD);
nsresult InflateItem(const nsZipItem* aItem, nsSeekableZipHandle &fd, PRFileDesc* outFD);
};
class nsZipHandle {
friend class nsZipArchive;
friend class nsSeekableZipHandle;
public:
static nsresult Init(PRFileDesc *fd, nsZipHandle **ret NS_OUTPARAM);
/**
* Reads data at a certain point
* @param aPosition seek ofset
* @param aBuffer buffer
* @param aCount number of bytes to read */
PRInt32 Read(PRUint32 aPosition, void *aBuffer, PRUint32 aCount);
nsrefcnt AddRef(void);
nsrefcnt Release(void);
protected:
PRFileDesc *mFd; // OS file-descriptor
PRUint8 *mFileData; // pointer to mmaped file
PRUint32 mLen; // length of file and memory mapped area
private:
nsZipHandle();
~nsZipHandle();
PRFileMap *mMap; // nspr datastructure for mmap
nsrefcnt mRefCnt; // ref count
};
/** nsSeekableZipHandle acts as a container for nsZipHandle,
emulates sequential file io */
class nsSeekableZipHandle {
// stick nsZipItem in here
public:
nsSeekableZipHandle()
: mOffset(0)
, mRemaining(0)
{
}
/** Initializes nsSeekableZipHandle with
* @param aOffset byte offset of the file to start reading at
* @param length of this descriptor
*/
bool Open(nsZipHandle *aHandle, PRUint32 aOffset, PRUint32 aLength) {
NS_ABORT_IF_FALSE (aHandle, "Argument must not be NULL");
if (aOffset > aHandle->mLen)
return false;
mFd = aHandle;
mOffset = aOffset;
mRemaining = aLength;
return true;
}
/** Releases the file handle. It is safe to call multiple times. */
void Close()
{
mFd = NULL;
}
/**
* Reads data at a certain point
* @param aBuffer input buffer
* @param aCount number of bytes to read */
PRInt32 Read(void *aBuffer, PRUint32 aCount)
{
if (!mFd.get())
return -1;
aCount = PR_MIN(mRemaining, aCount);
PRInt32 ret = mFd->Read(mOffset, aBuffer, aCount);
if (ret > 0) {
mOffset += ret;
mRemaining -= ret;
}
return ret;
}
private:
nsRefPtr<nsZipHandle> mFd; // file handle
PRUint32 mOffset; // current reading offset
PRUint32 mRemaining; // bytes remaining
nsresult CopyItemToDisk(PRUint32 size, PRUint32 crc, PRFileDesc* outFD);
nsresult InflateItem(const nsZipItem* aItem, PRFileDesc* outFD);
};

View File

@ -1,66 +0,0 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Taras Glek <tglek@mozilla.com>
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
function wrapInputStream(input)
{
var nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
var factory = Components.classes["@mozilla.org/scriptableinputstream;1"];
var wrapper = factory.createInstance(nsIScriptableInputStream);
wrapper.init(input);
return wrapper;
}
// Check that files can be read from after closing zipreader
function run_test() {
const Cc = Components.classes;
const Ci = Components.interfaces;
// the build script have created the zip we can test on in the current dir.
var file = do_get_file("data/test_bug333423.zip");
var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].
createInstance(Ci.nsIZipReader);
zipreader.open(file);
var entries = zipreader.findEntries(null);
var stream = wrapInputStream(zipreader.getInputStream("modules/libjar/test/Makefile.in"))
var dirstream= wrapInputStream(zipreader.getInputStream("modules/libjar/test/"))
zipreader.close();
zipreader = null;
Components.utils.forceGC();
do_check_true(stream.read(1024).length > 0);
do_check_true(dirstream.read(100).length > 0);
}