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,
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
/**

View File

@ -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;
}
}
}
/**

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);
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;
}