From d4deb4866fc0c7dbf34120c752649bcb83109837 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Thu, 6 Dec 2007 02:22:11 +0100 Subject: [PATCH] Take care about duplicate node names in iso_node_set_name(). --- src/libisofs.h | 14 ++++++++++---- src/node.c | 25 +++++++++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/libisofs.h b/src/libisofs.h index ff9647e..a80512a 100644 --- a/src/libisofs.h +++ b/src/libisofs.h @@ -191,11 +191,15 @@ void iso_node_ref(IsoNode *node); void iso_node_unref(IsoNode *node); /** - * Set the name of a node. + * Set the name of a node. Note that if the node is already added to a dir + * this can fail if dir already contains a node with the new name. * - * @param name The name in UTF-8 encoding + * @param name + * The name in UTF-8 encoding + * @return + * 1 on success, < 0 on error */ -void iso_node_set_name(IsoNode *node, const char *name); +int iso_node_set_name(IsoNode *node, const char *name); /** * Get the name of a node (in UTF-8). @@ -289,10 +293,12 @@ int iso_dir_add_node(IsoDir *dir, IsoNode *child, int replace); * 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. + * Note that you can pass NULL is the only thing you want to do is check + * if a node with such name already exists on dir. * @return * 1 node found, 0 child has no such node, < 0 error * Possible errors: - * ISO_NULL_POINTER, if dir, node or name are NULL + * ISO_NULL_POINTER, if dir or name are NULL */ int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node); diff --git a/src/node.c b/src/node.c index f2f2828..9aa5ea4 100644 --- a/src/node.c +++ b/src/node.c @@ -68,10 +68,17 @@ void iso_node_unref(IsoNode *node) * * @param name The name in UTF-8 encoding */ -void iso_node_set_name(IsoNode *node, const char *name) +int iso_node_set_name(IsoNode *node, const char *name) { + if (node->parent != NULL) { + /* check if parent already has a node with same name */ + if (iso_dir_get_node(node->parent, name, NULL) == 1) { + return ISO_NODE_NAME_NOT_UNIQUE; + } + } free(node->name); node->name = strdup(name); + return node->name != NULL ? ISO_SUCCESS : ISO_MEM_ERROR; } /** @@ -221,13 +228,19 @@ int iso_dir_add_node(IsoDir *dir, IsoNode *child, int replace) * @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. + * Note that you can pass NULL is the only thing you want to do is check + * if a node with such name already exists on dir. * @return * 1 node found, 0 child has no such node, < 0 error + * Possible errors: + * ISO_NULL_POINTER, if dir or name are NULL */ int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node) { IsoNode *pos; - if (dir == NULL || name == NULL || node == NULL) { + if (dir == NULL || name == NULL) { return ISO_NULL_POINTER; } @@ -237,11 +250,15 @@ int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node) } if (pos == NULL || strcmp(pos->name, name)) { - *node = NULL; + if (node) { + *node = NULL; + } return 0; /* node not found */ } - *node = pos; + if (node) { + *node = pos; + } return 1; }