Functions to add and get a node inside a dir.

This commit is contained in:
Vreixo Formoso
2007-12-01 01:42:21 +01:00
parent 3c7f1285d6
commit a4f283fac6
6 changed files with 303 additions and 3 deletions

View File

@ -21,6 +21,9 @@
#define ISO_INTERRUPTED -5
#define ISO_WRONG_ARG_VALUE -6
#define ISO_NODE_ALREADY_ADDED -50
#define ISO_NODE_NAME_NOT_UNIQUE -51
#define ISO_FILE_ERROR -100
#define ISO_FILE_ALREADY_OPENNED -101
#define ISO_FILE_ACCESS_DENIED -102

View File

@ -84,4 +84,44 @@ void iso_node_set_gid(IsoNode *node, gid_t gid);
*/
gid_t iso_node_get_gid(const IsoNode *node);
/**
* Add a new node to a dir. Note that this function don't add a new ref to
* the node, so you don't need to free it, it will be automatically freed
* when the dir is deleted. Of course, if you want to keep using the node
* after the dir life, you need to iso_node_ref() it.
*
* @param dir
* the dir where to add the node
* @param child
* the node to add. You must ensure that the node hasn't previously added
* to other dir, and that the node name is unique inside the child.
* Otherwise this function will return a failure, and the child won't be
* inserted.
* @return
* number of nodes in dir if succes, < 0 otherwise
* Possible errors:
* ISO_NULL_POINTER, if dir or child are NULL
* ISO_NODE_ALREADY_ADDED, if child is already added to other dir
* ISO_NODE_NAME_NOT_UNIQUE, a node with same name already exists
* ISO_WRONG_ARG_VALUE, if child == dir
*/
int iso_dir_add_node(IsoDir *dir, IsoNode *child);
/**
* Locate a node inside a given dir.
*
* @param name
* The name of the node
* @param node
* Location for a pointer to the node, it will filled with NULL if the dir
* doesn't have a child with the given name.
* The node will be owned by the dir and shouldn't be unref(). Just call
* iso_node_ref() to get your own reference to the node.
* @return
* 1 node found, 0 child has no such node, < 0 error
* Possible errors:
* ISO_NULL_POINTER, if dir, node or name are NULL
*/
int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node);
#endif /*LIBISO_LIBISOFS_H_*/

View File

@ -6,8 +6,9 @@
* published by the Free Software Foundation. See COPYING file for details.
*/
#include "node.h"
#include "libisofs.h"
#include "node.h"
#include "error.h"
#include <stdlib.h>
#include <string.h>
@ -120,3 +121,80 @@ gid_t iso_node_get_gid(const IsoNode *node)
{
return node->gid;
}
/**
* Add a new node to a dir. Note that this function don't add a new ref to
* the node, so you don't need to free it, it will be automatically freed
* when the dir is deleted. Of course, if you want to keep using the node
* after the dir life, you need to iso_node_ref() it.
*
* @param dir
* the dir where to add the node
* @param child
* the node to add. You must ensure that the node hasn't previously added
* to other dir, and that the node name is unique inside the child.
* Otherwise this function will return a failure, and the child won't be
* inserted.
* @return
* number of nodes in dir if succes, < 0 otherwise
*/
int iso_dir_add_node(IsoDir *dir, IsoNode *child)
{
IsoNode **pos;
if (dir == NULL || child == NULL) {
return ISO_NULL_POINTER;
}
if ((IsoNode*)dir == child) {
return ISO_WRONG_ARG_VALUE;
}
if (child->parent != NULL) {
return ISO_NODE_ALREADY_ADDED;
}
pos = &(dir->children);
while (*pos != NULL && strcmp((*pos)->name, child->name) < 0) {
pos = &((*pos)->next);
}
if (*pos != NULL && !strcmp((*pos)->name, child->name)) {
return ISO_NODE_NAME_NOT_UNIQUE;
}
child->next = *pos;
*pos = child;
child->parent = dir;
return ++dir->nchildren;
}
/**
* Locate a node inside a given dir.
*
* @param name
* The name of the node
* @param node
* Location for a pointer to the node, it will filled with NULL if the dir
* doesn't have a child with the given name.
* @return
* 1 node found, 0 child has no such node, < 0 error
*/
int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node)
{
IsoNode *pos;
if (dir == NULL || name == NULL || node == NULL) {
return ISO_NULL_POINTER;
}
pos = dir->children;
while (pos != NULL && strcmp(pos->name, name) < 0) {
pos = pos->next;
}
if (pos == NULL || strcmp(pos->name, name)) {
*node = NULL;
return 0; /* node not found */
}
*node = pos;
return 1;
}

View File

@ -69,7 +69,7 @@ struct Iso_Node
int hidden; /**< whether the node will be hidden, see IsoHideNodeFlag */
struct IsoDir *parent; /**< parent node, NULL for root */
IsoDir *parent; /**< parent node, NULL for root */
/*
* Pointers to the doubled linked list of children in a dir.
@ -85,7 +85,7 @@ struct Iso_Dir
IsoNode node;
size_t nchildren; /**< The number of children of this directory. */
struct IsoNode *children; /**< list of children. ptr to first child */
IsoNode *children; /**< list of children. ptr to first child */
};
struct Iso_File