1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set expandtab ts=4 shiftwidth=4: */
4 * Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Authors: Lin Ma <lin.ma@sun.com>
32 #define NODE_STAT(n) (((node_t*)(n))->stat)
40 #ifdef GIO_COMPILATION
41 #define FN_W if (fn_debug_enabled) g_warning
42 static gboolean fn_debug_enabled = FALSE;
44 #include "gam_error.h"
45 #define FN_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
48 G_LOCK_EXTERN (fen_lock);
49 #define PROCESS_DELETING_INTERVAL 900 /* in second */
51 static node_t* _head = NULL;
52 static GList *deleting_nodes = NULL;
53 static guint deleting_nodes_id = 0;
55 static node_t* node_new (node_t* parent, const gchar* basename);
56 static void node_delete (node_t* parent);
57 static gboolean remove_node_internal (node_t* node, node_op_t* op);
58 static void children_add (node_t *p, node_t *f);
59 static void children_remove (node_t *p, node_t *f);
60 static guint children_foreach_remove (node_t *f, GHRFunc func, gpointer user_data);
61 static void children_foreach (node_t *f, GHFunc func, gpointer user_data);
62 static gboolean children_remove_cb (gpointer key,
67 _dnode_new (const gchar* filename, node_op_t* op)
72 if ((d = g_new (struct _dnode, 1)) != NULL) {
73 d->filename = g_strdup (filename);
74 d->op = g_memdup (op, sizeof (node_op_t));
76 g_get_current_time (&d->tv);
77 g_time_val_add (&d->tv, PROCESS_DELETING_INTERVAL);
83 _dnode_free (struct _dnode* d)
92 g_timeval_lt (GTimeVal *val1, GTimeVal *val2)
94 if (val1->tv_sec < val2->tv_sec)
97 if (val1->tv_sec > val2->tv_sec)
100 /* val1->tv_sec == val2->tv_sec */
101 if (val1->tv_usec < val2->tv_usec)
108 scan_deleting_nodes (gpointer data)
113 GList* deleted_list = NULL;
117 g_get_current_time (&tv_now);
119 if (G_TRYLOCK (fen_lock)) {
120 for (i = deleting_nodes; i; i = i->next) {
121 d = (struct _dnode*)i->data;
122 /* Time to free, try only once */
123 if (g_timeval_lt (&d->tv, &tv_now)) {
124 if ((node = _find_node (d->filename)) != NULL) {
125 remove_node_internal (node, d->op);
128 deleted_list = g_list_prepend (deleted_list, i);
132 for (i = deleted_list; i; i = i->next) {
133 deleting_nodes = g_list_remove_link (deleting_nodes,
135 g_list_free_1 ((GList *)i->data);
137 g_list_free (deleted_list);
139 if (deleting_nodes == NULL) {
140 deleting_nodes_id = 0;
149 _node_get_data (node_t* node)
152 return node->user_data;
156 _node_set_data (node_t* node, gpointer user_data)
158 gpointer data = node->user_data;
160 node->user_data = user_data;
165 _travel_nodes (node_t* node, node_op_t* op)
172 op->hit (node, op->user_data);
175 children = g_hash_table_get_values (node->children);
177 for (i = children; i; i = i->next) {
178 _travel_nodes (i->data, op);
180 g_list_free (children);
185 find_node_internal (node_t* node, const gchar* filename, node_op_t* op)
193 g_assert (filename && filename[0] == '/');
197 str = g_strdup (filename + strlen (NODE_NAME(parent)));
199 if ((token = strtok_r (str, G_DIR_SEPARATOR_S, &lasts)) != NULL) {
201 FN_W ("%s %s + %s\n", __func__, NODE_NAME(parent), token);
202 child = _children_find (parent, token);
206 if (op && op->add_missing) {
207 child = op->add_missing (parent, op->user_data);
212 } while ((token = strtok_r (NULL, G_DIR_SEPARATOR_S, &lasts)) != NULL);
215 g_assert (parent == _head);
219 if (token == NULL && child) {
222 op->hit (child, op->user_data);
230 _find_node (const gchar *filename)
232 return find_node_internal (_head, filename, NULL);
236 _find_node_full (const gchar* filename, node_op_t* op)
238 return find_node_internal (_head, filename, op);
242 _add_node (node_t* parent, const gchar* filename)
247 node_t* child = NULL;
250 g_assert (filename && filename[0] == '/');
252 if (parent == NULL) {
256 str = g_strdup (filename + strlen (NODE_NAME(parent)));
258 if ((token = strtok_r (str, G_DIR_SEPARATOR_S, &lasts)) != NULL) {
260 FN_W ("%s %s + %s\n", __func__, NODE_NAME(parent), token);
261 child = node_new (parent, token);
263 children_add (parent, child);
268 } while ((token = strtok_r (NULL, G_DIR_SEPARATOR_S, &lasts)) != NULL);
282 remove_children (node_t* node, node_op_t* op)
284 FN_W ("%s 0x%p %s\n", __func__, node, NODE_NAME(node));
285 if (_children_num (node) > 0) {
286 children_foreach_remove (node, children_remove_cb,
289 if (_children_num (node) == 0) {
296 remove_node_internal (node_t* node, node_op_t* op)
298 node_t* parent = NULL;
300 * If the parent is passive and doesn't have children, delete it.
301 * NOTE node_delete_deep is a depth first delete recursively.
302 * Top node is deleted in node_cancel_sub
305 g_assert (op && op->pre_del);
307 if (remove_children (node, op)) {
308 if (node->user_data) {
309 if (!op->pre_del (node, op->user_data)) {
313 parent = node->parent;
314 children_remove (parent, node);
316 if (_children_num (parent) == 0) {
317 remove_node_internal (parent, op);
327 _pending_remove_node (node_t* node, node_op_t* op)
332 for (l = deleting_nodes; l; l=l->next) {
333 d = (struct _dnode*) l->data;
334 if (g_ascii_strcasecmp (d->filename, NODE_NAME(node)) == 0) {
339 d = _dnode_new (NODE_NAME(node), op);
341 deleting_nodes = g_list_prepend (deleting_nodes, d);
342 if (deleting_nodes_id == 0) {
343 deleting_nodes_id = g_timeout_add_seconds (PROCESS_DELETING_INTERVAL,
346 g_assert (deleting_nodes_id > 0);
351 _remove_node (node_t* node, node_op_t* op)
353 remove_node_internal (node, op);
357 node_new (node_t* parent, const gchar* basename)
361 g_assert (basename && basename[0]);
362 if ((f = g_new0 (node_t, 1)) != NULL) {
364 f->basename = g_strdup (basename);
365 f->filename = g_build_filename (G_DIR_SEPARATOR_S,
366 NODE_NAME(parent), basename, NULL);
368 f->basename = g_strdup (basename);
369 f->filename = g_strdup (basename);
371 f->children = g_hash_table_new_full (g_str_hash, g_str_equal,
372 NULL, (GDestroyNotify)node_delete);
373 FN_W ("[ %s ] 0x%p %s\n", __func__, f, NODE_NAME(f));
379 node_delete (node_t *f)
381 FN_W ("[ %s ] 0x%p %s\n", __func__, f, NODE_NAME(f));
382 g_assert (g_hash_table_size (f->children) == 0);
383 g_assert (f->user_data == NULL);
385 g_hash_table_unref (f->children);
386 g_free (f->basename);
387 g_free (f->filename);
392 children_add (node_t *p, node_t *f)
394 FN_W ("%s [p] %8s [c] %8s\n", __func__, p->basename, f->basename);
395 g_hash_table_insert (p->children, f->basename, f);
400 children_remove (node_t *p, node_t *f)
402 FN_W ("%s [p] %8s [c] %8s\n", __func__, p->basename, f->basename);
403 g_hash_table_steal (p->children, f->basename);
408 _children_num (node_t *f)
410 return g_hash_table_size (f->children);
414 _children_find (node_t *f, const gchar *basename)
416 return (node_t *) g_hash_table_lookup (f->children, (gpointer)basename);
420 * depth first delete recursively
423 children_remove_cb (gpointer key,
427 node_t* f = (node_t*)value;
428 node_op_t* op = (node_op_t*) user_data;
430 g_assert (f->parent);
432 FN_W ("%s [p] %8s [c] %8s\n", __func__, f->parent->basename, f->basename);
433 if (remove_children (f, op)) {
434 if (f->user_data != NULL) {
435 return op->pre_del (f, op->user_data);
443 children_foreach_remove (node_t *f, GHRFunc func, gpointer user_data)
447 return g_hash_table_foreach_remove (f->children, func, user_data);
451 children_foreach (node_t *f, GHFunc func, gpointer user_data)
455 g_hash_table_foreach (f->children, func, user_data);
461 FN_W ("%s\n", __func__);
463 _head = node_new (NULL, G_DIR_SEPARATOR_S);
465 return _head != NULL;