diff --git a/src/FileMenu.qml b/src/FileMenu.qml index 5e1ecb7c..a0c315a0 100644 --- a/src/FileMenu.qml +++ b/src/FileMenu.qml @@ -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 @@ -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, @@ -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 @@ -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") diff --git a/src/PreviewPane.qml b/src/PreviewPane.qml index 5cc8a970..e28dcc55 100644 --- a/src/PreviewPane.qml +++ b/src/PreviewPane.qml @@ -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. @@ -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 diff --git a/src/backend.cpp b/src/backend.cpp index 878f98ba..a1012b82 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include Backend::Backend(const QString &home, QObject *parent) @@ -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(); @@ -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)); if (inTrash) { read.insert("origin", FileOps::originalPath(path)); read.insert("deleted", FileOps::deletedAt(path)); diff --git a/src/fileops.cpp b/src/fileops.cpp index ca1b5f80..9b6a087e 100644 --- a/src/fileops.cpp +++ b/src/fileops.cpp @@ -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; + 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. diff --git a/src/fileops.h b/src/fileops.h index cea4af16..f8626a7c 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -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); @@ -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 ¬ice); void clashChanged(); void historyChanged(); diff --git a/src/i18n/en.json b/src/i18n/en.json index c2bd349a..4da6fe88 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -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", @@ -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.", @@ -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", diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6a0aeeb4..1143948c 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -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 @@ -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