Commit 97ec1c6e authored by Harald Sitter's avatar Harald Sitter
Browse files

replace open-fstat dance with lstat

since we know the path cannot be relative because of the dir separator
check there is no point in explicitly opening the path when we can
instead pass it to lstat as-is. the path will be absolutely of the form
/dev/foo so at most it could be a symlink that we'll not follow with

(cherry picked from commit 00659ebe)
parent 2bf4519d
......@@ -6,15 +6,12 @@
#include <QDebug>
#include <QProcess>
#include <QFileInfo>
#include <QScopeGuard>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
// Append name to /dev/ and ensure it is a trustable block device.
......@@ -32,22 +29,8 @@ static QString nameToPath(const QString &name)
const QString path = QStringLiteral("/dev/%1").arg(name);
int flags = O_NOFOLLOW; // POSIX.1-2008 should always be available
#ifdef O_PATH // Not available on FreeBSD and Linux < 2.6.39
flags |= O_PATH;
flags |= O_RDONLY;
int blockFD = open(QFile::encodeName(path), flags);
auto blockFDClose = qScopeGuard([blockFD] { close(blockFD); });
if (blockFD == -1) {
const int err = errno;
qWarning() << "Failed to open block device" << name << strerror(err);
return {};
struct stat sb;
if (fstat(blockFD, &sb) == -1) {
if (lstat(QFile::encodeName(path), &sb) == -1) {
const int err = errno;
qWarning() << "Failed to stat block device" << name << strerror(err);
return {};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment