summaryrefslogtreecommitdiff
path: root/src/file.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/file.cc')
-rw-r--r--src/file.cc31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/file.cc b/src/file.cc
index ca2d2834..2fbf2e78 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -2,14 +2,13 @@
#include "assert.hh"
#include "buffer.hh"
+#include "client.hh"
#include "exception.hh"
#include "flags.hh"
-#include "option_types.hh"
#include "event_manager.hh"
#include "ranked_match.hh"
#include "regex.hh"
#include "string.hh"
-#include "unicode.hh"
#include <limits>
#include <cerrno>
@@ -271,7 +270,7 @@ void write(int fd, StringView data)
count -= written;
}
else if (errno == EAGAIN and not atomic and EventManager::has_instance())
- EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, false);
+ EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, std::chrono::nanoseconds{});
else
throw file_access_error(format("fd: {}", fd), strerror(errno));
}
@@ -279,10 +278,28 @@ void write(int fd, StringView data)
template void write<true>(int fd, StringView data);
template void write<false>(int fd, StringView data);
+static int create_file(const Context& context, const char* filename)
+{
+ int fd;
+ const int flags = O_CREAT | O_WRONLY | O_TRUNC | (EventManager::has_instance() ? O_NONBLOCK : 0);
+ using namespace std::chrono;
+ BusyIndicator busy_indicator{context, [&](seconds elapsed) {
+ return DisplayLine{format("waiting to open file ({}s)", elapsed.count()),
+ context.faces()["Information"]};
+ }};
+ while ((fd = open(filename, 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, nanoseconds{1'000'000});
+ else
+ return -1;
+ }
+ return fd;
+}
-void write_to_file(StringView filename, StringView data)
+void write_to_file(const Context& context, StringView filename, StringView data)
{
- const int fd = open(filename.zstr(), O_CREAT | O_WRONLY | O_TRUNC, 0644);
+ int fd = create_file(context, filename.zstr());
if (fd == -1)
throw file_access_error(filename, strerror(errno));
auto close_fd = on_scope_end([fd]{ close(fd); });
@@ -332,7 +349,7 @@ int open_temp_file(StringView filename)
return open_temp_file(filename, buffer);
}
-void write_buffer_to_file(Buffer& buffer, StringView filename,
+void write_buffer_to_file(const Context& context, Buffer& buffer, StringView filename,
WriteMethod method, WriteFlags flags)
{
auto zfilename = filename.zstr();
@@ -353,7 +370,7 @@ void write_buffer_to_file(Buffer& buffer, StringView filename,
char temp_filename[PATH_MAX];
const int fd = replace ? open_temp_file(filename, temp_filename)
- : open(zfilename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
+ : create_file(context, zfilename);
if (fd == -1)
{
auto saved_errno = errno;