@@ -34,6 +34,7 @@ use std::fs;
3434use std:: io:: { Error as IoError , ErrorKind as IoErrorKind , Read , Write } ;
3535use std:: path:: Path ;
3636use std:: sync:: atomic;
37+ use std:: { thread, time} ;
3738use std:: time:: { Duration , Instant } ;
3839
3940// File where the peers are stored.
@@ -179,9 +180,29 @@ impl NetworkState {
179180 file {:?}", path) ;
180181 PeersStorage :: Json ( peerstore)
181182 } else {
182- warn ! ( target: "sub-libp2p" , "Failed to open peer storage {:?} \
183- ; peers won't be saved", path) ;
184- PeersStorage :: Memory ( MemoryPeerstore :: empty ( ) )
183+ warn ! ( target: "sub-libp2p" , "Failed to open peer storage {:?}\
184+ ; peers file will be reset", path) ;
185+ fs:: remove_file ( & path) . expect ( "Failed deleting peers.json" ) ;
186+
187+ // we check for about 1s if the file was really deleted and move on
188+ for _x in 0 ..200 {
189+ if !Path :: new ( & path) . exists ( ) {
190+ break ;
191+ } else {
192+ debug ! ( "Waiting for effective deletion of invalid/outdate \
193+ peers.json") ;
194+ thread:: sleep ( time:: Duration :: from_millis ( 5 ) ) ;
195+ }
196+ }
197+
198+ if let Ok ( peerstore) = JsonPeerstore :: new ( path. clone ( ) ) {
199+ debug ! ( "peers.json reset" ) ;
200+ PeersStorage :: Json ( peerstore)
201+ } else {
202+ warn ! ( target: "sub-libp2p" , "Failed to reset peer storage\
203+ {:?}; peers change will not be saved", path) ;
204+ PeersStorage :: Memory ( MemoryPeerstore :: empty ( ) )
205+ }
185206 }
186207 } else {
187208 debug ! ( target: "sub-libp2p" , "No peers file configured ; peers \
@@ -559,7 +580,7 @@ impl NetworkState {
559580 /// You must pass an `UnboundedSender` which will be used by the `send`
560581 /// method. Actually sending the data is not covered by this code.
561582 ///
562- /// The various methods of the `NetworkState` that close a connection do
583+ /// The various methods of the `NetworkState` that close a connection do
563584 /// so by dropping this sender.
564585 pub fn custom_proto (
565586 & self ,
@@ -826,7 +847,7 @@ fn parse_and_add_to_peerstore(addr_str: &str, peerstore: &PeersStorage)
826847 . peer_or_create ( & peer_id)
827848 . add_addr ( addr, Duration :: from_secs ( 100000 * 365 * 24 * 3600 ) ) ,
828849 }
829-
850+
830851 Ok ( peer_id)
831852}
832853
0 commit comments