Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/FileMenu.qml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import QtQuick

// The right-click menu. On an item: Open, Open With (each app that can),
// Preview, Rename, Duplicate, Copy, Cut, Paste Into, Copy Path, Compress,
// Extract (for archives), Burn (for disk images), plugins' own actions for
// it, Share (whatever plugins offer for it), Pin, Terminal, Trash. On empty
// space: Undo, Paste, New Folder, Copy Path, Terminal, hidden files. In the
// trash: Restore, Empty Trash and Delete…, without the entries that change
// files or plugins' actions. On a plugin's item: Open, and nothing for files. Entries show their
// keys, so the menu also teaches the shortcuts.
// Preview, Rename, Duplicate, Make Executable (for one's own files), Copy,
// Cut, Paste Into, Copy Path, Compress, Extract (for archives), Burn (for
// disk images), plugins' own actions for it, Share (whatever plugins offer
// for it), Pin, Terminal, Trash. On empty space: Undo, Paste, New Folder,
// Copy Path, Terminal, hidden files. In the trash: Restore, Empty Trash and
// Delete…, without the entries that change files or plugins' actions. On a
// plugin's item: Open, and nothing for files. Entries show their keys, so
// the menu also teaches the shortcuts.
FolioMenu {
id: fileMenu

Expand Down Expand Up @@ -45,6 +46,10 @@ FolioMenu {
// as the menu opens rather than on every binding.
property bool canBurn: false
property var burnTargets: []
// Whether these are one's own files that can be made to run or not, and
// whether they all run now, asked as the menu opens.
property bool canRun: false
property bool runs: false

// Opens at the pointer (`at`, in the window), or (from the keyboard,
// Alt+Return) beside the item it's about. Mirrored, it opens leftward,
Expand All @@ -61,6 +66,8 @@ FolioMenu {
return
canBurn = forItem && forFiles && !trashView && !forSelection && fileOps.canBurn(target.path)
burnTargets = canBurn ? fileOps.burnTargets(target.path) : []
canRun = forItem && forFiles && !trashView && fileOps.canSetExecutable(paths)
runs = canRun && fileOps.executable(paths)
// Submenus (Open With, Share, Burn) show only when they apply.
// Menu makes their entries itself, so they're set here rather than
// bound where they're declared. A plugin's own submenus are made
Expand Down Expand Up @@ -158,6 +165,11 @@ FolioMenu {
keys: "Ctrl+Shift+D"
onTriggered: fileOps.duplicate(fileMenu.target.path)
}
FolioMenuItem {
shown: fileMenu.canRun
text: fileMenu.runs ? qsTr("Make Not Executable") : qsTr("Make Executable")
onTriggered: fileOps.setExecutable(fileMenu.paths, !fileMenu.runs)
}
FolioMenuItem {
shown: fileMenu.forItem && fileMenu.forFiles
text: qsTr("Copy")
Expand Down
21 changes: 21 additions & 0 deletions src/PreviewPane.qml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Rectangle {
// Gone (a folder selected, or another view), it describes nothing, so
// plugins stop being asked about the file it showed last.
Component.onDestruction: backend.describe({}, false)
// Made executable or not from the menu: the item looks the same in the
// folder, so its permissions are read again here.
Connections {
target: pane.folderPane ? pane.folderPane.paneFileOps : null
function onModesChanged(paths) { if (pane.hasItem && paths.includes(pane.item.path)) pane.describe() }
}

// It sits on the columns' background, and their separator marks its
// edge.
Expand Down Expand Up @@ -259,6 +265,21 @@ Rectangle {
color: theme.foreground
size: 12
}
// As ls writes them; Make Executable in the menu changes them. The
// row keeps its place while they're read, so moving through files
// doesn't shift the rows below it. Not in the trash, where nothing
// changes them.
readonly property bool permissionsShown: pane.hasItem && !(pane.folderPane && pane.folderPane.inTrash) && !pane.pluginItem
&& pane.item.path.startsWith("/")
FolioText { visible: parent.permissionsShown; text: qsTr("Permissions"); color: theme.secondaryForeground; size: 12 }
FolioText {
visible: parent.permissionsShown
Layout.fillWidth: true
horizontalAlignment: Text.AlignRight
text: pane.info.permissions || ""
color: theme.foreground
size: 12
}
FolioText { visible: !!pane.drive; text: qsTr("Free space"); color: theme.secondaryForeground; size: 12}
FolioText {
visible: !!pane.drive
Expand Down
32 changes: 29 additions & 3 deletions src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QDir>
#include <QFileInfo>
#include <QFutureWatcher>
#include <QThreadPool>
#include <QGuiApplication>
#include <QImageReader>
#include <QMimeDatabase>
Expand All @@ -16,6 +17,7 @@
#include <QStorageInfo>
#include <QUrl>
#include <QtConcurrent>
#include <sys/stat.h>
#include <unistd.h>

Backend::Backend(const QString &home, QObject *parent)
Expand Down Expand Up @@ -446,6 +448,18 @@ QString Backend::takeSelection() {
return name;
}

// As ls writes them: read, write and run for the owner, the group and
// everyone, with the setuid, setgid and sticky bits in the run places.
static QString permissionsText(mode_t mode) {
const auto run = [mode](mode_t x, mode_t special, char set, char setOnly) {
return mode & special ? (mode & x ? set : setOnly) : (mode & x ? 'x' : '-');
};
const char text[] = {mode & S_IRUSR ? 'r' : '-', mode & S_IWUSR ? 'w' : '-', run(S_IXUSR, S_ISUID, 's', 'S'),
mode & S_IRGRP ? 'r' : '-', mode & S_IWGRP ? 'w' : '-', run(S_IXGRP, S_ISGID, 's', 'S'),
mode & S_IROTH ? 'r' : '-', mode & S_IWOTH ? 'w' : '-', run(S_IXOTH, S_ISVTX, 't', 'T'), 0};
return QString::fromLatin1(text);
}

void Backend::describe(const QVariantMap &item, bool inTrash) {
const QString path = item.value("path").toString();
const QString kind = item.value("kind").toString();
Expand Down Expand Up @@ -482,13 +496,25 @@ void Backend::describe(const QVariantMap &item, bool inTrash) {
});
// Stepping through files faster than they're read queues readings; the
// ones overtaken before they start are skipped rather than left to tie
// up the global pool's threads. A file its extension could not name has
// its type read from what it holds.
// up the threads the next folder is listed on.
// Every local item is read now (its permissions at least), so the
// readings get a few threads of their own: moving across folders on a
// mount that stopped answering leaves reads stuck, and in the shared
// pool they'd keep any folder from being listed. Never torn down, so a
// stuck one can't keep Folio from quitting either. A file its extension
// could not name has its type read from what it holds.
static QThreadPool *const reading = [] {
auto *pool = new QThreadPool;
pool->setMaxThreadCount(4);
return pool;
}();
const bool unnamed = mime == "application/octet-stream";
watcher->setFuture(QtConcurrent::run([path, kind, mime, unnamed, inTrash, latest = m_describing, generation] {
watcher->setFuture(QtConcurrent::run(reading, [path, kind, mime, unnamed, inTrash, latest = m_describing, generation] {
QVariantMap read;
if (*latest != generation)
return read;
if (struct stat st; ::stat(QFile::encodeName(path).constData(), &st) == 0)
read.insert("permissions", permissionsText(st.st_mode));
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if (inTrash) {
read.insert("origin", FileOps::originalPath(path));
read.insert("deleted", FileOps::deletedAt(path));
Expand Down
62 changes: 62 additions & 0 deletions src/fileops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,68 @@ void FileOps::compress(const QStringList &selected) {
connect(process, &QProcess::readyReadStandardError, this, [process, read] { read(process); });
}

// Only the owner may change a file's mode, so it's offered for one's own.
static bool ownFile(const QString &path, struct stat *st) {
return ::lstat(QFile::encodeName(path).constData(), st) == 0 && S_ISREG(st->st_mode) && st->st_uid == ::geteuid();
}

bool FileOps::canSetExecutable(const QStringList &paths) const {
struct stat st;
return !paths.isEmpty() && std::all_of(paths.begin(), paths.end(), [&st](const QString &path) { return ownFile(path, &st); });
}

bool FileOps::executable(const QStringList &paths) const {
struct stat st;
return !paths.isEmpty() && std::all_of(paths.begin(), paths.end(), [&st](const QString &path) {
return ownFile(path, &st) && (st.st_mode & S_IXUSR);
});
}

void FileOps::setExecutable(const QStringList &paths, bool on) {
// A copy still being written takes its source's mode once it's done.
if (paths.isEmpty() || !idle())
return;
QStringList changed, failures;
Comment thread
dhh marked this conversation as resolved.
Resolved
const auto fail = [&](const QString &path, int why) {
failures << tr("%1 (%2)").arg(QFileInfo(path).fileName(), QString::fromLocal8Bit(strerror(why)));
};
for (const QString &path : paths) {
// Looked at and changed through one handle, so a link put anywhere
// in the path after the menu opened can't send the change elsewhere.
const int fd = ::open(QFile::encodeName(path).constData(), O_PATH | O_NOFOLLOW | O_CLOEXEC);
struct stat st;
// One gone or out of reach since the menu opened says why; a
// folder, link or someone else's file is just left alone.
if (fd < 0 || ::fstat(fd, &st) != 0) {
fail(path, errno);
if (fd >= 0)
::close(fd);
continue;
}
if (!S_ISREG(st.st_mode) || st.st_uid != ::geteuid()) {
::close(fd);
continue;
}
// Whoever may read it may run it, so a file kept private stays so;
// off, no one may.
const mode_t read = st.st_mode & (S_IRUSR | S_IRGRP | S_IROTH);
const mode_t mode = on ? (st.st_mode | read >> 2) : (st.st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
// fchmod() can't take an O_PATH handle; its /proc link can be changed.
const bool done = ::chmod(("/proc/self/fd/" + QByteArray::number(fd)).constData(), mode & 07777) == 0;
const int why = errno;
::close(fd);
if (done)
changed << path;
else
fail(path, why);
}
if (!changed.isEmpty())
emit modesChanged(changed);
if (!failures.isEmpty())
emit failed((on ? tr("Couldn't make %1 executable.") : tr("Couldn't make %1 not executable."))
.arg(QLocale().createSeparatedList(failures)));
}

bool FileOps::canExtract(const QStringList &paths) const {
// A lone compressed file (notes.txt.gz) is no archive to bsdtar, and a
// disk image is for burning, not unpacking.
Expand Down
8 changes: 8 additions & 0 deletions src/fileops.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ class FileOps : public QObject {
// Unpacks each archive beside itself: an archive of one item gives
// that item, one of several a folder named after the archive.
Q_INVOKABLE void extract(const QStringList &paths);
// Whether these are all files of one's own that can be made to run or
// not: never a folder, whose x means something else, nor a link.
Q_INVOKABLE bool canSetExecutable(const QStringList &paths) const;
// Whether each of them runs as a program already.
Q_INVOKABLE bool executable(const QStringList &paths) const;
Q_INVOKABLE void setExecutable(const QStringList &paths, bool on);

Q_INVOKABLE void trash(const QStringList &paths);

Expand Down Expand Up @@ -277,6 +283,8 @@ class FileOps : public QObject {
void busyChanged();
void clipboardChanged();
void failed(const QString &message);
// Files whose permissions setExecutable changed.
void modesChanged(const QStringList &paths);
void notice(const QString &notice);
void clashChanged();
void historyChanged();
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
"Couldn't extract %1: it's damaged, or isn't an archive Folio can read.": "Couldn't extract %1: it's damaged, or isn't an archive Folio can read.",
"Couldn't extract %1: it's protected by a password.": "Couldn't extract %1: it's protected by a password.",
"Couldn't format %1. %2": "Couldn't format %1. %2",
"Couldn't make %1 executable.": "Couldn't make %1 executable.",
"Couldn't make %1 not executable.": "Couldn't make %1 not executable.",
"Couldn't make %1 the default app for %2: %3": "Couldn't make %1 the default app for %2: %3",
"Couldn't move %1 to the trash to replace it: %2": "Couldn't move %1 to the trash to replace it: %2",
"Couldn't move %1 to the trash: %2": "Couldn't move %1 to the trash: %2",
Expand Down Expand Up @@ -217,6 +219,8 @@
"Keep Both": "Keep Both",
"Keyboard shortcuts": "Keyboard shortcuts",
"Kind": "Kind",
"Make Executable": "Make Executable",
"Make Not Executable": "Make Not Executable",
"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.",
Expand Down Expand Up @@ -278,6 +282,7 @@
"Paste Into Folder": "Paste Into Folder",
"Pause (K)": "Pause (K)",
"Permission was refused.": "Permission was refused.",
"Permissions": "Permissions",
"Pin": "Pin",
"Pin or unpin the folder": "Pin or unpin the folder",
"Pinned": "Pinned",
Expand Down
77 changes: 77 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,66 @@ private slots:
QCOMPARE(FileOps::foldHandlerRepeats({other, writer, twin}), QVariantList({other, writer, twin}));
}

void fileOpsMakesFilesExecutable()
{
// Run is allowed for whoever may read, and taken away from
// everyone. Only for one's own files: not folders, whose x means
// something else, nor links.
QTemporaryDir dir;
const QString script = dir.filePath("run.sh"), secret = dir.filePath("secret.sh"),
link = dir.filePath("link"), folder = dir.filePath("folder");
write(script, "#!/bin/sh\n");
write(secret, "#!/bin/sh\n");
QCOMPARE(::chmod(QFile::encodeName(script).constData(), 0644), 0);
QCOMPARE(::chmod(QFile::encodeName(secret).constData(), 0600), 0);
QVERIFY(QFile::link(script, link));
QVERIFY(QDir().mkdir(folder));
const auto mode = [](const QString &path) {
struct stat st;
return ::stat(QFile::encodeName(path).constData(), &st) == 0 ? st.st_mode & 07777 : 0u;
};
FileOps ops;
QVERIFY(ops.canSetExecutable({script, secret}));
QVERIFY(!ops.canSetExecutable({script, folder}));
QVERIFY(!ops.canSetExecutable({link}));
QVERIFY(!ops.canSetExecutable({}));
if (::geteuid() != 0)
QVERIFY(!ops.canSetExecutable({"/etc/hostname"}));
QVERIFY(!ops.executable({script, secret}));

// The details pane reads the item again on hearing which changed.
QSignalSpy changed(&ops, &FileOps::modesChanged);
ops.setExecutable({script, secret, folder}, true);
QCOMPARE(changed.count(), 1);
QCOMPARE(changed.at(0).at(0).toStringList(), QStringList({script, secret}));
QCOMPARE(mode(script), 0755u);
QCOMPARE(mode(secret), 0700u);
QVERIFY(ops.executable({script, secret}));
QCOMPARE(::chmod(QFile::encodeName(secret).constData(), 0600), 0);
QVERIFY(!ops.executable({script, secret}));

ops.setExecutable({script, secret}, false);
QCOMPARE(mode(script), 0644u);
QCOMPARE(mode(secret), 0600u);

// A file gone since the menu opened says so; the folder beside it
// is still left alone without a word.
QSignalSpy failed(&ops, &FileOps::failed);
ops.setExecutable({dir.filePath("gone.sh"), folder}, true);
QCOMPARE(failed.count(), 1);
QVERIFY(failed.at(0).at(0).toString().contains("gone.sh"));

// Not while other work is under way: a copy still being written
// would put its source's mode back once it's done.
ops.trash({dir.filePath("gone.txt")});
QVERIFY(!ops.busy().isEmpty());
ops.setExecutable({script}, true);
QCOMPARE(failed.count(), 2);
QCOMPARE(failed.last().first().toString(), QString("Try again once the work under way is done."));
QCOMPARE(mode(script), 0644u);
QTRY_VERIFY(ops.busy().isEmpty());
}

void fileOpsDeleteForeverOutsideTheTrash()
{
// Shift+Delete deletes for good anywhere: the folder and what's in
Expand Down Expand Up @@ -4279,6 +4339,23 @@ private slots:
QVERIFY2(!said().contains(pdfName) && !said().contains("Reader"), qPrintable(said()));
}

void backendShowsWhoMayReadWriteAndRun()
{
// Any file or folder's permissions, as ls writes them, read with
// the rest of the details.
QTemporaryDir dir;
const QString report = dir.filePath("report.pdf"), tools = dir.filePath("tools");
write(report, "x");
QVERIFY(QDir().mkdir(tools));
QCOMPARE(::chmod(QFile::encodeName(report).constData(), 0640), 0);
QCOMPARE(::chmod(QFile::encodeName(tools).constData(), 01755), 0);
Backend backend(dir.path());
backend.describe({{"path", report}, {"kind", "pdf"}, {"mime", "application/pdf"}});
QTRY_COMPARE(backend.details().value("permissions").toString(), QString("rw-r-----"));
backend.describe({{"path", tools}, {"kind", "folder"}, {"mime", "inode/directory"}});
QTRY_COMPARE(backend.details().value("permissions").toString(), QString("rwxr-xr-t"));
}

void backendReadsTrashNotesOffTheUiThread()
{
// Finding a trashed item's note looks over every mounted drive, so
Expand Down
Loading