1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 07:18:08 +00:00

- Safer temporary filename generation: create the temporary file to make sure the name is not generated twice

This commit is contained in:
Christophe Dumez 2009-08-24 03:38:31 +00:00
parent f5c1343722
commit 91dee6058e
2 changed files with 23 additions and 11 deletions

View File

@ -77,13 +77,20 @@ subDownloadThread::~subDownloadThread(){
} }
void subDownloadThread::run(){ void subDownloadThread::run(){
// XXX: Trick to get a unique filename // Get a unique filename
QString filePath; QString filePath;
QTemporaryFile *tmpfile = new QTemporaryFile(); QTemporaryFile tmpfile;
if (tmpfile->open()) { tmpfile.setAutoRemove(false);
filePath = tmpfile->fileName(); 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"); FILE *f = fopen(filePath.toLocal8Bit().data(), "wb");
if(!f) { if(!f) {
std::cerr << "couldn't open destination file" << "\n"; std::cerr << "couldn't open destination file" << "\n";

View File

@ -200,14 +200,19 @@ void HttpConnection::respondCommand(QString command)
if(command == "upload") if(command == "upload")
{ {
QByteArray torrentfile = parser.torrent(); QByteArray torrentfile = parser.torrent();
// XXX: Trick to get a unique filename // Get a unique filename
QString filePath; QString filePath;
QTemporaryFile *tmpfile = new QTemporaryFile(); QTemporaryFile tmpfile;
if (tmpfile->open()) { tmpfile.setAutoRemove(false);
filePath = tmpfile->fileName(); if (tmpfile.open()) {
filePath = tmpfile.fileName();
} else {
std::cerr << "I/O Error: Could not create temporary file" << std::endl;
return;
} }
delete tmpfile; tmpfile.close();
// write it to HD // Now temporary file is created but closed so that it can be used.
// write torrent to temporary file
QFile torrent(filePath); QFile torrent(filePath);
if(torrent.open(QIODevice::WriteOnly)) { if(torrent.open(QIODevice::WriteOnly)) {
torrent.write(torrentfile); torrent.write(torrentfile);