From 8b10d3107a7f346378042b438e7dcacd9a8a760b Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Mon, 17 Mar 2008 16:02:50 +0100 Subject: [PATCH] Make IsoDirIters take also a ref on the dir they iterate. --- libisofs/find.c | 1 + libisofs/node.c | 2 ++ test/test_node.c | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/libisofs/find.c b/libisofs/find.c index 787c36d..6a63307 100644 --- a/libisofs/find.c +++ b/libisofs/find.c @@ -203,6 +203,7 @@ int iso_dir_find_children(IsoDir* dir, IsoFindCondition *cond, } update_next(data); + iso_node_ref((IsoNode*)dir); *iter = it; return ISO_SUCCESS; } diff --git a/libisofs/node.c b/libisofs/node.c index 6442473..9c8dee9 100644 --- a/libisofs/node.c +++ b/libisofs/node.c @@ -772,6 +772,7 @@ int iso_dir_get_children(const IsoDir *dir, IsoDirIter **iter) return ISO_OUT_OF_MEM; } + iso_node_ref((IsoNode*)dir); /* tak a ref to the dir */ *iter = it; return ISO_SUCCESS; } @@ -797,6 +798,7 @@ void iso_dir_iter_free(IsoDirIter *iter) if (iter != NULL) { iso_dir_iter_unregister(iter); iter->class->free(iter); + iso_node_unref((IsoNode*)iter->dir); free(iter); } } diff --git a/test/test_node.c b/test/test_node.c index 2fdcf0c..803d2eb 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -420,11 +420,13 @@ void test_iso_dir_get_children() /* init dir with default values, not all field need to be initialized */ dir = malloc(sizeof(IsoDir)); + dir->node.refcount = 1; dir->children = NULL; dir->nchildren = 0; result = iso_dir_get_children(dir, &iter); CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_EQUAL(dir->node.refcount, 2); /* item should have no items */ result = iso_dir_iter_has_next(iter); @@ -524,6 +526,7 @@ void test_iso_dir_get_children() CU_ASSERT_EQUAL(result, 0); CU_ASSERT_PTR_NULL(node); iso_dir_iter_free(iter); + CU_ASSERT_EQUAL(dir->node.refcount, 1); /* assert correct refcount */ CU_ASSERT_EQUAL(node1->refcount, 1); @@ -545,11 +548,13 @@ void test_iso_dir_iter_take() /* init dir with default values, not all field need to be initialized */ dir = malloc(sizeof(IsoDir)); + dir->node.refcount = 1; dir->children = NULL; dir->nchildren = 0; result = iso_dir_get_children(dir, &iter); CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_EQUAL(dir->node.refcount, 2); /* remove on empty dir! */ result = iso_dir_iter_take(iter); @@ -685,6 +690,7 @@ void test_iso_dir_iter_take() CU_ASSERT_PTR_EQUAL(node3->next, node1); iso_dir_iter_free(iter); + CU_ASSERT_EQUAL(dir->node.refcount, 1); /* assert correct refcount */ CU_ASSERT_EQUAL(node1->refcount, 1); @@ -706,6 +712,7 @@ void test_iso_dir_iter_remove() /* init dir with default values, not all field need to be initialized */ dir = malloc(sizeof(IsoDir)); + dir->node.refcount = 1; dir->children = NULL; dir->nchildren = 0; @@ -866,6 +873,7 @@ void test_iso_dir_iter_when_external_take() /* init dir with default values, not all field need to be initialized */ dir = malloc(sizeof(IsoDir)); + dir->node.refcount = 1; dir->children = NULL; dir->nchildren = 0;