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
This commit is contained in:
Maxime Coste 2020-03-14 12:58:22 +11:00
parent 21dc5284b2
commit 31caae20eb
6 changed files with 45 additions and 25 deletions

View File

@ -143,32 +143,38 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
char data[buffer_size]; char data[buffer_size];
BufferCoord insert_coord = m_buffer.back_coord(); BufferCoord insert_coord = m_buffer.back_coord();
const int fifo = fd(); const int fifo = fd();
do
{ {
const ssize_t count = ::read(fifo, data, buffer_size); auto restore_flags = on_scope_end([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; });
if (count <= 0) m_buffer.flags() &= ~Buffer::Flags::ReadOnly;
do
{ {
closed = true;
break; const ssize_t count = ::read(fifo, data, buffer_size);
} if (count <= 0)
{
auto pos = m_buffer.back_coord(); closed = true;
const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll; break;
if (prevent_scrolling) }
pos = m_buffer.next(pos);
auto pos = m_buffer.back_coord();
m_buffer.insert(pos, StringView(data, data+count)); const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll;
if (prevent_scrolling)
if (prevent_scrolling) pos = m_buffer.next(pos);
{
m_buffer.erase({0,0}, m_buffer.next({0,0})); m_buffer.insert(pos, StringView(data, data+count));
// in the other case, the buffer will have automatically
// inserted a \n to guarantee its invariant. if (prevent_scrolling)
if (data[count-1] == '\n') {
m_buffer.insert(m_buffer.end_coord(), "\n"); 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()) if (insert_coord != m_buffer.back_coord())
m_buffer.run_hook_in_own_context( m_buffer.run_hook_in_own_context(

View File

@ -472,14 +472,16 @@ const ParameterDesc write_params{
void do_write_buffer(Context& context, Optional<String> filename, WriteFlags flags, bool atomic = false) void do_write_buffer(Context& context, Optional<String> filename, WriteFlags flags, bool atomic = false)
{ {
Buffer& buffer = context.buffer(); 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"); 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 // 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 // or we try to write to it indirectly using e.g. a symlink, throw an error
if ((context.buffer().flags() & Buffer::Flags::ReadOnly) if (is_file and is_readonly and
and (not filename or real_path(*filename) == buffer.name())) (not filename or real_path(*filename) == buffer.name()))
throw runtime_error("cannot overwrite the buffer when in readonly mode"); throw runtime_error("cannot overwrite the buffer when in readonly mode");
auto effective_filename = not filename ? buffer.name() : parse_filename(*filename); auto effective_filename = not filename ? buffer.name() : parse_filename(*filename);

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
blah

View File

@ -0,0 +1,2 @@
nop %sh{ mkfifo test-fifo; ( printf 'blah' > test-fifo ) </dev/null >/dev/null 2>&1 & }
edit -readonly -fifo test-fifo out

View File

@ -0,0 +1,8 @@
ui_out '{ "jsonrpc": "2.0", "method": "set_ui_options", "params": [{}] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "black", "bg": "cyan", "attributes": [] }, "contents": "\u000a" }]], { "fg": "default", "bg": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "menu_hide", "params": [] }'
ui_out '{ "jsonrpc": "2.0", "method": "info_hide", "params": [] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw_status", "params": [[], [{ "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "out 1:1 " }, { "face": { "fg": "black", "bg": "yellow", "attributes": [] }, "contents": "[scratch][fifo]" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "blue", "bg": "default", "attributes": [] }, "contents": "1 sel" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": " - client0@[kak-tests]" }], { "fg": "cyan", "bg": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "set_cursor", "params": ["buffer", { "line": 0, "column": 0 }] }'
ui_out '{ "jsonrpc": "2.0", "method": "refresh", "params": [true] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "black", "bg": "white", "attributes": [] }, "contents": "b" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "lah\u000a" }]], { "fg": "default", "bg": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "attributes": [] }] }'