Pipe filesystem writes in `lightning-persister` through `BufWriter`
[rust-lightning] / lightning-persister / src / util.rs
index 1825980ad891fcb257cdaf956150a526ac5e326d..f26296794c6858a28fc4aa5bce46055a55657cb6 100644 (file)
@@ -3,6 +3,7 @@ extern crate winapi;
 
 use std::fs;
 use std::path::{Path, PathBuf};
+use std::io::{BufWriter, Write};
 
 #[cfg(not(target_os = "windows"))]
 use std::os::unix::io::AsRawFd;
@@ -14,7 +15,7 @@ use {
 };
 
 pub(crate) trait DiskWriteable {
-       fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error>;
+       fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), std::io::Error>;
 }
 
 pub(crate) fn get_full_filepath(mut filepath: PathBuf, filename: String) -> String {
@@ -52,9 +53,9 @@ pub(crate) fn write_to_file<D: DiskWriteable>(path: PathBuf, filename: String, d
        {
                // Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use
                // rust stdlib 1.36 or higher.
-               let mut f = fs::File::create(&tmp_filename)?;
-               data.write_to_file(&mut f)?;
-               f.sync_all()?;
+               let mut buf = BufWriter::new(fs::File::create(&tmp_filename)?);
+               data.write_to_file(&mut buf)?;
+               buf.into_inner()?.sync_all()?;
        }
        // Fsync the parent directory on Unix.
        #[cfg(not(target_os = "windows"))]
@@ -95,7 +96,7 @@ mod tests {
 
        struct TestWriteable{}
        impl DiskWriteable for TestWriteable {
-               fn write_to_file(&self, writer: &mut fs::File) -> Result<(), io::Error> {
+               fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
                        writer.write_all(&[42; 1])
                }
        }
@@ -135,7 +136,7 @@ mod tests {
                // Create the channel data file and make it a directory.
                fs::create_dir_all(get_full_filepath(path.clone(), filename.to_string())).unwrap();
                match write_to_file(path.clone(), filename.to_string(), &test_writeable) {
-                       Err(e) => assert_eq!(e.kind(), io::ErrorKind::Other),
+                       Err(e) => assert_eq!(e.raw_os_error(), Some(libc::EISDIR)),
                        _ => panic!("Unexpected Ok(())")
                }
                fs::remove_dir_all(path).unwrap();
@@ -145,7 +146,7 @@ mod tests {
        fn test_diskwriteable_failure() {
                struct FailingWriteable {}
                impl DiskWriteable for FailingWriteable {
-                       fn write_to_file(&self, _writer: &mut fs::File) -> Result<(), std::io::Error> {
+                       fn write_to_file<W: Write>(&self, _writer: &mut W) -> Result<(), std::io::Error> {
                                Err(std::io::Error::new(std::io::ErrorKind::Other, "expected failure"))
                        }
                }
@@ -178,7 +179,7 @@ mod tests {
                match write_to_file(path, filename, &test_writeable) {
                        Err(e) => {
                                #[cfg(not(target_os = "windows"))]
-                               assert_eq!(e.kind(), io::ErrorKind::Other);
+                               assert_eq!(e.raw_os_error(), Some(libc::EISDIR));
                                #[cfg(target_os = "windows")]
                                assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
                        }