From 5abb569e0cd716bba00be946993930ce781e68f3 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Thu, 6 Dec 2007 16:18:51 +0100 Subject: [PATCH] Keep children sorted when renaming a node. --- src/node.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/node.c b/src/node.c index 9aa5ea4..7b5d2b8 100644 --- a/src/node.c +++ b/src/node.c @@ -70,15 +70,31 @@ void iso_node_unref(IsoNode *node) */ int iso_node_set_name(IsoNode *node, const char *name) { + char *new; 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; } } + new = strdup(name); + if (new == NULL) { + return ISO_MEM_ERROR; + } free(node->name); - node->name = strdup(name); - return node->name != NULL ? ISO_SUCCESS : ISO_MEM_ERROR; + node->name = new; + if (node->parent != NULL) { + IsoDir *parent; + int res; + /* take and add again to ensure correct children order */ + parent = node->parent; + iso_node_take(node); + res = iso_dir_add_node(parent, node, 0); + if (res < 0) { + return res; + } + } + return ISO_SUCCESS; } /**