Skip to content

Commit 83595db

Browse files
authored
Merge pull request #1404 from TheBlueMatt/2022-04-buf-writes
Pipe filesystem writes in `lightning-persister` through `BufWriter`
2 parents dc1b36d + 7e93fdb commit 83595db

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lightning-persister/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use lightning::ln::channelmanager::ChannelManager;
2828
use lightning::util::logger::Logger;
2929
use lightning::util::ser::{ReadableArgs, Writeable};
3030
use std::fs;
31-
use std::io::{Cursor, Error};
31+
use std::io::{Cursor, Error, Write};
3232
use std::ops::Deref;
3333
use std::path::{Path, PathBuf};
3434

@@ -49,7 +49,7 @@ pub struct FilesystemPersister {
4949
}
5050

5151
impl<Signer: Sign> DiskWriteable for ChannelMonitor<Signer> {
52-
fn write_to_file(&self, writer: &mut fs::File) -> Result<(), Error> {
52+
fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
5353
self.write(writer)
5454
}
5555
}
@@ -62,13 +62,13 @@ where
6262
F::Target: FeeEstimator,
6363
L::Target: Logger,
6464
{
65-
fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error> {
65+
fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), std::io::Error> {
6666
self.write(writer)
6767
}
6868
}
6969

7070
impl DiskWriteable for NetworkGraph {
71-
fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error> {
71+
fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), std::io::Error> {
7272
self.write(writer)
7373
}
7474
}

lightning-persister/src/util.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate winapi;
33

44
use std::fs;
55
use std::path::{Path, PathBuf};
6+
use std::io::{BufWriter, Write};
67

78
#[cfg(not(target_os = "windows"))]
89
use std::os::unix::io::AsRawFd;
@@ -14,7 +15,7 @@ use {
1415
};
1516

1617
pub(crate) trait DiskWriteable {
17-
fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error>;
18+
fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), std::io::Error>;
1819
}
1920

2021
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
5253
{
5354
// Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use
5455
// rust stdlib 1.36 or higher.
55-
let mut f = fs::File::create(&tmp_filename)?;
56-
data.write_to_file(&mut f)?;
57-
f.sync_all()?;
56+
let mut buf = BufWriter::new(fs::File::create(&tmp_filename)?);
57+
data.write_to_file(&mut buf)?;
58+
buf.into_inner()?.sync_all()?;
5859
}
5960
// Fsync the parent directory on Unix.
6061
#[cfg(not(target_os = "windows"))]
@@ -95,7 +96,7 @@ mod tests {
9596

9697
struct TestWriteable{}
9798
impl DiskWriteable for TestWriteable {
98-
fn write_to_file(&self, writer: &mut fs::File) -> Result<(), io::Error> {
99+
fn write_to_file<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
99100
writer.write_all(&[42; 1])
100101
}
101102
}
@@ -145,7 +146,7 @@ mod tests {
145146
fn test_diskwriteable_failure() {
146147
struct FailingWriteable {}
147148
impl DiskWriteable for FailingWriteable {
148-
fn write_to_file(&self, _writer: &mut fs::File) -> Result<(), std::io::Error> {
149+
fn write_to_file<W: Write>(&self, _writer: &mut W) -> Result<(), std::io::Error> {
149150
Err(std::io::Error::new(std::io::ErrorKind::Other, "expected failure"))
150151
}
151152
}

0 commit comments

Comments
 (0)