File: gconf-listeners.c

package info (click to toggle)
gconf 3.2.6-6
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 15,120 kB
  • sloc: ansic: 39,709; sh: 11,189; python: 1,107; makefile: 445; xml: 332
file content (921 lines) | stat: -rw-r--r-- 21,119 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/* GConf
 * Copyright (C) 1999, 2000 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include "gconf-listeners.h"
#include "gconf.h"

#include <string.h>
#include <unistd.h>

/* #define DEBUG_LISTENERS 1 */
#ifdef DEBUG_LISTENERS
#include <stdio.h>
#endif

/* FIXME get rid of this stupid thing */
struct _GConfListeners {
  gpointer dummy;
};

/* 24 bits are the array index; 8 bits are just to reduce the
   chance of duplicate connection IDs. If CnxnID was just an
   array index, recycling the IDs would make it likely that
   a broken client would remove another client's connections.
   The unique-izing 8 bits make this much less likely.
   Yeah just using a hash table instead of an array would work too since
   we wouldn't need to fool with recycling indices to avoid sparseness */
#define CNXN_ID_INDEX(cid) (cid & 0xFFFFFF)

typedef struct _Listener Listener;

struct _Listener {
  guint cnxn;
  guint refcount : 24;
  guint removed : 1; /* has been removed */
  gpointer listener_data;
  GFreeFunc destroy_notify;
};

/* LTable is GConfListenersPrivate, but shorter */

typedef struct _LTable LTable;

struct _LTable {
  GNode* tree; /* Represents the configuration "filesystem" namespace. 
                *  Kept sorted. 
                */
  GPtrArray* listeners; /* Listeners are also kept in a flat array here, indexed by connection number */
  guint active_listeners; /* count of "alive" listeners */

  /* 0 is an error value */
  guint next_cnxn;
  
  /* Connection array indexes to be recycled */
  GSList* removed_indices;
};

typedef struct _LTableEntry LTableEntry;

struct _LTableEntry {
  gchar* name; /* The name of this "directory" */
  GList* listeners; /* Each listener listening *exactly* here. You probably 
                        want to notify all listeners *below* this node as well. 
                     */
  gchar *full_name; /* fully-qualified name */
};

static LTable* ltable_new(void);
static void    ltable_insert(LTable* ltable,
                             const gchar* where,
                             Listener* listener);
static void    ltable_remove(LTable* ltable,
                             guint cnxn);
static void    ltable_destroy(LTable* ltable);
static void    ltable_notify(LTable* ltable,
                             const gchar* key,
                             GConfListenersCallback callback,
                             gpointer user_data);

static guint   ltable_next_cnxn(LTable* ltable);
static void    ltable_foreach  (LTable* ltable,
                                GConfListenersForeach callback,
                                gpointer user_data);
static gboolean ltable_get_data (LTable *ltable,
                                 guint cnxn_id,
                                 gpointer *listener_data_p,
                                 const gchar  **location_p);
static void    ltable_remove_if  (LTable* ltable,
                                  GConfListenersPredicate predicate,
                                  gpointer user_data);

#ifdef DEBUG_LISTENERS
static void    ltable_spew(LTable* ltable);
#endif

static LTableEntry* ltable_entry_new(gchar **pathv,
				     int     end);
static void         ltable_entry_destroy(LTableEntry* entry);

static Listener* listener_new   (guint      cnxn_id,
                                 gpointer   listener_data,
                                 GFreeFunc  destroy_notify);
static void      listener_unref (Listener  *l);
static void      listener_ref   (Listener  *l);

/*
 * Public API
 */ 

GConfListeners*
gconf_listeners_new     (void)
{
  LTable* lt;

  lt = ltable_new();

  return (GConfListeners*)lt;
}

void
gconf_listeners_free (GConfListeners* listeners)
{
  LTable* lt = (LTable*)listeners;

  ltable_destroy(lt);
}

guint
gconf_listeners_add     (GConfListeners* listeners,
                         const gchar* listen_point,
                         gpointer listener_data,
                         GFreeFunc destroy_notify)
{
  LTable* lt = (LTable*)listeners;
  Listener* l;

  l = listener_new(ltable_next_cnxn(lt),
                   listener_data,
                   destroy_notify);
  
  ltable_insert(lt, listen_point, l);

  return l->cnxn;
}

void
gconf_listeners_remove  (GConfListeners* listeners,
                         guint cnxn_id)
{
  LTable* lt = (LTable*)listeners;

  /* Silently fail, since this can easily happen (e.g. adding the
   * notify fails)
   */
  if (cnxn_id == 0)
    return;
  
  ltable_remove(lt, cnxn_id);
}

void
gconf_listeners_notify  (GConfListeners* listeners,
                         const gchar* all_above,
                         GConfListenersCallback callback,
                         gpointer user_data)
{
  LTable* lt = (LTable*)listeners;

  ltable_notify(lt, all_above, callback, user_data);
}

guint
gconf_listeners_count   (GConfListeners* listeners)
{
  LTable* lt = (LTable*)listeners;

  return lt->active_listeners;
}

void
gconf_listeners_foreach (GConfListeners* listeners,
                         GConfListenersForeach callback,
                         gpointer user_data)
{
  LTable* lt = (LTable*)listeners;
  
  ltable_foreach (lt, callback, user_data);
}

gboolean
gconf_listeners_get_data (GConfListeners* listeners,
                          guint cnxn_id,
                          gpointer *listener_data_p,
                          const gchar **location_p)
{
  LTable* lt = (LTable*)listeners;

  return ltable_get_data (lt, cnxn_id, listener_data_p, location_p);
}

void
gconf_listeners_remove_if (GConfListeners         *listeners,
                           GConfListenersPredicate predicate,
                           gpointer                user_data)
{
  LTable* lt = (LTable*)listeners;

  ltable_remove_if (lt, predicate, user_data);
}

/*
 * LTable impl
 */

static Listener* 
listener_new(guint cnxn_id, gpointer listener_data, GFreeFunc destroy_notify)
{
  Listener* l;

  l = g_new0(Listener, 1);

  l->refcount = 1;
  l->removed = FALSE;
  l->listener_data = listener_data;
  l->cnxn = cnxn_id;  
  l->destroy_notify = destroy_notify;

  return l;
}

static void      
listener_unref (Listener* l)
{
  l->refcount -= 1;
  if (l->refcount == 0)
    {
      (*l->destroy_notify)(l->listener_data);
      g_free(l);
    }
}

static void
listener_ref (Listener  *l)
{
  l->refcount += 1;
}

static LTable* 
ltable_new(void)
{
  LTable* lt;

  lt = g_new0(LTable, 1);

  lt->listeners = g_ptr_array_new();

  /* Set initial size; note that GPtrArray's are initialized
     to 0-filled */
  g_ptr_array_set_size(lt->listeners, 5);
  
  lt->active_listeners = 0;

  lt->removed_indices = NULL;

  lt->next_cnxn = 1; /* 0 is invalid */
  
  return lt;
}

static guint
ltable_next_cnxn(LTable* lt)
{
  static guchar start = 0;
  static guchar uniqueness = 0;
  guint uniqueness_shifted;

  if (start == 0)
    {
      /* Don't start uniqueness at the same place every time */
      start = getpid () % 256;
      if (start == 0)
        start = 1;

      uniqueness = start;
    }
  
  /* this overflows and starts over occasionally */
  ++uniqueness;

  uniqueness_shifted = uniqueness;
  uniqueness_shifted = uniqueness_shifted << 24;
  
  if (lt->removed_indices != NULL)
    {
      guint retval = GPOINTER_TO_UINT(lt->removed_indices->data);

      lt->removed_indices = g_slist_remove(lt->removed_indices, lt->removed_indices->data);

      /* set the uniqueness bits */
      retval |= uniqueness_shifted;
      
      return retval;
    }
  else
    {
      /* make sure we fit in 24 bits */
      g_assert(lt->next_cnxn <= 0xFFFFFF);

      lt->next_cnxn += 1;
      return (lt->next_cnxn - 1) | uniqueness_shifted;
    }
}

static void
ltable_insert(LTable* lt, const gchar* where, Listener* l)
{
  gchar** dirnames;
  guint i;
  GNode* cur;
  GNode* found = NULL;
  LTableEntry* lte;
  const gchar* noroot_where = where + 1;

  g_return_if_fail(gconf_valid_key(where, NULL));
  
  if (lt->tree == NULL)
    {
      lte = ltable_entry_new(NULL, 0);
      
      lt->tree = g_node_new(lte);

      lte = NULL; /* paranoia */
    }
  
  /* Add to the tree */
  dirnames = g_strsplit(noroot_where, "/", -1);
  
  cur = lt->tree;
  i = 0;
  while (dirnames[i])
    {
      LTableEntry* ne;
      GNode* across;

      /* Find this dirname on this level, or add it. */
      g_assert (cur != NULL);        

      found = NULL;

      across = cur->children;

      while (across != NULL)
        {
          int cmp;

          lte = across->data;

          cmp = strcmp(lte->name, dirnames[i]);

          if (cmp == 0)
            {
              found = across;
              break;
            }
          else if (cmp > 0)
            {
              /* Past it */
              break;
            }
          else 
            {
              across = g_node_next_sibling(across);
            }
        }

      if (found == NULL)
        {
          ne = ltable_entry_new(dirnames, i);
              
          if (across != NULL) /* Across is at the one past */
            found = g_node_insert_data_before(cur, across, ne);
          else                /* Never went past, append - could speed this up by saving last visited */
            found = g_node_append_data(cur, ne);
        }

      g_assert(found != NULL);

      cur = found;

      ++i;
    }

  /* cur is still the root node ("/") if where was "/" since nothing
     was returned from g_strsplit */
  lte = cur->data;

  lte->listeners = g_list_prepend(lte->listeners, l);

  g_strfreev(dirnames);

  /* Add tree node to the flat table */
  g_ptr_array_set_size(lt->listeners, MAX(CNXN_ID_INDEX(lt->next_cnxn), CNXN_ID_INDEX(l->cnxn)));
  g_ptr_array_index(lt->listeners, CNXN_ID_INDEX(l->cnxn)) = cur;

  lt->active_listeners += 1;

#ifdef DEBUG_LISTENERS
  g_print ("Added %u at %s, spewing:\n",
	   l->cnxn, where);
  ltable_spew(lt);
#endif
}

static void    
ltable_remove(LTable* lt, guint cnxn)
{
  LTableEntry* lte;
  GList* tmp;
  GNode* node;
  guint index = CNXN_ID_INDEX(cnxn);

  g_return_if_fail(index < lt->listeners->len);
  
  if (index >= lt->listeners->len) /* robust even with checks off */
    return;
  
  /* Lookup in the flat table */
  node = g_ptr_array_index(lt->listeners, index);

  g_return_if_fail(node != NULL);
  if (node == NULL) /* a client is broken probably */
    return;

  g_assert(lt->tree != NULL);
  
  lte = node->data;
  
  tmp = lte->listeners;

  g_return_if_fail(tmp != NULL);

  while (tmp != NULL)
    {
      Listener* l = tmp->data;

      if (l->cnxn == cnxn)
        {
          if (tmp->prev)
            {
              tmp->prev->next = tmp->next;
            }
          else
            {
              /* tmp was the first (and maybe last) node */
              lte->listeners = tmp->next;
            }
          if (tmp->next)
            {
              tmp->next->prev = tmp->prev;
            }
          g_list_free_1(tmp);

          lt->removed_indices = g_slist_prepend(lt->removed_indices,
                                                GUINT_TO_POINTER(index));

          l->removed = TRUE;
          listener_unref (l);

          break;
        }

      tmp = g_list_next(tmp);
    }

  /* note that tmp is invalid, but should be nonzero if
     the connection was found. If the connection wasn't found,
     then this is a duplicate index and we have a broken client;
     we were saved by the uniqueness bits */

  if (tmp == NULL)
    return;

  /* Since we did have a valid connection, set the
     flat table entry to NULL */
  g_ptr_array_index(lt->listeners, index) = NULL;

  /* Remove from the tree if this node is now pointless */
  {
    GNode* cur = node;
    while (cur != NULL)
      {
        GNode* parent = cur->parent;
        lte = cur->data;
        if (lte->listeners == NULL && cur->children == NULL)
          {
            if (cur == lt->tree)
              lt->tree = NULL;
              
            ltable_entry_destroy(lte);
            g_node_destroy(cur);
          }
        else
          break; /* don't delete more parents since this node exists */
          
        cur = parent;
      }
  }

  lt->active_listeners -= 1;

#ifdef DEBUG_LISTENERS
  g_print ("Removed %u, spewing:\n", cnxn);
  ltable_spew(lt);
#endif
}

static gboolean
destroy_func(GNode* node, gpointer data)
{
  LTableEntry* lte = node->data;  
  GList* tmp;

  tmp = lte->listeners;
  while (tmp != NULL)
    {
      Listener* l = tmp->data;

      l->removed = TRUE;
      listener_unref (l);

      tmp = g_list_next(tmp);
    }
  g_list_free (lte->listeners);
  lte->listeners = NULL;
  
  ltable_entry_destroy (lte);
  
  return FALSE;
}

static void    
ltable_destroy(LTable* ltable)
{
  if (ltable->tree != NULL)
    {
      g_node_traverse(ltable->tree, G_POST_ORDER, G_TRAVERSE_ALL, -1,
                      destroy_func, NULL);  
      g_node_destroy(ltable->tree);
    }
      
  g_ptr_array_free(ltable->listeners, TRUE);

  g_slist_free(ltable->removed_indices);
  
  g_free(ltable);
}

static void
notify_listener_list(GConfListeners* listeners,
                     GList* list,
                     const gchar* key,
                     GConfListenersCallback callback,
                     gpointer user_data)
{
  GList* tmp;

  tmp = list;
  while (tmp != NULL)
    {
      Listener* l = tmp->data;

      /* don't notify listeners that were removed during the notify */
      if (!l->removed)
        (*callback)(listeners, key, l->cnxn, l->listener_data, user_data);

      tmp = g_list_next(tmp);
    }
}

static void    
ltable_notify(LTable* lt, const gchar* key,
              GConfListenersCallback callback, gpointer user_data)
{
  gchar** dirs;
  guint i;
  const gchar* noroot_key;
  GNode* cur;
  GList *to_notify;
  
  noroot_key = key + 1;

  g_return_if_fail(*key == '/');
  g_return_if_fail(gconf_valid_key(key, NULL));

  if (lt->tree == NULL)
    return; /* no one to notify */

  /* we build a list "to_notify" to be safe against tree
   * modifications during the notification.
   */
  
  /* Notify "/" listeners */
  to_notify = g_list_copy (((LTableEntry*)lt->tree->data)->listeners);  

  dirs = g_strsplit (noroot_key, "/", -1);

  cur = lt->tree;
  i = 0;
  while (dirs[i] && cur)
    {
      GNode* child = cur->children;

      while (child != NULL)
        {
          LTableEntry* lte = child->data;

          if (strcmp(lte->name, dirs[i]) == 0)
            {
              GList *copy = g_list_copy (lte->listeners);
              to_notify = g_list_concat (to_notify, copy);

              break;
            }

          child = g_node_next_sibling(child);
        }

      if (child != NULL) /* we found a child, scan below it */
        cur = child;
      else               /* end of the line */
        cur = NULL;

      ++i;
    }
  
  g_strfreev(dirs);

  g_list_foreach (to_notify, (GFunc) listener_ref, NULL);
  
  notify_listener_list ((GConfListeners*)lt,
                        to_notify, key,
                        callback, user_data);

  g_list_foreach (to_notify, (GFunc) listener_unref, NULL);

  g_list_free (to_notify);
}

struct NodeTraverseData
{
  GConfListenersForeach func;
  gpointer user_data;
};

static gboolean
node_traverse_func (GNode *node,
                    gpointer user_data)
{
  struct NodeTraverseData *td = user_data;
  LTableEntry *lte = node->data;
  GList *tmp;

  tmp = lte->listeners;
  while (tmp != NULL)
    {
      Listener *l = tmp->data;
      
      (* td->func) (lte->full_name,
                    l->cnxn,
                    l->listener_data,
                    td->user_data);
      
      tmp = g_list_next (tmp);
    }

  return FALSE; /* continue traversal */
}

static void
ltable_foreach  (LTable* ltable,
                 GConfListenersForeach callback,
                 gpointer user_data)
{
  struct NodeTraverseData td;
  td.func = callback;
  td.user_data = user_data;

  if (ltable->tree == NULL)
    return;
  
  g_node_traverse (ltable->tree,
                   G_IN_ORDER,
                   G_TRAVERSE_ALL,
                   -1,
                   node_traverse_func,
                   &td);
}

static gboolean
ltable_get_data (LTable *lt,
                 guint cnxn_id,
                 gpointer *listener_data_p,
                 const gchar   **location_p)
{
  LTableEntry* lte;
  GList* tmp;
  GNode* node;
  guint index = CNXN_ID_INDEX(cnxn_id);
  
  g_return_val_if_fail(index < lt->listeners->len, FALSE);
  if (index >= lt->listeners->len) /* robust even with checks off */
    return FALSE;
  
  /* Lookup in the flat table */
  node = g_ptr_array_index(lt->listeners, index);
  
  g_return_val_if_fail(node != NULL, FALSE);
  if (node == NULL) /* a client is broken probably */
    return FALSE;

  g_assert(lt->tree != NULL);
  
  lte = node->data;
  
  tmp = lte->listeners;

  g_return_val_if_fail(tmp != NULL, FALSE);

  while (tmp != NULL)
    {
      Listener* l = tmp->data;

      if (l->cnxn == cnxn_id)
        {
          if (listener_data_p)
            *listener_data_p = l->listener_data;
          if (location_p)
            *location_p = lte->full_name;
          return TRUE;
        }

      tmp = g_list_next(tmp);
    }

  return FALSE;
}



struct NodeRemoveData
{
  GConfListenersPredicate predicate;
  gpointer user_data;
  GSList *dead;
};

static gboolean
node_remove_func (GNode   *node,
                  gpointer user_data)
{
  struct NodeRemoveData *rd = user_data;
  LTableEntry *lte = node->data;
  GList *tmp;

  tmp = lte->listeners;
  while (tmp != NULL)
    {
      Listener *l = tmp->data;
      
      if ((* rd->predicate) (lte->full_name,
                             l->cnxn,
                             l->listener_data,
                             rd->user_data))
        rd->dead = g_slist_prepend (rd->dead, GINT_TO_POINTER (l->cnxn));
      
      tmp = g_list_next (tmp);
    }

  return FALSE; /* continue traversal */
}

static void
ltable_remove_if  (LTable                 *ltable,
                   GConfListenersPredicate predicate,
                   gpointer                user_data)
{
  GSList *tmp;
  struct NodeRemoveData rd;
  rd.predicate = predicate;
  rd.user_data = user_data;
  rd.dead = NULL;
  
  if (ltable->tree == NULL)
    return;
  
  g_node_traverse (ltable->tree,
                   G_IN_ORDER,
                   G_TRAVERSE_ALL,
                   -1,
                   node_remove_func,
                   &rd);

  tmp = rd.dead;
  while (tmp != NULL)
    {
      ltable_remove (ltable, GPOINTER_TO_INT (tmp->data));

      tmp = g_slist_next (tmp);
    }

  g_slist_free (rd.dead);
}

static LTableEntry* 
ltable_entry_new(gchar **pathv,
		 int     end)
{
  LTableEntry* lte;

  lte = g_new0(LTableEntry, 1);

  if (pathv != NULL)
    {
      GString* full_name;
      guint i;

      lte->name = g_strdup(pathv[end]);

      full_name = g_string_new("/");
      i = 0;
      while (i <= end)
	{
	  g_string_append(full_name, pathv[i]);
	  if (i != end)
	    g_string_append_c(full_name, '/');
	  i++;
	}
      lte->full_name = g_string_free(full_name, FALSE);
    }
  else
    {
      lte->name = g_strdup("/");
      lte->full_name = g_strdup("/");
    }
  
  return lte;
}

static void         
ltable_entry_destroy(LTableEntry* lte)
{
  g_return_if_fail(lte->listeners == NULL); /* should destroy all listeners first. */
  g_free(lte->name);
  g_free(lte->full_name);
  g_free(lte);
}

#ifdef DEBUG_LISTENERS
/* Debug */
gboolean
spew_func(GNode* node, gpointer data)
{
  LTableEntry* lte = node->data;  
  GList* tmp;
  gchar spaces[256];
  memset(spaces, ' ', 256);
  spaces[g_node_depth(node)] = '\0';

  
  g_print (" %sSpewing node `%s' (%p): ", spaces, lte->name, node);

  tmp = lte->listeners;
  while (tmp != NULL)
    {
      Listener* l = tmp->data;
      
      g_print ("  %slistener %u is here\n", spaces, (guint)l->cnxn);

      tmp = g_list_next(tmp);
    }

  if (lte->listeners == NULL)
    g_print ("\n");
  
  return FALSE;
}

static void    
ltable_spew(LTable* lt)
{
  guint i;
  g_print ("Flat table:\n");
  i = 0;
  while (i < lt->listeners->len)
    {
      GNode* node = g_ptr_array_index(lt->listeners, i);
      LTableEntry* lte = node ? node->data : NULL;
      g_print ("%u `%s' %p\n", i, lte ? lte->name : "", node);
      
      ++i;
    }
  
  g_node_traverse(lt->tree, G_PRE_ORDER, G_TRAVERSE_ALL, -1, spew_func, NULL);
}
#endif