@@ -5,11 +5,14 @@ use crate::{FilesystemLogger, LdkLiteConfig, NetworkGraph, Scorer};
55
66use lightning:: routing:: scoring:: { ProbabilisticScorer , ProbabilisticScoringParameters } ;
77use lightning:: util:: ser:: { Readable , ReadableArgs } ;
8+ use lightning_persister:: FilesystemPersister ;
89
910use rand:: { thread_rng, RngCore } ;
1011
1112use std:: fs;
1213use std:: io:: { BufReader , Write } ;
14+ use std:: os:: unix:: io:: AsRawFd ;
15+ use std:: path:: PathBuf ;
1316use std:: sync:: Arc ;
1417
1518pub ( crate ) fn read_or_generate_seed_file ( config : Arc < LdkLiteConfig > ) -> Result < [ u8 ; 32 ] , Error > {
@@ -83,3 +86,37 @@ pub(crate) fn read_payment_info(config: Arc<LdkLiteConfig>) -> Result<Vec<Paymen
8386
8487 Ok ( payments)
8588}
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+ let parent_directory = dest_file. parent ( ) . unwrap ( ) ;
108+ let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( parent_directory) ?;
109+ #[ cfg( not( target_os = "windows" ) ) ]
110+ {
111+ unsafe {
112+ libc:: fsync ( dir_file. as_raw_fd ( ) ) ;
113+ }
114+ }
115+
116+ if dest_file. is_file ( ) {
117+ return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "Unpersisting key failed" ) ) ;
118+ }
119+
120+ return Ok ( true ) ;
121+ }
122+ }
0 commit comments