Skip to content

Commit 261f13d

Browse files
committed
Allow keys to be unpersisted
Previously the `KVStorePersister` only allowed to `persist` a `Writeables` under a given key. However, we may want to be able to remove this set key after the fact. Here we therefore extend the trait with a `unpersist` method and implement it for `FilesystemPersister`.
1 parent 301efc8 commit 261f13d

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,14 @@ mod tests {
717717

718718
self.filesystem_persister.persist(key, object)
719719
}
720+
721+
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
722+
if key == "manager" || key == "network_graph" || key == "scorer" {
723+
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Refusing to unpersist this key"));
724+
}
725+
726+
self.filesystem_persister.unpersist(key)
727+
}
720728
}
721729

722730
fn get_full_filepath(filepath: String, filename: String) -> String {

lightning-persister/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ impl KVStorePersister for FilesystemPersister {
128128
dest_file.push(key);
129129
util::write_to_file(dest_file, object)
130130
}
131+
132+
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
133+
let mut dest_file = PathBuf::from(self.path_to_channel_data.clone());
134+
dest_file.push(key);
135+
util::delete_file(dest_file)
136+
}
131137
}
132138

133139
#[cfg(test)]

lightning-persister/src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ pub(crate) fn write_to_file<W: Writeable>(dest_file: PathBuf, data: &W) -> std::
7777
Ok(())
7878
}
7979

80+
pub(crate) fn delete_file(dest_file: PathBuf) -> std::io::Result<bool> {
81+
if !dest_file.is_file() {
82+
return Ok(false)
83+
}
84+
85+
fs::remove_file(&dest_file)?;
86+
let parent_directory = dest_file.parent().unwrap();
87+
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
88+
unsafe { libc::fsync(dir_file.as_raw_fd()); }
89+
90+
if dest_file.is_file() {
91+
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed"));
92+
}
93+
94+
return Ok(true)
95+
}
96+
8097
#[cfg(test)]
8198
mod tests {
8299
use lightning::util::ser::{Writer, Writeable};

lightning/src/util/persist.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ use super::{logger::Logger, ser::Writeable};
2121
/// and [`Persist`] traits. It uses "manager", "network_graph",
2222
/// and "monitors/{funding_txo_id}_{funding_txo_index}" for keys.
2323
pub trait KVStorePersister {
24-
/// Persist the given writeable using the provided key
24+
/// Persist the given writeable using the provided key.
2525
fn persist<W: Writeable>(&self, key: &str, object: &W) -> io::Result<()>;
26+
27+
/// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
28+
/// Returns `true` if the key was present, and `false` otherwise.
29+
fn unpersist(&self, key: &str) -> io::Result<bool>;
2630
}
2731

2832
/// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk.

0 commit comments

Comments
 (0)