summaryrefslogtreecommitdiff
path: root/packages/pcmanfm/files/no-fam.patch
blob: b87a02df013783760cb2c53657ff8cc56bd288c2 (plain)
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
diff -urNd ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/configure.in pcmanfm-0.3.0.1/configure.in
--- ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/configure.in	2006-08-20 15:45:28.000000000 +0100
+++ pcmanfm-0.3.0.1/configure.in	2006-08-20 20:37:36.000000000 +0100
@@ -63,8 +63,8 @@
   LIBS="$LIBS $FAM_LIBS"
   AC_CHECK_FUNCS([FAMNoExists])
   LIBS="$save_LIBS"
-else
-  AC_MSG_ERROR([Fatal Error: no fam or gamin detected.])
+#else
+#  AC_MSG_ERROR([Fatal Error: no fam or gamin detected.])
 fi
 
 AC_SUBST([FAM_CFLAGS])
diff -urNd ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c
--- ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c	2006-07-16 20:00:46.000000000 +0100
+++ pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c	2006-08-20 20:54:07.000000000 +0100
@@ -31,7 +31,6 @@
 VFSFileMonitorCallbackEntry;
 
 static GHashTable* monitor_hash = NULL;
-static FAMConnection fam;
 static GIOChannel* fam_io_channel = NULL;
 static guint fam_io_watch = 0;
 
@@ -40,6 +39,8 @@
                               GIOCondition cond,
                               gpointer user_data );
 
+#ifdef HAVE_FAM_H
+static FAMConnection fam;
 
 static gboolean connect_to_fam()
 {
@@ -81,6 +82,16 @@
         FAMClose( &fam );
     }
 }
+#else
+static gboolean connect_to_fam ()
+{
+	return FALSE;
+}
+
+static void disconnect_from_fam ()
+{
+}
+#endif
 
 /* final cleanup */
 void vfs_file_monitor_clean()
@@ -109,6 +120,7 @@
                                       VFSFileMonitorCallback cb,
                                       gpointer user_data )
 {
+#ifdef HAVE_FAM_H
     VFSFileMonitor * monitor;
     VFSFileMonitorCallbackEntry cb_ent;
     gboolean add_new = FALSE;
@@ -154,12 +166,16 @@
     }
     ++monitor->n_ref;
     return monitor;
+#else
+	return NULL;
+#endif
 }
 
 void vfs_file_monitor_remove( VFSFileMonitor* fm,
                               VFSFileMonitorCallback cb,
                               gpointer user_data )
 {
+#ifdef HAVE_FAM_H
     int i;
     VFSFileMonitorCallbackEntry* callbacks;
     if ( cb && fm->callbacks )
@@ -183,12 +199,14 @@
         g_array_free( fm->callbacks, TRUE );
         g_slice_free( VFSFileMonitor, fm );
     }
+#endif
 }
 
 static void reconnect_fam( gpointer key,
                            gpointer value,
                            gpointer user_data )
 {
+#ifdef HAVE_FAM_H
     struct stat file_stat;
     VFSFileMonitor* monitor = ( VFSFileMonitor* ) value;
     const char* path = ( const char* ) key;
@@ -209,6 +227,7 @@
                             monitor );
         }
     }
+#endif
 }
 
 /* event handler of all FAM events */
@@ -216,6 +235,7 @@
                               GIOCondition cond,
                               gpointer user_data )
 {
+#ifdef HAVE_FAM_H
     FAMEvent evt;
     VFSFileMonitor* monitor = NULL;
     VFSFileMonitorCallbackEntry* cb;
@@ -266,5 +286,8 @@
         }
     }
     return TRUE;
+#else
+	return TRUE;
+#endif
 }
 
diff -urNd ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c~ pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c~
--- ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c~	1970-01-01 01:00:00.000000000 +0100
+++ pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.c~	2006-08-20 20:50:49.000000000 +0100
@@ -0,0 +1,292 @@
+/*
+*  C Implementation: vfs-monitor
+*
+* Description:
+*
+*
+* Author: Hong Jen Yee (PCMan) <pcman.tw (AT) gmail.com>, (C) 2006
+*
+* Copyright: See COPYING file that comes with this distribution
+*
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "vfs-file-monitor.h"
+#include <sys/types.h>  /* for stat */
+#include <sys/stat.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "glib-mem.h"
+
+typedef struct
+{
+    VFSFileMonitorCallback callback;
+    gpointer user_data;
+}
+VFSFileMonitorCallbackEntry;
+
+static GHashTable* monitor_hash = NULL;
+static FAMConnection fam;
+static GIOChannel* fam_io_channel = NULL;
+static guint fam_io_watch = 0;
+
+/* event handler of all FAM events */
+static gboolean on_fam_event( GIOChannel *channel,
+                              GIOCondition cond,
+                              gpointer user_data );
+
+#ifdef HAVE_FAM_H
+static gboolean connect_to_fam()
+{
+    if ( FAMOpen( &fam ) )
+    {
+        fam_io_channel = NULL;
+        fam.fd = -1;
+        g_warning( "There is no FAM/gamin server\n" );
+        return FALSE;
+    }
+#if HAVE_FAMNOEXISTS
+    /*
+    * Disable the initital directory content loading.
+    * This can greatly speed up directory loading, but
+    * unfortunately, it's not compatible with original FAM.
+    */
+    FAMNoExists( &fam );  /* This is an extension of gamin */
+#endif
+
+    fam_io_channel = g_io_channel_unix_new( fam.fd );
+    g_io_channel_set_encoding( fam_io_channel, NULL, NULL );
+    g_io_channel_set_buffered( fam_io_channel, FALSE );
+
+    fam_io_watch = g_io_add_watch( fam_io_channel,
+                                   G_IO_IN | G_IO_HUP,
+                                   on_fam_event,
+                                   NULL );
+    return TRUE;
+}
+
+static void disconnect_from_fam()
+{
+    if ( fam_io_channel )
+    {
+        g_io_channel_unref( fam_io_channel );
+        fam_io_channel = NULL;
+        g_source_remove( fam_io_watch );
+
+        FAMClose( &fam );
+    }
+}
+#else
+static gboolean connect_to_fam ()
+{
+	return FALSE;
+}
+
+static void disconnect_from_fam ()
+{
+}
+#endif
+
+/* final cleanup */
+void vfs_file_monitor_clean()
+{
+    disconnect_from_fam();
+    if ( monitor_hash )
+    {
+        g_hash_table_destroy( monitor_hash );
+        monitor_hash = NULL;
+    }
+}
+
+/*
+* Init monitor:
+* Establish connection with gamin/fam.
+*/
+gboolean vfs_file_monitor_init()
+{
+    monitor_hash = g_hash_table_new( g_str_hash, g_str_equal );
+    if ( ! connect_to_fam() )
+        return FALSE;
+    return TRUE;
+}
+
+VFSFileMonitor* vfs_file_monitor_add( const char* path,
+                                      VFSFileMonitorCallback cb,
+                                      gpointer user_data )
+{
+#ifdef HAVE_FAM_H
+    VFSFileMonitor * monitor;
+    VFSFileMonitorCallbackEntry cb_ent;
+    gboolean add_new = FALSE;
+    struct stat file_stat;
+
+    if ( ! monitor_hash )
+    {
+        if ( !vfs_file_monitor_init() )
+            return NULL;
+    }
+    monitor = ( VFSFileMonitor* ) g_hash_table_lookup ( monitor_hash, path );
+    if ( ! monitor )
+    {
+        monitor = g_slice_new0( VFSFileMonitor );
+        monitor->path = g_strdup( path );
+        monitor->callbacks = g_array_new ( FALSE, FALSE, sizeof( VFSFileMonitorCallbackEntry ) );
+        g_hash_table_insert ( monitor_hash,
+                              path,
+                              monitor );
+        if ( lstat( path, &file_stat ) != -1 )
+        {
+            if ( S_ISDIR( file_stat.st_mode ) )
+            {
+                FAMMonitorDirectory( &fam,
+                                     path,
+                                     &monitor->request,
+                                     monitor );
+            }
+            else
+            {
+                FAMMonitorFile( &fam,
+                                path,
+                                &monitor->request,
+                                monitor );
+            }
+        }
+    }
+    if ( cb )
+    { /* Install a callback */
+        cb_ent.callback = cb;
+        cb_ent.user_data = user_data;
+        monitor->callbacks = g_array_append_val( monitor->callbacks, cb_ent );
+    }
+    ++monitor->n_ref;
+    return monitor;
+#else
+	return NULL;
+#endif
+}
+
+void vfs_file_monitor_remove( VFSFileMonitor* fm,
+                              VFSFileMonitorCallback cb,
+                              gpointer user_data )
+{
+#ifdef HAVE_FAM_H
+    int i;
+    VFSFileMonitorCallbackEntry* callbacks;
+    if ( cb && fm->callbacks )
+    {
+        callbacks = ( VFSFileMonitorCallbackEntry* ) fm->callbacks->data;
+        for ( i = 0; i < fm->callbacks->len; ++i )
+        {
+            if ( callbacks[ i ].callback == cb && callbacks[ i ].user_data == user_data )
+            {
+                fm->callbacks = g_array_remove_index_fast ( fm->callbacks, i );
+                break;
+            }
+        }
+    }
+    --fm->n_ref;
+    if ( 0 >= fm->n_ref )
+    {
+        FAMCancelMonitor( &fam, &fm->request );
+        g_hash_table_remove( monitor_hash, fm->path );
+        g_free( fm->path );
+        g_array_free( fm->callbacks, TRUE );
+        g_slice_free( VFSFileMonitor, fm );
+    }
+#endif
+}
+
+static void reconnect_fam( gpointer key,
+                           gpointer value,
+                           gpointer user_data )
+{
+#ifdef HAVE_FAM_H
+    struct stat file_stat;
+    VFSFileMonitor* monitor = ( VFSFileMonitor* ) value;
+    const char* path = ( const char* ) key;
+    if ( lstat( path, &file_stat ) != -1 )
+    {
+        if ( S_ISDIR( file_stat.st_mode ) )
+        {
+            FAMMonitorDirectory( &fam,
+                                 path,
+                                 &monitor->request,
+                                 monitor );
+        }
+        else
+        {
+            FAMMonitorFile( &fam,
+                            path,
+                            &monitor->request,
+                            monitor );
+        }
+    }
+#endif
+}
+
+/* event handler of all FAM events */
+static gboolean on_fam_event( GIOChannel *channel,
+                              GIOCondition cond,
+                              gpointer user_data )
+{
+#ifdef HAVE_FAM_H
+    FAMEvent evt;
+    VFSFileMonitor* monitor = NULL;
+    VFSFileMonitorCallbackEntry* cb;
+    VFSFileMonitorCallback func;
+    int i;
+
+    if ( cond & G_IO_HUP )
+    {
+        disconnect_from_fam();
+        if ( g_hash_table_size ( monitor_hash ) > 0 )
+        {
+            /*
+              Disconnected from FAM server, but there are still monitors.
+              This may be caused by crash of FAM server.
+              So we have to reconnect to FAM server.
+            */
+            connect_to_fam();
+            g_hash_table_foreach( monitor_hash, ( GHFunc ) reconnect_fam, NULL );
+        }
+        return TRUE; /* don't need to remove the event source since
+                            it has been removed by disconnect_from_fam(). */
+    }
+
+    while ( FAMPending( &fam ) )
+    {
+        if ( FAMNextEvent( &fam, &evt ) > 0 )
+        {
+            monitor = ( VFSFileMonitor* ) evt.userdata;
+            switch ( evt.code )
+            {
+            case FAMCreated:
+            case FAMDeleted:
+            case FAMChanged:
+                /* Call the callback functions */
+                if ( monitor->callbacks && monitor->callbacks->len )
+                {
+                    cb = ( VFSFileMonitorCallbackEntry* ) monitor->callbacks->data;
+                    for ( i = 0; i < monitor->callbacks->len; ++i )
+                    {
+                        func = cb[ i ].callback;
+                        func( monitor, evt.code, evt.filename, cb[ i ].user_data );
+                    }
+                }
+                break;
+            default:
+                return TRUE;  /* Other events are not supported */
+            }
+        }
+    }
+    return TRUE;
+#else
+	return TRUE;
+#endif
+}
+
diff -urNd ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.h pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.h
--- ../pcmanfm-0.3.0.1-r0/pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.h	2006-04-03 00:38:33.000000000 +0100
+++ pcmanfm-0.3.0.1/src/vfs/vfs-file-monitor.h	2006-08-20 20:47:00.000000000 +0100
@@ -20,7 +20,16 @@
 #define _VFS_FILE_MONITOR_H_
 
 #include <glib.h>
+#ifdef HAVE_FAM_H
 #include <fam.h>
+#else
+#	define FAMCreated 0
+#	define FAMDeleted 1
+#	define FAMChanged 2
+typedef struct {
+	int reqnum;
+} FAMRequest;
+#endif
 
 G_BEGIN_DECLS