2 * Unix SMB/CIFS implementation.
3 * Generic Abstract Data Types
4 * Copyright (C) Gerald Carter 2002.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 /**************************************************************************
25 *************************************************************************/
27 static bool trim_tree_keypath( char *path, char **base, char **new_path )
31 *new_path = *base = NULL;
38 p = strchr( path, '/' );
48 /**************************************************************************
49 Initialize the tree's root. The cmp_fn is a callback function used
50 for comparision of two children
51 *************************************************************************/
53 SORTED_TREE* pathtree_init( void *data_p, int (cmp_fn)(void*, void*) )
55 SORTED_TREE *tree = NULL;
57 if ( !(tree = TALLOC_ZERO_P(NULL, SORTED_TREE)) )
60 tree->compare = cmp_fn;
62 if ( !(tree->root = TALLOC_ZERO_P(tree, TREE_NODE)) ) {
67 tree->root->data_p = data_p;
73 /**************************************************************************
74 Find the next child given a key string
75 *************************************************************************/
77 static TREE_NODE* pathtree_birth_child( TREE_NODE *node, char* key )
79 TREE_NODE *infant = NULL;
83 if ( !(infant = TALLOC_ZERO_P( node, TREE_NODE)) )
86 infant->key = talloc_strdup( infant, key );
87 infant->parent = node;
89 siblings = TALLOC_REALLOC_ARRAY( node, node->children, TREE_NODE *, node->num_children+1 );
92 node->children = siblings;
98 if ( node->num_children == 1 ) {
99 DEBUG(11,("pathtree_birth_child: First child of node [%s]! [%s]\n",
100 node->key ? node->key : "NULL", infant->key ));
101 node->children[0] = infant;
106 * multiple siblings .... (at least 2 children)
108 * work from the end of the list forward
109 * The last child is not set at this point
110 * Insert the new infanct in ascending order
114 for ( i = node->num_children-1; i>=1; i-- )
116 DEBUG(11,("pathtree_birth_child: Looking for crib; infant -> [%s], child -> [%s]\n",
117 infant->key, node->children[i-1]->key));
119 /* the strings should never match assuming that we
120 have called pathtree_find_child() first */
122 if ( StrCaseCmp( infant->key, node->children[i-1]->key ) > 0 ) {
123 DEBUG(11,("pathtree_birth_child: storing infant in i == [%d]\n",
125 node->children[i] = infant;
129 /* bump everything towards the end on slot */
131 node->children[i] = node->children[i-1];
134 DEBUG(11,("pathtree_birth_child: Exiting loop (i == [%d])\n", i ));
136 /* if we haven't found the correct slot yet, the child
137 will be first in the list */
140 node->children[0] = infant;
146 /**************************************************************************
147 Find the next child given a key string
148 *************************************************************************/
150 static TREE_NODE* pathtree_find_child( TREE_NODE *node, char* key )
152 TREE_NODE *next = NULL;
156 DEBUG(0,("pathtree_find_child: NULL node passed into function!\n"));
161 DEBUG(0,("pathtree_find_child: NULL key string passed into function!\n"));
165 for ( i=0; i<node->num_children; i++ )
167 DEBUG(11,("pathtree_find_child: child key => [%s]\n",
168 node->children[i]->key));
170 result = StrCaseCmp( node->children[i]->key, key );
173 next = node->children[i];
175 /* if result > 0 then we've gone to far because
176 the list of children is sorted by key name
177 If result == 0, then we have a match */
183 DEBUG(11,("pathtree_find_child: %s [%s]\n",
184 next ? "Found" : "Did not find", key ));
189 /**************************************************************************
190 Add a new node into the tree given a key path and a blob of data
191 *************************************************************************/
193 WERROR pathtree_add( SORTED_TREE *tree, const char *path, void *data_p )
195 char *str, *base, *path2;
196 TREE_NODE *current, *next;
197 WERROR ret = WERR_OK;
199 DEBUG(8,("pathtree_add: Enter\n"));
201 if ( !path || *path != '/' ) {
202 DEBUG(0,("pathtree_add: Attempt to add a node with a bad path [%s]\n",
203 path ? path : "NULL" ));
204 return WERR_INVALID_PARAM;
208 DEBUG(0,("pathtree_add: Attempt to add a node to an uninitialized tree!\n"));
209 return WERR_INVALID_PARAM;
212 /* move past the first '/' */
215 path2 = SMB_STRDUP( path );
217 DEBUG(0,("pathtree_add: strdup() failed on string [%s]!?!?!\n", path));
223 * this works sort of like a 'mkdir -p' call, possibly
224 * creating an entire path to the new node at once
225 * The path should be of the form /<key1>/<key2>/...
230 current = tree->root;
233 /* break off the remaining part of the path */
235 str = strchr( str, '/' );
239 /* iterate to the next child--birth it if necessary */
241 next = pathtree_find_child( current, base );
243 next = pathtree_birth_child( current, base );
245 DEBUG(0,("pathtree_add: Failed to create new child!\n"));
252 /* setup the next part of the path */
261 } while ( base != NULL );
263 current->data_p = data_p;
265 DEBUG(10,("pathtree_add: Successfully added node [%s] to tree\n",
268 DEBUG(8,("pathtree_add: Exit\n"));
276 /**************************************************************************
277 Recursive routine to print out all children of a TREE_NODE
278 *************************************************************************/
280 static void pathtree_print_children(TALLOC_CTX *ctx,
293 DEBUG(debug,("%s: [%s] (%s)\n", path ? path : "NULL", node->key,
294 node->data_p ? "data" : "NULL" ));
297 path2 = talloc_strdup(ctx, path);
303 path2 = talloc_asprintf(ctx,
306 node->key ? node->key : "NULL");
311 num_children = node->num_children;
312 for ( i=0; i<num_children; i++ ) {
313 pathtree_print_children(ctx, node->children[i], debug, path2 );
317 /**************************************************************************
318 Dump the kys for a tree to the log file
319 *************************************************************************/
321 void pathtree_print_keys( SORTED_TREE *tree, int debug )
324 int num_children = tree->root->num_children;
326 if ( tree->root->key )
327 DEBUG(debug,("ROOT/: [%s] (%s)\n", tree->root->key,
328 tree->root->data_p ? "data" : "NULL" ));
330 for ( i=0; i<num_children; i++ ) {
331 TALLOC_CTX *ctx = talloc_stackframe();
332 pathtree_print_children(ctx, tree->root->children[i], debug,
333 tree->root->key ? tree->root->key : "ROOT/" );
339 /**************************************************************************
340 return the data_p for for the node in tree matching the key string
341 The key string is the full path. We must break it apart and walk
343 *************************************************************************/
345 void* pathtree_find( SORTED_TREE *tree, char *key )
347 char *keystr, *base = NULL, *str = NULL, *p;
351 DEBUG(10,("pathtree_find: Enter [%s]\n", key ? key : "NULL" ));
353 /* sanity checks first */
356 DEBUG(0,("pathtree_find: Attempt to search tree using NULL search string!\n"));
361 DEBUG(0,("pathtree_find: Attempt to search an uninitialized tree using string [%s]!\n",
362 key ? key : "NULL" ));
369 /* make a copy to play with */
372 keystr = SMB_STRDUP( key+1 );
374 keystr = SMB_STRDUP( key );
377 DEBUG(0,("pathtree_find: strdup() failed on string [%s]!?!?!\n", key));
381 /* start breaking the path apart */
384 current = tree->root;
386 if ( tree->root->data_p )
387 result = tree->root->data_p;
391 /* break off the remaining part of the path */
393 trim_tree_keypath( p, &base, &str );
395 DEBUG(11,("pathtree_find: [loop] base => [%s], new_path => [%s]\n",
399 /* iterate to the next child */
401 current = pathtree_find_child( current, base );
404 * the idea is that the data_p for a parent should
405 * be inherited by all children, but allow it to be
406 * overridden farther down
409 if ( current && current->data_p )
410 result = current->data_p;
412 /* reset the path pointer 'p' to the remaining part of the key string */
416 } while ( str && current );
418 /* result should be the data_p from the lowest match node in the tree */
420 DEBUG(11,("pathtree_find: Found data_p!\n"));
424 DEBUG(10,("pathtree_find: Exit\n"));