Implement function to get node from path on image. Little unit test too.

This commit is contained in:
Vreixo Formoso 2007-12-08 01:39:31 +01:00
parent f04ddb4435
commit b03fbf0ee0
4 changed files with 119 additions and 3 deletions

View File

@ -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, int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path,
IsoNode **node); 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 #define ISO_MSGS_MESSAGE_LEN 4096
/** /**

View File

@ -554,7 +554,7 @@ void iso_node_set_sort_weight(IsoNode *node, int w)
} }
} else if (node->type == LIBISO_FILE) { } else if (node->type == LIBISO_FILE) {
((IsoFile*)node)->sort_weight = w; ((IsoFile*)node)->sort_weight = w;
} }
} }
/** /**

View File

@ -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); result = iso_tree_add_node_builder(parent, file, image->builder, node);
return result; 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;
}

View File

@ -396,7 +396,7 @@ void test_iso_tree_add_node_link()
{ {
int result; int result;
IsoDir *root; IsoDir *root;
IsoNode *node1, *node2, *node3, *node4; IsoNode *node1, *node2, *node3;
IsoImage *image; IsoImage *image;
IsoFilesystem *fs; IsoFilesystem *fs;
struct stat info; struct stat info;
@ -498,6 +498,57 @@ void test_iso_tree_add_node_link()
iso_image_unref(image); 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() 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_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() [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_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);
} }