Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe filesystem writes in lightning-persister through BufWriter #1404

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lightning-persister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use lightning::ln::channelmanager::ChannelManager;
use lightning::util::logger::Logger;
use lightning::util::ser::{ReadableArgs, Writeable};
use std::fs;
use std::io::{Cursor, Error};
use std::io::{Cursor, Error, Write};
use std::ops::Deref;
use std::path::{Path, PathBuf};

Expand All @@ -49,7 +49,7 @@ pub struct FilesystemPersister {
}

impl<Signer: Sign> DiskWriteable for ChannelMonitor<Signer> {
fn write_to_file(&self, writer: &mut fs::File) -> Result<(), Error> {
fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
self.write(writer)
}
}
Expand All @@ -62,13 +62,13 @@ where
F::Target: FeeEstimator,
L::Target: Logger,
{
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> {
self.write(writer)
}
}

impl DiskWriteable for NetworkGraph {
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> {
self.write(writer)
}
}
Expand Down
13 changes: 7 additions & 6 deletions lightning-persister/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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"))]
Expand Down Expand Up @@ -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])
}
}
Expand Down Expand Up @@ -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"))
}
}
Expand Down