From 91dee6058e3302ff56ee8c903047c2356a8a4f83 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Mon, 24 Aug 2009 03:38:31 +0000 Subject: [PATCH] - Safer temporary filename generation: create the temporary file to make sure the name is not generated twice --- src/downloadThread.cpp | 17 ++++++++++++----- src/httpconnection.cpp | 17 +++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/downloadThread.cpp b/src/downloadThread.cpp index 34906e420..dd525f9f9 100644 --- a/src/downloadThread.cpp +++ b/src/downloadThread.cpp @@ -77,13 +77,20 @@ subDownloadThread::~subDownloadThread(){ } void subDownloadThread::run(){ - // XXX: Trick to get a unique filename + // Get a unique filename QString filePath; - QTemporaryFile *tmpfile = new QTemporaryFile(); - if (tmpfile->open()) { - filePath = tmpfile->fileName(); + QTemporaryFile tmpfile; + tmpfile.setAutoRemove(false); + if (tmpfile.open()) { + filePath = tmpfile.fileName(); + qDebug("Temporary filename is: %s", filePath.toLocal8Bit().data()); + } else { + emit downloadFailureST(this, url, tr("I/O Error")); + return; } - delete tmpfile; + tmpfile.close(); + // Now temporary file is created but closed so that + // curl can use it FILE *f = fopen(filePath.toLocal8Bit().data(), "wb"); if(!f) { std::cerr << "couldn't open destination file" << "\n"; diff --git a/src/httpconnection.cpp b/src/httpconnection.cpp index 2276c76f9..b86ca8206 100644 --- a/src/httpconnection.cpp +++ b/src/httpconnection.cpp @@ -200,14 +200,19 @@ void HttpConnection::respondCommand(QString command) if(command == "upload") { QByteArray torrentfile = parser.torrent(); - // XXX: Trick to get a unique filename + // Get a unique filename QString filePath; - QTemporaryFile *tmpfile = new QTemporaryFile(); - if (tmpfile->open()) { - filePath = tmpfile->fileName(); + QTemporaryFile tmpfile; + tmpfile.setAutoRemove(false); + if (tmpfile.open()) { + filePath = tmpfile.fileName(); + } else { + std::cerr << "I/O Error: Could not create temporary file" << std::endl; + return; } - delete tmpfile; - // write it to HD + tmpfile.close(); + // Now temporary file is created but closed so that it can be used. + // write torrent to temporary file QFile torrent(filePath); if(torrent.open(QIODevice::WriteOnly)) { torrent.write(torrentfile);