diff --git a/RNS/Identity.py b/RNS/Identity.py index e2d1b6bb..78b7a7ed 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -212,8 +212,17 @@ class Identity: RNS.log("Skipped recombining known destinations from disk, since an error occurred: "+str(e), RNS.LOG_WARNING) RNS.log("Saving "+str(len(Identity.known_destinations))+" known destinations to storage...", RNS.LOG_VERBOSE) - with open(RNS.Reticulum.storagepath+"/known_destinations","wb") as file: - umsgpack.dump(Identity.known_destinations.copy(), file) + temp_file = RNS.Reticulum.storagepath+f"/known_destinations.tmp.{time.time()}" + + try: + with open(temp_file,"wb") as file: umsgpack.dump(Identity.known_destinations.copy(), file) + os.rename(temp_file, RNS.Reticulum.storagepath+f"/known_destinations") + + except Exception as e: + RNS.log(f"Error while serializing and writing known destinations: {e}", RNS.LOG_ERROR) + try: os.unlink(temp_file) + except Exception as e: RNS.log(f"Could not clean up temporary file {temp_file}: {e}", RNS.LOG_WARNING) + raise e save_time = time.time() - save_start if save_time < 1: time_str = str(round(save_time*1000,2))+"ms"