From 21eb7b3790b843b7e4db6c2ac2acedbb5c78eee5 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 15 Jun 2025 23:43:03 -0400 Subject: block until whatever program is writing the file finishes --- buffer.c | 16 +++++++++++++++- os.h | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/buffer.c b/buffer.c index 9591e35..b728115 100644 --- a/buffer.c +++ b/buffer.c @@ -3192,7 +3192,21 @@ void buffer_reload(TextBuffer *buffer) { bool buffer_externally_changed(TextBuffer *buffer) { if (!buffer_is_named_file(buffer)) return false; - return buffer->last_write_time != timespec_to_seconds(time_last_modified(buffer->path)); + double last_modified = timespec_to_seconds(time_last_modified(buffer->path)); + if (last_modified == buffer->last_write_time) + return false; + + // block until whatever program is writing the file finishes + for (int i = 0; i < 10; i++) { // give up after 200ms + time_sleep_ms(20); + double new_last_modified = timespec_to_seconds(time_last_modified(buffer->path)); + if (new_last_modified == last_modified) { + // probably done now + break; + } + last_modified = new_last_modified; + } + return true; } void buffer_new_file(TextBuffer *buffer, const char *path) { diff --git a/os.h b/os.h index 23e94f2..b0be1c2 100644 --- a/os.h +++ b/os.h @@ -85,6 +85,11 @@ static double time_get_seconds(void) { + (double)t.tv_nsec * 1e-9; } +static void time_sleep_ms(double ms) { + if (ms <= 0) return; + time_sleep_ns((u64)(ms * 1000000)); +} + /// sleep for seconds static void time_sleep_seconds(double s) { if (s <= 0) return; -- cgit v1.2.3