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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set expandtab ts=4 shiftwidth=4: */
/*
* Copyright (c) 2008, 2010 Oracle and/or its affiliates, Inc. All rights
* reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lin Ma <lin.ma@sun.com>
*/
#include "config.h"
#include <glib.h>
#include "fen-helper.h"
#include "fen-kernel.h"
#ifdef GIO_COMPILATION
#include <gio/gfilemonitor.h>
#else
#include "gam_event.h"
#include "gam_server.h"
#include "gam_protocol.h"
#endif
#ifdef GIO_COMPILATION
#define FH_W if (FALSE) g_debug
#else
#include "gam_error.h"
#define FH_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
#endif
G_LOCK_EXTERN (fen_lock);
/* misc */
static void
scan_children_init(node_t *f, gpointer sub)
{
gboolean emit;
gint event;
FH_W ("%s %s [0x%p]\n", __func__, NODE_NAME(f), f);
#ifdef GIO_COMPILATION
emit = FALSE;
event = G_FILE_MONITOR_EVENT_CREATED;
#else
emit = TRUE;
event = GAMIN_EVENT_EXISTS;
#endif
if (!NODE_HAS_FLAG(f, NODE_FLAG_SNAPSHOT_UPDATED)) {
/* TODO snapshot should also compare to the sub created timestamp. */
/* GIO initially doesn't emit created/existed events. */
node_create_children_snapshot(f, event, emit);
} else {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init (&iter, f->children);
while (g_hash_table_iter_next (&iter, NULL, &value)) {
node_t *child = (node_t *)value;
#ifdef GIO_COMPILATION
/* GIO initially doesn't emit created/existed events. */
/* g_file_monitor_emit_event(G_FILE_MONITOR(sub), child->gfile, NULL, event); */
#else
gam_server_emit_one_event(NODE_NAME(child), gam_subscription_is_dir(sub), event, sub, 1);
#endif
}
}
}
/**
* fen_add
*
* Won't hold a ref, we have a timout callback to clean unused node_t.
* If there is no value for a key, add it and return it; else return the old
* one.
*/
void
fen_add (const gchar *filename, gpointer sub, gboolean is_mondir)
{
node_t* f;
g_assert (filename);
g_assert (sub);
G_LOCK (fen_lock);
f = node_find(NULL, filename, TRUE);
FH_W ("%s 0x%p sub[0x%p] %s\n", __func__, f, sub, filename);
g_assert (f);
/* Update timestamp, the events in global queue will compare itself to this
* timestamp to decide if be emitted. TODO, timestamp should be per sub.
*/
if (!NODE_IS_ACTIVE(f)) {
g_get_current_time(&f->atv);
}
if (is_mondir) {
f->dir_subs = g_list_prepend(f->dir_subs, sub);
} else {
f->subs = g_list_prepend(f->subs, sub);
}
if (NODE_HAS_STATE(f, NODE_STATE_ASSOCIATED) ||
(node_lstat(f) == 0 && port_add(f) == 0)) {
#ifndef GIO_COMPILATION
gam_server_emit_one_event (NODE_NAME(f),
gam_subscription_is_dir (sub), GAMIN_EVENT_EXISTS, sub, 1);
#endif
if (is_mondir) {
scan_children_init (f, sub);
}
} else {
#ifndef GIO_COMPILATION
gam_server_emit_one_event (NODE_NAME(f),
gam_subscription_is_dir (sub), GAMIN_EVENT_DELETED, sub, 1);
#endif
node_adjust_deleted (f);
}
#ifndef GIO_COMPILATION
gam_server_emit_one_event (NODE_NAME(f),
gam_subscription_is_dir (sub), GAMIN_EVENT_ENDEXISTS, sub, 1);
#endif
G_UNLOCK (fen_lock);
}
void
fen_remove (const gchar *filename, gpointer sub, gboolean is_mondir)
{
node_t* f;
g_assert (filename);
g_assert (sub);
G_LOCK (fen_lock);
f = node_find(NULL, filename, FALSE);
FH_W ("%s 0x%p sub[0x%p] %s\n", __func__, f, sub, filename);
if (f) {
if (is_mondir) {
f->dir_subs = g_list_remove(f->dir_subs, sub);
} else {
f->subs = g_list_remove(f->subs, sub);
}
if (!NODE_IS_ACTIVE(f)) {
node_try_delete (f);
}
}
G_UNLOCK (fen_lock);
}
/**
* fen_init:
*
* FEN subsystem initializing.
*/
gboolean
fen_init ()
{
static gboolean initialized = FALSE;
static gboolean result = FALSE;
G_LOCK (fen_lock);
if (initialized) {
G_UNLOCK (fen_lock);
return result;
}
result = node_class_init();
if (!result) {
G_UNLOCK (fen_lock);
return result;
}
initialized = TRUE;
G_UNLOCK (fen_lock);
return result;
}
|