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:
parent
21dc5284b2
commit
31caae20eb
|
@ -143,8 +143,13 @@ 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();
|
||||||
|
|
||||||
|
{
|
||||||
|
auto restore_flags = on_scope_end([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; });
|
||||||
|
m_buffer.flags() &= ~Buffer::Flags::ReadOnly;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
|
||||||
const ssize_t count = ::read(fifo, data, buffer_size);
|
const ssize_t count = ::read(fifo, data, buffer_size);
|
||||||
if (count <= 0)
|
if (count <= 0)
|
||||||
{
|
{
|
||||||
|
@ -169,6 +174,7 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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(
|
||||||
|
|
|
@ -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);
|
||||||
|
|
1
test/regression/3398-readonly-fifo-failure/cmd
Normal file
1
test/regression/3398-readonly-fifo-failure/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
1
test/regression/3398-readonly-fifo-failure/out
Normal file
1
test/regression/3398-readonly-fifo-failure/out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
blah
|
2
test/regression/3398-readonly-fifo-failure/rc
Normal file
2
test/regression/3398-readonly-fifo-failure/rc
Normal 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
|
8
test/regression/3398-readonly-fifo-failure/script
Normal file
8
test/regression/3398-readonly-fifo-failure/script
Normal 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": [] }] }'
|
Loading…
Reference in New Issue
Block a user