@@ -3,12 +3,15 @@ use crate::{Config, Error, FilesystemLogger, NetworkGraph, Scorer};
33
44use lightning:: routing:: scoring:: { ProbabilisticScorer , ProbabilisticScoringParameters } ;
55use lightning:: util:: ser:: { Readable , ReadableArgs } ;
6+ use lightning_persister:: FilesystemPersister ;
67
78use rand:: { thread_rng, RngCore } ;
89
910use std:: fs;
1011use std:: io:: { BufReader , Write } ;
12+ use std:: os:: unix:: io:: AsRawFd ;
1113use std:: path:: Path ;
14+ use std:: path:: PathBuf ;
1215use std:: sync:: Arc ;
1316
1417pub ( crate ) fn read_or_generate_seed_file ( config : & Config ) -> [ u8 ; 32 ] {
@@ -84,3 +87,37 @@ pub(crate) fn read_payment_info(config: &Config) -> Vec<PaymentInfo> {
8487
8588 payments
8689}
90+
91+ /// Provides an interface that allows a previously persisted key to be unpersisted.
92+ pub trait KVStoreUnpersister {
93+ /// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
94+ /// Returns `true` if the key was present, and `false` otherwise.
95+ fn unpersist ( & self , key : & str ) -> std:: io:: Result < bool > ;
96+ }
97+
98+ impl KVStoreUnpersister for FilesystemPersister {
99+ fn unpersist ( & self , key : & str ) -> std:: io:: Result < bool > {
100+ let mut dest_file = PathBuf :: from ( self . get_data_dir ( ) ) ;
101+ dest_file. push ( key) ;
102+
103+ if !dest_file. is_file ( ) {
104+ return Ok ( false ) ;
105+ }
106+
107+ fs:: remove_file ( & dest_file) ?;
108+ let parent_directory = dest_file. parent ( ) . unwrap ( ) ;
109+ let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( parent_directory) ?;
110+ #[ cfg( not( target_os = "windows" ) ) ]
111+ {
112+ unsafe {
113+ libc:: fsync ( dir_file. as_raw_fd ( ) ) ;
114+ }
115+ }
116+
117+ if dest_file. is_file ( ) {
118+ return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "Unpersisting key failed" ) ) ;
119+ }
120+
121+ return Ok ( true ) ;
122+ }
123+ }
0 commit comments