From ffaea9421bff787670664e772387a84602ba4365 Mon Sep 17 00:00:00 2001 From: hcorse Date: Wed, 29 Oct 2025 14:58:57 -0500 Subject: [PATCH] Adding check state and working toward a recovering script --- rdify | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/rdify b/rdify index cd45bb6..804248c 100755 --- a/rdify +++ b/rdify @@ -22,6 +22,28 @@ class RamDiskItem(): self.rsync_destination = self.full_persist_path if self.target.is_file() else self.persist + def check_state(self): + path_states = ( + self.target.is_symlink(), # Is the target a symlink? + self.target.resolve().exists(), # Does the symlink resolve to something that exists? + self.target.resolve() == self.full_rd_path, # Does the symlink point to the ramdisk location? + self.full_persist_path.exists() # Does the persistent copy exist? + ) + match path_states: + case (False, True, False, False): + return "UNINITIALIZED" # Normal file/dir, not ramdiskified + case (True, True, True, True): + return "RAMDISKIFIED" # Symlink to ramdisk, persistent copy exists + case (True, True, True, False): + return "NONPERSISTENT" # Symlink to ramdisk, persistent copy exists + case (True, False, _, True): + return "PERSIST_ONLY" # Symlink broken, persistent copy exists + case (True, False, _, False): + return "UNRECOVERABLE" # Symlink broken, persistent copy missing + case _: + eprint("Unhandled state:", path_states) + + def init(self): self.persist.mkdir(parents=True, exist_ok=True) self.ramdisk.mkdir(parents=True, exist_ok=True) @@ -45,6 +67,7 @@ class RamDiskItem(): self.sync() time.sleep(delay) except KeyboardInterrupt: + self.sync() eprint("Interrupt received, exiting...") return @@ -72,7 +95,9 @@ def main(): action.add_argument("-l", "--loop_sync", type=int, help="Starts a repeating sync that loops every LOOP_SYNC seconds") action.add_argument("-c", "--cleanup", action="store_true", - help="clean up an existing ramdiskified target") + help="Clean up an existing ramdiskified target") + action.add_argument("-d", "--diagnose", action="store_true", + help="Check the current state") # Arguments to specify what to act on parser.add_argument("target", help="The target file/directory to ramdiskify") parser.add_argument("persist", nargs="?", default=".persist", @@ -80,19 +105,24 @@ def main(): args = parser.parse_args() - if not Path(args.target).exists(): - eprint(f"Target: {args.target} doesn't exist!") - return + # if not Path(args.target).exists(): + # eprint(f"Target: {args.target} doesn't exist!") + # return rd_item = RamDiskItem(args.target, args.persist) + if args.diagnose: + state = rd_item.check_state() + print(f"State of {args.target}: {state}") + return + if args.init: rd_item.init() - elif args.sync: + if args.sync: rd_item.sync() - elif args.loop_sync: + if args.loop_sync: rd_item.loop_sync(args.loop_sync) - elif args.cleanup: + if args.cleanup: rd_item.cleanup() else: pass