Commit 015a265d authored by Stefan Brüns's avatar Stefan Brüns

[DjvuThumbnailer] Replace open coded ddjvu invocation with QProcess

This also fixes a kdeinit crash due to calling exit(1) in case
execvp failed (e.g. due to a missing ddjvu binary).

BUG: 420074
parent 9fedecf5
......@@ -116,6 +116,11 @@ install(TARGETS textthumbnail DESTINATION ${KDE_INSTALL_PLUGINDIR})
if(NOT WIN32)
set(djvuthumbnail_PART_SRCS djvucreator.cpp)
ecm_qt_declare_logging_category(djvuthumbnail_PART_SRCS
HEADER thumbnail-djvu-logsettings.h
IDENTIFIER KIO_THUMBNAIL_DJVU_LOG
CATEGORY_NAME log_kio_thumbnail.djvu)
add_library(djvuthumbnail MODULE ${djvuthumbnail_PART_SRCS})
target_link_libraries(djvuthumbnail KF5::KIOWidgets)
......
/* This file is part of the KDE libraries
Copyright (C) 2001 Malte Starostik <malte@kde.org>
Copyright (C) 2001 Leon Bottou <leon@bottou.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
/*
SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-KDE-Accepted-GPL
SPDX-FileCopyrightText: 2020 Stefan Brüns <stefan.bruens@rwth-aachen.de>
*/
#include "djvucreator.h"
#include "thumbnail-djvu-logsettings.h"
#include <config-runtime.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <sys/time.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <QFile>
#include <QProcess>
#include <QString>
#include <QImage>
extern "C"
{
Q_DECL_EXPORT ThumbCreator *new_creator()
......@@ -49,77 +20,28 @@ extern "C"
bool DjVuCreator::create(const QString &path, int width, int height, QImage &img)
{
int output[2];
QByteArray data(1024, 'k');
bool ok = false;
if (pipe(output) == -1)
return false;
const char* argv[8];
QByteArray sizearg, fnamearg;
sizearg = QByteArray::number(width) + 'x' + QByteArray::number(height);
fnamearg = QFile::encodeName( path );
argv[0] = "ddjvu";
argv[1] = "-page";
argv[2] = "1"; // krazy:exclude=doublequote_chars
argv[3] = "-size";
argv[4] = sizearg.data();
argv[5] = fnamearg.data();
argv[6] = nullptr;
pid_t pid = fork();
if (pid == 0)
{
close(output[0]);
dup2(output[1], STDOUT_FILENO);
execvp(argv[0], const_cast<char *const *>(argv));
exit(1);
}
else if (pid >= 0)
{
close(output[1]);
int offset = 0;
while (!ok) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(output[0], &fds);
struct timeval tv;
tv.tv_sec = 20;
tv.tv_usec = 0;
if (select(output[0] + 1, &fds, nullptr, nullptr, &tv) <= 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
break; // error or timeout
}
if (FD_ISSET(output[0], &fds)) {
int count = read(output[0], data.data() + offset, 1024);
if (count == -1)
break;
if (count) // prepare for next block
{
offset += count;
data.resize(offset + 1024);
}
else // got all data
{
data.resize(offset);
ok = true;
}
}
}
if (!ok)
kill(pid, SIGTERM);
int status = 0;
if (waitpid(pid, &status, 0) != pid || (status != 0 && status != 256) )
ok = false;
QProcess ddjvu;
const QStringList args{
QStringLiteral("-page=1"),
QStringLiteral("-size=") + QString::number(width) + QChar('x') + QString::number(height),
QStringLiteral("-format=ppm"),
path
};
ddjvu.start(QStringLiteral("ddjvu"), args);
ddjvu.waitForFinished();
static bool warnOnce = true;
if (ddjvu.exitCode() != 0) {
if (warnOnce) {
qCWarning(KIO_THUMBNAIL_DJVU_LOG) << ddjvu.error()
<< ddjvu.readAllStandardError();
warnOnce = false;
}
else
{
close(output[1]);
}
close(output[0]);
int l = img.loadFromData( data );
return ok && l;
return false;
}
img.load(&ddjvu, "ppm");
return true;
}
/* This file is part of the KDE libraries
Copyright (C) 2000 Malte Starostik <malte@kde.org>
Copyright (C) 2003 Leon Bottou <leon@bottou.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
/*
SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-KDE-Accepted-GPL
SPDX-FileCopyrightText: 2020 Stefan Brüns <stefan.bruens@rwth-aachen.de>
*/
#ifndef _DJVUCREATOR_H_
#define _DJVUCREATOR_H_
#ifndef DJVUCREATOR_H__
#define DJVUCREATOR_H__
#include <kio/thumbcreator.h>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment