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
, '/' );
49 /**************************************************************************
50 Initialize the tree's root. The cmp_fn is a callback function used
51 for comparision of two children
52 *************************************************************************/
54 SORTED_TREE
* pathtree_init( void *data_p
, int (cmp_fn
)(void*, void*) )
56 SORTED_TREE
*tree
= NULL
;
58 if ( !(tree
= TALLOC_ZERO_P(NULL
, SORTED_TREE
)) )
61 tree
->compare
= cmp_fn
;
63 if ( !(tree
->root
= TALLOC_ZERO_P(tree
, TREE_NODE
)) ) {
68 tree
->root
->data_p
= data_p
;
74 /**************************************************************************
75 Find the next child given a key string
76 *************************************************************************/
78 static TREE_NODE
* pathtree_birth_child( TREE_NODE
*node
, char* key
)
80 TREE_NODE
*infant
= NULL
;
84 if ( !(infant
= TALLOC_ZERO_P( node
, TREE_NODE
)) )
87 infant
->key
= talloc_strdup( infant
, key
);
88 infant
->parent
= node
;
90 siblings
= TALLOC_REALLOC_ARRAY( node
, node
->children
, TREE_NODE
*, node
->num_children
+1 );
93 node
->children
= siblings
;
99 if ( node
->num_children
== 1 ) {
100 DEBUG(11,("pathtree_birth_child: First child of node [%s]! [%s]\n",
101 node
->key
? node
->key
: "NULL", infant
->key
));
102 node
->children
[0] = infant
;
107 * multiple siblings .... (at least 2 children)
109 * work from the end of the list forward
110 * The last child is not set at this point
111 * Insert the new infanct in ascending order
115 for ( i
= node
->num_children
-1; i
>=1; i
-- )
117 DEBUG(11,("pathtree_birth_child: Looking for crib; infant -> [%s], child -> [%s]\n",
118 infant
->key
, node
->children
[i
-1]->key
));
120 /* the strings should never match assuming that we
121 have called pathtree_find_child() first */
123 if ( StrCaseCmp( infant
->key
, node
->children
[i
-1]->key
) > 0 ) {
124 DEBUG(11,("pathtree_birth_child: storing infant in i == [%d]\n",
126 node
->children
[i
] = infant
;
130 /* bump everything towards the end on slot */
132 node
->children
[i
] = node
->children
[i
-1];
135 DEBUG(11,("pathtree_birth_child: Exiting loop (i == [%d])\n", i
));
137 /* if we haven't found the correct slot yet, the child
138 will be first in the list */
141 node
->children
[0] = infant
;
147 /**************************************************************************
148 Find the next child given a key string
149 *************************************************************************/
151 static TREE_NODE
* pathtree_find_child( TREE_NODE
*node
, char* key
)
153 TREE_NODE
*next
= NULL
;
157 DEBUG(0,("pathtree_find_child: NULL node passed into function!\n"));
162 DEBUG(0,("pathtree_find_child: NULL key string passed into function!\n"));
166 for ( i
=0; i
<node
->num_children
; i
++ )
168 DEBUG(11,("pathtree_find_child: child key => [%s]\n",
169 node
->children
[i
]->key
));
171 result
= StrCaseCmp( node
->children
[i
]->key
, key
);
174 next
= node
->children
[i
];
176 /* if result > 0 then we've gone to far because
177 the list of children is sorted by key name
178 If result == 0, then we have a match */
184 DEBUG(11,("pathtree_find_child: %s [%s]\n",
185 next
? "Found" : "Did not find", key
));
190 /**************************************************************************
191 Add a new node into the tree given a key path and a blob of data
192 *************************************************************************/
194 WERROR
pathtree_add( SORTED_TREE
*tree
, const char *path
, void *data_p
)
196 char *str
, *base
, *path2
;
197 TREE_NODE
*current
, *next
;
198 WERROR ret
= WERR_OK
;
200 DEBUG(8,("pathtree_add: Enter\n"));
202 if ( !path
|| *path
!= '/' ) {
203 DEBUG(0,("pathtree_add: Attempt to add a node with a bad path [%s]\n",
204 path
? path
: "NULL" ));
205 return WERR_INVALID_PARAM
;
209 DEBUG(0,("pathtree_add: Attempt to add a node to an uninitialized tree!\n"));
210 return WERR_INVALID_PARAM
;
213 /* move past the first '/' */
216 path2
= SMB_STRDUP( path
);
218 DEBUG(0,("pathtree_add: strdup() failed on string [%s]!?!?!\n", path
));
224 * this works sort of like a 'mkdir -p' call, possibly
225 * creating an entire path to the new node at once
226 * The path should be of the form /<key1>/<key2>/...
231 current
= tree
->root
;
234 /* break off the remaining part of the path */
236 str
= strchr( str
, '/' );
240 /* iterate to the next child--birth it if necessary */
242 next
= pathtree_find_child( current
, base
);
244 next
= pathtree_birth_child( current
, base
);
246 DEBUG(0,("pathtree_add: Failed to create new child!\n"));
253 /* setup the next part of the path */
262 } while ( base
!= NULL
);
264 current
->data_p
= data_p
;
266 DEBUG(10,("pathtree_add: Successfully added node [%s] to tree\n",
269 DEBUG(8,("pathtree_add: Exit\n"));
277 /**************************************************************************
278 Recursive routine to print out all children of a TREE_NODE
279 *************************************************************************/
281 static void pathtree_print_children(TALLOC_CTX
*ctx
,
294 DEBUG(debug
,("%s: [%s] (%s)\n", path
? path
: "NULL", node
->key
,
295 node
->data_p
? "data" : "NULL" ));
298 path2
= talloc_strdup(ctx
, path
);
304 path2
= talloc_asprintf(ctx
,
307 node
->key
? node
->key
: "NULL");
312 num_children
= node
->num_children
;
313 for ( i
=0; i
<num_children
; i
++ ) {
314 pathtree_print_children(ctx
, node
->children
[i
], debug
, path2
);
318 /**************************************************************************
319 Dump the kys for a tree to the log file
320 *************************************************************************/
322 void pathtree_print_keys( SORTED_TREE
*tree
, int debug
)
325 int num_children
= tree
->root
->num_children
;
327 if ( tree
->root
->key
)
328 DEBUG(debug
,("ROOT/: [%s] (%s)\n", tree
->root
->key
,
329 tree
->root
->data_p
? "data" : "NULL" ));
331 for ( i
=0; i
<num_children
; i
++ ) {
332 TALLOC_CTX
*ctx
= talloc_stackframe();
333 pathtree_print_children(ctx
, tree
->root
->children
[i
], debug
,
334 tree
->root
->key
? tree
->root
->key
: "ROOT/" );
340 /**************************************************************************
341 return the data_p for for the node in tree matching the key string
342 The key string is the full path. We must break it apart and walk
344 *************************************************************************/
346 void* pathtree_find( SORTED_TREE
*tree
, char *key
)
348 char *keystr
, *base
= NULL
, *str
= NULL
, *p
;
352 DEBUG(10,("pathtree_find: Enter [%s]\n", key
? key
: "NULL" ));
354 /* sanity checks first */
357 DEBUG(0,("pathtree_find: Attempt to search tree using NULL search string!\n"));
362 DEBUG(0,("pathtree_find: Attempt to search an uninitialized tree using string [%s]!\n",
363 key
? key
: "NULL" ));
370 /* make a copy to play with */
373 keystr
= SMB_STRDUP( key
+1 );
375 keystr
= SMB_STRDUP( key
);
378 DEBUG(0,("pathtree_find: strdup() failed on string [%s]!?!?!\n", key
));
382 /* start breaking the path apart */
385 current
= tree
->root
;
387 if ( tree
->root
->data_p
)
388 result
= tree
->root
->data_p
;
392 /* break off the remaining part of the path */
394 trim_tree_keypath( p
, &base
, &str
);
396 DEBUG(11,("pathtree_find: [loop] base => [%s], new_path => [%s]\n",
400 /* iterate to the next child */
402 current
= pathtree_find_child( current
, base
);
405 * the idea is that the data_p for a parent should
406 * be inherited by all children, but allow it to be
407 * overridden farther down
410 if ( current
&& current
->data_p
)
411 result
= current
->data_p
;
413 /* reset the path pointer 'p' to the remaining part of the key string */
417 } while ( str
&& current
);
419 /* result should be the data_p from the lowest match node in the tree */
421 DEBUG(11,("pathtree_find: Found data_p!\n"));
425 DEBUG(10,("pathtree_find: Exit\n"));