Skip to content

Commit 04768ab

Browse files
committed
fix: Opening a file without permission results in a blank page
log: QFileInfo::isReadable() is unreliable in certain scenarios, causing files without permission to pass the permission check and create a tab, but then failing to open via FileLoadThread , leaving a blank tab and displaying an error message. Solution: Use QFile::open() to actually attempt to open the file for permission verification, and fix onReadAllocError() to display the correct prompt when a permission error occurs. pms: bug-339655
1 parent be1805a commit 04768ab

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

src/widgets/window.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,20 @@ void Window::addTab(const QString &filepath, bool activeTab)
676676
}
677677
}
678678

679-
// check if have permission to read the file.
680-
bool bIsReadable = fileInfo.isReadable();
681-
qDebug() << "File readable:" << bIsReadable;
682-
683-
if (fileInfo.exists() && !bIsReadable) {
684-
qWarning() << "No permission to read file:" << filepath;
685-
DMessageManager::instance()->sendMessage(m_editorWidget->currentWidget(), QIcon(":/images/warning.svg")
686-
, QString(tr("You do not have permission to open %1")).arg(filepath));
687-
return;
679+
// Verify the file is actually readable by attempting to open it.
680+
// This is more reliable than QFileInfo::isReadable(), which can be incorrect
681+
// in some edge cases (e.g. setuid processes, certain filesystems).
682+
if (fileInfo.exists()) {
683+
QFile testFile(filepath);
684+
if (testFile.open(QIODevice::ReadOnly)) {
685+
testFile.close();
686+
} else {
687+
qWarning() << "No permission to read file:" << filepath << "error:" << testFile.errorString();
688+
QWidget *const msgParent = m_editorWidget->currentWidget() ? m_editorWidget->currentWidget() : this;
689+
DMessageManager::instance()->sendMessage(msgParent, QIcon(":/images/warning.svg"),
690+
QString(tr("You do not have permission to open %1")).arg(filepath));
691+
return;
692+
}
688693
}
689694

690695
if (StartManager::instance()->checkPath(filepath)) {

0 commit comments

Comments
 (0)