diff --git a/src/file.cc b/src/file.cc index a2ad279b..58eb9284 100644 --- a/src/file.cc +++ b/src/file.cc @@ -252,13 +252,14 @@ bool regular_file_exists(StringView filename) (st.st_mode & S_IFMT) == S_IFREG; } +template void write(int fd, StringView data) { const char* ptr = data.data(); ssize_t count = (int)data.length(); int flags = fcntl(fd, F_GETFL, 0); - if (EventManager::has_instance()) + if (not atomic and EventManager::has_instance()) fcntl(fd, F_SETFL, flags | O_NONBLOCK); auto restore_flags = on_scope_end([&] { fcntl(fd, F_SETFL, flags); }); @@ -269,12 +270,15 @@ void write(int fd, StringView data) ptr += written; count -= written; } - else if (errno == EAGAIN and EventManager::has_instance()) + else if (errno == EAGAIN and not atomic and EventManager::has_instance()) EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, false); else throw file_access_error(format("fd: {}", fd), strerror(errno)); } } +template void write(int fd, StringView data); +template void write(int fd, StringView data); + void write_to_file(StringView filename, StringView data) { @@ -295,7 +299,7 @@ void write_buffer_to_fd(Buffer& buffer, int fd) eoldata = "\n"; - BufferedWriter writer{fd}; + BufferedWriter writer{fd}; if (buffer.options()["BOM"].get() == ByteOrderMark::Utf8) writer.write("\xEF\xBB\xBF"); diff --git a/src/file.hh b/src/file.hh index 1d9391fb..65abd3c8 100644 --- a/src/file.hh +++ b/src/file.hh @@ -39,6 +39,7 @@ bool fd_readable(int fd); bool fd_writable(int fd); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); +template void write(int fd, StringView data); void write_to_file(StringView filename, StringView data); @@ -121,7 +122,7 @@ CandidateList complete_filename(StringView prefix, const Regex& ignore_regex, CandidateList complete_command(StringView prefix, ByteCount cursor_pos = -1); -template +template struct BufferedWriter { BufferedWriter(int fd) @@ -149,7 +150,7 @@ struct BufferedWriter void flush() { - Kakoune::write(m_fd, {m_buffer, m_pos}); + Kakoune::write(m_fd, {m_buffer, m_pos}); m_pos = 0; } diff --git a/src/terminal_ui.cc b/src/terminal_ui.cc index d411dd67..9685129a 100644 --- a/src/terminal_ui.cc +++ b/src/terminal_ui.cc @@ -208,7 +208,7 @@ void TerminalUI::Window::draw(DisplayCoord pos, lines[(int)pos.line].append({}, size.column - pos.column, default_face); } -struct Writer : BufferedWriter<> +struct Writer : BufferedWriter { using Writer::BufferedWriter::BufferedWriter; ~Writer() noexcept(false) = default;