diff --git a/demo/find.c b/demo/find.c index 00cccbc..7adc6d0 100644 --- a/demo/find.c +++ b/demo/find.c @@ -23,7 +23,9 @@ print_dir(IsoDir *dir) cond = iso_new_find_conditions_and(c1, c2); iso_dir_find_children(dir, cond, &iter); while (iso_dir_iter_next(iter, &node) == 1) { - printf(" %s\n", iso_node_get_name(node)); + char *path = iso_tree_get_node_path(node); + printf(" %s\n", path); + free(path); } iso_dir_iter_free(iter); } diff --git a/libisofs/libisofs.h b/libisofs/libisofs.h index eb7e0af..6c827c0 100644 --- a/libisofs/libisofs.h +++ b/libisofs/libisofs.h @@ -2955,6 +2955,16 @@ int iso_tree_add_dir_rec(IsoImage *image, IsoDir *parent, const char *dir); */ int iso_tree_path_to_node(IsoImage *image, const char *path, IsoNode **node); +/** + * Get the path on image of the given node. + * + * @return + * The path on the image, that must be freed when no more needed. If the + * given node is not added to any image, this returns NULL. + * @since 0.6.4 + */ +char *iso_tree_get_node_path(IsoNode *node); + /** * Increments the reference counting of the given IsoDataSource. * diff --git a/libisofs/tree.c b/libisofs/tree.c index 624539a..8daf7f6 100644 --- a/libisofs/tree.c +++ b/libisofs/tree.c @@ -957,3 +957,27 @@ int iso_tree_path_to_node(IsoImage *image, const char *path, IsoNode **node) } return result; } + +char *iso_tree_get_node_path(IsoNode *node) +{ + if (node == NULL || node->parent == NULL) { + return NULL; + } + + if ((IsoNode*)node->parent == node) { + return strdup("/"); + } else { + char path[PATH_MAX]; + char *parent_path = iso_tree_get_node_path((IsoNode*)node->parent); + if (parent_path == NULL) { + return NULL; + } + if (strlen(parent_path) == 1) { + snprintf(path, PATH_MAX, "/%s", node->name); + } else { + snprintf(path, PATH_MAX, "%s/%s", parent_path, node->name); + } + free(parent_path); + return strdup(path); + } +}