Implemented functionality to search and add by path

This commit is contained in:
Mario Danic 2006-09-01 12:13:14 +00:00
parent 374166119e
commit 6b1c840c1c
2 changed files with 132 additions and 0 deletions

View File

@ -79,6 +79,41 @@ void iso_volume_set_publisher_id(struct iso_volume *volume,
void iso_volume_set_data_preparer_id(struct iso_volume *volume,
const char *data_preparer_id);
/**
* Locate a node by its path on disc.
*
* \param volume The volume to search in.
* \param path The path, in the image, of the file.
*
* \return The node found or NULL.
*
*/
struct iso_tree_node *iso_tree_volume_path_to_node(struct iso_volume *volume, const char *path);
/**
* Add a file or a directory (recursively) to a volume by specifying its path on the volume.
*
* \param volume The volume to add the file to.
* \param disc_path The path on the disc at which to add the disc.
* \param path The path, on the local filesystem, of the file.
*
* \return The node for the file or NULL if the parent doesn't exists on the disc.
*/
struct iso_tree_node *iso_tree_volume_add_path(struct iso_volume *volume,
const char *disc_path,
const char *path);
/**
* Creates a new, empty directory on the volume.
*
* \param volume The volume to add the directory to.
* \param disc_path The path on the volume at which to add the directory.
*
* \return A pointer to the newly created directory.
*/
struct iso_tree_node *iso_tree_volume_add_new_dir(struct iso_volume *volume,
const char *disc_path);
/**
* Create a new Volume Set consisting of only one volume.
* @param volume The first and only volume for the volset to contain.

View File

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include "libisofs.h"
#include "tree.h"
@ -88,3 +89,99 @@ struct iso_tree_node *iso_volume_get_root(const struct iso_volume *volume)
{
return volume->root;
}
struct iso_tree_node *
iso_tree_volume_path_to_node(struct iso_volume *volume, const char *path)
{
struct iso_tree_node *node;
char *ptr, *brk_info, *component;
/* get the first child at the root of the volume
* that is "/" */
node=iso_volume_get_root(volume);
if (!strcmp (path, "/"))
return node;
if (!node->nchildren)
return NULL;
/* the name of the nodes is in wide characters so first convert path
* into wide characters. */
ptr = strdup(path);
/* get the first component of the path */
component=strtok_r(ptr, "/", &brk_info);
while (component) {
size_t max;
size_t i;
/* search among all the children of this directory if this path component exists */
max=node->nchildren;
for (i=0; i < max; i++) {
if (!strcmp(component, node->children[i]->name)) {
node=node->children[i];
break;
}
}
/* see if a node could be found */
if (i==max) {
node=NULL;
break;
}
component=strtok_r(NULL, "/", &brk_info);
}
free(ptr);
return node;
}
struct iso_tree_node *
iso_tree_volume_add_path(struct iso_volume *volume,
const char *disc_path,
const char *path)
{
char *tmp;
struct iso_tree_node *node;
struct iso_tree_node *parent_node;
tmp=strdup(disc_path);
parent_node = iso_tree_volume_path_to_node(volume, dirname(tmp));
free(tmp);
if (!parent_node)
return NULL;
node = iso_tree_radd_dir(parent_node, path);
if (!node)
return NULL;
tmp=strdup(disc_path);
iso_tree_node_set_name(node, basename(tmp));
free(tmp);
return node;
}
struct iso_tree_node *
iso_tree_volume_add_new_dir(struct iso_volume *volume,
const char *disc_path)
{
char *tmp;
struct iso_tree_node *node;
struct iso_tree_node *parent_node;
tmp=strdup(disc_path);
parent_node = iso_tree_volume_path_to_node(volume, dirname(tmp));
free(tmp);
if (!parent_node)
return NULL;
tmp=strdup(disc_path);
node = iso_tree_add_new_dir(parent_node, basename(tmp));
free(tmp);
return node;
}