From 9d32fae6d22546464cdce20e2d47fd2921749a71 Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Wed, 11 Nov 2026 14:38:48 +0530 Subject: [PATCH 1/7] List drives that aren't mounted yet, and mount and eject them Devices listed only what was already mounted, so another system's partitions and a stick that nothing had mounted never showed. It now lists GIO's volumes, as Nautilus does: udisks leaves out the system's own and hidden partitions (EFI, recovery), and the list follows plugging, mounting and unmounting at once instead of on the 15-second poll. - A click on one that isn't mounted mounts it and opens it. udisks asks the session's polkit agent where it needs permission, and an encrypted drive's passphrase is asked for in the window, kept for no session. - A drive plugged in that udisks marks to mount by itself is mounted, unless GNOME's automount setting is off. A desktop automounter (udiskie, a shell) gets two seconds' head start, so nothing is mounted, or asked for its passphrase, twice. - A mounted drive has an eject button: removable ones are ejected, others unmounted. Panes in a drive that goes away, from here or elsewhere, go home first. - An encrypted drive that unlocks with nothing mountable inside says so, instead of nothing happening. --- README.md | 2 +- src/Glyph.qml | 1 + src/HelpOverlay.qml | 1 + src/MountPrompt.qml | 14 +-- src/Sidebar.qml | 48 ++++++++- src/backend.cpp | 44 +++++++- src/backend.h | 13 +++ src/drives.cpp | 234 ++++++++++++++++++++++++++++++++++++++++++ src/drives.h | 65 ++++++++++++ src/main.cpp | 21 ++++ src/mounter.cpp | 157 ++++++++++++++++++++++++---- src/mounter.h | 29 +++++- src/places.cpp | 35 ++++++- src/places.h | 13 +++ src/windows.cpp | 14 +++ src/windows.h | 5 + tests/folio_tests.cpp | 27 +++++ 17 files changed, 686 insertions(+), 37 deletions(-) create mode 100644 src/drives.cpp create mode 100644 src/drives.h diff --git a/README.md b/README.md index 862f9d05..5518032c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Files (`folio`) is Omarchy's file manager. It's one quiet window that follows yo - **Three views**: a grid with real previews, a list, and Finder-style columns. Ctrl+1/2/3 switches. - **Space previews** pictures, video, audio, PDFs, text and Markdown at full size, 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. +- **The sidebar** holds everything in your home folder, pinned folders (shared with GTK file dialogs), Recents, Trash with restore, your drives (mounted or not, as Nautilus lists them: a click mounts one, a drive plugged in mounts by itself, and one can be ejected from there), 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/Glyph.qml b/src/Glyph.qml index 641aecf5..6eb01e0a 100644 --- a/src/Glyph.qml +++ b/src/Glyph.qml @@ -61,6 +61,7 @@ Item { case "recent": return circle(12, 12, 9) + "M12 7v5l3 2" case "trash": return "M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13" case "drive": return box(3, 6, 18, 12, r) + "M7 14h2" + case "eject": return "M12 5l7 8H5zM5 18h14" case "peer": return box(4, 4, 16, 11, r) + "M9 20h6M12 15v5" // Machines joined to one another: the whole tailnet. case "tailnet": return circle(12, 5, 2.5) + circle(5, 18, 2.5) + circle(19, 18, 2.5) + "M10.8 7.2L6.2 15.8M13.2 7.2l4.6 8.6M7.5 18h9" diff --git a/src/HelpOverlay.qml b/src/HelpOverlay.qml index 07fc7b88..ef407327 100644 --- a/src/HelpOverlay.qml +++ b/src/HelpOverlay.qml @@ -19,6 +19,7 @@ Rectangle { ["Drag on empty space", qsTr("Select with a rectangle (Alt adds)")], ["Ctrl+A", qsTr("Select everything")], ["Tab", qsTr("Move between the sidebar and the panes")], + ["E", qsTr("In the sidebar: eject the drive")], ["⏎", qsTr("Open")], ["Backspace", qsTr("Go to the parent folder")], ["Alt+← / Alt+→", qsTr("Back / forward (or the mouse's thumb buttons)")], diff --git a/src/MountPrompt.qml b/src/MountPrompt.qml index 16a571d2..aea86666 100644 --- a/src/MountPrompt.qml +++ b/src/MountPrompt.qml @@ -2,14 +2,14 @@ import QtQuick import QtQuick.Layouts // GVfs's password request while connecting to another machine whose keys -// aren't set up, put to the user in the window. The wording comes from GVfs, -// already translated. +// aren't set up, or an encrypted drive's passphrase, put to the user in the +// window. The wording comes from GVfs, already translated. FolioDialog { id: prompt readonly property var mounter: backend.mounter - title: qsTr("Sign in") + title: mounter.drive ? qsTr("Unlock") : qsTr("Sign in") cardWidth: 600 spacing: 18 visible: mounter.asking @@ -20,7 +20,7 @@ FolioDialog { onVisibleChanged: { password.text = "" if (visible) { - user.text = mounter.user + user.text = mounter.drive ? "" : mounter.user password.forceActiveFocus() } } @@ -44,12 +44,14 @@ FolioDialog { PlainField { id: user Layout.fillWidth: true + // A drive's passphrase has no user to go with it. + visible: !prompt.mounter.drive placeholderText: qsTr("User") } PlainField { id: password Layout.fillWidth: true - placeholderText: qsTr("Password") + placeholderText: prompt.mounter.drive ? qsTr("Passphrase") : qsTr("Password") echoMode: TextInput.Password onAccepted: prompt.submit() } @@ -63,7 +65,7 @@ FolioDialog { onClicked: prompt.mounter.cancel() } PlainButton { - text: qsTr("Connect") + text: prompt.mounter.drive ? qsTr("Unlock") : qsTr("Connect") primary: true onClicked: prompt.submit() } diff --git a/src/Sidebar.qml b/src/Sidebar.qml index ec352325..e2182f3d 100644 --- a/src/Sidebar.qml +++ b/src/Sidebar.qml @@ -13,7 +13,8 @@ Rectangle { clip: true // Keyboard: Tab moves here from the files and back. ↑ ↓ move between - // rows, ⏎ (or →) opens one and hands the keyboard back to the files. + // rows, ⏎ (or →) opens one and hands the keyboard back to the files, and + // E ejects a drive. signal finished() // The ? in the corner: show or hide the shortcuts. signal helpRequested() @@ -51,10 +52,17 @@ Rectangle { if (row.host !== undefined) sidebar.target.openPeer(row.host) else - sidebar.target.navigate(row.path) + openDevice(row) finished() } function onMachine(host) { return sidebar.target.path.indexOf("/sftp:host=" + host) >= 0 } + // A drive that isn't mounted yet has no path: opening it mounts it. + function openDevice(row) { + if (row.path) + sidebar.target.navigate(row.path) + else if (row.volume !== undefined) + sidebar.target.openVolume(row.volume, row.name) + } function reveal(item) { const y = item.mapToItem(column, 0, 0).y if (y < flick.contentY) @@ -69,6 +77,15 @@ Rectangle { Keys.onEnterPressed: activate(cursor) Keys.onRightPressed: activate(cursor) Keys.onEscapePressed: finished() + // E: ejects the drive the keyboard is on. + Keys.onPressed: (event) => { + if (event.key === Qt.Key_E && !(event.modifiers & (Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier))) { + const row = rows[cursor] + if (cursor >= devicesAt && cursor < peersAt && row && row.ejectable) + places.eject(row.volume) + event.accepted = true + } + } // The deepest place the current folder sits under. readonly property string activePath: { @@ -112,6 +129,9 @@ Rectangle { readonly property bool dropHover: rowDrop.containsDrag property string status property color statusColor + // A drive that can be unmounted or ejected shows a button for it. + property bool ejectable: false + signal ejectClicked() Layout.fillWidth: true implicitHeight: 34 @@ -154,6 +174,24 @@ Rectangle { color: row.active ? theme.brightForeground : theme.foreground } Text { visible: row.status.length > 0; text: row.status; color: row.statusColor; font.pixelSize: 11 } + // Its own click, so the row's isn't taken too. + Glyph { + visible: row.ejectable + name: "eject" + size: 16 + color: ejectArea.containsMouse ? theme.brightForeground : theme.secondaryForeground + Accessible.role: Accessible.Button + Accessible.name: qsTr("Eject") + Accessible.onPressAction: row.ejectClicked() + MouseArea { + id: ejectArea + anchors.fill: parent + anchors.margins: -6 + hoverEnabled: true + cursorShape: Qt.PointingHandCursor + onClicked: row.ejectClicked() + } + } } } @@ -305,8 +343,10 @@ Rectangle { onKeyedChanged: if (keyed) sidebar.reveal(this) text: modelData.name glyph: modelData.icon - active: sidebar.activePath === modelData.path - onClicked: sidebar.target.navigate(modelData.path) + active: !!modelData.path && sidebar.activePath === modelData.path + ejectable: !!modelData.ejectable + onEjectClicked: places.eject(modelData.volume) + onClicked: sidebar.openDevice(modelData) } } diff --git a/src/backend.cpp b/src/backend.cpp index 758c1129..7b7e8265 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -22,6 +22,14 @@ Backend::Backend(const QString &home, QObject *parent) }); connect(&m_mounter, &Mounter::mounted, this, [this](const QString &root) { setBusy({}); + if (!m_mountDrive.isEmpty()) { + if (m_mountOpens) + navigate(root); + else + showNotice(tr("%1 is mounted").arg(m_mountDrive)); + m_mountDrive.clear(); + return; + } // Land in the matching home folder when the other machine has one, // which on a tailnet of one person's machines it usually does. const QString remoteHome = root + QDir::homePath(); @@ -31,7 +39,9 @@ Backend::Backend(const QString &home, QObject *parent) setBusy({}); // An empty error is the user cancelling the sign-in: nothing to report. if (!error.isEmpty()) - setMessage(tr("Couldn't open %1 over SFTP: %2").arg(m_mountHost, error)); + setMessage(m_mountDrive.isEmpty() ? tr("Couldn't open %1 over SFTP: %2").arg(m_mountHost, error) + : tr("Couldn't open %1: %2").arg(m_mountDrive, error)); + m_mountDrive.clear(); }); // The preview reports each file it steps to, and the grid follows. connect(&m_preview, &QProcess::readyReadStandardOutput, this, [this] { @@ -213,10 +223,29 @@ void Backend::openPeer(const QString &host) { // are no keys or passwords to manage, and GVfs makes the mount look like // any other folder. m_mountHost = host; + m_mountDrive.clear(); setBusy(tr("Connecting to %1…").arg(host)); m_mounter.mount("sftp://" + host + "/"); } +void Backend::openVolume(const QString &volume, const QString &name) { + if (m_mounter.busy()) + return; + m_mountDrive = name.isEmpty() ? tr("the drive") : name; + m_mountOpens = true; + setBusy(tr("Opening %1…").arg(m_mountDrive)); + m_mounter.mountVolume(volume); +} + +bool Backend::mountVolume(const QString &volume, const QString &name) { + if (m_mounter.busy()) + return false; + m_mountDrive = name.isEmpty() ? tr("the drive") : name; + m_mountOpens = false; + m_mounter.mountVolume(volume); + return true; +} + void Backend::showNotice(const QString ¬ice) { m_notice = notice; emit noticeChanged(); @@ -396,6 +425,19 @@ void Backend::followSelection(const QString &path) { m_preview.write(path.toUtf8() + '\n'); } +void Backend::closePreviewUnder(const QString &root) { + if (!previewing() || !(m_previewed == root || m_previewed.startsWith(root + "/"))) + return; + m_closingPreview = true; + m_preview.terminate(); + // The unmount goes out right after this; a player still holding the + // file would make it fail as busy. + if (!m_preview.waitForFinished(1000)) { + m_preview.kill(); + m_preview.waitForFinished(1000); + } +} + void Backend::showFolder(const QString &path) { navigate(path); emit activateRequested(); diff --git a/src/backend.h b/src/backend.h index 0a469e38..4c188075 100644 --- a/src/backend.h +++ b/src/backend.h @@ -51,6 +51,13 @@ class Backend : public QObject { Q_INVOKABLE void back(); Q_INVOKABLE void forward(); Q_INVOKABLE void openPeer(const QString &host); + // Opens a drive from the sidebar's Devices, mounting it first when it + // isn't, which may ask for its passphrase or (through polkit) for + // permission. + Q_INVOKABLE void openVolume(const QString &volume, const QString &name); + // Mounts a drive just plugged in, staying where it is. False when this + // window is mounting something else already. + bool mountVolume(const QString &volume, const QString &name); Q_INVOKABLE void dismissMessage(); Q_INVOKABLE void showMessage(const QString &message) { setMessage(message); } // Good news that needs no answer ("Link copied"): shown for a few @@ -76,6 +83,9 @@ class Backend : public QObject { Q_INVOKABLE void newWindow(); // Keeps an open preview on the item selected in the grid. Q_INVOKABLE void followSelection(const QString &path); + // A drive at `root` is going away: closes the preview if it shows a file + // on it, and waits for it to let go, so it doesn't keep the drive busy. + void closePreviewUnder(const QString &root); // Called for org.freedesktop.FileManager1: open a folder, or open the // folder holding a file and select it. @@ -119,6 +129,9 @@ class Backend : public QObject { QString m_pendingSelection; Mounter m_mounter; QString m_mountHost; + // What's being mounted, when it's a drive, and whether to go there. + QString m_mountDrive; + bool m_mountOpens = false; QProcess m_preview; // Set while Folio closes the preview itself, which isn't a crash. bool m_closingPreview = false; diff --git a/src/drives.cpp b/src/drives.cpp new file mode 100644 index 00000000..a4d9d8b5 --- /dev/null +++ b/src/drives.cpp @@ -0,0 +1,234 @@ +#include "drives.h" + +#include +#include + +// GIO's headers use `signals` as a struct member, which Qt defines as a +// keyword. +#undef signals +#include +#define signals Q_SIGNALS + +namespace { + +QString take(char *text) { + const QString s = QString::fromUtf8(text ? text : ""); + g_free(text); + return s; +} + +QString rootOf(GMount *mount) { + GFile *root = g_mount_get_root(mount); + const QString path = take(g_file_get_path(root)); + g_object_unref(root); + return path; +} + +// What the callbacks carry: the Drives (gone if Folio quit first) and the +// drive's name for what they say. +struct Pending { + QPointer drives; + QString name; +}; + +// The user dismissing the polkit prompt, or a question nobody answered. +bool cancelled(const GError *error) { + return g_error_matches(error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED) + || g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED); +} + +} // namespace + +struct DrivesCallbacks { + static void changed(GVolumeMonitor *, gpointer, gpointer data) { + emit static_cast(data)->changed(); + } + + static void added(GVolumeMonitor *, GVolume *volume, gpointer data) { + auto *self = static_cast(data); + emit self->changed(); + // Only what udisks marks to mount by itself (removable media, not + // another internal partition). + if (!g_volume_should_automount(volume) || !g_volume_can_mount(volume) || !Drives::automountWanted()) + return; + // An automounter of the desktop's own (udiskie, a shell) gets the + // first go, so a drive isn't mounted twice, nor an encrypted one's + // passphrase asked for twice. + const QString id = Drives::idOf(volume); + const QString name = take(g_volume_get_name(volume)); + QTimer::singleShot(Drives::automountDelayMs, self, [self, id, name] { + GVolume *now = Drives::find(id); + if (!now) + return; + GMount *mount = g_volume_get_mount(now); + const bool unmounted = !mount && g_volume_can_mount(now); + if (mount) + g_object_unref(mount); + g_object_unref(now); + if (unmounted) + emit self->plugged(id, name); + }); + } + + // Unmounted from anywhere (here, another app, the drive pulled out): + // nothing should keep showing a folder that's gone. + static void mountRemoved(GVolumeMonitor *, GMount *mount, gpointer data) { + auto *self = static_cast(data); + const QString root = rootOf(mount); + if (!root.isEmpty()) + emit self->leaving(root); + emit self->changed(); + } + + static void mounted(GObject *source, GAsyncResult *result, gpointer data) { + auto *pending = static_cast(data); + GError *error = nullptr; + const bool ok = g_volume_mount_finish(G_VOLUME(source), result, &error); + if (Drives *self = pending->drives.data()) { + if (ok) + emit self->notice(QObject::tr("%1 is mounted").arg(pending->name)); + else if (!cancelled(error) && !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED)) + emit self->failed(QObject::tr("Couldn't mount %1: %2").arg(pending->name, QString::fromUtf8(error->message))); + } + if (error) + g_error_free(error); + delete pending; + } + + static void unmounted(GObject *source, GAsyncResult *result, gpointer data, bool ejected) { + auto *pending = static_cast(data); + GError *error = nullptr; + bool ok; + if (G_IS_MOUNT(source)) + ok = ejected ? g_mount_eject_with_operation_finish(G_MOUNT(source), result, &error) + : g_mount_unmount_with_operation_finish(G_MOUNT(source), result, &error); + else + ok = g_volume_eject_with_operation_finish(G_VOLUME(source), result, &error); + if (Drives *self = pending->drives.data()) { + if (ok && ejected) + emit self->notice(QObject::tr("%1 can be unplugged").arg(pending->name)); + else if (!ok && !cancelled(error)) + // Busy is the usual one: a file open in another app. + emit self->failed(QObject::tr("Couldn't eject %1: %2").arg(pending->name, QString::fromUtf8(error->message))); + } + if (error) + g_error_free(error); + delete pending; + } + static void unmountedOnly(GObject *s, GAsyncResult *r, gpointer d) { unmounted(s, r, d, false); } + static void ejectedToo(GObject *s, GAsyncResult *r, gpointer d) { unmounted(s, r, d, true); } +}; + +Drives::Drives(QObject *parent) : QObject(parent), m_monitor(g_volume_monitor_get()) { + // Qt runs on GLib's main loop on Linux, so these arrive on the UI + // thread, as soon as udisks sees the change. + for (const char *signal : {"volume-removed", "volume-changed", "mount-added", "mount-changed", + "drive-connected", "drive-disconnected", "drive-changed"}) + g_signal_connect(m_monitor, signal, G_CALLBACK(DrivesCallbacks::changed), this); + g_signal_connect(m_monitor, "volume-added", G_CALLBACK(DrivesCallbacks::added), this); + g_signal_connect(m_monitor, "mount-removed", G_CALLBACK(DrivesCallbacks::mountRemoved), this); +} + +Drives::~Drives() { + g_signal_handlers_disconnect_by_data(m_monitor, this); + g_object_unref(m_monitor); +} + +QString Drives::idOf(GVolume *volume) { + // The device first: two disks copied from one image share a filesystem + // UUID, but never a device while both are connected. + for (const char *kind : {G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE, G_VOLUME_IDENTIFIER_KIND_UUID}) { + const QString id = take(g_volume_get_identifier(volume, kind)); + if (!id.isEmpty()) + return id; + } + return take(g_volume_get_name(volume)); +} + +GVolume *Drives::find(const QString &id) { + GVolumeMonitor *monitor = g_volume_monitor_get(); + GList *volumes = g_volume_monitor_get_volumes(monitor); + GVolume *found = nullptr; + for (GList *l = volumes; l && !found; l = l->next) { + if (idOf(G_VOLUME(l->data)) == id) + found = G_VOLUME(g_object_ref(l->data)); + } + g_list_free_full(volumes, g_object_unref); + g_object_unref(monitor); + return found; +} + +QList Drives::volumes() const { + QList list; + GList *volumes = g_volume_monitor_get_volumes(m_monitor); + for (GList *l = volumes; l; l = l->next) { + GVolume *volume = G_VOLUME(l->data); + Volume v{take(g_volume_get_name(volume)), idOf(volume), {}, false}; + if (GMount *mount = g_volume_get_mount(volume)) { + v.root = rootOf(mount); + v.ejectable = g_mount_can_unmount(mount) || g_mount_can_eject(mount); + g_object_unref(mount); + } else { + // A stick that's plugged in but not mounted can still be ejected. + v.ejectable = g_volume_can_eject(volume); + if (!g_volume_can_mount(volume)) + continue; + } + list << v; + } + g_list_free_full(volumes, g_object_unref); + return list; +} + +void Drives::eject(const QString &id) { + GVolume *volume = find(id); + if (!volume) + return; + const QString name = take(g_volume_get_name(volume)); + // An operation with nothing answering it: udisks still asks polkit + // (it's only left out without one), while a "files are open, unmount + // anyway?" question goes unanswered, so nothing is cut off mid-write. + GMountOperation *operation = g_mount_operation_new(); + if (GMount *mount = g_volume_get_mount(volume)) { + emit leaving(rootOf(mount)); + if (g_mount_can_eject(mount)) + g_mount_eject_with_operation(mount, G_MOUNT_UNMOUNT_NONE, operation, nullptr, + DrivesCallbacks::ejectedToo, new Pending{this, name}); + else + g_mount_unmount_with_operation(mount, G_MOUNT_UNMOUNT_NONE, operation, nullptr, + DrivesCallbacks::unmountedOnly, new Pending{this, name}); + g_object_unref(mount); + } else if (g_volume_can_eject(volume)) { + g_volume_eject_with_operation(volume, G_MOUNT_UNMOUNT_NONE, operation, nullptr, + DrivesCallbacks::ejectedToo, new Pending{this, name}); + } + g_object_unref(operation); + g_object_unref(volume); +} + +void Drives::mountQuietly(const QString &id) { + GVolume *volume = find(id); + if (!volume) + return; + GMountOperation *operation = g_mount_operation_new(); + g_volume_mount(volume, G_MOUNT_MOUNT_NONE, operation, nullptr, DrivesCallbacks::mounted, + new Pending{this, take(g_volume_get_name(volume))}); + g_object_unref(operation); + g_object_unref(volume); +} + +bool Drives::automountWanted() { + GSettingsSchemaSource *source = g_settings_schema_source_get_default(); + GSettingsSchema *schema = source ? g_settings_schema_source_lookup(source, "org.gnome.desktop.media-handling", TRUE) + : nullptr; + if (!schema) + return true; + bool wanted = true; + if (g_settings_schema_has_key(schema, "automount")) { + GSettings *settings = g_settings_new_full(schema, nullptr, nullptr); + wanted = g_settings_get_boolean(settings, "automount"); + g_object_unref(settings); + } + g_settings_schema_unref(schema); + return wanted; +} diff --git a/src/drives.h b/src/drives.h new file mode 100644 index 00000000..4bd3e966 --- /dev/null +++ b/src/drives.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include + +typedef struct _GVolume GVolume; +typedef struct _GVolumeMonitor GVolumeMonitor; + +// The drives GIO knows, through GVfs's udisks monitor: the list Nautilus +// shows, partitions not mounted yet included (another system's, a USB stick +// just plugged in), and the system's own and hidden ones (EFI, recovery) +// already left out by udisks. Unmounting, ejecting and mounting a drive as +// it's plugged in go through here too; mounting one to open it is the +// window's Mounter, which can put a passphrase request to the user. +class Drives : public QObject { + Q_OBJECT + +public: + struct Volume { + QString name; + // What finds the volume again: its device, else its UUID, else its name. + QString id; + // Where it's mounted; empty while it isn't. + QString root; + // It can be unmounted or ejected from here. + bool ejectable = false; + }; + + explicit Drives(QObject *parent = nullptr); + ~Drives() override; + + QList volumes() const; + + // Unmounts the volume, or ejects its drive when it's removable, after + // asking the windows to step out of it. udisks puts any authentication + // to the user through the session's polkit agent. + void eject(const QString &id); + // Mounts it with nobody to ask for a passphrase, for a drive plugged in + // when no window can. + void mountQuietly(const QString &id); + + // The volume `id` names, with a reference the caller drops; null when + // it's gone. + static GVolume *find(const QString &id); + static QString idOf(GVolume *volume); + // GNOME's "mount drives as they're plugged in" setting, on where it + // isn't installed. + static bool automountWanted(); + // How long a plugged drive is left for another automounter first. + static constexpr int automountDelayMs = 2000; + +signals: + void changed(); + // A drive was plugged in that udisks says should mount by itself. + void plugged(const QString &id, const QString &name); + // `root` is about to go away, or just went: windows in it move out. + void leaving(const QString &root); + void notice(const QString ¬ice); + void failed(const QString &message); + +private: + friend struct DrivesCallbacks; + GVolumeMonitor *m_monitor = nullptr; +}; diff --git a/src/main.cpp b/src/main.cpp index 31843fa8..17b07ec0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include #include +#include "backend.h" #include "filemanager1.h" #include "foldermodel.h" #include "places.h" @@ -147,5 +148,25 @@ int main(int argc, char *argv[]) { return 1; FileManager1::registerOn(&windows); + if (Drives *drives = places.drives()) { + QObject::connect(drives, &Drives::leaving, &windows, &Windows::leave); + // A drive plugged in mounts through the last window used, which can + // ask for an encrypted drive's passphrase; with none free, it mounts + // without asking. + QObject::connect(drives, &Drives::plugged, &windows, [&windows, drives](const QString &id, const QString &name) { + Backend *backend = windows.latest(); + if (!backend || !backend->mountVolume(id, name)) + drives->mountQuietly(id); + }); + QObject::connect(drives, &Drives::notice, &windows, [&windows](const QString ¬ice) { + if (Backend *backend = windows.latest()) + backend->showNotice(notice); + }); + QObject::connect(drives, &Drives::failed, &windows, [&windows](const QString &message) { + if (Backend *backend = windows.latest()) + backend->showMessage(message); + }); + } + return app.exec(); } diff --git a/src/mounter.cpp b/src/mounter.cpp index ee31f9c3..19ede101 100644 --- a/src/mounter.cpp +++ b/src/mounter.cpp @@ -1,6 +1,10 @@ #include "mounter.h" +#include "drives.h" +#include +#include #include +#include // GIO's headers use `signals` as a struct member, which Qt defines as a // keyword. @@ -8,6 +12,24 @@ #include #define signals Q_SIGNALS +namespace { + +// The encrypted volume `id` names is unlocked: the kernel shows its +// cleartext (dm-crypt) device holding it. +bool unlockedNow(const QString &id) { + GVolume *volume = Drives::find(id); + if (!volume) + return false; + char *device = g_volume_get_identifier(volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE); + const QString name = QFileInfo(QFileInfo(QString::fromUtf8(device ? device : "")).canonicalFilePath()).fileName(); + g_free(device); + g_object_unref(volume); + return !name.isEmpty() + && !QDir(QStringLiteral("/sys/class/block/%1/holders").arg(name)).isEmpty(QDir::NoDotAndDotDot | QDir::AllEntries); +} + +} // namespace + struct MounterCallbacks { // GMountOperation's own handlers run after these and answer // "unhandled" at once, which GVfs reports as a cancelled login; stopping @@ -19,7 +41,11 @@ struct MounterCallbacks { // reached over WireGuard and already authenticated by Tailscale, so the // answer is always yes. A changed key for a known machine never gets // here: ssh refuses it outright. - static void question(GMountOperation *op, const char *, const char **, gpointer) { + static void question(GMountOperation *op, const char *, const char **, gpointer data) { + // A drive's questions are left to GIO, which declines them, rather + // than answered yes for the user. + if (static_cast(data)->m_drive) + return; g_signal_stop_emission_by_name(op, "ask-question"); g_mount_operation_set_choice(op, 0); g_mount_operation_reply(op, G_MOUNT_OPERATION_HANDLED); @@ -46,29 +72,74 @@ struct MounterCallbacks { GError *error = nullptr; const bool ok = g_file_mount_enclosing_volume_finish(G_FILE(source), result, &error); if (self) { - // Mounted earlier (by Folio or another app) is as good as now. - const bool already = error && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED); - if (ok || already) { - char *path = g_file_get_path(G_FILE(source)); - const QString local = QString::fromUtf8(path ? path : ""); + char *path = g_file_get_path(G_FILE(source)); + self->report(ok, error && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED), + QString::fromUtf8(path ? path : ""), error); + g_free(path); + } + if (error) + g_error_free(error); + } + + static void volumeMounted(GObject *source, GAsyncResult *result, gpointer data) { + auto *guard = static_cast *>(data); + Mounter *self = guard->data(); + delete guard; + GError *error = nullptr; + const bool ok = g_volume_mount_finish(G_VOLUME(source), result, &error); + if (self) { + QString local; + if (GMount *mount = g_volume_get_mount(G_VOLUME(source))) { + GFile *root = g_mount_get_root(mount); + char *path = g_file_get_path(root); + local = QString::fromUtf8(path ? path : ""); g_free(path); - self->finish(); - if (local.isEmpty()) - emit self->failed(QObject::tr("GVfs mounted it but gave no local folder for it")); - else - emit self->mounted(local); - } else { - const bool cancelled = error && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED); - const QString message = cancelled ? QString() : QString::fromUtf8(error ? error->message : ""); - self->finish(); - emit self->failed(message); + g_object_unref(root); + g_object_unref(mount); } + self->report(ok, error && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED), local, error); } if (error) g_error_free(error); } }; +void Mounter::report(bool ok, bool already, const QString &local, const GError *error) { + // Given up on after the passphrase went in, or GVfs calling it + // cancelled when nobody did: either the drive unlocked with nothing to + // mount inside, or it's still working. Which, the kernel says. + const bool stopped = m_drive && m_answered && !ok && !already + && (!error || g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)); + const bool unlocked = stopped && unlockedNow(m_volume); + const bool timedOut = stopped && m_lost; + finish(); + // Mounted earlier (by Folio or another app) is as good as now. + if (ok || already) { + if (local.isEmpty()) + emit failed(tr("GVfs mounted it but gave no local folder for it")); + else + emit mounted(local); + return; + } + // The user turning down the password, or polkit's prompt for a drive, + // is nothing to report. + if (error && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED)) { + emit failed(QString()); + return; + } + // An encrypted drive that unlocked and then didn't mount has nothing + // udisks can mount inside (no filesystem, or LVM). + if (unlocked) { + emit failed(tr("it unlocked, but there's nothing on it that can be mounted")); + return; + } + if (timedOut) { + emit failed(tr("it didn't finish mounting within a minute")); + return; + } + emit failed(QString::fromUtf8(error ? error->message : "")); +} + Mounter::Mounter(QObject *parent) : QObject(parent) {} Mounter::~Mounter() { @@ -79,14 +150,19 @@ Mounter::~Mounter() { finish(); } -void Mounter::mount(const QString &uri) { - if (m_operation) - return; - GFile *file = g_file_new_for_uri(uri.toUtf8().constData()); +void Mounter::begin(bool drive) { + m_drive = drive; m_operation = g_mount_operation_new(); g_signal_connect(m_operation, "ask-question", G_CALLBACK(MounterCallbacks::question), this); g_signal_connect(m_operation, "ask-password", G_CALLBACK(MounterCallbacks::password), this); g_signal_connect(m_operation, "aborted", G_CALLBACK(MounterCallbacks::aborted), this); +} + +void Mounter::mount(const QString &uri) { + if (m_operation) + return; + GFile *file = g_file_new_for_uri(uri.toUtf8().constData()); + begin(false); // Qt runs on GLib's main loop on Linux, so the reply arrives on the UI // thread like any other event. The window can close before GVfs answers, // and GIO calls back regardless, so the callback gets a guard, not `this`. @@ -96,6 +172,23 @@ void Mounter::mount(const QString &uri) { g_object_unref(file); } +void Mounter::mountVolume(const QString &id) { + if (m_operation) + return; + GVolume *volume = Drives::find(id); + if (!volume) { + emit failed(tr("It's no longer connected")); + return; + } + begin(true); + m_volume = id; + m_lost = false; + m_cancellable = g_cancellable_new(); + g_volume_mount(volume, G_MOUNT_MOUNT_NONE, m_operation, m_cancellable, MounterCallbacks::volumeMounted, + new QPointer(this)); + g_object_unref(volume); +} + void Mounter::providePassword(const QString &user, const QString &password) { if (!m_operation || !m_asking) return; @@ -103,8 +196,23 @@ void Mounter::providePassword(const QString &user, const QString &password) { if (!user.isEmpty()) g_mount_operation_set_username(m_operation, user.toUtf8().constData()); g_mount_operation_set_password(m_operation, password.toUtf8().constData()); - g_mount_operation_set_password_save(m_operation, G_PASSWORD_SAVE_FOR_SESSION); + // A drive's passphrase isn't kept at all: once unlocked it stays so. + g_mount_operation_set_password_save(m_operation, m_drive ? G_PASSWORD_SAVE_NEVER : G_PASSWORD_SAVE_FOR_SESSION); g_mount_operation_reply(m_operation, G_MOUNT_OPERATION_HANDLED); + // An encrypted drive that unlocks but holds nothing udisks can mount + // (no filesystem, or LVM inside) leaves GVfs's mount pending for good; + // past this, it's given up on. Asked only once the passphrase is in, so + // a polkit prompt before it isn't cut short. + if (m_drive && m_cancellable) { + m_answered = true; + const int attempt = m_attempt; + QTimer::singleShot(stalledAfterMs, this, [this, attempt] { + if (attempt != m_attempt || !m_cancellable || m_asking) + return; + m_lost = true; + g_cancellable_cancel(m_cancellable); + }); + } } void Mounter::cancel() { @@ -124,6 +232,13 @@ void Mounter::setAsking(bool asking) { void Mounter::finish() { if (m_asking) setAsking(false); + if (m_cancellable) { + g_object_unref(m_cancellable); + m_cancellable = nullptr; + } + m_lost = false; + m_answered = false; + ++m_attempt; if (m_operation) { g_object_unref(m_operation); m_operation = nullptr; diff --git a/src/mounter.h b/src/mounter.h index 12e2d790..b9f75683 100644 --- a/src/mounter.h +++ b/src/mounter.h @@ -4,18 +4,25 @@ #include typedef struct _GMountOperation GMountOperation; +typedef struct _GError GError; +typedef struct _GCancellable GCancellable; -// Mounts tailnet machines over SFTP through GIO, the way Nautilus does, so -// they land under GVfs where every app can see them. GVfs asks questions -// while mounting. Host-key questions are answered here for the user, since +// Mounts tailnet machines over SFTP, and drives from the sidebar, through +// GIO, the way Nautilus does, so they land where every app can see them. +// GVfs asks questions while mounting. Host-key questions are answered here for the user, since // Tailscale has already authenticated the machine; only a password request // (when keys aren't set up) goes to the window to put to the user, instead // of dying silently as it does under `gio mount` with no terminal to ask in. +// A drive's questions aren't answered for the user, and its passphrase +// request (an encrypted drive) goes to the window the same way; anything +// udisks needs authorized goes to the session's polkit agent. class Mounter : public QObject { Q_OBJECT Q_PROPERTY(bool asking READ asking NOTIFY askingChanged) Q_PROPERTY(QString prompt READ prompt NOTIFY askingChanged) Q_PROPERTY(QString user READ user NOTIFY askingChanged) + // The request is for a drive's passphrase, not a sign-in. + Q_PROPERTY(bool drive READ drive NOTIFY askingChanged) public: explicit Mounter(QObject *parent = nullptr); @@ -25,9 +32,14 @@ class Mounter : public QObject { bool asking() const { return m_asking; } QString prompt() const { return m_prompt; } QString user() const { return m_user; } + bool drive() const { return m_drive; } // Mounts the location; `mounted` carries its local GVfs path. void mount(const QString &uri); + // Mounts the volume Drives knows by `id`; `mounted` carries its folder. + void mountVolume(const QString &id); + // How long a drive's mount may run once its passphrase is in. + static constexpr int stalledAfterMs = 60000; // The user's answer to a password request; remembered for this session // only, never written to a keyring without asking. @@ -45,9 +57,20 @@ class Mounter : public QObject { friend struct MounterCallbacks; void setAsking(bool asking); void finish(); + void begin(bool drive); + void report(bool ok, bool already, const QString &local, const GError *error); GMountOperation *m_operation = nullptr; bool m_asking = false; + bool m_drive = false; + // A drive's mount, cancelled when it stalls after unlocking (see + // providePassword); each mount counts as a new attempt. + QString m_volume; + GCancellable *m_cancellable = nullptr; + bool m_lost = false; + // Its passphrase was given. + bool m_answered = false; + int m_attempt = 0; QString m_prompt; QString m_user; }; diff --git a/src/places.cpp b/src/places.cpp index cba64d11..bef060e6 100644 --- a/src/places.cpp +++ b/src/places.cpp @@ -87,6 +87,16 @@ Places::Places(const QString &home, QObject *parent) // Tailnet peers come and go, drives get plugged in, Dropbox syncs: a // gentle poll keeps the sidebar honest without any background service. m_poll.setInterval(15000); + // Drives also say at once when they come, go or mount. + if (m_realHome) { + m_drives = new Drives(this); + connect(m_drives, &Drives::changed, this, [this] { + const QVariantList before = m_devices; + refreshDevices(); + if (m_devices != before) + emit changed(); + }); + } connect(&m_poll, &QTimer::timeout, this, &Places::refresh); if (m_realHome) m_poll.start(); @@ -324,6 +334,28 @@ QVariantList Places::physicalDrives(const QList &mounts, const QString &s return drives; } +QVariantList Places::devicesFrom(const QList &volumes, const QVariantList &drives) { + QVariantList devices; + QStringList roots; + for (const Drives::Volume &v : volumes) { + QVariantMap device{{"name", v.name}, {"path", v.root}, {"icon", "drive"}, {"volume", v.id}, + {"ejectable", v.ejectable}}; + devices << device; + if (!v.root.isEmpty()) + roots << v.root; + } + for (const QVariant &drive : drives) { + if (!roots.contains(drive.toMap().value("path").toString())) + devices << drive; + } + return devices; +} + +void Places::eject(const QString &volume) { + if (m_drives) + m_drives->eject(volume); +} + void Places::refreshDevices() { QList mounts; auto volumes = QStorageInfo::mountedVolumes(); @@ -335,7 +367,8 @@ void Places::refreshDevices() { mounts << Mount{v.rootPath(), QString::fromUtf8(v.device()), QString::fromUtf8(v.fileSystemType()), v.name()}; } - m_devices = physicalDrives(mounts, QString::fromUtf8(QStorageInfo::root().device())); + const QVariantList mounted = physicalDrives(mounts, QString::fromUtf8(QStorageInfo::root().device())); + m_devices = m_drives ? devicesFrom(m_drives->volumes(), mounted) : mounted; } void Places::pollTailscale() { diff --git a/src/places.h b/src/places.h index d53d4dc1..357fdd94 100644 --- a/src/places.h +++ b/src/places.h @@ -5,6 +5,8 @@ #include #include +#include "drives.h" + // The sidebar: the user's places (with Dropbox first-class when it's set up), // the drives, and the tailnet machines opened in Folio. Each list is small, // so it is handed to QML as plain maps rather than a model. @@ -54,6 +56,12 @@ class Places : public QObject { // The peers among `opened`, in its order, at most `limit` of them. static QVariantList onlineOf(const QStringList &opened, const QVariantList &peers, int limit = 6); + // Unmounts a drive from the Devices list, or ejects it when removable. + Q_INVOKABLE void eject(const QString &volume); + + // GIO's drives; null for a test's home, which has none. + Drives *drives() const { return m_drives; } + static QVariantList parseTailscale(const QByteArray &json); static QString parseDropboxStatus(const QByteArray &output); @@ -66,6 +74,10 @@ class Places : public QObject { // The physical drives among the mounts, one entry per drive: removable // and extra internal disks, never the system's own partitions. static QVariantList physicalDrives(const QList &mounts, const QString &systemDevice); + // The Devices list: GIO's volumes in its order, each mounted one at its + // folder and each unmounted one by `volume` (no path until it's + // mounted), then any mounted drive GIO doesn't list (an fstab mount). + static QVariantList devicesFrom(const QList &volumes, const QVariantList &drives); signals: void changed(); @@ -97,4 +109,5 @@ class Places : public QObject { QString m_dropboxStatus; QProcess m_tailscale, m_dropbox; QTimer m_poll; + Drives *m_drives = nullptr; }; diff --git a/src/windows.cpp b/src/windows.cpp index 1aa741d8..1bf37104 100644 --- a/src/windows.cpp +++ b/src/windows.cpp @@ -1,5 +1,6 @@ #include "windows.h" +#include #include #include #include @@ -63,6 +64,19 @@ Backend *Windows::latest() const { return m_windows.isEmpty() ? nullptr : m_windows.first().main.backend; } +void Windows::leave(const QString &root) { + for (const Window &w : m_windows) { + for (Backend *backend : {w.main.backend, w.split.backend}) { + if (!backend) + continue; + backend->closePreviewUnder(root); + const QString path = backend->path(); + if (path == root || path.startsWith(root + "/")) + backend->navigate(QDir::homePath()); + } + } +} + void Windows::close(QObject *window) { for (int i = 0; i < m_windows.size(); ++i) { if (m_windows[i].main.item != window) diff --git a/src/windows.h b/src/windows.h index 470a04ce..efd6c657 100644 --- a/src/windows.h +++ b/src/windows.h @@ -27,6 +27,11 @@ class Windows : public QObject { // The window used most recently, for "Show in folder" from other apps. Backend *latest() const; + // A drive at `root` is going away: every pane in it goes home and every + // preview of a file on it closes, so none holds it busy or shows a + // folder that's gone. + void leave(const QString &root); + // Splits a window: a second folder pane, with its own folder, history // and selection, starting at `folder`. Returns the pane, for Main.qml to // put on the right. diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 8b6463b0..5e66aec9 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1582,6 +1582,33 @@ private slots: QCOMPARE(drives.at(1).toMap().value("path").toString(), QString("/run/media/me/Photos")); } + void placesListDrivesNotMountedYet() + { + // GIO's volumes come first, in its order: one mounted, one not. + const QList volumes{ + {"Windows", "A4AE2807AE27D09A", "", false}, + {"Photos", "1234-ABCD", "/run/media/me/Photos", true}, + }; + // What's mounted outside GIO (an fstab mount) follows; what it also + // lists isn't listed twice. + const QVariantList mounted{ + QVariantMap{{"name", "Photos"}, {"path", "/run/media/me/Photos"}, {"icon", "drive"}}, + QVariantMap{{"name", "archive"}, {"path", "/mnt/archive"}, {"icon", "drive"}}, + }; + const auto devices = Places::devicesFrom(volumes, mounted); + QCOMPARE(devices.size(), 3); + const QVariantMap windows = devices.at(0).toMap(); + QCOMPARE(windows.value("name").toString(), QString("Windows")); + QVERIFY(windows.value("path").toString().isEmpty()); + QCOMPARE(windows.value("volume").toString(), QString("A4AE2807AE27D09A")); + QCOMPARE(windows.value("ejectable").toBool(), false); + const QVariantMap photos = devices.at(1).toMap(); + QCOMPARE(photos.value("path").toString(), QString("/run/media/me/Photos")); + QCOMPARE(photos.value("ejectable").toBool(), true); + QCOMPARE(devices.at(2).toMap().value("path").toString(), QString("/mnt/archive")); + QVERIFY(!devices.at(2).toMap().contains("volume")); + } + void placesParseDropboxStatus() { QCOMPARE(Places::parseDropboxStatus("Up to date\n"), QString("synced")); From fbcac15c370bec20cd2d5743fce78182da3911a8 Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Wed, 11 Nov 2026 15:00:00 +0530 Subject: [PATCH 2/7] Give each passphrase reply its own minute A wrong passphrase and a retry left the first reply's timer running, so it could cancel the corrected attempt early. A new request now stops the last reply's timer, and each reply starts its own. --- src/mounter.cpp | 5 ++++- src/mounter.h | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mounter.cpp b/src/mounter.cpp index 19ede101..c2635263 100644 --- a/src/mounter.cpp +++ b/src/mounter.cpp @@ -55,6 +55,8 @@ struct MounterCallbacks { GAskPasswordFlags, gpointer data) { g_signal_stop_emission_by_name(op, "ask-password"); auto *self = static_cast(data); + // Asked again (a wrong passphrase): the last reply's clock stops. + ++self->m_attempt; self->m_prompt = QString::fromUtf8(message).trimmed(); self->m_user = QString::fromUtf8(user ? user : ""); self->setAsking(true); @@ -205,7 +207,8 @@ void Mounter::providePassword(const QString &user, const QString &password) { // a polkit prompt before it isn't cut short. if (m_drive && m_cancellable) { m_answered = true; - const int attempt = m_attempt; + // Each reply gets its own minute, from when it went in. + const int attempt = ++m_attempt; QTimer::singleShot(stalledAfterMs, this, [this, attempt] { if (attempt != m_attempt || !m_cancellable || m_asking) return; diff --git a/src/mounter.h b/src/mounter.h index b9f75683..0db4ce5a 100644 --- a/src/mounter.h +++ b/src/mounter.h @@ -64,7 +64,8 @@ class Mounter : public QObject { bool m_asking = false; bool m_drive = false; // A drive's mount, cancelled when it stalls after unlocking (see - // providePassword); each mount counts as a new attempt. + // providePassword); each mount, passphrase request and reply counts as + // a new attempt, so only the latest reply's timer can cancel it. QString m_volume; GCancellable *m_cancellable = nullptr; bool m_lost = false; From cf6773f99d4d7de99ae6e113bdfa63bdf0c8078c Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Wed, 11 Nov 2026 15:03:42 +0530 Subject: [PATCH 3/7] Say a drive has nothing to mount only when udev agrees An encrypted drive that unlocked but was slow to mount its filesystem still read as "nothing on it that can be mounted" once the minute ran out. Now that's said only when nothing inside the unlocked device is a filesystem, by udev's record of it; otherwise it's reported as timed out. --- src/mounter.cpp | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/mounter.cpp b/src/mounter.cpp index c2635263..30f075da 100644 --- a/src/mounter.cpp +++ b/src/mounter.cpp @@ -2,6 +2,7 @@ #include "drives.h" #include +#include #include #include #include @@ -14,9 +15,19 @@ namespace { -// The encrypted volume `id` names is unlocked: the kernel shows its -// cleartext (dm-crypt) device holding it. -bool unlockedNow(const QString &id) { +// A block device holds a filesystem, by what udev found on it. +bool holdsFilesystem(const QString &name) { + QFile dev(QStringLiteral("/sys/class/block/%1/dev").arg(name)); + if (!dev.open(QIODevice::ReadOnly)) + return false; + QFile data(QStringLiteral("/run/udev/data/b%1").arg(QString::fromLatin1(dev.readAll()).trimmed())); + return data.open(QIODevice::ReadOnly) && data.readAll().contains("\nE:ID_FS_USAGE=filesystem\n"); +} + +// The encrypted volume `id` names unlocked (the kernel shows a cleartext, +// dm-crypt, device holding it) and nothing inside is a filesystem: no +// filesystem at all, or LVM. A filesystem inside means it's only slow. +bool unlockedEmpty(const QString &id) { GVolume *volume = Drives::find(id); if (!volume) return false; @@ -24,8 +35,16 @@ bool unlockedNow(const QString &id) { const QString name = QFileInfo(QFileInfo(QString::fromUtf8(device ? device : "")).canonicalFilePath()).fileName(); g_free(device); g_object_unref(volume); - return !name.isEmpty() - && !QDir(QStringLiteral("/sys/class/block/%1/holders").arg(name)).isEmpty(QDir::NoDotAndDotDot | QDir::AllEntries); + if (name.isEmpty()) + return false; + const QStringList holders = QDir(QStringLiteral("/sys/class/block/%1/holders").arg(name)) + .entryList(QDir::NoDotAndDotDot | QDir::AllEntries); + if (holders.isEmpty()) + return false; + for (const QString &holder : holders) + if (holdsFilesystem(holder)) + return false; + return true; } } // namespace @@ -109,10 +128,11 @@ struct MounterCallbacks { void Mounter::report(bool ok, bool already, const QString &local, const GError *error) { // Given up on after the passphrase went in, or GVfs calling it // cancelled when nobody did: either the drive unlocked with nothing to - // mount inside, or it's still working. Which, the kernel says. + // mount inside, or it's still working (unlocking, or mounting a slow + // filesystem). Which, the kernel and udev say. const bool stopped = m_drive && m_answered && !ok && !already && (!error || g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)); - const bool unlocked = stopped && unlockedNow(m_volume); + const bool empty = stopped && unlockedEmpty(m_volume); const bool timedOut = stopped && m_lost; finish(); // Mounted earlier (by Folio or another app) is as good as now. @@ -129,9 +149,8 @@ void Mounter::report(bool ok, bool already, const QString &local, const GError * emit failed(QString()); return; } - // An encrypted drive that unlocked and then didn't mount has nothing - // udisks can mount inside (no filesystem, or LVM). - if (unlocked) { + // An encrypted drive that unlocked with nothing udisks can mount inside. + if (empty) { emit failed(tr("it unlocked, but there's nothing on it that can be mounted")); return; } From 33941dcaf124d3fc63471ba9e698807b3c4acaf7 Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Thu, 12 Nov 2026 12:22:44 +0530 Subject: [PATCH 4/7] Ignore pkgbuild output --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3416bd1f..bf31cca4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /build/ *.pkg.tar.* __pycache__/ +/pkgbuild/ From 42a79042a69e682d95f06bca10112976cd9a5a0f Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Tue, 17 Nov 2026 09:25:55 +0530 Subject: [PATCH 5/7] Name a mounted unlabeled drive as GIO does, not by its UUID folder --- src/drives.cpp | 5 +++++ src/places.cpp | 4 ++++ src/places.h | 2 +- tests/folio_tests.cpp | 16 ++++++++++++---- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/drives.cpp b/src/drives.cpp index fb893f58..ee24b686 100644 --- a/src/drives.cpp +++ b/src/drives.cpp @@ -143,6 +143,11 @@ QList Drives::volumes() const { Volume v{take(g_volume_get_name(volume)), idOf(volume), {}}; if (GMount *mount = g_volume_get_mount(volume)) { v.root = rootOf(mount); + // What the mount calls itself: its label, else "1.0 TB Volume", + // never the UUID folder udisks mounts an unlabeled one in. + const QString name = take(g_mount_get_name(mount)); + if (!name.isEmpty()) + v.name = name; g_object_unref(mount); } else if (!g_volume_can_mount(volume)) { continue; diff --git a/src/places.cpp b/src/places.cpp index fe84ed7b..289f262f 100644 --- a/src/places.cpp +++ b/src/places.cpp @@ -301,6 +301,10 @@ QVariantList Places::devicesFrom(const QList &volumes, const QVa if (drive.value("path").toString() != v.root) continue; drive.insert("volume", v.id); + // GIO's name, as the row had before it mounted; the mount folder + // an unlabeled drive would be named after is its UUID. + if (!v.name.isEmpty()) + drive.insert("name", v.name); devices << drive; listed << v.root; break; diff --git a/src/places.h b/src/places.h index b71300a9..b300507a 100644 --- a/src/places.h +++ b/src/places.h @@ -63,7 +63,7 @@ class Places : public QObject { // it can't be) and how much is free. static QVariantList physicalDrives(const QList &mounts, const QString &systemDevice); // The Devices list: GIO's volumes in its order, each mounted one as its - // drive among `drives` and each unmounted one by `volume` (no path, and + // drive among `drives` under GIO's name, and each unmounted one by `volume` (no path, and // nothing to let go of, until it's mounted), then the drives GIO // doesn't list (an fstab mount). A mounted volume `drives` leaves out // (the system's own) stays out. diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 02931d3c..172e8c95 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5424,6 +5424,7 @@ private slots: {"Windows", "/dev/nvme0n1p3", ""}, {"Photos", "/dev/sdb1", "/run/media/me/Photos"}, {"Home", "/dev/nvme0n1p2", "/home"}, + {"1.0 TB Volume", "/dev/sdc1", "/run/media/me/8CAE187EAE18634E"}, }; // The physical drives: a mounted volume is its drive, with what's // free and how it's let go of; one GIO doesn't list (an fstab @@ -5431,9 +5432,12 @@ private slots: const QVariantList drives{ QVariantMap{{"name", "archive"}, {"path", "/mnt/archive"}, {"icon", "drive"}, {"release", ""}, {"free", 5}}, QVariantMap{{"name", "Photos"}, {"path", "/run/media/me/Photos"}, {"icon", "drive"}, {"release", "eject"}, {"free", 7}}, + // Unlabeled, so named after its mount folder, the UUID. + QVariantMap{{"name", "8CAE187EAE18634E"}, {"path", "/run/media/me/8CAE187EAE18634E"}, {"icon", "drive"}, + {"release", "eject"}, {"free", 9}}, }; const auto devices = Places::devicesFrom(volumes, drives); - QCOMPARE(devices.size(), 3); + QCOMPARE(devices.size(), 4); const QVariantMap windows = devices.at(0).toMap(); QCOMPARE(windows.value("name").toString(), QString("Windows")); QVERIFY(windows.value("path").toString().isEmpty()); @@ -5444,9 +5448,13 @@ private slots: QCOMPARE(photos.value("volume").toString(), QString("/dev/sdb1")); QCOMPARE(photos.value("release").toString(), QString("eject")); QCOMPARE(photos.value("free").toLongLong(), 7); - // The system's own stays out, as physicalDrives left it. - QCOMPARE(devices.at(2).toMap().value("path").toString(), QString("/mnt/archive")); - QVERIFY(!devices.at(2).toMap().contains("volume")); + // The system's own stays out, as physicalDrives left it. An + // unlabeled drive takes GIO's name, not its UUID folder's. + const QVariantMap unlabeled = devices.at(2).toMap(); + QCOMPARE(unlabeled.value("name").toString(), QString("1.0 TB Volume")); + QCOMPARE(unlabeled.value("path").toString(), QString("/run/media/me/8CAE187EAE18634E")); + QCOMPARE(devices.at(3).toMap().value("path").toString(), QString("/mnt/archive")); + QVERIFY(!devices.at(3).toMap().contains("volume")); } void drivesSayWhatsFreeInTheLanguagesDigits() From 20c01af5900bc8a3edb005429d6e9aea13ab0d3a Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Tue, 17 Nov 2026 09:28:32 +0530 Subject: [PATCH 6/7] Open a drive clicked while it automounts, and keep mounted loop images listed --- src/backend.cpp | 9 ++++++++- src/drives.cpp | 6 ++++++ src/drives.h | 4 ++++ src/mounter.h | 2 ++ src/places.cpp | 11 +++++++++++ src/places.h | 8 +++++--- tests/folio_tests.cpp | 16 +++++++++++++--- 7 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/backend.cpp b/src/backend.cpp index 30e015b1..72268cd3 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -364,8 +364,15 @@ void Backend::finish(int run, const Plugins::Result &result) { } void Backend::openVolume(const QString &volume, const QString &name) { - if (m_mounter.busy()) + if (m_mounter.busy()) { + // Clicked while it's mounting as it was plugged in: that mount + // goes on, and opens the drive once it's done. + if (m_mountDrive && !m_mountOpens && m_mounter.volume() == volume) { + m_mountOpens = true; + setBusy(tr("Opening %1…").arg(m_mountName)); + } return; + } m_mountName = name.isEmpty() ? tr("the drive") : name; m_mountDrive = true; m_mountOpens = true; diff --git a/src/drives.cpp b/src/drives.cpp index ee24b686..ffd1a3d5 100644 --- a/src/drives.cpp +++ b/src/drives.cpp @@ -1,6 +1,7 @@ #include "drives.h" #include +#include #include // GIO's headers use `signals` as a struct member, which Qt defines as a @@ -148,6 +149,11 @@ QList Drives::volumes() const { const QString name = take(g_mount_get_name(mount)); if (!name.isEmpty()) v.name = name; + v.canEject = g_mount_can_eject(mount); + v.canUnmount = g_mount_can_unmount(mount); + const QStorageInfo storage(v.root); + if (storage.isValid() && storage.isReady() && !storage.isReadOnly()) + v.free = storage.bytesAvailable(); g_object_unref(mount); } else if (!g_volume_can_mount(volume)) { continue; diff --git a/src/drives.h b/src/drives.h index 825030ab..fa15aedc 100644 --- a/src/drives.h +++ b/src/drives.h @@ -24,6 +24,10 @@ class Drives : public QObject { QString id; // Where it's mounted; empty while it isn't. QString root; + // While mounted: how it's let go of, and what's free (-1 unknown). + bool canEject = false; + bool canUnmount = false; + qint64 free = -1; }; explicit Drives(QObject *parent = nullptr); diff --git a/src/mounter.h b/src/mounter.h index 452ea361..75bf462e 100644 --- a/src/mounter.h +++ b/src/mounter.h @@ -38,6 +38,8 @@ class Mounter : public QObject { QString user() const { return m_user; } QStringList choices() const { return m_choices; } bool drive() const { return m_drive; } + // The volume a drive's mount is for, while busy with one. + QString volume() const { return busy() && m_drive ? m_volume : QString(); } // Mounts the location; `mounted` carries its local GVfs path. A // verified location's questions are answered here, with their first diff --git a/src/places.cpp b/src/places.cpp index 289f262f..d085fd6e 100644 --- a/src/places.cpp +++ b/src/places.cpp @@ -296,6 +296,7 @@ QVariantList Places::devicesFrom(const QList &volumes, const QVa {"release", QString()}, {"free", -1}}; continue; } + bool found = false; for (const QVariant &d : drives) { QVariantMap drive = d.toMap(); if (drive.value("path").toString() != v.root) @@ -307,8 +308,18 @@ QVariantList Places::devicesFrom(const QList &volumes, const QVa drive.insert("name", v.name); devices << drive; listed << v.root; + found = true; break; } + // One physicalDrives passes over that isn't the system's (a disk + // image set up with udisksctl loop-setup) is still the drive it was + // before it mounted, so it stays, with its way to let go of it. + if (!found && !Disks::isSystemMount(v.root)) { + const QString release = v.canEject ? "eject" : v.canUnmount ? "unmount" : ""; + devices << QVariantMap{{"name", v.name.isEmpty() ? v.root : v.name}, {"path", v.root}, {"icon", "drive"}, + {"volume", v.id}, {"release", release}, {"free", v.free}}; + listed << v.root; + } } for (const QVariant &drive : drives) if (!listed.contains(drive.toMap().value("path").toString())) diff --git a/src/places.h b/src/places.h index b300507a..6651f5a4 100644 --- a/src/places.h +++ b/src/places.h @@ -63,10 +63,12 @@ class Places : public QObject { // it can't be) and how much is free. static QVariantList physicalDrives(const QList &mounts, const QString &systemDevice); // The Devices list: GIO's volumes in its order, each mounted one as its - // drive among `drives` under GIO's name, and each unmounted one by `volume` (no path, and - // nothing to let go of, until it's mounted), then the drives GIO + // drive among `drives` under GIO's name, and each unmounted one by + // `volume` (no path, and nothing to let go of, until it's mounted), + // then the drives GIO // doesn't list (an fstab mount). A mounted volume `drives` leaves out - // (the system's own) stays out. + // stays out when it's the system's own, and is listed from GIO's mount + // otherwise (a loop device). static QVariantList devicesFrom(const QList &volumes, const QVariantList &drives); // GIO's drives; null for a test's home, which has none. diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 172e8c95..3c3b22a6 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5425,6 +5425,9 @@ private slots: {"Photos", "/dev/sdb1", "/run/media/me/Photos"}, {"Home", "/dev/nvme0n1p2", "/home"}, {"1.0 TB Volume", "/dev/sdc1", "/run/media/me/8CAE187EAE18634E"}, + // A disk image set up with udisksctl loop-setup, which + // physicalDrives passes over. + {"Image", "/dev/loop3", "/run/media/me/Image", false, true, 11}, }; // The physical drives: a mounted volume is its drive, with what's // free and how it's let go of; one GIO doesn't list (an fstab @@ -5437,7 +5440,7 @@ private slots: {"release", "eject"}, {"free", 9}}, }; const auto devices = Places::devicesFrom(volumes, drives); - QCOMPARE(devices.size(), 4); + QCOMPARE(devices.size(), 5); const QVariantMap windows = devices.at(0).toMap(); QCOMPARE(windows.value("name").toString(), QString("Windows")); QVERIFY(windows.value("path").toString().isEmpty()); @@ -5453,8 +5456,15 @@ private slots: const QVariantMap unlabeled = devices.at(2).toMap(); QCOMPARE(unlabeled.value("name").toString(), QString("1.0 TB Volume")); QCOMPARE(unlabeled.value("path").toString(), QString("/run/media/me/8CAE187EAE18634E")); - QCOMPARE(devices.at(3).toMap().value("path").toString(), QString("/mnt/archive")); - QVERIFY(!devices.at(3).toMap().contains("volume")); + // The image stays once mounted, from GIO's mount, and unmounts. + const QVariantMap image = devices.at(3).toMap(); + QCOMPARE(image.value("name").toString(), QString("Image")); + QCOMPARE(image.value("path").toString(), QString("/run/media/me/Image")); + QCOMPARE(image.value("volume").toString(), QString("/dev/loop3")); + QCOMPARE(image.value("release").toString(), QString("unmount")); + QCOMPARE(image.value("free").toLongLong(), 11); + QCOMPARE(devices.at(4).toMap().value("path").toString(), QString("/mnt/archive")); + QVERIFY(!devices.at(4).toMap().contains("volume")); } void drivesSayWhatsFreeInTheLanguagesDigits() From da1f8aa739d609c1404ffdb563c19081fff8ff79 Mon Sep 17 00:00:00 2001 From: AyushKr2003 Date: Tue, 17 Nov 2026 09:37:25 +0530 Subject: [PATCH 7/7] Take a loop image's free space from the mounts read off the UI thread --- src/drives.cpp | 4 ---- src/drives.h | 3 +-- src/places.cpp | 15 ++++++++++++--- src/places.h | 5 +++-- tests/folio_tests.cpp | 7 +++++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/drives.cpp b/src/drives.cpp index ffd1a3d5..ac4c7c3f 100644 --- a/src/drives.cpp +++ b/src/drives.cpp @@ -1,7 +1,6 @@ #include "drives.h" #include -#include #include // GIO's headers use `signals` as a struct member, which Qt defines as a @@ -151,9 +150,6 @@ QList Drives::volumes() const { v.name = name; v.canEject = g_mount_can_eject(mount); v.canUnmount = g_mount_can_unmount(mount); - const QStorageInfo storage(v.root); - if (storage.isValid() && storage.isReady() && !storage.isReadOnly()) - v.free = storage.bytesAvailable(); g_object_unref(mount); } else if (!g_volume_can_mount(volume)) { continue; diff --git a/src/drives.h b/src/drives.h index fa15aedc..816119fc 100644 --- a/src/drives.h +++ b/src/drives.h @@ -24,10 +24,9 @@ class Drives : public QObject { QString id; // Where it's mounted; empty while it isn't. QString root; - // While mounted: how it's let go of, and what's free (-1 unknown). + // While mounted: how it's let go of. bool canEject = false; bool canUnmount = false; - qint64 free = -1; }; explicit Drives(QObject *parent = nullptr); diff --git a/src/places.cpp b/src/places.cpp index d085fd6e..3f3e5317 100644 --- a/src/places.cpp +++ b/src/places.cpp @@ -287,7 +287,8 @@ QVariantList Places::physicalDrives(const QList &mounts, const QString &s return drives; } -QVariantList Places::devicesFrom(const QList &volumes, const QVariantList &drives) { +QVariantList Places::devicesFrom(const QList &volumes, const QVariantList &drives, + const QList &mounts) { QVariantList devices; QStringList listed; for (const Drives::Volume &v : volumes) { @@ -316,8 +317,16 @@ QVariantList Places::devicesFrom(const QList &volumes, const QVa // before it mounted, so it stays, with its way to let go of it. if (!found && !Disks::isSystemMount(v.root)) { const QString release = v.canEject ? "eject" : v.canUnmount ? "unmount" : ""; + // What's free comes from the mounts read off the UI thread; a + // drive slow to answer never holds up the window here. + qint64 free = -1; + for (const Mount &m : mounts) + if (m.root == v.root) { + free = m.free; + break; + } devices << QVariantMap{{"name", v.name.isEmpty() ? v.root : v.name}, {"path", v.root}, {"icon", "drive"}, - {"volume", v.id}, {"release", release}, {"free", v.free}}; + {"volume", v.id}, {"release", release}, {"free", free}}; listed << v.root; } } @@ -370,7 +379,7 @@ void Places::mountsRead() { g_object_unref(gmount); } const QVariantList physical = physicalDrives(mounts, m_systemDevice); - const QVariantList devices = m_drives ? devicesFrom(m_drives->volumes(), physical) : physical; + const QVariantList devices = m_drives ? devicesFrom(m_drives->volumes(), physical, mounts) : physical; if (devices != m_devices) { m_devices = devices; emit changed(); diff --git a/src/places.h b/src/places.h index 6651f5a4..f7726c65 100644 --- a/src/places.h +++ b/src/places.h @@ -68,8 +68,9 @@ class Places : public QObject { // then the drives GIO // doesn't list (an fstab mount). A mounted volume `drives` leaves out // stays out when it's the system's own, and is listed from GIO's mount - // otherwise (a loop device). - static QVariantList devicesFrom(const QList &volumes, const QVariantList &drives); + // otherwise (a loop device), with what's free from `mounts`. + static QVariantList devicesFrom(const QList &volumes, const QVariantList &drives, + const QList &mounts = {}); // GIO's drives; null for a test's home, which has none. Drives *drives() const { return m_drives; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 548fb5ff..6a6f1770 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5788,7 +5788,7 @@ private slots: {"1.0 TB Volume", "/dev/sdc1", "/run/media/me/8CAE187EAE18634E"}, // A disk image set up with udisksctl loop-setup, which // physicalDrives passes over. - {"Image", "/dev/loop3", "/run/media/me/Image", false, true, 11}, + {"Image", "/dev/loop3", "/run/media/me/Image", false, true}, }; // The physical drives: a mounted volume is its drive, with what's // free and how it's let go of; one GIO doesn't list (an fstab @@ -5800,7 +5800,10 @@ private slots: QVariantMap{{"name", "8CAE187EAE18634E"}, {"path", "/run/media/me/8CAE187EAE18634E"}, {"icon", "drive"}, {"release", "eject"}, {"free", 9}}, }; - const auto devices = Places::devicesFrom(volumes, drives); + // What's free on the image comes from the mounts already read. + Places::Mount imageMount{"/run/media/me/Image", "/dev/loop3", "ext4", "Image"}; + imageMount.free = 11; + const auto devices = Places::devicesFrom(volumes, drives, {imageMount}); QCOMPARE(devices.size(), 5); const QVariantMap windows = devices.at(0).toMap(); QCOMPARE(windows.value("name").toString(), QString("Windows"));