feat(flatpak): report the download flatpak never announces
Installing a .flatpakref looked frozen: flatpak prints two lines and then stays silent for the minutes a runtime takes to come down, so the window repeated the same sentence over a stalled bar and the journal recorded nothing at all. It has no meter to print — measured here, on a real 658 MB install, it draws nothing on a pipe nor on a full terminal, only on the deltas the CDN happens to serve — so the provider now measures the transfer itself: the bytes landing in flatpak's own staging directory, reported as they grow. When even those stop, the window takes over and says which step is running and for how long, in the status bar and in the journal. Along the way the runner reads with read() instead of stdio so in-place updates are not held back by a buffer, strips the ANSI escapes some tools colour their output with (the AUR plugin's own copy is gone), and turns a rewritten meter line into a journal entry every 10 %.
This commit is contained in:
11 files changed
+529
-63
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <QElapsedTimer>
|
||||
#include <QHash>
|
||||
#include <QMainWindow>
|
||||
#include <QString>
|
||||
@@ -67,6 +68,9 @@ private slots:
|
||||
void onUpdateAll();
|
||||
void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
|
||||
void onCheckForUpdates();
|
||||
// Ticks once a second while an operation runs: what it does when the
|
||||
// provider has printed nothing for a while is described in onActivityTick().
|
||||
void onActivityTick();
|
||||
|
||||
// ---- What a page asks the frame to do ----
|
||||
// A page hands over a request so the frame can walk the user through what
|
||||
@@ -134,6 +138,9 @@ private:
|
||||
// dialog, anything else is shown as-is.
|
||||
void showActionError(const QString& title, const QString& error, bool locked);
|
||||
void applyCredentialMode(AppCore::CredentialMode mode);
|
||||
// Records that the running operation just said something: the step is kept
|
||||
// for the heartbeat and the "nothing printed" clock restarts.
|
||||
void noteOperationOutput(const QString& step);
|
||||
|
||||
// The files a drag carries that the core can install; empty when the drop is
|
||||
// nothing the app handles (which is also how dragEnterEvent refuses it).
|
||||
@@ -180,6 +187,16 @@ private:
|
||||
int m_progressValue = 0;
|
||||
int m_progressGeneration = 0;
|
||||
|
||||
// What the window knows about the operation it is waiting on. A download
|
||||
// that takes minutes can print nothing at all for minutes, and a window
|
||||
// that only repeats the same sentence looks broken: the timer re-states the
|
||||
// current step with the elapsed time, and the journal notes the wait.
|
||||
QTimer* m_activityTimer = nullptr;
|
||||
QElapsedTimer m_operationClock;
|
||||
QString m_operationStep;
|
||||
qint64 m_lastOutputAtMs = 0;
|
||||
qint64 m_lastWaitNoteMs = 0;
|
||||
|
||||
// Files waiting to be installed (dropped or opened), and whether one is
|
||||
// being installed right now.
|
||||
QStringList m_pendingFileInstalls;
|
||||
|
||||
+100
-21
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
@@ -10,6 +11,12 @@
|
||||
|
||||
#include "package-manager.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// Reading a package manager's output: streaming it as it is produced, and
|
||||
// turning it into a progress bar.
|
||||
//
|
||||
@@ -39,6 +46,42 @@ struct OutputChunk {
|
||||
|
||||
using OutputHandler = std::function<void(const OutputChunk& chunk)>;
|
||||
|
||||
// Drops the ANSI escape sequences a tool emits once it thinks it is on a
|
||||
// terminal (colours, cursor moves, erase-line). They are noise in the status
|
||||
// bar and garbage in the journal, which replays the output as a terminal would
|
||||
// show it.
|
||||
inline std::string stripAnsi(const std::string& text) {
|
||||
std::string out;
|
||||
out.reserve(text.size());
|
||||
|
||||
for (std::size_t i = 0; i < text.size(); ++i) {
|
||||
if (text[i] != '\033') {
|
||||
out += text[i];
|
||||
continue;
|
||||
}
|
||||
// CSI (ESC [ … final byte): colours, cursor moves, erase.
|
||||
if (i + 1 < text.size() && text[i + 1] == '[') {
|
||||
std::size_t j = i + 2;
|
||||
while (j < text.size() && static_cast<unsigned char>(text[j]) >= 0x20
|
||||
&& static_cast<unsigned char>(text[j]) <= 0x3f) {
|
||||
++j;
|
||||
}
|
||||
i = j; // the final byte, or the last one when the run was cut
|
||||
continue;
|
||||
}
|
||||
// OSC (ESC ] … BEL or ESC \): window titles, hyperlinks.
|
||||
if (i + 1 < text.size() && text[i + 1] == ']') {
|
||||
std::size_t j = i + 2;
|
||||
while (j < text.size() && text[j] != '\a' && text[j] != '\033') ++j;
|
||||
i = (j < text.size() && text[j] == '\033') ? j + 1 : j;
|
||||
continue;
|
||||
}
|
||||
// Anything else is a two-byte sequence (ESC ( B, ESC =, …).
|
||||
if (i + 1 < text.size()) ++i;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Closes a popen() stream. A named deleter avoids the GCC warning about the
|
||||
// function-pointer attributes carried by decltype(&pclose) being dropped.
|
||||
struct ProcessCloser {
|
||||
@@ -47,42 +90,66 @@ struct ProcessCloser {
|
||||
}
|
||||
};
|
||||
|
||||
// Reads whatever the command has already written, without waiting for a buffer
|
||||
// to fill: that is what hands a '\r' update over while the tool is still
|
||||
// running. One name for both platforms, the Windows CRT calling read() _read().
|
||||
inline long readOutput(int fd, char* buffer, unsigned int size) {
|
||||
#ifdef _WIN32
|
||||
return static_cast<long>(::_read(fd, buffer, size));
|
||||
#else
|
||||
return static_cast<long>(::read(fd, buffer, size));
|
||||
#endif
|
||||
}
|
||||
|
||||
// Runs `command` through a shell, reports its output as it is produced and
|
||||
// returns everything it printed (byte-for-byte, so callers keep inspecting the
|
||||
// full output for `error:` lines and the like).
|
||||
//
|
||||
// Chunks are cut on '\n' (a real line) and on '\r' (an in-place update). Reads
|
||||
// go through stdio's buffer one character at a time, which is what makes the
|
||||
// '\r' updates arrive while the command is still running: a line-oriented read
|
||||
// would sit on them until the next newline, and a download meter never emits
|
||||
// one.
|
||||
// Chunks are cut on '\n' (a real line) and on '\r' (an in-place update), and
|
||||
// the escape sequences some tools colour their output with are removed. Reads
|
||||
// go through read() rather than stdio, so a '\r' update is handed over as soon
|
||||
// as the tool writes it: a line-oriented read would sit on it until the next
|
||||
// newline, and a download meter never emits one.
|
||||
inline std::string runStreamed(const std::string& command, const OutputHandler& onChunk) {
|
||||
std::string output;
|
||||
std::string pending;
|
||||
|
||||
std::unique_ptr<FILE, ProcessCloser> pipe(popen(command.c_str(), "r"));
|
||||
if (!pipe) return output;
|
||||
|
||||
// Empty lines are noise for a progress bar, so only non-empty chunks are
|
||||
// handed over.
|
||||
auto deliver = [&](bool partial) {
|
||||
if (!pending.empty() && onChunk) onChunk(OutputChunk{pending, partial});
|
||||
if (!pending.empty() && onChunk) onChunk(OutputChunk{stripAnsi(pending), partial});
|
||||
pending.clear();
|
||||
};
|
||||
|
||||
int read = 0;
|
||||
while ((read = std::fgetc(pipe.get())) != EOF) {
|
||||
const char c = static_cast<char>(read);
|
||||
if (c == '\n' || c == '\r') {
|
||||
output += pending;
|
||||
output += c;
|
||||
deliver(c == '\r');
|
||||
} else {
|
||||
pending += c;
|
||||
auto feed = [&](const char* data, std::size_t length) {
|
||||
for (std::size_t i = 0; i < length; ++i) {
|
||||
const char c = data[i];
|
||||
if (c == '\n' || c == '\r') {
|
||||
output += pending;
|
||||
output += c;
|
||||
deliver(c == '\r');
|
||||
} else {
|
||||
pending += c;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<FILE, ProcessCloser> pipe(popen(command.c_str(), "r"));
|
||||
if (!pipe) return output;
|
||||
|
||||
char buffer[512];
|
||||
for (;;) {
|
||||
const auto count = readOutput(fileno(pipe.get()), buffer, sizeof(buffer));
|
||||
if (count > 0) {
|
||||
feed(buffer, static_cast<std::size_t>(count));
|
||||
continue;
|
||||
}
|
||||
if (count < 0 && errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
deliver(/*partial=*/false); // Last line when the command ends without one.
|
||||
|
||||
// pclose() is what waits for the shell.
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -290,18 +357,30 @@ private:
|
||||
// in-place updates that have no text of their own — so the whole thing lives
|
||||
// here rather than being copied into each provider. The full output is still
|
||||
// returned for the `error:` checks the providers run afterwards.
|
||||
//
|
||||
// An in-place meter is the one update that carries a number but no sentence, so
|
||||
// every 10 % step of it is also reported as a line ("⏳ 40 %"): it is what the
|
||||
// status bar shows and what the journal records while a tool rewrites its
|
||||
// progress bar without ever printing a newline.
|
||||
inline std::string runWithProgress(const std::string& command, ProgressCallback callback) {
|
||||
ProgressTracker tracker;
|
||||
constexpr int kMilestoneStep = 10;
|
||||
int milestone = 0;
|
||||
|
||||
return runStreamed(command, [&](const OutputChunk& chunk) {
|
||||
if (!callback) return;
|
||||
|
||||
const int percent = tracker.feed(chunk.text);
|
||||
// A partial chunk is an in-place progress meter: it carries a real
|
||||
// percentage but nothing worth printing in the status bar.
|
||||
if (chunk.partial) {
|
||||
if (percent >= 0) callback(std::string(), percent);
|
||||
if (!chunk.partial) {
|
||||
callback(chunk.text, percent);
|
||||
return;
|
||||
}
|
||||
callback(chunk.text, percent);
|
||||
if (percent >= 0) callback(std::string(), percent);
|
||||
if (percent < 0 || percent - milestone < kMilestoneStep) return;
|
||||
|
||||
milestone = percent;
|
||||
callback("⏳ " + std::to_string(percent) + " %", percent);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user