From a37f845ebbea35cefc43f3efbb681c0428ffec78 Mon Sep 17 00:00:00 2001
From: Roey Darwish Dror <roey.ghost@gmail.com>
Date: Thu, 1 Nov 2018 22:05:34 +0200
Subject: [PATCH] Add sync (fix #959)

---
 CHANGELOG.md  |  2 ++
 src/unistd.rs | 14 ++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd76763d1d..6688c8f8ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
 ## [Unreleased]
 
 ### Added
+- Added a `sync` wrapper.
+  ([#961](https://github.com/nix-rust/nix/pull/961))
 - Added a `sysinfo` wrapper.
   ([#922](https://github.com/nix-rust/nix/pull/922))
 - Support the `SO_PEERCRED` socket option and the `UnixCredentials` type on all Linux and Android targets.
diff --git a/src/unistd.rs b/src/unistd.rs
index c9c129c974..aadd3c6bd4 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -1101,6 +1101,20 @@ pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
     Errno::result(res).map(drop)
 }
 
+/// Commit filesystem caches to disk
+///
+/// See also [sync(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sync.html)
+#[cfg(any(
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "linux",
+    target_os = "netbsd",
+    target_os = "openbsd"
+))]
+pub fn sync() -> () {
+    unsafe { libc::sync() };
+}
+
 /// Synchronize changes to a file
 ///
 /// See also [fsync(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html)