summaryrefslogtreecommitdiff
path: root/src/file.cc
diff options
context:
space:
mode:
authorFrank LENORMAND <lenormf@gmail.com>2018-07-04 09:49:53 +0300
committerFrank LENORMAND <lenormf@gmail.com>2018-07-04 09:49:53 +0300
commit5309b7b5e53c6d742a779bc82dcafe815de0dd56 (patch)
tree0785e39cd7f12650426be69e6b91e33a818cfb44 /src/file.cc
parenta13e36b4b7034af9085dbf5a69ab99ba2443030f (diff)
src: Add a `-sync` flag to the write commands
Diffstat (limited to 'src/file.cc')
-rw-r--r--src/file.cc9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/file.cc b/src/file.cc
index 8d067d6c..a71bdb55 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -257,7 +257,7 @@ void write(int fd, StringView data)
}
}
-void write_buffer_to_fd(Buffer& buffer, int fd)
+void write_buffer_to_fd(Buffer& buffer, int fd, bool sync)
{
auto eolformat = buffer.options()["eolformat"].get<EolFormat>();
StringView eoldata;
@@ -278,9 +278,12 @@ void write_buffer_to_fd(Buffer& buffer, int fd)
write(fd, linedata.substr(0, linedata.length()-1));
write(fd, eoldata);
}
+
+ if (sync)
+ ::fsync(fd);
}
-void write_buffer_to_file(Buffer& buffer, StringView filename, bool force)
+void write_buffer_to_file(Buffer& buffer, StringView filename, bool force, bool sync)
{
struct stat st;
auto zfilename = filename.zstr();
@@ -306,7 +309,7 @@ void write_buffer_to_file(Buffer& buffer, StringView filename, bool force)
{
auto close_fd = on_scope_end([fd]{ close(fd); });
- write_buffer_to_fd(buffer, fd);
+ write_buffer_to_fd(buffer, fd, sync);
}
if ((buffer.flags() & Buffer::Flags::File) and