diff options
| author | Maxime Coste <mawww@kakoune.org> | 2020-03-14 12:58:22 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2020-03-14 12:58:22 +1100 |
| commit | 31caae20ebbd514730fdbf4ac82c61daf54c4686 (patch) | |
| tree | 248e803da10abef0c600f15491c6ce8254d56d62 /src | |
| parent | 21dc5284b2743a5ed3c946bcdc08186b28dad22e (diff) | |
Allow reading from fifo in readonly buffers
readonly is supposed to prevent the user from modifying the buffer
and it can be useful to generate a readonly fifo buffer.
Fixes #3398
Diffstat (limited to 'src')
| -rw-r--r-- | src/buffer_utils.cc | 48 | ||||
| -rw-r--r-- | src/commands.cc | 8 |
2 files changed, 32 insertions, 24 deletions
diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index cbead31d..0162822c 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -143,32 +143,38 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll char data[buffer_size]; BufferCoord insert_coord = m_buffer.back_coord(); const int fifo = fd(); - do + { - const ssize_t count = ::read(fifo, data, buffer_size); - if (count <= 0) + auto restore_flags = on_scope_end([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; }); + m_buffer.flags() &= ~Buffer::Flags::ReadOnly; + do { - closed = true; - break; - } - - auto pos = m_buffer.back_coord(); - const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll; - if (prevent_scrolling) - pos = m_buffer.next(pos); - m_buffer.insert(pos, StringView(data, data+count)); - - if (prevent_scrolling) - { - m_buffer.erase({0,0}, m_buffer.next({0,0})); - // in the other case, the buffer will have automatically - // inserted a \n to guarantee its invariant. - if (data[count-1] == '\n') - m_buffer.insert(m_buffer.end_coord(), "\n"); + const ssize_t count = ::read(fifo, data, buffer_size); + if (count <= 0) + { + closed = true; + break; + } + + auto pos = m_buffer.back_coord(); + const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll; + if (prevent_scrolling) + pos = m_buffer.next(pos); + + m_buffer.insert(pos, StringView(data, data+count)); + + if (prevent_scrolling) + { + m_buffer.erase({0,0}, m_buffer.next({0,0})); + // in the other case, the buffer will have automatically + // inserted a \n to guarantee its invariant. + if (data[count-1] == '\n') + m_buffer.insert(m_buffer.end_coord(), "\n"); + } } + while (++loop < max_loop and fd_readable(fifo)); } - while (++loop < max_loop and fd_readable(fifo)); if (insert_coord != m_buffer.back_coord()) m_buffer.run_hook_in_own_context( diff --git a/src/commands.cc b/src/commands.cc index ee45bcde..09e73ce3 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -472,14 +472,16 @@ const ParameterDesc write_params{ void do_write_buffer(Context& context, Optional<String> filename, WriteFlags flags, bool atomic = false) { Buffer& buffer = context.buffer(); + const bool is_file = (bool)(buffer.flags() & Buffer::Flags::File); - if (not filename and !(buffer.flags() & Buffer::Flags::File)) + if (not filename and !is_file) throw runtime_error("cannot write a non file buffer without a filename"); + const bool is_readonly = (bool)(context.buffer().flags() & Buffer::Flags::ReadOnly); // 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) - and (not filename or real_path(*filename) == buffer.name())) + if (is_file and is_readonly and + (not filename or real_path(*filename) == buffer.name())) throw runtime_error("cannot overwrite the buffer when in readonly mode"); auto effective_filename = not filename ? buffer.name() : parse_filename(*filename); |
