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