Skip to content

Commit

Permalink
Fix creation of temporary directory
Browse files Browse the repository at this point in the history
Since QComicBook switched to Qt5, it is posssible to use QTemporaryDir
to create temporary directories: this solves few problems:
- picking a unique random name
- creating safely the directory (so far the temporary directory has been
  readable by anyone, not just the user, leaving a data leak)
- cleanup is done automatically, in the same way, no matter what is the
  structure of the files there

Fixes: #39
  • Loading branch information
pinotree committed Nov 24, 2020
1 parent d0fc37d commit 0792507
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 41 deletions.
46 changes: 8 additions & 38 deletions src/Sink/ImgArchiveSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QRegExp>
#include <QApplication>
#include <QDir>
#include <QTemporaryDir>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -98,23 +99,13 @@ void ImgArchiveSink::init()
connect(pinf, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(infoExited(int, QProcess::ExitStatus)));
connect(pext, SIGNAL(readyReadStandardOutput()), this, SLOT(extractStdoutReady()));
connect(pext, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(extractExited(int, QProcess::ExitStatus)));
tmpdir = NULL;
}

void ImgArchiveSink::doCleanup()
{
if (!tmppath.isEmpty())
{
QDir dir(tmppath);
//
// remove temporary files and dirs
foreach (const QString f, archfiles)
dir.remove(f);
foreach (const QString f, archdirs)
{
dir.rmdir(f);
}
dir.rmdir(tmppath);
}
delete tmpdir;
tmpdir = NULL;
}

int ImgArchiveSink::waitForFinished(QProcess *p)
Expand Down Expand Up @@ -173,7 +164,7 @@ int ImgArchiveSink::open(const QString &path) //TODO: cleanup if already opened?
{
if (info.isReadable())
{
tmppath = makeTempDir(ComicBookSettings::instance().tmpDir());
const QString tmppath = makeTempDir(ComicBookSettings::instance().tmpDir());
archdirs.prepend(tmppath);
QStringList extractargs, listargs;
ArchiversConfiguration::instance().getExtractArguments(path, extractargs, listargs);
Expand Down Expand Up @@ -239,30 +230,9 @@ void ImgArchiveSink::extractStdoutReady()

QString ImgArchiveSink::makeTempDir(const QString &parent)
{
static bool initsrand = false;

//
// make sure srand is called only once
if (!initsrand)
{
srand(time(NULL));
initsrand = true;
}

QDir dir(parent);
for (;;)
{
const int n = rand();
const QString tmpd = QString("qcomic-") + QString::number(n);
if (!dir.exists(tmpd))
{
if (!dir.mkdir(tmpd))
{
break;
}
return parent + QDir::separator() + tmpd;
}
}
tmpdir = new QTemporaryDir(parent + QString("/qcomic-XXXXXX"));
if (tmpdir->isValid())
return tmpdir->path();
qFatal("Failed to create temporary directory");
return QString::null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Sink/ImgArchiveSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ImgDirSink.h"

class QImage;
class QTemporaryDir;

namespace QComicBook
{
Expand All @@ -37,7 +38,7 @@ namespace QComicBook
QProcess *pinf; ///< file list extracing process
QString archivename; ///< archive file name, without path
QString archivepath; ///< full path, including archive name
QString tmppath; ///< path to extracted archive
QTemporaryDir *tmpdir; ///< temporary location to extracted archive
QStringList archfiles; ///< list of archive files
QStringList archdirs; ///< list of archive dirs
int filesnum; ///< number of files gathered from parsing archiver output, used for progress bar
Expand All @@ -46,6 +47,7 @@ namespace QComicBook
static int waitForFinished(QProcess *p);
int extract(const QString &filename, const QString &destdir, QStringList extargs, QStringList infargs);
void init();
QString makeTempDir(const QString &parent);
virtual void doCleanup();

virtual bool fileHandler(const QFileInfo &finfo);
Expand All @@ -68,8 +70,6 @@ namespace QComicBook
virtual bool supportsNext() const;
virtual QString getNext() const;
virtual QString getPrevious() const;

static QString makeTempDir(const QString &parent = QDir::tempPath());
};
}

Expand Down

0 comments on commit 0792507

Please sign in to comment.