diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index c0ae8b78..b7a1e472 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -81,6 +81,21 @@ QStringList expandCommand(const QString &command, const QString &input, const QS return args; } +// How a started helper ended. One that never started has said nothing about the file it was given. +enum class Ran { NotStarted, Failed, Finished }; + +Ran finish(QProcess &process) { + if (!process.waitForStarted(15000)) + return Ran::NotStarted; + // A broken file must never hang a tile forever. + if (!process.waitForFinished(15000)) { + process.kill(); + process.waitForFinished(1000); + return Ran::Failed; + } + return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0 ? Ran::Finished : Ran::Failed; +} + } // namespace Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories, @@ -117,7 +132,10 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna } const QString exec = fields.value("Exec"); const QString tryExec = fields.value("TryExec"); - if (exec.isEmpty() || (!tryExec.isEmpty() && QStandardPaths::findExecutable(tryExec).isEmpty())) + // The program Exec names, read as it will run (%% is one percent sign), has to be there: a .thumbnailer file a removed program left behind draws nothing. + const QStringList command = expandCommand(exec, {}, {}, 0); + const auto missing = [](const QString &program) { return QStandardPaths::findExecutable(program).isEmpty(); }; + if (command.isEmpty() || missing(command.first()) || (!tryExec.isEmpty() && missing(tryExec))) continue; for (const QString &mime : fields.value("MimeType").split(';', Qt::SkipEmptyParts)) m_thumbnailers.insert(mime.trimmed(), exec); @@ -231,6 +249,9 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons const QString named = mime.endsWith("/*") ? QMimeDatabase().mimeTypeForFile(path, QMimeDatabase::MatchExtension).name() : mime; + // A file that cannot be read is not a broken one: nothing is tried, so nothing is remembered. + if (!info.isReadable()) + return {}; // A file its extension could not name goes to the reader or thumbnailer for what it holds. const QString type = FolderModel::typeByContent(path, named); const int pixels = bucketPixels(bucketFor(size)); @@ -238,8 +259,9 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons QImage image = generate(path, type, pixels, attempted); if (image.isNull()) { // Only a reader or thumbnailer that ran and failed is remembered, so - // one installed later still gets its turn. - if (attempted) + // one installed later still gets its turn. A file moved away or + // written to meanwhile was never judged as it is now. + if (attempted && QFileInfo(path).lastModified() == modified) rememberFailure(path, modified); return {}; } @@ -267,14 +289,18 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels } const QString command = m_thumbnailers.value(mime); if (!command.isEmpty()) { - attempted = true; - return runThumbnailer(command, path, pixels); + // A thumbnailer that could not start has not judged the file, whatever a reader made of it first; a PDF is then left to pdftoppm. + QImage image; + attempted = runThumbnailer(command, path, pixels, image); + if (attempted) + return image; } // Without a PDF thumbnailer installed, Poppler's pdftoppm (there with // CUPS) draws the first page. if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) { - attempted = true; - return pdfPage(path, pixels); + QImage image; + attempted = pdfPage(path, pixels, image); + return image; } return {}; } @@ -326,27 +352,23 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper return page; } -QImage Thumbnails::pdfPage(const QString &path, int pixels) { +bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image) { QTemporaryDir scratch; if (!scratch.isValid()) - return {}; + return false; QProcess process; process.start("pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), path, scratch.filePath("page")}); - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); - return {}; - } - if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) - return {}; - return QImage(scratch.filePath("page.png")); + const Ran ran = finish(process); + if (ran == Ran::Finished) + image = QImage(scratch.filePath("page.png")); + return ran != Ran::NotStarted; } -QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels) const { +bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const { QTemporaryDir scratch; if (!scratch.isValid()) - return {}; + return false; const QString output = scratch.filePath("thumbnail.png"); // A thumbnailer given a URI asks GIO for the file, and GIO turns one on // a GVfs mount (another machine's) back into its network address, @@ -361,7 +383,7 @@ QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, i } QStringList args = expandCommand(command, input, output, pixels); if (args.isEmpty()) - return {}; + return false; const QString program = args.takeFirst(); // ffmpegthumbnailer's -f frames the picture as a film strip; the tile // marks a video with its own play badge instead. @@ -371,15 +393,10 @@ QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, i QProcess process; process.setProcessChannelMode(QProcess::ForwardedErrorChannel); process.start(program, args); - // A broken file must never hang a tile forever. - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); - return {}; - } - if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) - return {}; - return QImage(output); + const Ran ran = finish(process); + if (ran == Ran::Finished) + image = QImage(output); + return ran != Ran::NotStarted; } void Thumbnails::remember(QImage image, const QString &path, int size, const QDateTime &modified) const { @@ -388,8 +405,9 @@ void Thumbnails::remember(QImage image, const QString &path, int size, const QDa } // Markers say which round of thumbnailing failed: ones from before a fix -// (round 2: files on other machines) are tried again once. -static const QString failedRound = QStringLiteral("2"); +// (round 2: files on other machines; round 3: names holding a %, helpers +// that never started, camera RAW) are tried again once. +static const QString failedRound = QStringLiteral("3"); void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified) const { QImage marker(1, 1, QImage::Format_ARGB32); diff --git a/src/thumbnails.h b/src/thumbnails.h index 531e9c8d..297e6c9f 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -50,8 +50,9 @@ class Thumbnails { QString sharpPath(const QString &path, int pixels, bool alpha) const; // Sets `attempted` when Qt's reader or a thumbnailer could take the file. QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const; - QImage runThumbnailer(const QString &command, const QString &path, int pixels) const; - static QImage pdfPage(const QString &path, int pixels); + // Both answer whether the program ran at all: one that could not start has not judged the file. + bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const; + static bool pdfPage(const QString &path, int pixels, QImage &image); void remember(QImage image, const QString &path, int size, const QDateTime &modified) const; void rememberFailure(const QString &path, const QDateTime &modified) const; bool failedBefore(const QString &path, const QDateTime &modified) const; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6a0aeeb4..53eb3076 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6564,6 +6564,107 @@ private slots: QVERIFY(QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(dir.filePath("clip.mp4"))))); } + void thumbnailsRememberOnlyAVerdictOnTheFile() + { + QTemporaryDir dir; + const auto marked = [&](const QString &path) { + return QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(path))); + }; + const auto thumbnailer = [&](const QString &name, const QString &exec, const QString &type) { + write(dir.filePath(name + ".thumbnailer"), ("[Thumbnailer Entry]\nExec=" + exec + "\nMimeType=" + type + ";\n").toUtf8()); + }; + const auto script = [&](const QString &name, const QByteArray &body) { + write(dir.filePath(name), body); + return "/bin/sh \"" + dir.filePath(name) + "\" %i %o"; + }; + const auto clip = [&](const QString &name) { + write(dir.filePath(name), "not a video"); + return dir.filePath(name); + }; + const QString frame = dir.filePath("frame.png"), copier = dir.filePath("copier"), unstartable = dir.filePath("unstartable"); + const auto owner = QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner; + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(frame)); + QVERIFY(QFile::copy("/bin/cp", copier) && QFile::setPermissions(copier, owner)); + write(unstartable, "#!/no/such/interpreter\n"); + QVERIFY(QFile::setPermissions(unstartable, owner)); + + thumbnailer("gone", "\"" + dir.filePath("no-such-program") + "\" %i %o", "video/mp4"); + thumbnailer("copier", "\"" + copier + "\" \"" + frame + "\" %o", "video/webm"); + thumbnailer("taker", script("take.sh", "rm \"$1\"\nexit 1\n"), "video/ogg"); + thumbnailer("writer", script("write.sh", "echo more >> \"$1\"\nexit 1\n"), "video/quicktime"); + thumbnailer("failer", script("fail.sh", "echo run >> \"$0.runs\"\nexit 1\n"), "video/mpeg"); + thumbnailer("unstartable", "\"" + unstartable + "\" %i %o", "application/pdf"); + // A program in a folder with a percent sign in its name is spelled with %% in Exec. + const QString percent = dir.filePath("100%tools/copier"); + QVERIFY(QDir(dir.path()).mkpath("100%tools") && QFile::copy("/bin/cp", percent) && QFile::setPermissions(percent, owner)); + thumbnailer("percent", "\"" + QString(percent).replace("%", "%%") + "\" \"" + frame + "\" %o", "video/x-matroska"); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + + // A .thumbnailer file whose program is not installed is no thumbnailer. + QVERIFY(thumbnails.thumbnailerFor("video/mp4").isEmpty()); + QVERIFY(thumbnails.load(clip("gone.mp4"), "video/mp4", 128).isNull()); + QVERIFY(!marked(dir.filePath("gone.mp4"))); + // One that is there under a name with a percent sign is found and draws. + QCOMPARE(thumbnails.load(clip("percent.mkv"), "video/x-matroska", 128).size(), QSize(10, 20)); + + // A thumbnailer that could not start has said nothing about the file: once it can, it draws it. + const QString later = clip("later.webm"); + QVERIFY(QFile::remove(copier)); + QVERIFY(thumbnails.load(later, "video/webm", 128).isNull()); + QVERIFY(!marked(later)); + QVERIFY(QFile::copy("/bin/cp", copier) && QFile::setPermissions(copier, owner)); + QCOMPARE(thumbnails.load(later, "video/webm", 128).size(), QSize(10, 20)); + + // A PDF's thumbnailer that cannot start leaves the page to pdftoppm, whose verdict counts only when it ran too. + const QString pdftoppm = dir.filePath("bin/pdftoppm"); + const QByteArray programs = qgetenv("PATH"); + QVERIFY(QDir(dir.path()).mkpath("bin")); + const auto pageWith = [&](const QByteArray &program, const QString &pdf) { + write(pdftoppm, program); + QFile::setPermissions(pdftoppm, owner); + qputenv("PATH", dir.filePath("bin").toUtf8()); + const QImage page = thumbnails.load(pdf, "application/pdf", 128); + qputenv("PATH", programs); + return page; + }; + const QString torn = clip("torn.pdf"), paper = clip("paper.pdf"); + QVERIFY(pageWith("#!/bin/sh\nexit 1\n", torn).isNull()); + QVERIFY(marked(torn)); + QVERIFY(pageWith("#!/no/such/interpreter\n", paper).isNull()); + QVERIFY(!marked(paper)); + // Its last argument is the page's path without the ending. + const QByteArray drawing = "#!/bin/sh\nfor last; do :; done\n/bin/cp \"" + frame.toUtf8() + "\" \"$last.png\"\n"; + QCOMPARE(pageWith(drawing, clip("drawn.pdf")).size(), QSize(10, 20)); + + // A file taken away during the run, or written to, was not judged as it is now. + const QString taken = clip("taken.ogv"), written = clip("written.mov"); + QVERIFY(thumbnails.load(taken, "video/ogg", 128).isNull()); + QVERIFY(!QFile::exists(taken)); + QVERIFY(!marked(taken)); + { + QFile old(written); + QVERIFY(old.open(QIODevice::ReadWrite)); + QVERIFY(old.setFileTime(QDateTime::currentDateTime().addSecs(-60), QFileDevice::FileModificationTime)); + } + QVERIFY(thumbnails.load(written, "video/quicktime", 128).isNull()); + QCOMPARE(QFileInfo(written).size(), 16); + QVERIFY(!marked(written)); + + // A file that cannot be read is not tried, so it is not held back once it can be; one that fails is. + if (geteuid() == 0) + QSKIP("root reads every file"); + const QString locked = clip("locked.mpg"), runs = dir.filePath("fail.sh.runs"); + QVERIFY(QFile::setPermissions(locked, QFile::WriteOwner)); + QVERIFY(thumbnails.load(locked, "video/mpeg", 128).isNull()); + QVERIFY(!QFile::exists(runs)); + QVERIFY(!marked(locked)); + QVERIFY(QFile::setPermissions(locked, QFile::ReadOwner | QFile::WriteOwner)); + QVERIFY(thumbnails.load(locked, "video/mpeg", 128).isNull()); + QVERIFY(thumbnails.load(locked, "video/mpeg", 128).isNull()); + QVERIFY(marked(locked)); + QCOMPARE(QFileInfo(runs).size(), 4); + } + void thumbnailsRememberOnlyWhatFailed() { QTemporaryDir dir; @@ -6593,12 +6694,13 @@ private slots: write(dir.filePath("never.broken"), "x"); QVERIFY(withBroken.load(dir.filePath("never.broken"), "image/x-broken", 128).isNull()); const QImage marker(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(dir.filePath("never.broken")))); - QCOMPARE(marker.text("X-Folio::Round"), QString("2")); - // An old marker, without its round, for a picture that can be made. + QCOMPARE(marker.text("X-Folio::Round"), QString("3")); + // A marker from round 2, for a picture that can be made. const QFileInfo picture(dir.filePath("frame.png")); QImage old(1, 1, QImage::Format_ARGB32); old.setText("Thumb::MTime", QString::number(picture.lastModified().toSecsSinceEpoch())); old.setText("X-Folio::MTime", QString::number(picture.lastModified().toMSecsSinceEpoch())); + old.setText("X-Folio::Round", "2"); QVERIFY(old.save(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(picture.filePath())))); QVERIFY(!withBroken.load(picture.filePath(), "image/png", 128).isNull()); }