summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2024-11-16 10:50:17 +1100
committerMaxime Coste <mawww@kakoune.org>2024-11-16 10:50:17 +1100
commite74a3ac6a3bad1b74af71aa0bfdacb41ffcb7355 (patch)
treeca29354bbbc4b2d58e342304259d11b1ca7e3918 /src
parentd86e505fad397251a8a618c545a7d78f43a70a34 (diff)
Run urgent event loop when blocking on opening a file for writing
This makes <c-g> cancel the writing operation if we block on trying to open a fifo with no reader. Fixes #5256
Diffstat (limited to 'src')
-rw-r--r--src/file.cc12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/file.cc b/src/file.cc
index ac19a089..f75b0685 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -280,9 +280,15 @@ template void write<false>(int fd, StringView data);
void write_to_file(StringView filename, StringView data)
{
- const int fd = open(filename.zstr(), O_CREAT | O_WRONLY | O_TRUNC, 0644);
- if (fd == -1)
- throw file_access_error(filename, strerror(errno));
+ int fd = -1;
+ const int flags = O_CREAT | O_WRONLY | O_TRUNC | (EventManager::has_instance() ? O_NONBLOCK : 0);
+ while ((fd = open(filename.zstr(), flags, 0644)) == -1)
+ {
+ if (errno == ENXIO and EventManager::has_instance()) // trying to open a FIFO with no readers yet
+ EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, false);
+ else
+ throw file_access_error(filename, strerror(errno));
+ }
auto close_fd = on_scope_end([fd]{ close(fd); });
write(fd, data);
}