Ramdiskification is now fully resilient, I think...

This commit is contained in:
2025-10-30 11:06:58 -05:00
parent ffaea9421b
commit 66d11c934a

28
rdify
View File

@@ -40,11 +40,16 @@ class RamDiskItem():
return "PERSIST_ONLY" # Symlink broken, persistent copy exists return "PERSIST_ONLY" # Symlink broken, persistent copy exists
case (True, False, _, False): case (True, False, _, False):
return "UNRECOVERABLE" # Symlink broken, persistent copy missing return "UNRECOVERABLE" # Symlink broken, persistent copy missing
case (False, False, _, _):
return "MISSING_TARGET" # Target missing
case _: case _:
eprint("Unhandled state:", path_states) eprint("Unhandled state:", path_states)
sys.exit(1)
def init(self): def init(self):
match self.check_state():
case "UNINITIALIZED":
self.persist.mkdir(parents=True, exist_ok=True) self.persist.mkdir(parents=True, exist_ok=True)
self.ramdisk.mkdir(parents=True, exist_ok=True) self.ramdisk.mkdir(parents=True, exist_ok=True)
@@ -55,9 +60,23 @@ class RamDiskItem():
shutil.move(self.target, self.persist) shutil.move(self.target, self.persist)
os.symlink(self.full_rd_path, self.target) os.symlink(self.full_rd_path, self.target)
case "RAMDISKIFIED":
pass
case "NONPERSISTENT":
self.persist.mkdir(parents=True, exist_ok=True)
self.sync()
case "PERSIST_ONLY":
if self.full_persist_path.is_file():
shutil.copy2(self.full_persist_path, self.ramdisk)
else:
shutil.copytree(self.full_persist_path, self.full_rd_path, dirs_exist_ok=True)
case _:
eprint(f"Cannot initialize: {self.check_state()}")
def sync(self): def sync(self):
if self.check_state() != "RAMDISKIFIED":
self.init()
# TODO: actually look at output from this... # TODO: actually look at output from this...
subprocess.run(["rsync", "-aP", "--delete", self.full_rd_path, self.rsync_destination]) subprocess.run(["rsync", "-aP", "--delete", self.full_rd_path, self.rsync_destination])
@@ -72,9 +91,8 @@ class RamDiskItem():
return return
def cleanup(self): def cleanup(self):
if not self.target.is_symlink(): if self.check_state() != "RAMDISKIFIED":
eprint(f"Target: {self.target} isn't a symlink!") self.init()
return
self.sync() self.sync()
self.target.unlink() self.target.unlink()
shutil.move(self.full_persist_path, self.target.parent) shutil.move(self.full_persist_path, self.target.parent)
@@ -105,10 +123,6 @@ def main():
args = parser.parse_args() args = parser.parse_args()
# if not Path(args.target).exists():
# eprint(f"Target: {args.target} doesn't exist!")
# return
rd_item = RamDiskItem(args.target, args.persist) rd_item = RamDiskItem(args.target, args.persist)
if args.diagnose: if args.diagnose: