From 338d00d6054b7832cf4da83ca7a052258bf5aad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Tue, 9 Nov 2021 07:52:09 +0100 Subject: [PATCH] Do not crash on opendir() failures Show an error message, and a way out. --- sdn.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/sdn.cpp b/sdn.cpp index 6017c51..6d0ac40 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -841,6 +841,11 @@ fun resort (const string anchor = at_cursor ().filename) { focus (anchor); } +fun show_message (const string &message, int ttl = 30) { + g.message = to_wide (message); + g.message_ttl = ttl; +} + fun reload (bool keep_anchor) { g.unames.clear (); while (auto *ent = getpwent ()) @@ -859,6 +864,16 @@ fun reload (bool keep_anchor) { auto now = time (NULL); g.now = *localtime (&now); auto dir = opendir ("."); g.entries.clear (); + if (!dir) { + show_message (strerror (errno)); + if (g.cwd != "/") { + struct dirent f = {}; + strncpy(f.d_name, "..", sizeof f.d_name); + f.d_type = DT_DIR; + g.entries.push_back (make_entry (&f)); + } + goto readfail; + } while (auto f = readdir (dir)) { string name = f->d_name; // Two dots are for navigation but this ain't as useful @@ -869,6 +884,7 @@ fun reload (bool keep_anchor) { } closedir (dir); +readfail: g.out_of_date = false; for (int col = 0; col < entry::COLUMNS; col++) { auto &longest = g.max_widths[col] = 0; @@ -889,11 +905,6 @@ fun reload (bool keep_anchor) { (IN_ALL_EVENTS | IN_ONLYDIR | IN_EXCL_UNLINK) & ~(IN_ACCESS | IN_OPEN)); } -fun show_message (const string &message, int ttl = 30) { - g.message = to_wide (message); - g.message_ttl = ttl; -} - fun run_program (initializer_list list, const string &filename) { if (g.ext_helpers) { // XXX: this doesn't try them all out, though it shouldn't make any