From b03fbf0ee0ab5420d60c19f3efc9c521844df140 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Sat, 8 Dec 2007 01:39:31 +0100 Subject: [PATCH] Implement function to get node from path on image. Little unit test too. --- src/libisofs.h | 15 +++++++++++++ src/node.c | 2 +- src/tree.c | 49 ++++++++++++++++++++++++++++++++++++++++++ test/test_tree.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 119 insertions(+), 3 deletions(-) diff --git a/src/libisofs.h b/src/libisofs.h index e651c1a..f07da83 100644 --- a/src/libisofs.h +++ b/src/libisofs.h @@ -638,6 +638,21 @@ int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode, int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path, IsoNode **node); +/** + * Locate a node by its path on image. + * + * @param node + * Location for a pointer to the node, it will filled with NULL if the + * given path does not exists on image. + * The node will be owned by the image and shouldn't be unref(). Just call + * iso_node_ref() to get your own reference to the node. + * Note that you can pass NULL is the only thing you want to do is check + * if a node with such path really exists. + * @return + * 1 found, 0 not found, < 0 error + */ +int iso_tree_path_to_node(IsoImage *image, const char *path, IsoNode **node); + #define ISO_MSGS_MESSAGE_LEN 4096 /** diff --git a/src/node.c b/src/node.c index 40519c3..41edd51 100644 --- a/src/node.c +++ b/src/node.c @@ -554,7 +554,7 @@ void iso_node_set_sort_weight(IsoNode *node, int w) } } else if (node->type == LIBISO_FILE) { ((IsoFile*)node)->sort_weight = w; - } + } } /** diff --git a/src/tree.c b/src/tree.c index 12935d7..ad752c0 100644 --- a/src/tree.c +++ b/src/tree.c @@ -441,3 +441,52 @@ int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path, result = iso_tree_add_node_builder(parent, file, image->builder, node); return result; } + +int iso_tree_path_to_node(IsoImage *image, const char *path, IsoNode **node) +{ + int result; + IsoNode *n; + IsoDir *dir; + char *ptr, *brk_info, *component; + + if (image == NULL || path == NULL) { + return ISO_NULL_POINTER; + } + + /* get the first child at the root of the image that is "/" */ + dir = image->root; + n = (IsoNode *)dir; + if (!strcmp(path, "/")) { + if (node) { + *node = n; + } + return ISO_SUCCESS; + } + + ptr = strdup(path); + result = 0; + + /* get the first component of the path */ + component = strtok_r(ptr, "/", &brk_info); + while (component) { + if (n->type != LIBISO_DIR) { + n = NULL; + break; + } + dir = (IsoDir *)n; + + result = iso_dir_get_node(dir, component, &n); + if (result != 1) { + n = NULL; + break; + } + + component = strtok_r(NULL, "/", &brk_info); + } + + free(ptr); + if (node) { + *node = n; + } + return result; +} diff --git a/test/test_tree.c b/test/test_tree.c index 182ad31..0f65e95 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -396,7 +396,7 @@ void test_iso_tree_add_node_link() { int result; IsoDir *root; - IsoNode *node1, *node2, *node3, *node4; + IsoNode *node1, *node2, *node3; IsoImage *image; IsoFilesystem *fs; struct stat info; @@ -498,6 +498,57 @@ void test_iso_tree_add_node_link() iso_image_unref(image); } +static +void test_iso_tree_path_to_node() +{ + int result; + IsoDir *root; + IsoDir *node1, *node2, *node11; + IsoNode *node; + IsoImage *image; + IsoFilesystem *fs; + struct stat info; + + result = iso_image_new("volume_id", &image); + CU_ASSERT_EQUAL(result, 1); + root = iso_image_get_root(image); + CU_ASSERT_PTR_NOT_NULL(root); + + /* replace image filesystem with out mockep one */ + iso_filesystem_unref(image->fs); + result = test_mocked_filesystem_new(&fs); + CU_ASSERT_EQUAL(result, 1); + image->fs = fs; + + /* add some files */ + result = iso_tree_add_new_dir(root, "Dir1", &node1); + CU_ASSERT_EQUAL(result, 1); + result = iso_tree_add_new_dir(root, "Dir2", (IsoDir**)&node2); + CU_ASSERT_EQUAL(result, 2); + result = iso_tree_add_new_dir((IsoDir*)node1, "Dir11", (IsoDir**)&node11); + CU_ASSERT_EQUAL(result, 1); + + /* retrive some items */ + result = iso_tree_path_to_node(image, "/", &node); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_PTR_EQUAL(node, root); + result = iso_tree_path_to_node(image, "/Dir1", &node); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_PTR_EQUAL(node, node1); + result = iso_tree_path_to_node(image, "/Dir2", &node); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_PTR_EQUAL(node, node2); + result = iso_tree_path_to_node(image, "/Dir1/Dir11", &node); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_PTR_EQUAL(node, node11); + + /* some failtures */ + result = iso_tree_path_to_node(image, "/Dir2/Dir11", &node); + CU_ASSERT_EQUAL(result, 0); + CU_ASSERT_PTR_NULL(node); + + iso_image_unref(image); +} void add_tree_suite() { @@ -508,5 +559,6 @@ void add_tree_suite() CU_add_test(pSuite, "iso_tree_add_new_special()", test_iso_tree_add_new_special); CU_add_test(pSuite, "iso_tree_add_node() [1. dir]", test_iso_tree_add_node_dir); CU_add_test(pSuite, "iso_tree_add_node() [2. symlink]", test_iso_tree_add_node_link); - + CU_add_test(pSuite, "iso_tree_path_to_node()", test_iso_tree_path_to_node); + }