editor: Refuse to load non-regular file types

This commit is contained in:
apio 2024-03-14 13:09:03 +01:00 committed by apio
parent aee100753d
commit eb3af60497

View File

@ -11,6 +11,7 @@
#include <ctype.h> #include <ctype.h>
#include <luna/Utf8.h> #include <luna/Utf8.h>
#include <os/File.h> #include <os/File.h>
#include <os/FileSystem.h>
#include <ui/App.h> #include <ui/App.h>
EditorWidget::EditorWidget(SharedPtr<ui::Font> font) : ui::Widget(), m_font(font) EditorWidget::EditorWidget(SharedPtr<ui::Font> font) : ui::Widget(), m_font(font)
@ -21,6 +22,15 @@ EditorWidget::EditorWidget(SharedPtr<ui::Font> font) : ui::Widget(), m_font(font
Result<void> EditorWidget::load_file(const os::Path& path) Result<void> 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()); os::eprintln("Loading file: %s", path.name().chars());
auto file = TRY(os::File::open_or_create(path, os::File::ReadOnly)); auto file = TRY(os::File::open_or_create(path, os::File::ReadOnly));
@ -98,10 +108,10 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
auto result = save_file(); auto result = save_file();
if (result.has_error()) 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; 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; return ui::EventResult::DidNotHandle;
} }
default: return ui::EventResult::DidNotHandle; default: return ui::EventResult::DidNotHandle;
@ -150,7 +160,7 @@ Result<void> EditorWidget::save_file()
{ {
if (m_path.is_empty_path()) 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); return err(ENOENT);
} }