Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix creation of temporary directory #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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