Skip to content

Commit

Permalink
Fix crash on inaccessible directory (#14)
Browse files Browse the repository at this point in the history
qmarkdown crashes if a directory with inaccessible permissions (e.g.
owner root, mode 0700) is encountered while scanning the
workspace. The crash is caused by an exception which is thrown by the
fs::directory_iterator.
  • Loading branch information
michaeladler committed Dec 20, 2021
1 parent ce08edc commit 979da49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/fileinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void FileInput::auto_complete() {

count = 0;
if (directory_exists(q_path)) {
for (const auto &entry : FILESYSTEM::directory_iterator(q_path->toStdString())) {
for (auto it = FILESYSTEM::recursive_directory_iterator(q_path->toStdString(), FILESYSTEM::directory_options::skip_permission_denied); it != FILESYSTEM::recursive_directory_iterator(); ++it) {
if (it.depth() > 0) continue;
const auto entry = *it;
string entry_str = entry.path();

size_t e_slash = entry_str.rfind("/") + 1;
Expand Down
12 changes: 5 additions & 7 deletions src/resgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
#if __has_include(<filesystem>)
#include <filesystem>
#define FILESYSTEM filesystem
#define IS_DIRECTORY(p) p.is_directory()
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
#define FILESYSTEM std::experimental::filesystem
#define IS_DIRECTORY(p) FILESYSTEM::is_directory(p.symlink_status())
#endif

#include <QString>
#include <fstream>

using namespace std;

void read_directory(ofstream *outfile, int depth, QString path, bool *status) {
for (const auto &entry : FILESYSTEM::directory_iterator(path.toStdString())) {
if (IS_DIRECTORY(entry) && depth < 3)
read_directory(outfile, depth + 1, entry.path().c_str(), status);
void read_directory(ofstream *outfile, QString path, bool *status) {
for (auto it = FILESYSTEM::recursive_directory_iterator(path.toStdString(), FILESYSTEM::directory_options::skip_permission_denied); it != FILESYSTEM::recursive_directory_iterator(); ++it) {
if (it.depth() > 3) continue;
const auto entry = *it;
const QString e_path = entry.path().c_str();
const QString image_types[] = {".png", ".jpg", ".jpeg", ".gif"};
for (QString type : image_types) {
Expand All @@ -42,7 +40,7 @@ void res_gen() {
outfile << "<qresource>" << endl;

bool image_exists = false;
read_directory(&outfile, 0, ".", &image_exists);
read_directory(&outfile, ".", &image_exists);

outfile << "</qresource>" << endl;
outfile << "</RCC>" << endl;
Expand Down

0 comments on commit 979da49

Please sign in to comment.