diff --git a/src/event.rs b/src/event.rs index d6d5a1069..c8ab989c7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -3,7 +3,7 @@ use crate::{ PaymentInfo, PaymentInfoStorage, PaymentStatus, Wallet, }; -use crate::io_utils::KVStoreUnpersister; +use crate::io::KVStoreUnpersister; use crate::logger::{log_error, log_info, Logger}; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; diff --git a/src/io/mod.rs b/src/io/mod.rs new file mode 100644 index 000000000..680ca9cd6 --- /dev/null +++ b/src/io/mod.rs @@ -0,0 +1,47 @@ +pub(crate) mod utils; + +use lightning_persister::FilesystemPersister; + +use std::fs; +use std::os::unix::io::AsRawFd; +use std::path::PathBuf; + +/// Provides an interface that allows a previously persisted key to be unpersisted. +pub trait KVStoreUnpersister { + /// Unpersist (i.e., remove) the writeable previously persisted under the provided key. + /// Returns `true` if the key was present, and `false` otherwise. + fn unpersist(&self, key: &str) -> std::io::Result; +} + +impl KVStoreUnpersister for FilesystemPersister { + fn unpersist(&self, key: &str) -> std::io::Result { + let mut dest_file = PathBuf::from(self.get_data_dir()); + dest_file.push(key); + + if !dest_file.is_file() { + return Ok(false); + } + + fs::remove_file(&dest_file)?; + #[cfg(not(target_os = "windows"))] + { + let parent_directory = dest_file.parent().unwrap(); + let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?; + unsafe { + // The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes + // to the inode might get cached (and hence possibly lost on crash), depending on + // the target platform and file system. + // + // In order to assert we permanently removed the file in question we therefore + // call `fsync` on the parent directory on platforms that support it, + libc::fsync(dir_file.as_raw_fd()); + } + } + + if dest_file.is_file() { + return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed")); + } + + return Ok(true); + } +} diff --git a/src/io_utils.rs b/src/io/utils.rs similarity index 65% rename from src/io_utils.rs rename to src/io/utils.rs index b76e514e3..c7acaef88 100644 --- a/src/io_utils.rs +++ b/src/io/utils.rs @@ -3,15 +3,12 @@ use crate::{Config, FilesystemLogger, NetworkGraph, Scorer, WALLET_KEYS_SEED_LEN use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters}; use lightning::util::ser::{Readable, ReadableArgs}; -use lightning_persister::FilesystemPersister; use rand::{thread_rng, RngCore}; use std::fs; use std::io::{BufReader, Write}; -use std::os::unix::io::AsRawFd; use std::path::Path; -use std::path::PathBuf; use std::sync::Arc; 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 { payments } - -/// Provides an interface that allows a previously persisted key to be unpersisted. -pub trait KVStoreUnpersister { - /// Unpersist (i.e., remove) the writeable previously persisted under the provided key. - /// Returns `true` if the key was present, and `false` otherwise. - fn unpersist(&self, key: &str) -> std::io::Result; -} - -impl KVStoreUnpersister for FilesystemPersister { - fn unpersist(&self, key: &str) -> std::io::Result { - let mut dest_file = PathBuf::from(self.get_data_dir()); - dest_file.push(key); - - if !dest_file.is_file() { - return Ok(false); - } - - fs::remove_file(&dest_file)?; - #[cfg(not(target_os = "windows"))] - { - let parent_directory = dest_file.parent().unwrap(); - let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?; - unsafe { - // The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes - // to the inode might get cached (and hence possibly lost on crash), depending on - // the target platform and file system. - // - // In order to assert we permanently removed the file in question we therefore - // call `fsync` on the parent directory on platforms that support it, - libc::fsync(dir_file.as_raw_fd()); - } - } - - if dest_file.is_file() { - return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed")); - } - - return Ok(true); - } -} diff --git a/src/lib.rs b/src/lib.rs index 37f47617f..3fd8f51a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ mod error; mod event; mod hex_utils; -mod io_utils; +mod io; mod logger; mod payment_store; mod peer_store; @@ -245,13 +245,13 @@ impl Builder { match entropy_source { WalletEntropySource::SeedBytes(bytes) => bytes.clone(), WalletEntropySource::SeedFile(seed_path) => { - io_utils::read_or_generate_seed_file(seed_path) + io::utils::read_or_generate_seed_file(seed_path) } } } else { // Default to read or generate from the default location generate a seed file. let seed_path = format!("{}/keys_seed", config.storage_dir_path); - io_utils::read_or_generate_seed_file(&seed_path) + io::utils::read_or_generate_seed_file(&seed_path) }; let xprv = bitcoin::util::bip32::ExtendedPrivKey::new_master(config.network, &seed_bytes) @@ -313,8 +313,8 @@ impl Builder { // Initialize the network graph, scorer, and router let network_graph = - Arc::new(io_utils::read_network_graph(config.as_ref(), Arc::clone(&logger))); - let scorer = Arc::new(Mutex::new(io_utils::read_scorer( + Arc::new(io::utils::read_network_graph(config.as_ref(), Arc::clone(&logger))); + let scorer = Arc::new(Mutex::new(io::utils::read_scorer( config.as_ref(), Arc::clone(&network_graph), Arc::clone(&logger), @@ -423,7 +423,7 @@ impl Builder { )); // Init payment info storage - let payments = io_utils::read_payment_info(config.as_ref()); + let payments = io::utils::read_payment_info(config.as_ref()); let payment_store = Arc::new(PaymentInfoStorage::from_payments(payments, Arc::clone(&persister))); diff --git a/src/payment_store.rs b/src/payment_store.rs index c4ca2d231..e46aa991b 100644 --- a/src/payment_store.rs +++ b/src/payment_store.rs @@ -1,5 +1,5 @@ use crate::hex_utils; -use crate::io_utils::KVStoreUnpersister; +use crate::io::KVStoreUnpersister; use crate::Error; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; diff --git a/src/tests/test_utils.rs b/src/tests/test_utils.rs index eb65ec3ce..1b9740183 100644 --- a/src/tests/test_utils.rs +++ b/src/tests/test_utils.rs @@ -1,4 +1,4 @@ -use crate::io_utils::KVStoreUnpersister; +use crate::io::KVStoreUnpersister; use lightning::util::persist::KVStorePersister; use lightning::util::ser::Writeable;