From 015a265d32bc216d198410f11d63cbf84245c124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Tue, 22 Sep 2020 14:43:59 +0200 Subject: [PATCH] [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 --- thumbnail/CMakeLists.txt | 5 ++ thumbnail/djvucreator.cpp | 136 ++++++++------------------------------ thumbnail/djvucreator.h | 25 ++----- 3 files changed, 39 insertions(+), 127 deletions(-) diff --git a/thumbnail/CMakeLists.txt b/thumbnail/CMakeLists.txt index 70da759d..d967ce8a 100644 --- a/thumbnail/CMakeLists.txt +++ b/thumbnail/CMakeLists.txt @@ -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) diff --git a/thumbnail/djvucreator.cpp b/thumbnail/djvucreator.cpp index ce07bbd2..71a83425 100644 --- a/thumbnail/djvucreator.cpp +++ b/thumbnail/djvucreator.cpp @@ -1,44 +1,15 @@ -/* This file is part of the KDE libraries - Copyright (C) 2001 Malte Starostik - Copyright (C) 2001 Leon Bottou - - 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 */ - #include "djvucreator.h" +#include "thumbnail-djvu-logsettings.h" -#include - -#include -#include -#include -#include -#ifdef HAVE_SYS_SELECT_H -#include -#endif -#include -#include -#include -#include - -#include +#include +#include #include - 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(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; } diff --git a/thumbnail/djvucreator.h b/thumbnail/djvucreator.h index 45f1fb3d..b29bc48b 100644 --- a/thumbnail/djvucreator.h +++ b/thumbnail/djvucreator.h @@ -1,25 +1,10 @@ -/* This file is part of the KDE libraries - Copyright (C) 2000 Malte Starostik - Copyright (C) 2003 Leon Bottou - - 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 */ -#ifndef _DJVUCREATOR_H_ -#define _DJVUCREATOR_H_ +#ifndef DJVUCREATOR_H__ +#define DJVUCREATOR_H__ #include -- GitLab