Check ratchet dir exists before cleaning

This commit is contained in:
Mark Qvist 2024-09-05 15:58:54 +02:00
parent 6a392fdb0f
commit 971f5ffadd

View File

@ -249,7 +249,7 @@ class Identity:
@staticmethod @staticmethod
def _remember_ratchet(destination_hash, ratchet): def _remember_ratchet(destination_hash, ratchet):
# TODO: Remove at some point # TODO: Remove at some point, and only log new ratchets
RNS.log(f"Remembering ratchet {RNS.prettyhexrep(Identity.truncated_hash(ratchet))} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_EXTREME) RNS.log(f"Remembering ratchet {RNS.prettyhexrep(Identity.truncated_hash(ratchet))} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_EXTREME)
try: try:
Identity.known_ratchets[destination_hash] = ratchet Identity.known_ratchets[destination_hash] = ratchet
@ -286,20 +286,21 @@ class Identity:
try: try:
now = time.time() now = time.time()
ratchetdir = RNS.Reticulum.storagepath+"/ratchets" ratchetdir = RNS.Reticulum.storagepath+"/ratchets"
for filename in os.listdir(ratchetdir): if os.path.isdir(ratchetdir):
try: for filename in os.listdir(ratchetdir):
expired = False try:
with open(f"{ratchetdir}/{filename}", "rb") as rf: expired = False
ratchet_data = umsgpack.unpackb(rf.read()) with open(f"{ratchetdir}/{filename}", "rb") as rf:
if now > ratchet_data["received"]+Identity.RATCHET_EXPIRY: ratchet_data = umsgpack.unpackb(rf.read())
expired = True if now > ratchet_data["received"]+Identity.RATCHET_EXPIRY:
expired = True
if expired: if expired:
os.unlink(f"{ratchetdir}/{filename}") os.unlink(f"{ratchetdir}/{filename}")
except Exception as e: except Exception as e:
RNS.log(f"An error occurred while cleaning ratchets, in the processing of {ratchetdir}/{filename}.", RNS.LOG_ERROR) RNS.log(f"An error occurred while cleaning ratchets, in the processing of {ratchetdir}/{filename}.", RNS.LOG_ERROR)
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR) RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
except Exception as e: except Exception as e:
RNS.log(f"An error occurred while cleaning ratchets. The contained exception was: {e}", RNS.LOG_ERROR) RNS.log(f"An error occurred while cleaning ratchets. The contained exception was: {e}", RNS.LOG_ERROR)