Skip to content

Commit c1b9ef8

Browse files
committed
Reorganize IO things into io submodules
1 parent 53e0ca5 commit c1b9ef8

File tree

6 files changed

+56
-52
lines changed

6 files changed

+56
-52
lines changed

src/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
PaymentInfo, PaymentInfoStorage, PaymentStatus, Wallet,
44
};
55

6-
use crate::io_utils::KVStoreUnpersister;
6+
use crate::io::KVStoreUnpersister;
77
use crate::logger::{log_error, log_info, Logger};
88

99
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};

src/io/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub(crate) mod utils;
2+
3+
use lightning_persister::FilesystemPersister;
4+
5+
use std::fs;
6+
use std::os::unix::io::AsRawFd;
7+
use std::path::PathBuf;
8+
9+
/// Provides an interface that allows a previously persisted key to be unpersisted.
10+
pub trait KVStoreUnpersister {
11+
/// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
12+
/// Returns `true` if the key was present, and `false` otherwise.
13+
fn unpersist(&self, key: &str) -> std::io::Result<bool>;
14+
}
15+
16+
impl KVStoreUnpersister for FilesystemPersister {
17+
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
18+
let mut dest_file = PathBuf::from(self.get_data_dir());
19+
dest_file.push(key);
20+
21+
if !dest_file.is_file() {
22+
return Ok(false);
23+
}
24+
25+
fs::remove_file(&dest_file)?;
26+
#[cfg(not(target_os = "windows"))]
27+
{
28+
let parent_directory = dest_file.parent().unwrap();
29+
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
30+
unsafe {
31+
// The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes
32+
// to the inode might get cached (and hence possibly lost on crash), depending on
33+
// the target platform and file system.
34+
//
35+
// In order to assert we permanently removed the file in question we therefore
36+
// call `fsync` on the parent directory on platforms that support it,
37+
libc::fsync(dir_file.as_raw_fd());
38+
}
39+
}
40+
41+
if dest_file.is_file() {
42+
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed"));
43+
}
44+
45+
return Ok(true);
46+
}
47+
}

src/io_utils.rs renamed to src/io/utils.rs

-43
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ use crate::{Config, FilesystemLogger, NetworkGraph, Scorer, WALLET_KEYS_SEED_LEN
33

44
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
55
use lightning::util::ser::{Readable, ReadableArgs};
6-
use lightning_persister::FilesystemPersister;
76

87
use rand::{thread_rng, RngCore};
98

109
use std::fs;
1110
use std::io::{BufReader, Write};
12-
use std::os::unix::io::AsRawFd;
1311
use std::path::Path;
14-
use std::path::PathBuf;
1512
use std::sync::Arc;
1613

1714
pub(crate) fn read_or_generate_seed_file(keys_seed_path: &str) -> [u8; WALLET_KEYS_SEED_LEN] {
@@ -86,43 +83,3 @@ pub(crate) fn read_payment_info(config: &Config) -> Vec<PaymentInfo> {
8683

8784
payments
8885
}
89-
90-
/// Provides an interface that allows a previously persisted key to be unpersisted.
91-
pub trait KVStoreUnpersister {
92-
/// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
93-
/// Returns `true` if the key was present, and `false` otherwise.
94-
fn unpersist(&self, key: &str) -> std::io::Result<bool>;
95-
}
96-
97-
impl KVStoreUnpersister for FilesystemPersister {
98-
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
99-
let mut dest_file = PathBuf::from(self.get_data_dir());
100-
dest_file.push(key);
101-
102-
if !dest_file.is_file() {
103-
return Ok(false);
104-
}
105-
106-
fs::remove_file(&dest_file)?;
107-
#[cfg(not(target_os = "windows"))]
108-
{
109-
let parent_directory = dest_file.parent().unwrap();
110-
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
111-
unsafe {
112-
// The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes
113-
// to the inode might get cached (and hence possibly lost on crash), depending on
114-
// the target platform and file system.
115-
//
116-
// In order to assert we permanently removed the file in question we therefore
117-
// call `fsync` on the parent directory on platforms that support it,
118-
libc::fsync(dir_file.as_raw_fd());
119-
}
120-
}
121-
122-
if dest_file.is_file() {
123-
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed"));
124-
}
125-
126-
return Ok(true);
127-
}
128-
}

src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
mod error;
7070
mod event;
7171
mod hex_utils;
72-
mod io_utils;
72+
mod io;
7373
mod logger;
7474
mod payment_store;
7575
mod peer_store;
@@ -287,13 +287,13 @@ impl Builder {
287287
match entropy_source {
288288
WalletEntropySource::SeedBytes(bytes) => bytes.clone(),
289289
WalletEntropySource::SeedFile(seed_path) => {
290-
io_utils::read_or_generate_seed_file(seed_path)
290+
io::utils::read_or_generate_seed_file(seed_path)
291291
}
292292
}
293293
} else {
294294
// Default to read or generate from the default location generate a seed file.
295295
let seed_path = format!("{}/keys_seed", config.storage_dir_path);
296-
io_utils::read_or_generate_seed_file(&seed_path)
296+
io::utils::read_or_generate_seed_file(&seed_path)
297297
};
298298

299299
let xprv = bitcoin::util::bip32::ExtendedPrivKey::new_master(config.network, &seed_bytes)
@@ -355,8 +355,8 @@ impl Builder {
355355

356356
// Initialize the network graph, scorer, and router
357357
let network_graph =
358-
Arc::new(io_utils::read_network_graph(config.as_ref(), Arc::clone(&logger)));
359-
let scorer = Arc::new(Mutex::new(io_utils::read_scorer(
358+
Arc::new(io::utils::read_network_graph(config.as_ref(), Arc::clone(&logger)));
359+
let scorer = Arc::new(Mutex::new(io::utils::read_scorer(
360360
config.as_ref(),
361361
Arc::clone(&network_graph),
362362
Arc::clone(&logger),
@@ -465,7 +465,7 @@ impl Builder {
465465
));
466466

467467
// Init payment info storage
468-
let payments = io_utils::read_payment_info(config.as_ref());
468+
let payments = io::utils::read_payment_info(config.as_ref());
469469
let payment_store =
470470
Arc::new(PaymentInfoStorage::from_payments(payments, Arc::clone(&persister)));
471471

src/payment_store.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::hex_utils;
2-
use crate::io_utils::KVStoreUnpersister;
2+
use crate::io::KVStoreUnpersister;
33
use crate::Error;
44

55
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};

src/tests/test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::io_utils::KVStoreUnpersister;
1+
use crate::io::KVStoreUnpersister;
22
use lightning::util::persist::KVStorePersister;
33
use lightning::util::ser::Writeable;
44

0 commit comments

Comments
 (0)