Skip to content

Commit 24b20d5

Browse files
committed
fix: DictSupplementaryFileContainer increment refcount in _assign_unique_name
1 parent 148bd85 commit 24b20d5

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

sdk/basyx/aas/adapter/aasx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ def rename_file(self, old_name: str, new_name: str) -> str:
880880
if new_name == old_name:
881881
return new_name
882882
file_hash, file_content_type = self._name_map[old_name]
883+
self._store_refcount[file_hash] -= 1
883884
del self._name_map[old_name]
884885
return self._assign_unique_name(new_name, file_hash, file_content_type)
885886

@@ -889,6 +890,7 @@ def _assign_unique_name(self, name: str, sha: bytes, content_type: str) -> str:
889890
while True:
890891
if new_name not in self._name_map:
891892
self._name_map[new_name] = (sha, content_type)
893+
self._store_refcount[sha] += 1
892894
return new_name
893895
elif self._name_map[new_name] == (sha, content_type):
894896
return new_name

sdk/test/adapter/aasx/test_aasx.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ def test_supplementary_file_container(self) -> None:
8888
with self.assertRaises(KeyError):
8989
container.write_file(duplicate_file, file_content)
9090

91+
def test_supplementary_file_container_refcount(self) -> None:
92+
container = aasx.DictSupplementaryFileContainer()
93+
data = b"test content"
94+
name1 = container.add_file("/file1.bin", io.BytesIO(data), "application/octet-stream")
95+
name2 = container.add_file("/file2.bin", io.BytesIO(data), "application/octet-stream")
96+
content_hash = container.get_sha256(name1)
97+
98+
# Both names point to same content — backing store must be present
99+
self.assertIn(content_hash, container._store)
100+
101+
# Deleting one reference must NOT free the backing store
102+
container.delete_file(name1)
103+
self.assertIn(content_hash, container._store)
104+
105+
# Deleting the last reference must free the backing store
106+
container.delete_file(name2)
107+
self.assertNotIn(content_hash, container._store)
108+
91109

92110
class AASXWriterTest(unittest.TestCase):
93111
def test_writing_reading_example_aas(self) -> None:

0 commit comments

Comments
 (0)