From 31ca5bda9b24c3ae42d6b907ffadeb82c3436998 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Wed, 1 May 2013 17:48:04 +0200 Subject: [PATCH] some minor fixes, more debug output for zip --- zip/qtzipbase.cpp | 3 +- zip/qtzipbase_p.h | 13 +++- zip/qtziphlp.cpp | 177 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 164 insertions(+), 29 deletions(-) diff --git a/zip/qtzipbase.cpp b/zip/qtzipbase.cpp index 476c9ad..583160a 100644 --- a/zip/qtzipbase.cpp +++ b/zip/qtzipbase.cpp @@ -62,7 +62,8 @@ static long ZCALLBACK myseek OF((voidpf opaque, voidpf , uLong offset, int origi static int ZCALLBACK myclose OF((voidpf opaque, voidpf )) { - reinterpret_cast(opaque)->close(); +// reinterpret_cast(opaque)->close(); + //we don't open it ourselves, so we do not close it either return 0; } diff --git a/zip/qtzipbase_p.h b/zip/qtzipbase_p.h index 371a925..2726c58 100644 --- a/zip/qtzipbase_p.h +++ b/zip/qtzipbase_p.h @@ -37,9 +37,13 @@ struct QtZipHelper::ZipHlpBase::Private { unzFile unzip; void setDevice(QIODevice*d){ - if(device)delete device; - device=d; - ffunc.opaque=d; + if(d && d->isOpen()){ + device=d; + ffunc.opaque=d; + }else{ + device=0; + ffunc.opaque=0; + } } Private(){ @@ -47,7 +51,8 @@ struct QtZipHelper::ZipHlpBase::Private { zip=0;unzip=0;device=0; } ~Private(){ - if(device)delete device; +// if(device)delete device; device=0; + ffunc.opaque=0; } }; diff --git a/zip/qtziphlp.cpp b/zip/qtziphlp.cpp index 0af2484..64b9171 100644 --- a/zip/qtziphlp.cpp +++ b/zip/qtziphlp.cpp @@ -25,6 +25,7 @@ THE SOFTWARE. */ #include "qtziphlp.h" #include +#include static const int BUFSIZE=4096; @@ -76,8 +77,18 @@ Zip::~Zip() bool Zip::open(QIODevice* dev, Zip::OpenMode mode) { - if(!dev)return false; - if(!dev->isOpen() || !dev->isWritable() || dev->isSequential())return false; + if(isOpen()){ + qWarning()<<"Zip::open trying to open another device, while already open. Call close() first."; + return false; + } + if(!dev){ + qWarning()<<"Zip::open called with NULL device, are you joking?"; + return false; + } + if(!dev->isOpen() || !dev->isWritable() || dev->isSequential()){ + qWarning()<<"Zip::open failed: the device must be open, writeable and random-access!"; + return false; + } d->setDevice(dev); d->zip=zipOpen2("",mode==OpenAppend?APPEND_STATUS_ADDINZIP:0,0,&d->ffunc); return d->zip!=0; @@ -90,14 +101,24 @@ bool Zip::isOpen() const void Zip::close() { - if(!isOpen())return; + if(!isOpen()){ + qDebug()<<"Zip::close called twice. Ignoring."; + return; + } zipClose(d->zip,""); d->zip=0; } bool Zip::storeFile(const QString& name, QIODevice& file, const QDateTime& dtime) { - if(!isOpen() || !file.isReadable())return false; + if(!isOpen()){ + qWarning()<<"Called Zip::storeFile while no zip is open."; + return false; + } + if(!file.isReadable()){ + qWarning()<<"Zip::storeFile failed: the device to copy data from is not readable."; + return false; + } //create meta data zip_fileinfo info; @@ -113,7 +134,10 @@ bool Zip::storeFile(const QString& name, QIODevice& file, const QDateTime& dtime //create new header const int r=zipOpenNewFileInZip(d->zip, name.toLatin1(), &info, 0,0,0,0,0, Z_DEFLATED, Z_DEFAULT_COMPRESSION); - if(r!=ZIP_OK)return false; + if(r!=ZIP_OK){ + qWarning()<<"Zip::storeFile unable to create new ZIP entry, code"<zip, zinfo.fileName().toLatin1(), &info, 0,0,0,0,0, zinfo.rawCompressionMethod(), Z_DEFAULT_COMPRESSION, 1); - if(r!=ZIP_OK)return false; + if(r!=ZIP_OK){ + qWarning()<<"Zip::storeRawFile unable to create new ZIP entry, code"<isOpen() || !dev->isReadable() || dev->isSequential())return false; + if(isOpen()){ + qWarning()<<"Unzip::open called while already open."; + return false; + } + if(dev==0){ + qWarning()<<"Unzip::open called with NULL device. Sorry, but no."; + return false; + } + if(!dev->isOpen() || !dev->isReadable() || dev->isSequential()){ + qWarning()<<"Unzip::open failed: the device must be open, readable and random-access."; + return false; + } d->ffunc.opaque=dev; d->unzip=unzOpen2("",&d->ffunc); @@ -199,7 +251,10 @@ bool Unzip::open(QIODevice* dev) void Unzip::close() { - if(!isOpen())return; + if(!isOpen()){ + qWarning()<<"Double close on Unzip. Ignoring."; + return; + } unzClose(d->unzip); d->unzip=0; } @@ -207,23 +262,39 @@ void Unzip::close() int Unzip::fileCount() const { + if(!isOpen()){ + qWarning()<<"Unzip::fileCount failed: no zip file open."; + return 0; + } unz_global_info info; if(unzGetGlobalInfo(d->unzip,&info) == UNZ_OK) return info.number_entry; - else + else{ + qWarning()<<"Unzip::fileCount failed, assuming it is empty."; return 0; + } } ZipFileInfo Unzip::firstFile() { + if(!isOpen()){ + qWarning()<<"Unzip::firstFile failed: no zip file open."; + return ZipFileInfo(); + } if(unzGoToFirstFile(d->unzip) == UNZ_OK) return currentFile(); - else + else{ + qWarning()<<"Unzip::firstFile cannot go to first file."; return ZipFileInfo(); + } } ZipFileInfo Unzip::nextFile() { + if(!isOpen()){ + qWarning()<<"Unzip::nextFile failed: no zip file open."; + return ZipFileInfo(); + } if(unzGoToNextFile(d->unzip) == UNZ_OK) return currentFile(); else @@ -232,10 +303,16 @@ ZipFileInfo Unzip::nextFile() ZipFileInfo Unzip::locateFile(const QString& name, Qt::CaseSensitivity cs) { - if(unzLocateFile(d->unzip,name.toLatin1(),cs==Qt::CaseSensitive?1:2)) + if(!isOpen()){ + qWarning()<<"Unzip::locateFile failed: no zip file open."; + return ZipFileInfo(); + } + if(unzLocateFile(d->unzip,name.toLatin1(),cs==Qt::CaseSensitive?1:2) == UNZ_OK) return currentFile(); - else + else{ + qWarning()<<"Unable to locate file"<unzip,&info,0,0,0,0,0,0) != UNZ_OK) + if(unzGetCurrentFileInfo(d->unzip,&info,0,0,0,0,0,0) != UNZ_OK){ + qWarning()<<"Unable to get file info for current file in Unzip::currentFile()"; return ZipFileInfo(); + } if(info.size_filename<=0)return ZipFileInfo(); char buf[info.size_filename]; if(unzGetCurrentFileInfo(d->unzip,&info,buf,info.size_filename,0,0,0,0) != UNZ_OK) @@ -267,9 +350,15 @@ ZipFileInfo Unzip::currentFile() const ZipFileInfo Unzip::currentFile(QIODevice& dev) const { + if(!isOpen()){ + qWarning()<<"Unzip::currentFile(QIODevice&) failed: no zip file open."; + return ZipFileInfo(); + } if(!dev.isWritable())return ZipFileInfo(); - if(unzOpenCurrentFile(d->unzip) != UNZ_OK) + if(unzOpenCurrentFile(d->unzip) != UNZ_OK){ + qWarning()<<"Unable to open current file in Unzip::currentFile(QIODevice&)"; return ZipFileInfo(); + } char buf[BUFSIZE];int l; while((l=unzReadCurrentFile(d->unzip,buf,BUFSIZE)) > 0) dev.write(buf,l); @@ -279,6 +368,10 @@ ZipFileInfo Unzip::currentFile(QIODevice& dev) const ZipFileInfo Unzip::currentFile(QByteArray& a) const { + if(!isOpen()){ + qWarning()<<"Unzip::currentFile(QByteArray&) failed: no zip file open."; + return ZipFileInfo(); + } a.clear(); QBuffer buf; buf.setBuffer(&a); @@ -288,8 +381,14 @@ ZipFileInfo Unzip::currentFile(QByteArray& a) const QByteArray Unzip::currentFileContent() const { - if(unzOpenCurrentFile(d->unzip) != UNZ_OK) + if(!isOpen()){ + qWarning()<<"Unzip::currentFileContent failed: no zip file open."; return QByteArray(); + } + if(unzOpenCurrentFile(d->unzip) != UNZ_OK){ + qWarning()<<"Unable to open current file in Unzip::currentFileContent"; + return QByteArray(); + } QByteArray ret; char buf[BUFSIZE];int l; while((l=unzReadCurrentFile(d->unzip,buf,BUFSIZE)) > 0) @@ -300,9 +399,18 @@ QByteArray Unzip::currentFileContent() const ZipFileInfo Unzip::currentFileRaw(QIODevice& dev) const { - if(!dev.isWritable())return ZipFileInfo(); - if(unzOpenCurrentFile2(d->unzip,0,0,1) != UNZ_OK) + if(!isOpen()){ + qWarning()<<"Unzip::currentFileRaw(QIODevice&) failed: no zip file open."; + return ZipFileInfo(); + } + if(!dev.isWritable()){ + qWarning()<<"Device must be writeable to retrieve data in Unzip::currentFileRaw(QIODevice&)"; + return ZipFileInfo(); + } + if(unzOpenCurrentFile2(d->unzip,0,0,1) != UNZ_OK){ + qWarning()<<"Unable to open current file in Unzip::currentFileRaw(QIODevice&)"; return ZipFileInfo(); + } char buf[BUFSIZE];int l; while((l=unzReadCurrentFile(d->unzip,buf,BUFSIZE)) > 0) dev.write(buf,l); @@ -312,6 +420,10 @@ ZipFileInfo Unzip::currentFileRaw(QIODevice& dev) const ZipFileInfo Unzip::currentFileRaw(QByteArray& a) const { + if(!isOpen()){ + qWarning()<<"Unzip::currentFileRaw(QByteArray&) failed: no zip file open."; + return ZipFileInfo(); + } a.clear(); QBuffer buf; buf.setBuffer(&a); @@ -321,8 +433,14 @@ ZipFileInfo Unzip::currentFileRaw(QByteArray& a) const QByteArray Unzip::currentFileRawContent() const { - if(unzOpenCurrentFile2(d->unzip,0,0,1) != UNZ_OK) + if(!isOpen()){ + qWarning()<<"Unzip::currentFileRawContent failed: no zip file open."; return QByteArray(); + } + if(unzOpenCurrentFile2(d->unzip,0,0,1) != UNZ_OK){ + qWarning()<<"Unable to open current file in Unzip::currentFileRawContent"; + return QByteArray(); + } QByteArray ret; char buf[BUFSIZE];int l; while((l=unzReadCurrentFile(d->unzip,buf,BUFSIZE)) > 0) @@ -333,17 +451,27 @@ QByteArray Unzip::currentFileRawContent() const bool Unzip::copyCurrentFile(Zip& zip) const { - if(!zip.isOpen())return false; - if(!isOpen())return false; + if(!zip.isOpen()){ + qWarning()<<"Unzip::copyCurrentFile failed: target zip is not open"; + return false; + } + if(!isOpen()){ + qWarning()<<"Unzip::copyCurrentFile failed: no zip file open to copy from."; + return false; + } //get meta data unz_file_info info; - if(unzGetCurrentFileInfo(d->unzip,&info,0,0,0,0,0,0) != UNZ_OK) + if(unzGetCurrentFileInfo(d->unzip,&info,0,0,0,0,0,0) != UNZ_OK){ + qWarning()<<"Unzip::copyCurrentFile failed: Unable to open source entry for reading"; return false; + } if(info.size_filename<=0)return false; char name[info.size_filename+1]; - if(unzGetCurrentFileInfo(d->unzip,&info,name,info.size_filename,0,0,0,0) != UNZ_OK) + if(unzGetCurrentFileInfo(d->unzip,&info,name,info.size_filename,0,0,0,0) != UNZ_OK){ + qWarning()<<"Unzip::copyCurrentFile failed: Unable to retrieve source file info"; return false; + } name[info.size_filename]=0; //open unzip file @@ -361,6 +489,7 @@ bool Unzip::copyCurrentFile(Zip& zip) const //create new header if(zipOpenNewFileInZip2(zip.d->zip, name, &zinfo, 0,0,0,0,0, info.compression_method, Z_DEFAULT_COMPRESSION, 1) != ZIP_OK){ + qWarning()<<"Unzip::copyCurrentFile failed: Unable to create destination entry."; unzCloseCurrentFile(d->unzip); return false; } -- 1.7.2.5