From 2aa0612400da01702be2ac609fe14566d34c247b Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 14 Mar 2024 13:09:03 +0100 Subject: [PATCH] editor: Refuse to load non-regular file types --- editor/EditorWidget.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/editor/EditorWidget.cpp b/editor/EditorWidget.cpp index 7b5e619e..3bba0713 100644 --- a/editor/EditorWidget.cpp +++ b/editor/EditorWidget.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include EditorWidget::EditorWidget(SharedPtr font) : ui::Widget(), m_font(font) @@ -21,6 +22,15 @@ EditorWidget::EditorWidget(SharedPtr font) : ui::Widget(), m_font(font Result EditorWidget::load_file(const os::Path& path) { + struct stat st; + TRY(os::FileSystem::stat(path, st, true)); + + if (!S_ISREG(st.st_mode)) + { + os::eprintln("editor: not loading %s as it is not a regular file", path.name().chars()); + return {}; + } + os::eprintln("Loading file: %s", path.name().chars()); auto file = TRY(os::File::open_or_create(path, os::File::ReadOnly)); @@ -98,10 +108,10 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest auto result = save_file(); if (result.has_error()) { - os::eprintln("TextEditor: failed to save file: %s", result.error_string()); + os::eprintln("editor: failed to save file: %s", result.error_string()); return ui::EventResult::DidNotHandle; } - os::println("TextEditor: buffer saved to %s successfully", m_path.name().chars()); + os::println("editor: buffer saved to %s successfully", m_path.name().chars()); return ui::EventResult::DidNotHandle; } default: return ui::EventResult::DidNotHandle; @@ -150,7 +160,7 @@ Result EditorWidget::save_file() { if (m_path.is_empty_path()) { - os::eprintln("TextEditor: no file to save buffer to!"); + os::eprintln("editor: no file to save buffer to!"); return err(ENOENT); }