From 530f36048283adb22a4b1615ff56b6d7d194d823 Mon Sep 17 00:00:00 2001 From: giantpune Date: Sat, 28 May 2011 06:05:34 +0000 Subject: [PATCH] *nusDownloader:: fix cache bug caused be overzealous optimizations; add better error handeling when reading and saving data to/fromcache *ohneschwanzenegger:: change possible misleading version text when overwriting a title --- WiiQt/nandbin.cpp | 7 ++-- WiiQt/nusdownloader.cpp | 53 +++++++++++++++++++++++-------- WiiQt/nusdownloader.h | 4 ++- ohneschwanzenegger/mainwindow.cpp | 4 +-- ohneschwanzenegger/newnandbin.cpp | 6 ++-- ohneschwanzenegger/newnandbin.h | 6 ++-- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/WiiQt/nandbin.cpp b/WiiQt/nandbin.cpp index 6d6b445..da7b3bb 100755 --- a/WiiQt/nandbin.cpp +++ b/WiiQt/nandbin.cpp @@ -48,6 +48,7 @@ bool NandBin::SetPath( const QString &path ) return ret; } + const QString NandBin::FilePath() { if( !f.isOpen() ) @@ -55,7 +56,6 @@ const QString NandBin::FilePath() return QFileInfo( f ).absoluteFilePath(); } -//#if 0 // apparently you dont need any extra reserved blocks for the thing to boot? bool NandBin::CreateNew( const QString &path, const QByteArray &keys, const QByteArray &first8, const QList &badBlocks ) { #ifndef NAND_BIN_CAN_WRITE @@ -158,7 +158,6 @@ bool NandBin::CreateNew( const QString &path, const QByteArray &keys, const QByt return true; #endif } -//#endif bool NandBin::Format( bool secure ) { @@ -1344,7 +1343,7 @@ bool NandBin::DeleteItem( QTreeWidgetItem *item ) return false; } - qDebug() << "NandBin::DeleteItem" << item->text( 0 ); + //qDebug() << "NandBin::DeleteItem" << item->text( 0 ); bool ok = false; quint16 idx = item->text( 1 ).toInt( &ok );//get the index of the entry to remove if( !ok || idx > 0x17fe ) @@ -1428,7 +1427,7 @@ bool NandBin::DeleteItem( QTreeWidgetItem *item ) case 1: { //int q = 0; - qDebug() << "deleting clusters of" << item->text( 0 ) << idx; + //qDebug() << "deleting clusters of" << item->text( 0 ) << idx; QList toFree = GetFatsForFile( idx ); foreach( quint16 cl, toFree ) { diff --git a/WiiQt/nusdownloader.cpp b/WiiQt/nusdownloader.cpp index 30adfd1..186983e 100644 --- a/WiiQt/nusdownloader.cpp +++ b/WiiQt/nusdownloader.cpp @@ -126,7 +126,19 @@ QByteArray NusDownloader::GetDataFromCache( const downloadJob &job ) } QFile f( GetCachePath( job.index ) ); - if( !f.exists() || !f.open( QIODevice::ReadOnly ) ) + if( !f.exists() ) + { + //qWarning() << "NusDownloader::GetDataFromCache -> file doesnt exist" << QFileInfo( f ).absoluteFilePath(); + return QByteArray(); + } + if( !f.size() ) + { + f.remove(); + qWarning() << "NusDownloader::GetDataFromCache -> file was 0 bytes" << QFileInfo( f ).absoluteFilePath(); + return QByteArray(); + } + + if( !f.open( QIODevice::ReadOnly ) ) { //qWarning() << "NusDownloader::GetDataFromCache -> file cant be opened for reading" << QFileInfo( f ).absoluteFilePath(); return QByteArray(); @@ -178,7 +190,7 @@ void NusDownloader::ReadTmdAndGetTicket( const QByteArray &ba ) if( stuff.isEmpty() ) { dlJob = tikJob; - QTimer::singleShot( 0, this, SLOT( StartDownload() ) ); + QTimer::singleShot( 50, this, SLOT( StartDownload() ) ); } else { @@ -188,13 +200,20 @@ void NusDownloader::ReadTmdAndGetTicket( const QByteArray &ba ) //AesSetKey( t.DecryptedKey() ); //add the ticket data to the return currentJob.data << stuff; - QTimer::singleShot( 0, this, SLOT( GetNextItemForCurrentTitle() ) ); + QTimer::singleShot( 50, this, SLOT( GetNextItemForCurrentTitle() ) ); } } //save data downloaded from the internet to local HDD for future downloads bool NusDownloader::SaveDataToCache( const QString &path, const QByteArray &stuff ) { + //qDebug() << "NusDownloader::SaveDataToCache ->" << path; + if( !stuff.size() ) + { + qWarning() << "NusDownloader::SaveDataToCache -> !size" << path; + return false; + } + //make sure there is all the parent folders needed to hold this folder if( path.count( "/" ) < 4 || !path.startsWith( cachePath + "/" )) { @@ -219,9 +238,16 @@ bool NusDownloader::SaveDataToCache( const QString &path, const QByteArray &stuf qWarning() << "NusDownloader::SaveDataToCache -> can't create file" << path; return false; } - f.write( stuff );//probably should check the return values on these. but if they dont go right, then the person has bigger things to worry about + if( f.write( stuff ) != stuff.size() ) + { + f.close(); + f.remove(); + qWarning() << "NusDownloader::SaveDataToCache -> error writing data to" << path; + return false; + } + f.flush(); f.close(); - qDebug() << "saved" << hex << stuff.size() << "bytes to" << path; + //qDebug() << "saved" << hex << stuff.size() << "bytes to" << path; return true; } @@ -240,7 +266,7 @@ void NusDownloader::CurrentJobErrored( const QString &str ) { qWarning() << "NusDownloader::CurrentJobErrored ->" << str; emit SendError( str, currentJob ); - QTimer::singleShot( 0, this, SLOT( StartNextJob() ) ); + QTimer::singleShot( 50, this, SLOT( StartNextJob() ) ); } //get the next content for the current title @@ -286,7 +312,7 @@ void NusDownloader::GetNextItemForCurrentTitle() } //qDebug() << "hash matched for index" << alreadyHave; if( alreadyHave + 1 < qFromBigEndian( curTmd.payload()->num_contents ) ) - QTimer::singleShot( 0, this, SLOT( GetNextItemForCurrentTitle() ) );//next content + QTimer::singleShot( 50, this, SLOT( GetNextItemForCurrentTitle() ) );//next content else { @@ -294,7 +320,7 @@ void NusDownloader::GetNextItemForCurrentTitle() emit SendTotalProgress( progress ); emit SendTitleProgress( 100 ); emit SendData( currentJob ); - QTimer::singleShot( 0, this, SLOT( StartNextJob() ) );//start next job + QTimer::singleShot( 50, this, SLOT( StartNextJob() ) );//start next job } } } @@ -371,7 +397,7 @@ bool NusDownloader::DecryptCheckHashAndAppendData( const QByteArray &encData, qu } //something is done downloading -void NusDownloader::FileIsFinishedDownloading( const downloadJob &job ) +void NusDownloader::FileIsFinishedDownloading( downloadJob job ) { //qDebug() << "NusDownloader::FileIsFinishedDownloading" << job.index; if( job.data.isEmpty() ) @@ -401,7 +427,8 @@ void NusDownloader::FileIsFinishedDownloading( const downloadJob &job ) //add the ticket data to the return currentJob.data << job.data; //start downloading the contents - GetNextItemForCurrentTitle(); + //GetNextItemForCurrentTitle(); + QTimer::singleShot( 50, this, SLOT( GetNextItemForCurrentTitle() ) );//next content cPath = GetCachePath( job.index ); } @@ -433,11 +460,11 @@ void NusDownloader::FileIsFinishedDownloading( const downloadJob &job ) emit SendTotalProgress( progress ); emit SendTitleProgress( 100 ); emit SendData( currentJob ); - QTimer::singleShot( 0, this, SLOT( StartNextJob() ) );//move on to next job + QTimer::singleShot( 50, this, SLOT( StartNextJob() ) );//move on to next job } else - QTimer::singleShot( 0, this, SLOT( GetNextItemForCurrentTitle() ) );//next content + QTimer::singleShot( 50, this, SLOT( GetNextItemForCurrentTitle() ) );//next content cPath = GetCachePath( job.index ); } @@ -457,7 +484,7 @@ void NusDownloader::StartDownload() //qDebug() << "NusDownloader::StartDownload" << dlJob.index; emit SendDownloadProgress( 0 ); QString dlUrl = NUS_BASE_URL + dlJob.tid + "/" + dlJob.name; - qDebug() << "url" << dlUrl; + //qDebug() << "url" << dlUrl; currentJobText = dlUrl; QUrl url( dlUrl ); diff --git a/WiiQt/nusdownloader.h b/WiiQt/nusdownloader.h index 5a59379..a847a61 100644 --- a/WiiQt/nusdownloader.h +++ b/WiiQt/nusdownloader.h @@ -121,7 +121,9 @@ private: bool DecryptCheckHashAndAppendData( const QByteArray &encData, quint16 idx ); //triggered when a file is done downloading - void FileIsFinishedDownloading( const downloadJob &job ); + //! dont use const reference here, as the job is really a variable which is reused + //! for every download + void FileIsFinishedDownloading( downloadJob job ); //send a fail message about the current job and skip to the next void CurrentJobErrored( const QString &str ); diff --git a/ohneschwanzenegger/mainwindow.cpp b/ohneschwanzenegger/mainwindow.cpp index 2c1f9e7..c97425b 100644 --- a/ohneschwanzenegger/mainwindow.cpp +++ b/ohneschwanzenegger/mainwindow.cpp @@ -685,7 +685,7 @@ bool MainWindow::InstallNUSItem( NusJob job ) { //nand.WriteMetaData(); UpdateTree(); - ShowMessage( tr( "Deleted old TMD and private contents for
%1" ).arg( title ) ); + ShowMessage( tr( "Deleted old TMD and private contents for
%1" ).arg( job.tid, 16, 16, QChar( '0' ) ) ); } cnt = t.Count(); @@ -982,7 +982,7 @@ void MainWindow::on_actionFormat_triggered() //wipe all user-created entries from uid.sys QByteArray uidData = uid.Data(); QBuffer buf( &uidData ); - buf.open( QIODevice::ReadWrite ); + buf.open( QIODevice::ReadOnly ); quint64 tid; quint16 titles = 0; diff --git a/ohneschwanzenegger/newnandbin.cpp b/ohneschwanzenegger/newnandbin.cpp index 260d02e..7286bf2 100644 --- a/ohneschwanzenegger/newnandbin.cpp +++ b/ohneschwanzenegger/newnandbin.cpp @@ -3,7 +3,7 @@ #include "../WiiQt/uidmap.h" #include "../WiiQt/tools.h" -NewNandBin::NewNandBin( QWidget *parent, QList badBlocks ) : QDialog(parent), ui(new Ui::NewNandBin), nand( this ) +NewNandBin::NewNandBin( QWidget *parent, const QList &badBlocks ) : QDialog(parent), ui(new Ui::NewNandBin), nand( this ) { dir = QDir::currentPath(); ui->setupUi(this); @@ -204,7 +204,7 @@ void NewNandBin::on_buttonBox_accepted() ret = ui->lineEdit_dest->text(); } -QString NewNandBin::GetNewNandPath( QWidget *parent, QList badBlocks ) +QString NewNandBin::GetNewNandPath( QWidget *parent, const QList &badBlocks ) { NewNandBin d( parent, badBlocks ); if( !d.exec() ) @@ -339,7 +339,7 @@ void NewNandBin::on_pushButton_oldNand_clicked() QByteArray NewNandBin::GetCleanUid( QByteArray old ) { QBuffer buf( &old ); - buf.open( QIODevice::ReadWrite ); + buf.open( QIODevice::ReadOnly ); quint64 tid; quint16 titles = 0; diff --git a/ohneschwanzenegger/newnandbin.h b/ohneschwanzenegger/newnandbin.h index 48253b9..a175bab 100644 --- a/ohneschwanzenegger/newnandbin.h +++ b/ohneschwanzenegger/newnandbin.h @@ -13,10 +13,10 @@ class NewNandBin : public QDialog Q_OBJECT public: - explicit NewNandBin( QWidget *parent = 0, QListbadBlocks = QList() ); + explicit NewNandBin( QWidget *parent = 0, const QList &badBlocks = QList() ); ~NewNandBin(); - static QString GetNewNandPath( QWidget *parent = 0, QListbadBlocks = QList() ); + static QString GetNewNandPath( QWidget *parent = 0, const QList &badBlocks = QList() ); private: Ui::NewNandBin *ui; @@ -32,7 +32,7 @@ private: QByteArray uidSys; bool CreateDefaultEntries(); - QByteArray GetCleanUid( QByteArray old ); + QByteArray GetCleanUid( QByteArray old ); private slots: void on_pushButton_oldNand_clicked();