diff --git a/README.md b/README.md index aa126641..cd739c48 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Files (`folio`) is Omarchy's file manager. It's one quiet window that follows your Omarchy theme and language, works from the keyboard, and never crawls your home folder in the background: folders are read when you open them, and thumbnails are made when a tile needs one, shared with every other app through `~/.cache/thumbnails`. - **Three views**: Finder-style columns, a list that opens folders in place as a tree, and a grid with real previews. Ctrl+1/2/3 switches. -- **Space previews** pictures, video, audio, PDFs, text and Markdown at full size, in their own window. +- **Space previews** pictures, video, audio, PDFs, text and Markdown at full size, and the names inside archives, in their own window. - **The sidebar** holds everything in your home folder, pinned folders (shared with GTK file dialogs), Recents, Trash with restore, your drives, and the machines on your tailnet, which open over SFTP. - **Right-click** (or Alt+Enter) to open with another app, rename, duplicate, compress, share by Dropbox link, Taildrop or LocalSend, and delete. Copy and paste work with other apps, and nothing is ever overwritten. - **Split** the window in two with Ctrl+Alt+N to move things between folders. diff --git a/src/PreviewPane.qml b/src/PreviewPane.qml index 5cc8a970..2b2079b5 100644 --- a/src/PreviewPane.qml +++ b/src/PreviewPane.qml @@ -46,6 +46,9 @@ Rectangle { readonly property var info: backend.details // The item's type: its name's, or the one read from what it holds when its name gave none, never one read for another item. readonly property string type: (info.path === item.path && info.mime) || item.mime || "" + // Text files show their opening lines in a frame, archives bsdtar can list the names inside. + readonly property bool archive: hasItem && backend.listsArchive(type) + readonly property bool listed: hasItem && (item.kind === "text" || archive) // Described once the folder's loaded has put the cursor back on its // item: a re-read leaves another item on the cursor's row for a moment, // and describing that would drop what's been read of this one. @@ -117,7 +120,7 @@ Rectangle { // As many pixels as it's drawn with, in steps, so resizing // the window doesn't read it again at every width. readonly property int pixels: Math.min(8192, Math.ceil(Math.max(width, height) * Screen.devicePixelRatio / 512) * 512) - readonly property bool shown: pane.hasItem && !pane.item.isDir && pane.item.kind !== "text" && !pane.pluginItem + readonly property bool shown: pane.hasItem && !pane.item.isDir && !pane.listed && !pane.pluginItem // The file the cursor last rested on, and those read ahead. property string rested: "" property var ahead: [] @@ -150,10 +153,10 @@ Rectangle { visible: status === Image.Ready } - // Text files show their opening lines instead of a picture. + // Text files show their opening lines instead of a picture, archives the names inside. Rectangle { anchors.fill: parent - visible: pane.hasItem && pane.item.kind === "text" + visible: pane.listed color: theme.background border.width: 1 border.color: Qt.alpha(theme.foreground, 0.12) @@ -168,6 +171,13 @@ Rectangle { textFormat: Text.PlainText elide: Text.ElideRight } + Sentence { + visible: pane.archive && !pane.info.text + margin: 14 + text: pane.info.sentence || qsTr("Reading the archive…") + color: theme.secondaryForeground + size: 12 + } } // Only for what has no picture: while one's on its way (moving @@ -175,7 +185,7 @@ Rectangle { // flashing the icon. Glyph { anchors.centerIn: parent - visible: pane.hasItem && pane.item.kind !== "text" + visible: pane.hasItem && !pane.listed && (!big.shown || (big.failed && quick.status !== Image.Ready)) name: pane.pluginItem ? pane.item.icon : pane.hasItem && pane.item.isDir ? "folder" : "file" size: 72 diff --git a/src/PreviewWindow.qml b/src/PreviewWindow.qml index 2b710086..d5b55efb 100644 --- a/src/PreviewWindow.qml +++ b/src/PreviewWindow.qml @@ -149,7 +149,7 @@ ApplicationWindow { anchors.fill: parent active: !win.external sourceComponent: win.viewer === "image" || win.viewer === "animated" ? imageView - : win.viewer === "text" || win.viewer === "markdown" ? textView + : win.viewer === "text" || win.viewer === "markdown" || win.viewer === "archive" ? textView : win.viewer === "folder" ? folderView : otherView } @@ -173,6 +173,13 @@ ApplicationWindow { : qsTr("Playing video and audio needs Qt Multimedia, from the qt6-multimedia package.") color: theme.foreground } + + // An archive's names are read as bsdtar prints them. + Sentence { + visible: win.viewer === "archive" && !previewer.text + text: previewer.sentence || qsTr("Reading the archive…") + color: previewer.sentence ? theme.foreground : theme.secondaryForeground + } } } @@ -285,7 +292,7 @@ ApplicationWindow { } } - // Text to read, Markdown rendered; selectable, never editable. + // Text to read, Markdown rendered, an archive's names; selectable, never editable. Component { id: textView @@ -299,11 +306,7 @@ ApplicationWindow { function scroll(direction) { contentY = Math.max(0, Math.min(contentHeight - height, contentY + direction * 60)) } function page(direction) { contentY = Math.max(0, Math.min(contentHeight - height, contentY + direction * (height - 60))) } - Connections { - target: previewer - function onChanged() { flick.contentY = 0 } - } - + // Never sent to the top when the text changes: an archive's names keep arriving, and another file empties the text first, which brings the view back by itself, even one still gliding. TextEdit { id: body width: Math.min(flick.width - 96, win.viewer === "markdown" ? 820 : 1200) diff --git a/src/Sentence.qml b/src/Sentence.qml new file mode 100644 index 00000000..7f678413 --- /dev/null +++ b/src/Sentence.qml @@ -0,0 +1,13 @@ +import QtQuick + +// A sentence in the middle of an empty view: what it waits for, that it's empty, or why not. +FolioText { + // Kept clear on either side. + property int margin: 48 + + anchors.centerIn: parent + width: parent.width - 2 * margin + horizontalAlignment: Text.AlignHCenter + wrapMode: Text.Wrap + color: theme.foreground +} diff --git a/src/backend.cpp b/src/backend.cpp index 878f98ba..3fb36970 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -451,6 +451,8 @@ void Backend::describe(const QVariantMap &item, bool inTrash) { const QString kind = item.value("kind").toString(); const QString mime = item.value("mime").toString(); const quint64 generation = ++*m_describing; + // Whatever a helper program was reading for the last item goes unread. + m_helper.stop(); // The same item again (its folder re-read) keeps what it shows until // the new reading is in, rather than flickering. QVariantMap details; @@ -464,6 +466,19 @@ void Backend::describe(const QVariantMap &item, bool inTrash) { // a plugin's own items. m_pluginItem = path.startsWith('/') && !inTrash ? item : QVariantMap(); showPluginRows(askPlugins()); + // What a helper program reads comes on top of the rest. + const auto help = [this, path, mime](const QVariantMap &details) { + // An archive's names, shown as bsdtar prints them: by the type read + // from what it holds when its name gave none. + if (Previewer::listsArchive(details.value("mime", mime).toString())) { + Previewer::listArchive(m_helper, path, [this, details](const QString &text, const QString &sentence) { + m_details = details; + m_details.insert("text", text); + m_details.insert("sentence", sentence); + emit detailsChanged(); + }); + } + }; // Every real file and folder has a creation time to read; a plugin's // item has none. if (!path.startsWith('/')) @@ -472,13 +487,14 @@ void Backend::describe(const QVariantMap &item, bool inTrash) { // Reading the file waits on its disk, which for a slow mount would hold // up moving through files, so it happens off the UI thread. auto *watcher = new QFutureWatcher(this); - connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, details]() mutable { + connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, details, help]() mutable { watcher->deleteLater(); if (generation != *m_describing) return; // The selection moved on while this was read. details.insert(watcher->result()); m_details = details; emit detailsChanged(); + help(details); }); // Stepping through files faster than they're read queues readings; the // ones overtaken before they start are skipped rather than left to tie diff --git a/src/backend.h b/src/backend.h index 4e5ea958..9886c81d 100644 --- a/src/backend.h +++ b/src/backend.h @@ -9,6 +9,7 @@ #include "foldermodel.h" #include "mounter.h" #include "plugins.h" +#include "previewer.h" // Where the window is and how it got there: the current folder, its // breadcrumbs, back/forward history, and the actions that leave the folder @@ -29,9 +30,10 @@ class Backend : public QObject { Q_PROPERTY(bool previewing READ previewing NOTIFY previewingChanged) // What the details column shows beyond the listing, for the item last // given to describe(), once they've been read in the background: a - // picture's dimensions, a text's opening lines, or a trashed item's - // origin and deletion time; and under `plugins`, plugins' rows about a - // file or folder, [[label, value]...]. + // picture's dimensions, a text's opening lines, an archive's names (or + // a sentence in their place), or a trashed item's origin and deletion + // time; and under `plugins`, plugins' rows about a file or folder, + // [[label, value]...]. // Changes whenever an order is chosen, for what shows other folders // than the one shown (the columns) to look their orders up again. Q_PROPERTY(int sortsRevision READ sortsRevision NOTIFY sortsChanged) @@ -96,6 +98,8 @@ class Backend : public QObject { // The system's own name for a type ("JPEG image"), in the language of // the moment. Q_INVOKABLE static QString typeName(const QString &mime); + // Whether the details list an archive of this type's names. + Q_INVOKABLE static bool listsArchive(const QString &mime) { return Previewer::listsArchive(mime); } // The item to select once the folder has loaded (where we came from, or // what "Show in folder" asked for); empty means the first item. Q_INVOKABLE QString takeSelection(); @@ -218,4 +222,6 @@ class Backend : public QObject { // The pane the working directory follows: the one used last. static inline Backend *s_working = nullptr; void moveWorkingDirectory(); + // Reads what only a helper program can, for the item described last. + Helper m_helper{this}; }; diff --git a/src/i18n/en.json b/src/i18n/en.json index c2bd349a..6fc98e21 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -40,12 +40,24 @@ "one": "%Ln item will be deleted permanently. This can't be undone.", "other": "%Ln items will be deleted permanently. This can't be undone." }, + "%Ln more": { + "one": "%Ln more", + "other": "%Ln more" + }, "A folder here is already called %1.": "A folder here is already called %1.", "A folder, or a server's address like sftp://host/": "A folder, or a server's address like sftp://host/", "A name can't contain \"/\".": "A name can't contain \"/\".", "A screenful up or down": "A screenful up or down", "Add or remove one": "Add or remove one", "Archive": "Archive", + "At least %Ln items": { + "one": "At least %Ln item", + "other": "At least %Ln items" + }, + "At least %Ln more": { + "one": "At least %Ln more", + "other": "At least %Ln more" + }, "Back / ahead 5 seconds": "Back / ahead 5 seconds", "Back / forward": "Back / forward", "Bigger, smaller or normal tiles": "Bigger, smaller or normal tiles", @@ -121,6 +133,7 @@ "Couldn't open this PDF: %1": "Couldn't open this PDF: %1", "Couldn't play this file: %1": "Couldn't play this file: %1", "Couldn't read %1.": "Couldn't read %1.", + "Couldn't read this archive.": "Couldn't read this archive.", "Couldn't read this picture.": "Couldn't read this picture.", "Couldn't recreate %1.": "Couldn't recreate %1.", "Couldn't remove %1.": "Couldn't remove %1.", @@ -161,6 +174,7 @@ "one": "Emptied the trash: %Ln item deleted.", "other": "Emptied the trash: %Ln items deleted." }, + "Empty": "Empty", "Empty Trash": "Empty Trash", "Empty Trash…": "Empty Trash…", "Empty the trash?": "Empty the trash?", @@ -217,6 +231,8 @@ "Keep Both": "Keep Both", "Keyboard shortcuts": "Keyboard shortcuts", "Kind": "Kind", + "Listing archives needs bsdtar, from the libarchive package.": "Listing archives needs bsdtar, from the libarchive package.", + "Listing this archive took too long.": "Listing this archive took too long.", "Making a preview…": "Making a preview…", "Merge": "Merge", "Merging adds what's in this folder to the one there. What has the same name inside is replaced, and the replaced items go to the trash.": "Merging adds what's in this folder to the one there. What has the same name inside is replaced, and the replaced items go to the trash.", @@ -289,6 +305,7 @@ "Previewing PDFs needs Qt PDF, from the qt6-webengine package.": "Previewing PDFs needs Qt PDF, from the qt6-webengine package.", "Previous (%1)": "Previous (%1)", "Previous / next file": "Previous / next file", + "Reading the archive…": "Reading the archive…", "Recents": "Recents", "Redid compressing into %1.": "Redid compressing into %1.", "Redid copying %Ln items.": { @@ -395,6 +412,7 @@ "The drive is gone.": "The drive is gone.", "The drive stopped taking data (%1).": "The drive stopped taking data (%1).", "The drive was changed or removed.": "The drive was changed or removed.", + "The names in this archive are encrypted.": "The names in this archive are encrypted.", "The preview closed unexpectedly on %1.": "The preview closed unexpectedly on %1.", "There's nothing in %1 to extract.": "There's nothing in %1 to extract.", "There's nothing to redo.": "There's nothing to redo.", diff --git a/src/previewer.cpp b/src/previewer.cpp index 91735f27..55e220df 100644 --- a/src/previewer.cpp +++ b/src/previewer.cpp @@ -8,7 +8,10 @@ #include #include #include +#include +#include #include +#include #include Previewer::Previewer(QObject *parent) : Previewer(FileOps::openInDefaultApp, parent) {} @@ -36,6 +39,8 @@ QString Previewer::viewerFor(const QString &mime, bool isDir) { return "image"; return "thumbnail"; } + if (listsArchive(mime)) + return "archive"; if (mime == "text/markdown") return "markdown"; const QMimeType type = QMimeDatabase().mimeTypeForName(mime); @@ -44,6 +49,13 @@ QString Previewer::viewerFor(const QString &mime, bool isDir) { return "other"; } +bool Previewer::listsArchive(const QString &mime) { + // Whatever Folio calls an archive (every compressed tar among them), + // but a lone compressed file (notes.txt.gz), which bsdtar can't list. + static const QStringList single{"application/gzip", "application/x-xz", "application/zstd"}; + return FolderModel::kindForMime(mime) == "archive" && !single.contains(mime); +} + QString Previewer::readText(const QString &path, qint64 limit) { QFile file(path); if (!file.open(QIODevice::ReadOnly)) @@ -218,6 +230,18 @@ void Previewer::setIndex(int index, bool report) { m_viewer = viewerFor(e.mime, e.isDir); m_text.clear(); m_readType.clear(); + m_sentence.clear(); + m_helper.stop(); + // An archive's names are listed by bsdtar as they come. + const auto listNames = [this](const QString &path) { + listArchive(m_helper, path, [this](const QString &text, const QString &sentence) { + m_text = text; + m_sentence = sentence; + emit changed(); + }); + }; + if (m_viewer == "archive") + listNames(e.path); // A folder's count and first pictures, and a text's opening part, are // read off the UI thread: a big folder, or any file on a slow mount, @@ -229,7 +253,7 @@ void Previewer::setIndex(int index, bool report) { const bool unnamed = e.mime == "application/octet-stream"; if (count || text || unnamed) { auto *watcher = new QFutureWatcher>(this); - connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, unnamed] { + connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, unnamed, listNames] { watcher->deleteLater(); if (generation != *m_reading) return; // Stepped on to another item while this was read. @@ -243,6 +267,9 @@ void Previewer::setIndex(int index, bool report) { // A ".txt" that turns out to be binary is shown as a plain file. if ((m_viewer == "text" || m_viewer == "markdown") && m_text.isEmpty() && entry.size > 0) m_viewer = "other"; + // An archive its name didn't say is one is listed once its bytes have said so. + if (unnamed && m_viewer == "archive") + listNames(entry.path); emit changed(); }); const bool showHidden = m_showHidden; @@ -288,3 +315,188 @@ void Previewer::connectToFolio() { } }); } + +// An archive's listing is cut there; bsdtar prints each name on a line. +static constexpr qint64 archiveOutputLimit = 32 * 1024 * 1024; + +Helper::Helper(QObject *parent, int milliseconds) : QObject(parent) { + m_limit.setSingleShot(true); + m_limit.setInterval(milliseconds); + connect(&m_limit, &QTimer::timeout, this, [this] { end(stopped, {}); }); +} + +Helper::~Helper() { + stop(); +} + +void Helper::run(const QString &program, const QStringList &args, qint64 outputLimit, + const std::function &output, + const std::function &finished) { + stop(); + m_read = 0; + m_outputLimit = outputLimit; + m_errors.clear(); + m_finished = finished; + auto *process = new QProcess(this); + m_process = process; + // UTF-8 names print as they are, whatever Folio's language. + QProcessEnvironment environment = QProcessEnvironment::systemEnvironment(); + environment.insert("LC_ALL", "C.UTF-8"); + process->setProcessEnvironment(environment); + // No terminal or input to ask for a passphrase on. + process->setStandardInputFile(QProcess::nullDevice()); + process->setUnixProcessParameters(QProcess::UnixProcessFlag::CreateNewSession); + // It goes with Folio, even when Folio crashes. + process->setChildProcessModifier([] { ::prctl(PR_SET_PDEATHSIG, SIGKILL); }); + const auto take = [this, process, output] { + const QByteArray part = process->readAllStandardOutput(); + m_read += part.size(); + if (m_read > m_outputLimit) + return false; + if (!part.isEmpty()) + output(part); + return true; + }; + connect(process, &QProcess::readyReadStandardOutput, this, [this, take] { + if (!take()) + end(stopped, {}); + }); + // Read as it arrives, so a program that prints errors without end fills no memory. + const auto keep = [this, process] { + const QByteArray part = process->readAllStandardError(); + m_errors += part.left(errorsKept - m_errors.size()); + }; + connect(process, &QProcess::readyReadStandardError, this, keep); + connect(process, &QProcess::finished, this, [this, take, keep](int code, QProcess::ExitStatus status) { + if (!take()) { + end(stopped, {}); + return; + } + keep(); + end(status == QProcess::NormalExit ? code : crashed, std::exchange(m_errors, {})); + }); + // A program that isn't installed never finishes, so this is the only word from it. + connect(process, &QProcess::errorOccurred, this, [this](QProcess::ProcessError error) { + if (error == QProcess::FailedToStart) + end(missing, {}); + }); + m_limit.start(); + process->start(program, args); +} + +void Helper::stop() { + m_limit.stop(); + m_finished = nullptr; + QProcess *process = std::exchange(m_process, nullptr); + if (!process) + return; + process->disconnect(this); + if (process->state() == QProcess::NotRunning) { + process->deleteLater(); + return; + } + // Killed without waiting on a hung disk; the object goes once the process has exited, or has failed to start. + process->setParent(nullptr); + connect(process, &QProcess::finished, process, &QObject::deleteLater); + connect(process, &QProcess::errorOccurred, process, [process](QProcess::ProcessError error) { + if (error == QProcess::FailedToStart) + process->deleteLater(); + }); + // It leads a session of its own, so what it started (a program bsdtar unpacks with) goes with it. + if (const qint64 pid = process->processId(); pid > 0) + ::killpg(pid_t(pid), SIGKILL); + process->kill(); +} + +void Helper::end(int code, const QByteArray &errors) { + const auto finished = m_finished; + stop(); + if (finished) + finished(code, errors); +} + +bool ArchiveNames::add(const QByteArray &output) { + // What was left had no line end in it: the look for one starts at the new part. + qsizetype from = m_pending.size(); + m_pending += output; + const qsizetype before = m_names.size(); + qsizetype start = 0; + qsizetype newline; + while ((newline = m_pending.indexOf('\n', from)) >= 0) { + const QByteArrayView line(m_pending.constData() + start, newline - start); + start = from = newline + 1; + // The "./" a tar made with `-C folder .` starts with, or an ISO's ".", is the archive itself. + if (line.isEmpty() || line == "./" || line == ".") + continue; + ++m_count; + // Names are shown up to a count and up to a size together: a few very long ones would hold the window as a huge text does. + m_full = m_full || m_names.size() == shown || m_bytes + line.size() > shownBytes; + if (m_full) + continue; + m_bytes += line.size(); + m_names << nameFrom(line); + } + m_pending.remove(0, start); + return m_names.size() > before; +} + +QString ArchiveNames::text(bool whole) const { + const int count = int(qMin(m_count, INT_MAX)); + const int more = count - int(m_names.size()); + QStringList lines{whole ? Previewer::tr("%Ln items", nullptr, count) : Previewer::tr("At least %Ln items", nullptr, count), + QString()}; + lines += m_names; + if (more > 0) + lines << (whole ? Previewer::tr("%Ln more", nullptr, more) : Previewer::tr("At least %Ln more", nullptr, more)); + return lines.join('\n'); +} + +// Sample: `./sub dir/caf\303\251 \\ two\nlines.txt` reads `sub dir/café \ two\nlines.txt` ("\n" stays). +QString ArchiveNames::nameFrom(QByteArrayView line) { + if (line.startsWith("./")) + line = line.sliced(2); + const auto octal = [&](qsizetype at) { return at < line.size() && line[at] >= '0' && line[at] <= '7'; }; + QByteArray name; + name.reserve(line.size()); + for (qsizetype i = 0; i < line.size(); ++i) { + if (line[i] == '\\' && i + 1 < line.size() && line[i + 1] == '\\') { + name += '\\'; + ++i; + } else if (line[i] == '\\' && octal(i + 1) && octal(i + 2) && octal(i + 3)) { + name += char((line[i + 1] - '0') * 64 + (line[i + 2] - '0') * 8 + (line[i + 3] - '0')); + i += 3; + } else { + name += line[i]; + } + } + return QString::fromUtf8(name); +} + +void Previewer::listArchive(Helper &helper, const QString &path, + const std::function &show) { + auto names = std::make_shared(); + // An absolute path is never read as an option, or as "-" for stdin. + helper.run("bsdtar", {"-tf", QFileInfo(path).absoluteFilePath()}, archiveOutputLimit, + [names, show](const QByteArray &output) { + if (names->add(output)) + show(names->text(false), {}); + }, + [names, show](int code, const QByteArray &errors) { + if (code == 0 && names->count() > 0) + show(names->text(true), {}); + else if (code == 0) + show({}, tr("Empty")); + else if (code == Helper::stopped && names->count() > 0) + show(names->text(false), {}); + // Stopped before its first name: a large archive on a slow drive, not a damaged one. + else if (code == Helper::stopped) + show({}, tr("Listing this archive took too long.")); + else if (code == Helper::missing) + show({}, tr("Listing archives needs bsdtar, from the libarchive package.")); + // A failed run shows no names: bsdtar reads a renamed text file as a list of names. + else if (errors.contains("ncrypt")) + show({}, tr("The names in this archive are encrypted.")); + else + show({}, tr("Couldn't read this archive.")); + }); +} diff --git a/src/previewer.h b/src/previewer.h index 0bd4d1c2..36c652e4 100644 --- a/src/previewer.h +++ b/src/previewer.h @@ -1,14 +1,66 @@ #pragma once -#include - #include +#include #include +#include #include #include +#include #include "foldermodel.h" +// A helper program (bsdtar, ffprobe) reading the shown item, stopped unread when the view moves on or goes. +class Helper : public QObject { +public: + // How a run ended when it didn't exit by itself. + static constexpr int crashed = -1, stopped = -2, missing = -3; + // As much of stderr as a run keeps for its caller; the rest is read and dropped. + static constexpr qint64 errorsKept = 64 * 1024; + + explicit Helper(QObject *parent = nullptr, int milliseconds = 5000); + ~Helper() override; + + // Stops the last run; `output` gets stdout as it arrives, `finished` the exit code and stderr. + void run(const QString &program, const QStringList &args, qint64 outputLimit, + const std::function &output, + const std::function &finished); + void stop(); + +private: + void end(int code, const QByteArray &errors); + + QProcess *m_process = nullptr; + QTimer m_limit; + qint64 m_read = 0; + qint64 m_outputLimit = 0; + QByteArray m_errors; + std::function m_finished; +}; + +// An archive's names from `bsdtar -tf`, read as they arrive: the first kept, the rest counted. +class ArchiveNames { +public: + static constexpr int shown = 500; + // As much as a text file shows of itself. + static constexpr qint64 shownBytes = 128 * 1024; + + // Takes the next part of the output; true when it adds a name to those shown. + bool add(const QByteArray &output); + qint64 count() const { return m_count; } + // The count, a blank line, the names and how many more; "at least" until it's whole. + QString text(bool whole) const; + // One line of `bsdtar -tf` back to the name it escapes. + static QString nameFrom(QByteArrayView line); + +private: + QByteArray m_pending; + QStringList m_names; + qint64 m_count = 0; + qint64 m_bytes = 0; + bool m_full = false; +}; + // The state behind `folio preview`: the file being looked at, its siblings // to step through, and which viewer shows it. It runs in its own process, as // Sushi does for Nautilus, so a video or PDF that crashes a decoder takes the @@ -29,6 +81,8 @@ class Previewer : public QObject { Q_PROPERTY(QString url READ url NOTIFY changed) Q_PROPERTY(QString text READ text NOTIFY changed) Q_PROPERTY(QString type READ type NOTIFY changed) + // Said in place of an archive's names: it's empty, can't be read, or needs bsdtar. + Q_PROPERTY(QString sentence READ sentence NOTIFY changed) public: explicit Previewer(QObject *parent = nullptr); @@ -54,14 +108,17 @@ class Previewer : public QObject { QString url() const; QString text() const { return m_text; } QString type() const; + QString sentence() const { return m_sentence; } Q_INVOKABLE void next(); Q_INVOKABLE void previous(); Q_INVOKABLE void open(); // Which view shows a type: image, animated, thumbnail, video, audio, - // pdf, markdown, text, folder or other. + // pdf, markdown, text, archive, folder or other. static QString viewerFor(const QString &mime, bool isDir); + // Whether `bsdtar -tf` lists an archive of this type; a lone .gz, .xz or .zst isn't one. + static bool listsArchive(const QString &mime); // The opening part of a text file, ending at a line when there's more, // or nothing when it looks binary. Laying out text takes the UI thread // about a third of a millisecond per KB, so the preview reads 128 KB. @@ -70,6 +127,9 @@ class Previewer : public QObject { // `cells` cells: laid out, each costs the UI thread about as much as a // whole line of prose, and a long table froze the preview for seconds. static QString markdownOpening(const QString &text, int cells = 600); + // Lists an archive with `helper`: `show` gets the names so far, then all, or a sentence instead. + static void listArchive(Helper &helper, const QString &path, + const std::function &show); signals: void changed(); @@ -87,6 +147,8 @@ class Previewer : public QObject { QString m_text; // The type read from what the shown file holds when its name gave none, for this showing only: a file written since is read again. QString m_readType; + QString m_sentence; + Helper m_helper{this}; bool m_showHidden = false; QString m_sort = "name"; quint64 m_generation = 0; diff --git a/src/resources.qrc b/src/resources.qrc index 4912c739..ca0a4de9 100644 --- a/src/resources.qrc +++ b/src/resources.qrc @@ -23,6 +23,7 @@ FileListView.qml PlainButton.qml PlainField.qml + Sentence.qml SortMenu.qml FileMenu.qml FolioDialog.qml diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6a0aeeb4..d75d2123 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -6731,7 +6732,17 @@ private slots: QCOMPARE(Previewer::viewerFor("text/markdown", false), QString("markdown")); QCOMPARE(Previewer::viewerFor("text/x-c++src", false), QString("text")); QCOMPARE(Previewer::viewerFor("application/x-shellscript", false), QString("text")); - QCOMPARE(Previewer::viewerFor("application/zip", false), QString("other")); + QCOMPARE(Previewer::viewerFor("application/zip", false), QString("archive")); + QCOMPARE(Previewer::viewerFor("application/x-compressed-tar", false), QString("archive")); + QCOMPARE(Previewer::viewerFor("application/x-xz-compressed-tar", false), QString("archive")); + QCOMPARE(Previewer::viewerFor("application/x-lzma-compressed-tar", false), QString("archive")); + QCOMPARE(Previewer::viewerFor("application/x-lzip-compressed-tar", false), QString("archive")); + QCOMPARE(Previewer::viewerFor("application/x-iso9660-image", false), QString("archive")); + // bsdtar can't list a lone compressed file, so it stays as 0.2.0 showed it. + QCOMPARE(Previewer::viewerFor("application/gzip", false), QString("other")); + QCOMPARE(Previewer::viewerFor("application/zstd", false), QString("other")); + QCOMPARE(Previewer::viewerFor("application/x-xz", false), QString("other")); + QCOMPARE(Previewer::viewerFor("application/x-sharedlib", false), QString("other")); QCOMPARE(Previewer::viewerFor("inode/directory", true), QString("folder")); } @@ -6856,12 +6867,12 @@ private slots: previewer.show(download); QTRY_COMPARE(previewer.viewer(), QString("pdf")); - // Bytes nothing knows stay a plain file, and so does a name that said a type. + // Bytes nothing knows stay a plain file, and a name that said a type is believed. for (const QString &plain : {blob, claimed}) { previewer.show(plain); QThreadPool::globalInstance()->waitForDone(); QCoreApplication::processEvents(); - QCOMPARE(previewer.viewer(), QString("other")); + QCOMPARE(previewer.viewer(), QString(plain == blob ? "other" : "archive")); } QCOMPARE(previewer.item().value("mime").toString(), QString("application/zip")); } @@ -7639,6 +7650,247 @@ private slots: QCOMPARE(buffers(), QVariantList(buffers().size(), 0)); } + void archiveNamesAreReadAsTheyArrive() + { + // bsdtar's output comes in parts that can end inside a line; a name + // is taken once its line is whole, decoded from bsdtar's escapes. + ArchiveNames names; + QVERIFY(names.add(".\n./\n./sub dir/\n./sub dir/caf\\303\\251 \\\\ two\\nlines.txt\n./ze")); + QVERIFY(!names.add("b")); + QVERIFY(names.add("ra.txt\n")); + QCOMPARE(names.text(false), QString::fromUtf8("At least 3 items\n\nsub dir/\nsub dir/café \\ two\\nlines.txt\nzebra.txt")); + QCOMPARE(names.text(true), QString::fromUtf8("3 items\n\nsub dir/\nsub dir/café \\ two\\nlines.txt\nzebra.txt")); + + // Past the names shown, the rest are only counted. + ArchiveNames many; + QByteArray output; + for (int i = 0; i < ArchiveNames::shown + 3; ++i) + output += QByteArray::number(i) + ".txt\n"; + QVERIFY(many.add(output)); + QVERIFY(!many.add("more.txt\n")); + const QStringList lines = many.text(true).split('\n'); + QCOMPARE(lines.size(), ArchiveNames::shown + 3); + QCOMPARE(lines.first(), QString("%1 items").arg(ArchiveNames::shown + 4)); + QCOMPARE(lines.last(), QString("4 more")); + QCOMPARE(many.text(false).section('\n', -1), QString("At least 4 more")); + + // Past the size shown too, however few the names are; a short one after a long one isn't shown out of its place. + ArchiveNames longOnes; + const QByteArray longName(int(ArchiveNames::shownBytes) * 2 / 5, 'n'); + QVERIFY(longOnes.add(longName + "1\n" + longName + "2\n")); + QVERIFY(!longOnes.add(longName + "3\nshort.txt\n")); + const QStringList few = longOnes.text(true).split('\n'); + QCOMPARE(few.size(), 5); + QCOMPARE(few.first(), QString("4 items")); + QCOMPARE(few.at(3), QString::fromLatin1(longName + "2")); + QCOMPARE(few.last(), QString("2 more")); + + // A name that comes in many parts is one name, whatever part its line ends in. + ArchiveNames parts; + QVERIFY(!parts.add("one")); + QVERIFY(!parts.add(" long")); + QVERIFY(parts.add(" name\ntwo")); + QVERIFY(parts.add("\n")); + QCOMPARE(parts.text(true), QString("2 items\n\none long name\ntwo")); + } + + void archivesListTheirNamesOffTheUiThread() + { + // The details pane and the preview window list an archive with + // bsdtar, and say so when there's nothing to list. + QTemporaryDir dir; + QDir(dir.path()).mkpath("files/sub dir"); + write(dir.filePath("files/sub dir/a b.txt"), "x"); + write(dir.filePath("files/-n.txt"), "x"); + QCOMPARE(QProcess::execute("bsdtar", {"-cf", dir.filePath("-files.tar"), "-C", dir.filePath("files"), "--", "sub dir", "-n.txt"}), 0); + QCOMPARE(QProcess::execute("bsdtar", {"-cf", dir.filePath("empty.tar"), "-T", "/dev/null"}), 0); + write(dir.filePath("notes.zip"), "hello\n"); + const QString listing = "3 items\n\nsub dir/\nsub dir/a b.txt\n-n.txt"; + + Backend backend(dir.path()); + auto describe = [&](const QString &name) { + backend.describe({{"path", dir.filePath(name)}, {"kind", "archive"}, {"mime", "application/x-tar"}}); + }; + describe("-files.tar"); + QVERIFY(!backend.details().contains("text")); + QTRY_COMPARE(backend.details().value("text").toString(), listing); + describe("empty.tar"); + QTRY_COMPARE(backend.details().value("sentence").toString(), QString("Empty")); + QVERIFY(backend.details().value("text").toString().isEmpty()); + // bsdtar takes a text file for a list of names, then fails. + describe("notes.zip"); + QTRY_COMPARE(backend.details().value("sentence").toString(), QString("Couldn't read this archive.")); + QVERIFY(backend.details().value("text").toString().isEmpty()); + + Previewer previewer; + previewer.start(dir.filePath("-files.tar"), false, "name"); + QCOMPARE(previewer.viewer(), QString("archive")); + QTRY_COMPARE(previewer.text(), listing); + QVERIFY(previewer.sentence().isEmpty()); + // The commonest tarball, named by its extension as the folder lists it. + QCOMPARE(QProcess::execute("bsdtar", {"-J", "-cf", dir.filePath("-files.tar.xz"), "-C", dir.filePath("files"), "--", "sub dir", "-n.txt"}), 0); + previewer.show(dir.filePath("-files.tar.xz")); + QCOMPARE(previewer.viewer(), QString("archive")); + QTRY_COMPARE(previewer.text(), listing); + // One whose name says nothing is listed once what it holds says it's an archive. + QVERIFY(QFile::copy(dir.filePath("-files.tar"), dir.filePath("-files"))); + backend.describe({{"path", dir.filePath("-files")}, {"kind", "other"}, {"mime", "application/octet-stream"}}); + QTRY_COMPARE(backend.details().value("text").toString(), listing); + previewer.show(dir.filePath("-files")); + QTRY_COMPARE(previewer.viewer(), QString("archive")); + QTRY_COMPARE(previewer.text(), listing); + + { + const auto restore = pathAt(dir.filePath("files")); + describe("-files.tar"); + QTRY_COMPARE(backend.details().value("sentence").toString(), + QString("Listing archives needs bsdtar, from the libarchive package.")); + } + } + + void archivePreviewKeepsItsPlaceWhileNamesArrive() + { + // A stand-in bsdtar that lists half of a.tar's names, and the rest once `go` is there. + QTemporaryDir tools, dir, home; + QCOMPARE(QProcess::execute("bsdtar", {"-cf", dir.filePath("a.tar"), "-T", "/dev/null"}), 0); + QCOMPARE(QProcess::execute("bsdtar", {"-cf", dir.filePath("b.tar"), "-T", "/dev/null"}), 0); + QCOMPARE(QProcess::execute("bsdtar", {"-cf", dir.filePath("c.tar"), "-T", "/dev/null"}), 0); + const QByteArray go = QFile::encodeName(tools.filePath("go")); + write(tools.filePath("bsdtar"), "#!/bin/sh\nPATH=/usr/bin:/bin\ncase \"$2\" in\n" + "*/a.tar) seq -f name-%g 1 200; while [ ! -e '" + go + "' ]; do sleep 0.05; done; seq -f name-%g 201 400 ;;\n" + "*) seq -f name-%g 1 400 ;;\n" + "esac\n"); + QVERIFY(QFile::setPermissions(tools.filePath("bsdtar"), QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)); + const auto restore = pathAt(tools.path()); + + Theme theme(home.filePath("theme")); + Previewer previewer; + previewer.start(dir.filePath("a.tar"), false, "name"); + QQmlEngine engine; + engine.rootContext()->setContextProperty("theme", &theme); + engine.rootContext()->setContextProperty("previewer", &previewer); + engine.rootContext()->setContextProperty("translator", QVariantMap{{"language", "en"}}); + QQmlComponent component(&engine, QUrl::fromLocalFile(sourceFolder() + "/PreviewWindow.qml")); + std::unique_ptr window(component.create()); + QVERIFY2(window, qPrintable(component.errorString())); + const auto view = [&] { return window->property("view").value(); }; + const auto place = [&] { return view()->property("contentY").toReal(); }; + + // Scrolled down while the rest is still to come: the place is kept when it comes. + QTRY_VERIFY(previewer.text().contains("name-200")); + QTRY_VERIFY(view() && view()->property("contentHeight").toReal() > view()->property("height").toReal() + 300); + QVERIFY(view()->setProperty("contentY", 300)); + write(tools.filePath("go"), "x"); + QTRY_VERIFY(previewer.text().contains("name-400")); + QCOMPARE(place(), 300.0); + + // Another file starts at its top. + previewer.next(); + QCOMPARE(previewer.item().value("name").toString(), QString("b.tar")); + QTRY_VERIFY(previewer.text().contains("name-400")); + QCOMPARE(place(), 0.0); + + // And does so while the last one still glides. + const qreal glide = 3000; + QVERIFY(view()->setProperty("contentY", 300)); + QVERIFY(QMetaObject::invokeMethod(view(), "flick", Q_ARG(qreal, 0), Q_ARG(qreal, -glide))); + QTRY_VERIFY(view()->property("moving").toBool()); + previewer.next(); + QCOMPARE(previewer.item().value("name").toString(), QString("c.tar")); + QTRY_VERIFY(previewer.text().contains("name-400")); + QTRY_VERIFY(!view()->property("moving").toBool()); + QCOMPARE(place(), 0.0); + } + + void helperKeepsOnlyTheStartOfWhatItsProgramComplainsOf() + { + // A program that fails with far more on stderr than a run keeps. + const qint64 printed = 64 * Helper::errorsKept; + Helper helper; + int ended = 0; + QByteArray errors; + helper.run("sh", {"-c", QString("echo first-line >&2; head -c %1 /dev/zero >&2; exit 3").arg(printed)}, 1024, + [](const QByteArray &) {}, [&](int code, const QByteArray &said) { ended = code; errors = said; }); + QTRY_COMPARE(ended, 3); + QCOMPARE(qint64(errors.size()), Helper::errorsKept); + QVERIFY(errors.startsWith("first-line\n")); + } + + void helperStopsReadingForWhatsNoLongerShown() + { + // A stand-in bsdtar that never finishes on stuck.tar, says the + // names are encrypted on locked.7z, and lists one name otherwise. + QTemporaryDir dir; + const QString pid = dir.filePath("pid"); + const QString kid = dir.filePath("kid"); + write(dir.filePath("bsdtar"), "#!/bin/sh\nPATH=/usr/bin:/bin\ncase \"$2\" in\n" + "*/stuck.tar) echo $$ > '" + pid.toUtf8() + "'; exec sleep 30 ;;\n" + "*/family.tar) sleep 30 & echo $! > '" + kid.toUtf8() + "'; echo $$ > '" + pid.toUtf8() + "'; wait ;;\n" + "*/locked.7z) echo 'Encrypted file is unsupported' >&2; exit 1 ;;\n" + "esac\necho one.txt\n"); + QVERIFY(QFile::setPermissions(dir.filePath("bsdtar"), QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)); + const auto restore = pathAt(dir.path()); + auto stuckPid = [&] { + QFile file(pid); + return file.open(QIODevice::ReadOnly) ? pid_t(file.readAll().trimmed().toInt()) : pid_t(0); + }; + + // Moving on stops it at once, and what it said is never shown. + Backend backend(dir.path()); + backend.describe({{"path", dir.filePath("stuck.tar")}, {"kind", "archive"}, {"mime", "application/x-tar"}}); + QTRY_VERIFY(stuckPid() > 0); + const pid_t stuck = stuckPid(); + backend.describe({{"path", dir.filePath("other.tar")}, {"kind", "archive"}, {"mime", "application/x-tar"}}); + QTRY_VERIFY_WITH_TIMEOUT(::kill(stuck, 0) != 0, 2000); + QTRY_COMPARE(backend.details().value("text").toString(), QString("1 items\n\none.txt")); + backend.describe({{"path", dir.filePath("locked.7z")}, {"kind", "archive"}, {"mime", "application/x-7z-compressed"}}); + QTRY_COMPARE(backend.details().value("sentence").toString(), QString("The names in this archive are encrypted.")); + + // What the program started (as bsdtar starts a program to unpack with) is stopped with it. + QFile::remove(pid); + backend.describe({{"path", dir.filePath("family.tar")}, {"kind", "archive"}, {"mime", "application/x-tar"}}); + QTRY_VERIFY(stuckPid() > 0); + QFile started(kid); + QVERIFY(started.open(QIODevice::ReadOnly)); + const pid_t child = pid_t(started.readAll().trimmed().toInt()); + QVERIFY(child > 0 && !ended(child)); + backend.describe({{"path", dir.filePath("other.tar")}, {"kind", "archive"}, {"mime", "application/x-tar"}}); + QTRY_VERIFY_WITH_TIMEOUT(ended(child), 2000); + + // A plugin's own item is no file on the disk: no program is asked about it. + QFile::remove(pid); + backend.describe({{"path", "notes:/stuck.tar"}, {"kind", "archive"}, {"mime", "application/x-tar"}}); + QTest::qWait(200); + QCOMPARE(stuckPid(), pid_t(0)); + QVERIFY(!backend.details().contains("sentence")); + + // An archive that gives no name in its time took too long: it isn't called damaged. + { + Helper slow(nullptr, 300); + QString sentence; + Previewer::listArchive(slow, dir.filePath("stuck.tar"), [&](const QString &, const QString &said) { sentence = said; }); + QTRY_COMPARE(sentence, QString("Listing this archive took too long.")); + } + + // Past its time it's stopped too, and so is one whose view goes. + QFile::remove(pid); + Helper helper(nullptr, 300); + int ended = 0; + helper.run("bsdtar", {"-tf", dir.filePath("stuck.tar")}, 1024, [](const QByteArray &) {}, + [&](int code, const QByteArray &) { ended = code; }); + QTRY_VERIFY(stuckPid() > 0); + QTRY_COMPARE(ended, Helper::stopped); + QTRY_VERIFY_WITH_TIMEOUT(::kill(stuckPid(), 0) != 0, 2000); + QFile::remove(pid); + { + Helper going; + going.run("bsdtar", {"-tf", dir.filePath("stuck.tar")}, 1024, [](const QByteArray &) {}, + [](int, const QByteArray &) {}); + QTRY_VERIFY(stuckPid() > 0); + } + QTRY_VERIFY_WITH_TIMEOUT(::kill(stuckPid(), 0) != 0, 2000); + } + void themeFallsBack() { QTemporaryDir dir;