Fixed file access bug, added fail-safe access

This commit is contained in:
faragher 2024-10-19 12:39:48 -05:00
parent 30248854ed
commit 8445417661
1 changed files with 12 additions and 14 deletions

View File

@ -155,9 +155,9 @@ class Identity:
storage_known_destinations = {} storage_known_destinations = {}
if os.path.isfile(RNS.Reticulum.storagepath+"/known_destinations"): if os.path.isfile(RNS.Reticulum.storagepath+"/known_destinations"):
try: try:
file = open(RNS.Reticulum.storagepath+"/known_destinations","rb") with open(RNS.Reticulum.storagepath+"/known_destinations","rb") as file:
storage_known_destinations = umsgpack.load(file) storage_known_destinations = umsgpack.load(file)
file.close()
except: except:
pass pass
@ -169,9 +169,9 @@ class Identity:
RNS.log("Skipped recombining known destinations from disk, since an error occurred: "+str(e), RNS.LOG_WARNING) 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_DEBUG) RNS.log("Saving "+str(len(Identity.known_destinations))+" known destinations to storage...", RNS.LOG_DEBUG)
file = open(RNS.Reticulum.storagepath+"/known_destinations","wb") with open(RNS.Reticulum.storagepath+"/known_destinations","wb") as file:
umsgpack.dump(Identity.known_destinations, file) umsgpack.dump(Identity.known_destinations, file)
file.close()
save_time = time.time() - save_start save_time = time.time() - save_start
if save_time < 1: if save_time < 1:
@ -191,9 +191,8 @@ class Identity:
def load_known_destinations(): def load_known_destinations():
if os.path.isfile(RNS.Reticulum.storagepath+"/known_destinations"): if os.path.isfile(RNS.Reticulum.storagepath+"/known_destinations"):
try: try:
file = open(RNS.Reticulum.storagepath+"/known_destinations","rb") with open(RNS.Reticulum.storagepath+"/known_destinations","rb") as file:
loaded_known_destinations = umsgpack.load(file) loaded_known_destinations = umsgpack.load(file)
file.close()
Identity.known_destinations = {} Identity.known_destinations = {}
for known_destination in loaded_known_destinations: for known_destination in loaded_known_destinations:
@ -285,9 +284,8 @@ class Identity:
outpath = f"{ratchetdir}/{hexhash}.out" outpath = f"{ratchetdir}/{hexhash}.out"
finalpath = f"{ratchetdir}/{hexhash}" finalpath = f"{ratchetdir}/{hexhash}"
ratchet_file = open(outpath, "wb") with open(outpath, "wb") as ratchet_file:
ratchet_file.write(umsgpack.packb(ratchet_data)) ratchet_file.write(umsgpack.packb(ratchet_data))
ratchet_file.close()
os.replace(outpath, finalpath) os.replace(outpath, finalpath)
@ -331,7 +329,7 @@ class Identity:
ratchet_path = f"{ratchetdir}/{hexhash}" ratchet_path = f"{ratchetdir}/{hexhash}"
if os.path.isfile(ratchet_path): if os.path.isfile(ratchet_path):
try: try:
ratchet_file = open(ratchet_path, "rb") with open(ratchet_path, "rb") as ratchet_file:
ratchet_data = umsgpack.unpackb(ratchet_file.read()) ratchet_data = umsgpack.unpackb(ratchet_file.read())
if time.time() < ratchet_data["received"]+Identity.RATCHET_EXPIRY and len(ratchet_data["ratchet"]) == Identity.RATCHETSIZE//8: if time.time() < ratchet_data["received"]+Identity.RATCHET_EXPIRY and len(ratchet_data["ratchet"]) == Identity.RATCHETSIZE//8:
Identity.known_ratchets[destination_hash] = ratchet_data["ratchet"] Identity.known_ratchets[destination_hash] = ratchet_data["ratchet"]