From 2207539dcfa49c3b3a72abde2d8c7b3dc56991c0 Mon Sep 17 00:00:00 2001 From: Vasilito Date: Sat, 18 Apr 2026 00:48:58 +0100 Subject: [PATCH] Update fat_tool helper script Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- local/scripts/fat_tool.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/local/scripts/fat_tool.py b/local/scripts/fat_tool.py index b4598e32..f73b4bc2 100644 --- a/local/scripts/fat_tool.py +++ b/local/scripts/fat_tool.py @@ -50,10 +50,17 @@ class Fat32: try: self._read_bpb() self._load_fat() - except: + except Exception: self.f.close() raise + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + return False + def _read_bpb(self): self.f.seek(self.offset) bpb = self.f.read(512) @@ -119,6 +126,8 @@ class Fat32: return bytearray(self.f.read(self.cluster_size)) def _write_cluster(self, cluster, data): + if len(data) > self.cluster_size: + raise RuntimeError(f"_write_cluster: data size {len(data)} exceeds cluster size {self.cluster_size}") self.f.seek(self._cluster_offset(cluster)) self.f.write(data[: self.cluster_size]) @@ -536,6 +545,8 @@ class Fat32: if parent_entry is None or not parent_entry["is_dir"]: raise RuntimeError(f"cp-in: directory '{parent_path}' not found") parent_cluster = parent_entry["first_cluster"] + if parent_cluster < 2: + raise RuntimeError(f"cp-in: parent cluster {parent_cluster} is invalid") else: parent_cluster = self.root_cluster