summaryrefslogtreecommitdiff
path: root/src/commands.cc
diff options
context:
space:
mode:
authorFrank LENORMAND <lenormf@gmail.com>2016-07-20 20:45:50 +0300
committerFrank LENORMAND <lenormf@gmail.com>2016-07-23 10:03:21 +0300
commit8a4596bea9d85c86d03efa129c41530faa2a34a7 (patch)
tree8cd6a789c734e8507042fb5126cf692dd3ae789c /src/commands.cc
parent88a9607552fd3c614e6d70a271b663323306a8db (diff)
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
Diffstat (limited to 'src/commands.cc')
-rw-r--r--src/commands.cc9
1 files changed, 8 insertions, 1 deletions
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());
}
}