From 8a4596bea9d85c86d03efa129c41530faa2a34a7 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Wed, 20 Jul 2016 20:45:50 +0300 Subject: [PATCH] Implement a `readonly` mode This commit introduces the `readonly` variable as well as the `-ro` command line option which prevent buffers from being overwritten on disk when the `write` command is used without arguments. Some buffers can selectively be put in readonly mode by setting the `readonly` variable on the `buffer` scope, the `global` mode will affect all buffers (even those who will be open subsequently), using the `window` scope will have no effect. Closes #685 --- src/buffer.cc | 7 +++++++ src/buffer.hh | 13 +++++++------ src/buffer_manager.cc | 3 ++- src/commands.cc | 9 ++++++++- src/main.cc | 16 ++++++++++++---- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 089103b1..f447f2db 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -591,6 +591,13 @@ void Buffer::set_fs_timestamp(timespec ts) void Buffer::on_option_changed(const Option& option) { + if (option.name() == "readonly") + { + if (option.get()) + m_flags |= Flags::ReadOnly; + else + m_flags &= ~Flags::ReadOnly; + } run_hook_in_own_context("BufSetOption", format("{}={}", option.name(), option.get_as_string())); } diff --git a/src/buffer.hh b/src/buffer.hh index 2b118c89..1ed88cf7 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -101,12 +101,13 @@ class Buffer : public SafeCountable, public OptionManagerWatcher, public Scope public: enum class Flags { - None = 0, - File = 1 << 0, - New = 1 << 1, - Fifo = 1 << 2, - NoUndo = 1 << 3, - Debug = 1 << 4 + None = 0, + File = 1 << 0, + New = 1 << 1, + Fifo = 1 << 2, + NoUndo = 1 << 3, + Debug = 1 << 4, + ReadOnly = 1 << 5, }; Buffer(String name, Flags flags, StringView data = {}, diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index e743bff0..688e864a 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -78,7 +78,8 @@ void BufferManager::backup_modified_buffers() { for (auto& buf : m_buffers) { - if ((buf->flags() & Buffer::Flags::File) and buf->is_modified()) + if ((buf->flags() & Buffer::Flags::File) and buf->is_modified() + and !(buf->flags() & Buffer::Flags::ReadOnly)) write_buffer_to_backup_file(*buf); } } diff --git a/src/commands.cc b/src/commands.cc index 0a13938e..6104975c 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -239,6 +239,12 @@ void write_buffer(const ParametersParser& parser, Context& context, const ShellC if (parser.positional_count() == 0 and !(buffer.flags() & Buffer::Flags::File)) throw runtime_error("cannot write a non file buffer without a filename"); + // if the buffer is in read-only mode and we try to save it directly + // or we try to write to it indirectly using e.g. a symlink, throw an error + if ((context.buffer().flags() & Buffer::Flags::ReadOnly) + && (parser.positional_count() == 0 || real_path(parser[0]) == buffer.name())) + throw runtime_error("cannot overwrite the buffer when in readonly mode"); + auto filename = parser.positional_count() == 0 ? buffer.name() : parse_filename(parser[0]); write_buffer_to_file(buffer, filename); @@ -260,7 +266,8 @@ void write_all_buffers() { for (auto& buffer : BufferManager::instance()) { - if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()) + if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified() + and !(buffer->flags() & Buffer::Flags::ReadOnly)) write_buffer_to_file(*buffer, buffer->name()); } } diff --git a/src/main.cc b/src/main.cc index 82444571..020c1e59 100644 --- a/src/main.cc +++ b/src/main.cc @@ -265,6 +265,7 @@ void register_options() reg.declare_option("modelinefmt", "format string used to generate the modeline", "%val{bufname} %val{cursor_line}:%val{cursor_char_column} "_str); reg.declare_option("debug", "various debug flags", DebugFlags::None); + reg.declare_option("readonly", "prevent buffers from being modified", false); } struct convert_to_client_mode @@ -460,7 +461,7 @@ int run_client(StringView session, StringView init_command, UIType ui_type) } int run_server(StringView session, StringView init_command, - bool ignore_kakrc, bool daemon, UIType ui_type, + bool ignore_kakrc, bool daemon, bool readonly, UIType ui_type, ConstArrayView files, ByteCoord target_coord) { static bool terminate = false; @@ -503,6 +504,8 @@ int run_server(StringView session, StringView init_command, write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***"); + GlobalScope::instance().options().get_local_option("readonly").set(readonly); + Server server(session.empty() ? to_string(getpid()) : session.str()); bool startup_error = false; @@ -536,7 +539,9 @@ int run_server(StringView session, StringView init_command, { try { - open_or_create_file_buffer(file); + Buffer *buffer = open_or_create_file_buffer(file); + if (readonly) + buffer->flags() |= Buffer::Flags::ReadOnly; } catch (Kakoune::runtime_error& error) { @@ -741,7 +746,8 @@ int main(int argc, char* argv[]) { "q", { false, "in filter mode, be quiet about errors applying keys" } }, { "ui", { true, "set the type of user interface to use (ncurses, dummy, or json)" } }, { "l", { false, "list existing sessions" } }, - { "clear", { false, "clear dead sessions" } } } + { "clear", { false, "clear dead sessions" } }, + { "ro", { false, "readonly mode" } } } }; try { @@ -793,7 +799,8 @@ int main(int argc, char* argv[]) for (size_t i = 0; i < parser.positional_count(); ++i) files.emplace_back(parser[i]); - return run_filter(*keys, init_command, files, (bool)parser.get_switch("q")); + return run_filter(*keys, init_command, files, + (bool)parser.get_switch("q")); } if (auto server_session = parser.get_switch("c")) @@ -840,6 +847,7 @@ int main(int argc, char* argv[]) return run_server(session, init_command, (bool)parser.get_switch("n"), (bool)parser.get_switch("d"), + (bool)parser.get_switch("ro"), ui_type, files, target_coord); } catch (convert_to_client_mode& convert)