summaryrefslogtreecommitdiff
path: root/client/gdaemonfileenumerator.c
blob: d056359270afc1696064fd8a95bd8692db7a9918 (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
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
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gdaemonfileenumerator.h>
#include <gio/gio.h>
#include <gvfsdaemondbus.h>
#include <gvfsdaemonprotocol.h>
#include "gdaemonfile.h"
#include "metatree.h"
#include <gvfsdbus.h>

#define OBJ_PATH_PREFIX "/org/gtk/vfs/client/enumerator/"

/* atomic */
static volatile gint path_counter = 1;

G_LOCK_DEFINE_STATIC(infos);

struct _GDaemonFileEnumerator
{
  GFileEnumerator parent;

  gint id;
  GDBusConnection *sync_connection; /* NULL if async, i.e. we're listening on main dbus connection */

  GVfsDBusEnumerator *skeleton;

  /* protected by infos lock */
  GList *infos;
  gboolean done;

  /* For async ops, also protected by infos lock */
  int async_requested_files;
  gulong cancelled_tag;
  guint timeout_tag;
  GSimpleAsyncResult *async_res;
  GMainLoop *next_files_mainloop;
  GMainContext *next_files_context;
  GSource *next_files_sync_timeout_source;
  GMutex next_files_mutex;

  GFileAttributeMatcher *matcher;
  MetaTree *metadata_tree;
};

G_DEFINE_TYPE (GDaemonFileEnumerator, g_daemon_file_enumerator, G_TYPE_FILE_ENUMERATOR)

static GFileInfo *       g_daemon_file_enumerator_next_file   (GFileEnumerator  *enumerator,
							       GCancellable     *cancellable,
							       GError          **error);
static void              g_daemon_file_enumerator_next_files_async (GFileEnumerator     *enumerator,
								    int                  num_files,
								    int                  io_priority,
								    GCancellable        *cancellable,
								    GAsyncReadyCallback  callback,
								    gpointer             user_data);
static GList *           g_daemon_file_enumerator_next_files_finish (GFileEnumerator  *enumerator,
								     GAsyncResult     *result,
								     GError          **error);
static gboolean          g_daemon_file_enumerator_close       (GFileEnumerator  *enumerator,
							       GCancellable     *cancellable,
							       GError          **error);
static void              g_daemon_file_enumerator_close_async (GFileEnumerator      *enumerator,
							       int                   io_priority,
							       GCancellable         *cancellable,
							       GAsyncReadyCallback   callback,
							       gpointer              user_data);
static gboolean          g_daemon_file_enumerator_close_finish (GFileEnumerator      *enumerator,
							       GAsyncResult         *result,
							       GError              **error);
static void              trigger_async_done (GDaemonFileEnumerator *daemon, gboolean ok);


static void
free_info_list (GList *infos)
{
  g_list_free_full (infos, g_object_unref);
}

static GSource *
add_timeout_for_context (GMainContext *context,
                                guint32        interval,
                                GSourceFunc    function,
                                gpointer       data)
{
  GSource *source;

  g_return_val_if_fail (function != NULL, 0);

  source = g_timeout_source_new (interval);

  g_source_set_callback (source, function, data, NULL);
  g_source_attach (source, context);

  return source;
}


static void
g_daemon_file_enumerator_finalize (GObject *object)
{
  GDaemonFileEnumerator *daemon;

  daemon = G_DAEMON_FILE_ENUMERATOR (object);

  if (daemon->skeleton)
    {
      g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (daemon->skeleton));
      g_object_unref (daemon->skeleton);
    }

  free_info_list (daemon->infos);

  g_file_attribute_matcher_unref (daemon->matcher);
  if (daemon->metadata_tree)
    meta_tree_unref (daemon->metadata_tree);

  g_clear_object (&daemon->sync_connection);

  if (daemon->next_files_context)
    g_main_context_unref (daemon->next_files_context);

  g_mutex_clear (&daemon->next_files_mutex);
  
  if (G_OBJECT_CLASS (g_daemon_file_enumerator_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_daemon_file_enumerator_parent_class)->finalize) (object);
}


static void
g_daemon_file_enumerator_class_init (GDaemonFileEnumeratorClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
  
  gobject_class->finalize = g_daemon_file_enumerator_finalize;

  enumerator_class->next_file = g_daemon_file_enumerator_next_file;
  enumerator_class->next_files_async = g_daemon_file_enumerator_next_files_async;
  enumerator_class->next_files_finish = g_daemon_file_enumerator_next_files_finish;
  enumerator_class->close_fn = g_daemon_file_enumerator_close;
  enumerator_class->close_async = g_daemon_file_enumerator_close_async;
  enumerator_class->close_finish = g_daemon_file_enumerator_close_finish;
}

static void
next_files_sync_check (GDaemonFileEnumerator *enumerator)
{
  g_mutex_lock (&enumerator->next_files_mutex);
  if ((enumerator->infos || enumerator->done) && 
      enumerator->next_files_mainloop != NULL)
    {
      g_main_loop_quit (enumerator->next_files_mainloop);
    }
  g_mutex_unlock (&enumerator->next_files_mutex);
}

static gboolean
handle_done (GVfsDBusEnumerator *object,
             GDBusMethodInvocation *invocation,
             gpointer user_data)
{
  GDaemonFileEnumerator *enumerator = G_DAEMON_FILE_ENUMERATOR (user_data);

  G_LOCK (infos);
  enumerator->done = TRUE;
  if (enumerator->async_requested_files > 0)
    trigger_async_done (enumerator, TRUE);
  next_files_sync_check (enumerator);
  G_UNLOCK (infos);

  gvfs_dbus_enumerator_complete_done (object, invocation);
  
  return TRUE;
}

static gboolean
handle_got_info (GVfsDBusEnumerator *object,
                 GDBusMethodInvocation *invocation,
                 GVariant *arg_infos,
                 gpointer user_data)
{
  GDaemonFileEnumerator *enumerator = G_DAEMON_FILE_ENUMERATOR (user_data);
  GList *infos;
  GFileInfo *info;
  GVariantIter iter;
  GVariant *child;

  infos = NULL;
    
  g_variant_iter_init (&iter, arg_infos);
  while ((child = g_variant_iter_next_value (&iter)))
    {
      info = _g_dbus_get_file_info (child, NULL);
      if (info)
        g_assert (G_IS_FILE_INFO (info));

      if (info)
        infos = g_list_prepend (infos, info);

      g_variant_unref (child);
    }
  
  infos = g_list_reverse (infos);
  
  G_LOCK (infos);
  enumerator->infos = g_list_concat (enumerator->infos, infos);
  if (enumerator->async_requested_files > 0 &&
      g_list_length (enumerator->infos) >= enumerator->async_requested_files)
    trigger_async_done (enumerator, TRUE);
  next_files_sync_check (enumerator);
  G_UNLOCK (infos);

  gvfs_dbus_enumerator_complete_got_info (object, invocation);
  
  return TRUE;
}

static void
create_skeleton (GDaemonFileEnumerator *daemon,
                 GDBusConnection *connection,
                 const char *obj_path)
{
  GVfsDBusEnumerator *skeleton;
  GError *error;

  if (daemon->next_files_context)
    g_main_context_push_thread_default (daemon->next_files_context);

  skeleton = gvfs_dbus_enumerator_skeleton_new ();
  g_signal_connect (skeleton, "handle-done", G_CALLBACK (handle_done), daemon);
  g_signal_connect (skeleton, "handle-got-info", G_CALLBACK (handle_got_info), daemon);

  error = NULL;
  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
                                         connection,
                                         obj_path,
                                         &error))
    {
      g_warning ("Error registering path: %s (%s, %d)\n",
                  error->message, g_quark_to_string (error->domain), error->code);
      g_error_free (error);
    }

  if (daemon->next_files_context)
    g_main_context_pop_thread_default (daemon->next_files_context);

  daemon->skeleton = skeleton;
}

static void
g_daemon_file_enumerator_init (GDaemonFileEnumerator *daemon)
{
  daemon->id = g_atomic_int_add (&path_counter, 1);

  g_mutex_init (&daemon->next_files_mutex);
}

GDaemonFileEnumerator *
g_daemon_file_enumerator_new (GFile *file,
                              GVfsDBusMount *mount_proxy,
			      const char *attributes,
			      gboolean sync)
{
  GDaemonFileEnumerator *daemon;
  char *treename;
  char *path;

  daemon = g_object_new (G_TYPE_DAEMON_FILE_ENUMERATOR,
                         "container", file,
                         NULL);

  if (sync)
    daemon->next_files_context = g_main_context_new ();

  path = g_daemon_file_enumerator_get_object_path (daemon);

  create_skeleton (daemon,
                   g_dbus_proxy_get_connection (G_DBUS_PROXY (mount_proxy)),
                   path);

  g_free (path);

  daemon->matcher = g_file_attribute_matcher_new (attributes);
  if (g_file_attribute_matcher_enumerate_namespace (daemon->matcher, "metadata") ||
      g_file_attribute_matcher_enumerate_next (daemon->matcher) != NULL)
    {
      treename = g_mount_spec_to_string (G_DAEMON_FILE (file)->mount_spec);
      daemon->metadata_tree = meta_tree_lookup_by_name (treename, FALSE);
      g_free (treename);
    }

  return daemon;
}

static gboolean
enumerate_keys_callback (const char *key,
			 MetaKeyType type,
			 gpointer value,
			 gpointer user_data)
{
  GFileInfo *info = G_FILE_INFO (user_data);
  char *attr;

  attr = g_strconcat ("metadata::", key, NULL);

  if (type == META_KEY_TYPE_STRING)
    g_file_info_set_attribute_string (info, attr, (char *)value);
  else
    g_file_info_set_attribute_stringv (info, attr, (char **)value);

  g_free (attr);

  return TRUE;
}

static void
add_metadata (GFileInfo *info,
	      GDaemonFileEnumerator *daemon)
{
  GFile *container;
  const char *name;
  char *path;

  if (!daemon->metadata_tree)
    return;

  name = g_file_info_get_name (info);
  container = g_file_enumerator_get_container (G_FILE_ENUMERATOR (daemon));
  path = g_build_filename (G_DAEMON_FILE (container)->path, name, NULL);

  g_file_info_set_attribute_mask (info, daemon->matcher);
  meta_tree_enumerate_keys (daemon->metadata_tree, path,
			    enumerate_keys_callback, info);
  g_file_info_unset_attribute_mask (info);

  g_free (path);
}

static GCancellable *
simple_async_result_get_cancellable (GSimpleAsyncResult *res)
{
  return g_object_get_data (G_OBJECT (res), "file-enumerator-cancellable");
}

static void
simple_async_result_set_cancellable (GSimpleAsyncResult *res,
                                     GCancellable       *cancellable)
{
  if (!cancellable)
    return;

  g_object_set_data_full (G_OBJECT (res),
                          "file-enumerator-cancellable",
                          g_object_ref (cancellable),
                          g_object_unref);
}

/* Called with infos lock held */
static void
trigger_async_done (GDaemonFileEnumerator *daemon, gboolean ok)
{
  GList *rest, *l;
  
  if (daemon->cancelled_tag != 0)
    {
      GCancellable *cancellable = simple_async_result_get_cancellable (daemon->async_res);

      /* If ok, we're a normal callback on the main thread,
	 ensure protection against a thread cancelling and
	 running the callback again.
	 If !ok then we're in a callback which may be
	 from another thread, but we're guaranteed that
	 cancellation will only happen once. However
	 we can't use g_cancellable_disconnect, as this
	 deadlocks if we call it from within the signal
	 handler.
      */
      if (ok)
	g_cancellable_disconnect (cancellable,
				  daemon->cancelled_tag);
      else
	g_signal_handler_disconnect (cancellable,
				     daemon->cancelled_tag);
    }

  /* cancelled signal handler won't execute after this */

  /* TODO: There is actually a small race here, where
     trigger_async_done could happen twice if the operation
     succeeds for some other reason and cancellation happens
     on some other thread right before the above call.
     However, this can only happen if cancellation happens
     outside the main thread which is kinda unusual for async
     ops. However, it would be nice to handle this too. */

  if (ok)
    {
      l = daemon->infos;
      rest = g_list_nth (l, daemon->async_requested_files);
      if (rest)
	{
	  /* Split list */
	  rest->prev->next = NULL;
	  rest->prev = NULL;
	}
      daemon->infos = rest;

      g_list_foreach (l, (GFunc)add_metadata, daemon);

      g_simple_async_result_set_op_res_gpointer (daemon->async_res,
						 l,
						 (GDestroyNotify)free_info_list);
    }

  g_simple_async_result_complete_in_idle (daemon->async_res);
  
  daemon->cancelled_tag = 0;

  if (daemon->timeout_tag != 0)
    g_source_remove (daemon->timeout_tag);
  daemon->timeout_tag = 0;
  
  daemon->async_requested_files = 0;
  
  g_object_unref (daemon->async_res);
  daemon->async_res = NULL;
}

char  *
g_daemon_file_enumerator_get_object_path (GDaemonFileEnumerator *enumerator)
{
  return g_strdup_printf (OBJ_PATH_PREFIX"%d", enumerator->id);
}

void
g_daemon_file_enumerator_set_sync_connection (GDaemonFileEnumerator *enumerator,
                                              GDBusConnection       *connection)
{
  enumerator->sync_connection = g_object_ref (connection);
}

static gboolean
sync_timeout (gpointer data)
{
  GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (data);

  g_mutex_lock (&daemon->next_files_mutex);
  g_main_loop_quit (daemon->next_files_mainloop);
  g_mutex_unlock (&daemon->next_files_mutex);
  
  return FALSE;
}

static GFileInfo *
g_daemon_file_enumerator_next_file (GFileEnumerator *enumerator,
				    GCancellable     *cancellable,
				    GError **error)
{
  GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (enumerator);
  GFileInfo *info;

  if (daemon->sync_connection == NULL)
    {
      /* The enumerator was initialized by an async call, so responses will
         come to the async dbus connection. We can't pump that as that would
         cause all sort of filters and stuff to run, possibly on the wrong
         thread. If you want to do async next_files you must create the
         enumerator asynchrounously.
      */
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                           "Can't do synchronous next_files() on a file enumerator created asynchronously");
      return NULL;
    }

  if (! daemon->infos && ! daemon->done)
    {
      /* Wait for incoming data */
      g_mutex_lock (&daemon->next_files_mutex);
      daemon->next_files_mainloop = g_main_loop_new (daemon->next_files_context, FALSE);

      g_mutex_unlock (&daemon->next_files_mutex);

      g_main_context_push_thread_default (daemon->next_files_context);
      daemon->next_files_sync_timeout_source = add_timeout_for_context (daemon->next_files_context,
									G_VFS_DBUS_TIMEOUT_MSECS,
									sync_timeout, daemon);
      g_main_loop_run (daemon->next_files_mainloop);
      g_main_context_pop_thread_default (daemon->next_files_context);

      g_mutex_lock (&daemon->next_files_mutex);
      g_source_destroy (daemon->next_files_sync_timeout_source);
      g_source_unref (daemon->next_files_sync_timeout_source);

      g_main_loop_unref (daemon->next_files_mainloop);
      daemon->next_files_mainloop = NULL;
      g_mutex_unlock (&daemon->next_files_mutex);
    }
  
  info = NULL;

  G_LOCK (infos);
  if (daemon->infos)
    {
      info = daemon->infos->data;
      if (info)
        {
          g_assert (G_IS_FILE_INFO (info));
          add_metadata (G_FILE_INFO (info), daemon);
        }
      daemon->infos = g_list_delete_link (daemon->infos, daemon->infos);
    }
  G_UNLOCK (infos);

  if (info)
    g_assert (G_IS_FILE_INFO (info));
      
  return info;
}

static void
async_cancelled (GCancellable *cancellable,
		 GDaemonFileEnumerator *daemon)
{
  g_simple_async_result_set_error (daemon->async_res,
				   G_IO_ERROR,
				   G_IO_ERROR_CANCELLED,
				   _("Operation was cancelled"));
  G_LOCK (infos);
  trigger_async_done (daemon, FALSE);
  G_UNLOCK (infos);
}

static gboolean
async_timeout (gpointer data)
{
  GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (data);

  G_LOCK (infos);
  trigger_async_done (daemon, TRUE);
  G_UNLOCK (infos);
  return FALSE;
}

static void
g_daemon_file_enumerator_next_files_async (GFileEnumerator     *enumerator,
					   int                  num_files,
					   int                  io_priority,
					   GCancellable        *cancellable,
					   GAsyncReadyCallback  callback,
					   gpointer             user_data)
{
  GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (enumerator);

  if (daemon->sync_connection != NULL)
    {
      /* If the enumerator was created synchronously then the connection used
       * for return messages will be the private connection for that thread.
       * We can't rely on it being pumped, so we don't support this.
       * We could possibly pump it ourselves in this case, but i'm not sure
       * how much sense this makes, so we don't for now.
       */
      g_simple_async_report_error_in_idle  (G_OBJECT (enumerator),
					    callback,
					    user_data,
					    G_IO_ERROR, G_IO_ERROR_FAILED,
					    "Can't do asynchronous next_files() on a file enumerator created synchronously");
      return;
    }
  
  G_LOCK (infos);
  daemon->cancelled_tag = 0;
  daemon->timeout_tag = 0;
  daemon->async_requested_files = num_files;
  daemon->async_res = g_simple_async_result_new (G_OBJECT (enumerator), callback, user_data,
						 g_daemon_file_enumerator_next_files_async);
  simple_async_result_set_cancellable (daemon->async_res, cancellable);

  /* Maybe we already have enough info to fulfill the requeust already */
  if (daemon->done ||
      g_list_length (daemon->infos) >= daemon->async_requested_files)
    trigger_async_done (daemon, TRUE);
  else
    {
      daemon->timeout_tag = g_timeout_add (G_VFS_DBUS_TIMEOUT_MSECS,
					   async_timeout, daemon);
      if (cancellable)
	daemon->cancelled_tag =
	  g_cancellable_connect (cancellable, (GCallback)async_cancelled,
				 daemon, NULL);
    }
  
  G_UNLOCK (infos);
}

static GList *
g_daemon_file_enumerator_next_files_finish (GFileEnumerator  *enumerator,
					    GAsyncResult     *res,
					    GError          **error)
{
  GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (res);
  GCancellable *cancellable;
  GList *l;

  cancellable = simple_async_result_get_cancellable (result);
  if (g_cancellable_is_cancelled (cancellable))
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_CANCELLED,
                   "%s", _("Operation was cancelled"));
      return NULL;
    }

  l = g_simple_async_result_get_op_res_gpointer (result);
  g_list_foreach (l, (GFunc)g_object_ref, NULL);
  return g_list_copy (l);
}

static gboolean
g_daemon_file_enumerator_close (GFileEnumerator *enumerator,
				GCancellable     *cancellable,
				GError          **error)
{
  /*GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (enumerator); */

  return TRUE;
}


/* We want an explicitly async version of close (doing nothing) to avoid
   the default thread-using version. */
static void
g_daemon_file_enumerator_close_async (GFileEnumerator      *enumerator,
				      int                   io_priority,
				      GCancellable         *cancellable,
				      GAsyncReadyCallback   callback,
				      gpointer              user_data)
{
  GSimpleAsyncResult *res;

  res = g_simple_async_result_new (G_OBJECT (enumerator), callback, user_data,
				   g_daemon_file_enumerator_close_async);
  simple_async_result_set_cancellable (res, cancellable);
  g_simple_async_result_complete_in_idle (res);
  g_object_unref (res);
}

static gboolean
g_daemon_file_enumerator_close_finish (GFileEnumerator      *enumerator,
				       GAsyncResult         *result,
				       GError              **error)
{
  GCancellable *cancellable;
  
  cancellable = simple_async_result_get_cancellable (G_SIMPLE_ASYNC_RESULT (result));
  if (g_cancellable_is_cancelled (cancellable))
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_CANCELLED,
                   "%s", _("Operation was cancelled"));
      return FALSE;
    }

  return TRUE;
}