diff options
| author | Enrico Borba <enricozb@users.noreply.github.com> | 2024-12-23 09:23:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-23 09:23:58 +0100 |
| commit | 52125e6336d596aebdd4da91080b3178ddca7449 (patch) | |
| tree | 27d3e5c01660d567f22fee621c97753f294256b0 /src/file.cc | |
| parent | 14cb35f62b36b2f1aa530adb5e31c05ff1347bfc (diff) | |
| parent | 9c458c50661446fc6e7295787b06422137af099d (diff) | |
Merge branch 'master' into enricozb/daemon-stdin
Diffstat (limited to 'src/file.cc')
| -rw-r--r-- | src/file.cc | 31 |
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; |
