From d203d8cfdc2b10664647dccbfff33f665e238852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20=22sp1rit=22=E2=80=8B?= Date: Wed, 3 May 2023 12:28:35 +0200 Subject: [PATCH 1/2] mainwindow: avoid SIGSEGV when writing a note into a readonly dir --- mainwindow.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index 927ac87..41e8d56 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,6 +3,7 @@ #include "settings.h" #include #include +#include #include #include @@ -510,8 +511,16 @@ void CMainWindow::FetchAndSave() tmp_filename = filename; tmp_filename += ".tmp"; + errno = 0; /* TODO: this kills the xattrs */ FILE *fl = fopen(tmp_filename.c_str(), "wb"); + if (errno != 0) { + const int err = errno; + throw Glib::FileError( + Glib::FileError::Code(g_file_error_from_errno(err)), + Glib::ustring::compose("Failed opening file for writing: %1", Glib::strerror(err)) + ); + } fwrite(str.c_str(),str.length(),1,fl); fclose(fl); From 637da8da39b66cc63a854de9ccc3e3e1252a4455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20=22sp1rit=22=E2=80=8B?= Date: Wed, 3 May 2023 12:38:06 +0200 Subject: [PATCH 2/2] mainwindow: allow viewing of read-only *mostly* documents After opening a document, NoteKit now attempts to open the file as readwrite too. If this fails, the "editable" property of the view will be set to false. Additionaly, FetchAndSave() is now a no-op if "editable" is set to false, to prevent saving non-savable files. --- mainwindow.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 41e8d56..8e11e22 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -493,7 +493,8 @@ void CMainWindow::FocusDocument() void CMainWindow::FetchAndSave() { sview.last_modified=0; - + + if (!sview.get_editable()) return; if(active_document=="") return; gsize len; @@ -748,6 +749,14 @@ void CMainWindow::SettingDocumentUpdate() { SetupDocumentWindow(filename); g_free(buf); + + try { + Glib::RefPtr stream = file->open_readwrite(); + stream->close(); + } catch (Gio::Error& e) { + g_printerr("Failed opening document for writing (%s), disabling editing.\n", e.what().c_str()); + sview.set_editable(false); + } } catch(Gio::Error &e) { sview.set_editable(false); sview.set_can_focus(false);