summaryrefslogtreecommitdiff
path: root/daemon/gvfschannel.c
blob: 021d292a3115216c49c0ff72b3ab867821d1795d (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
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
/* 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 <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <gvfsreadchannel.h>
#include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h>
#include <gvfsdaemonprotocol.h>
#include <gvfsdaemonutils.h>
#include <gvfsjobcloseread.h>
#include <gvfsjobclosewrite.h>
#include <gvfsjoberror.h>
#include <gvfsfileinfo.h>

static void g_vfs_channel_job_source_iface_init (GVfsJobSourceIface *iface);

G_DEFINE_TYPE_WITH_CODE (GVfsChannel, g_vfs_channel, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_VFS_TYPE_JOB_SOURCE,
						g_vfs_channel_job_source_iface_init))

/* TODO: Real P_() */
#define P_(_x) (_x)

enum {
  PROP_0,
  PROP_BACKEND,
  PROP_ACTUAL_CONSUMER
};

typedef struct
{
  GVfsChannel *channel;
  GInputStream *command_stream;
  GCancellable *cancellable;
  char buffer[G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SIZE];
  int buffer_size;
  char *data;
  gsize data_len;
  gsize data_pos;
} RequestReader;

typedef struct {
  guint32 command;
  guint32 arg1;
  guint32 arg2;
  guint32 seq_nr;

  gpointer data;
  gsize data_len;
  gboolean cancelled;
} Request;

struct _GVfsChannelPrivate
{
  GVfsBackend *backend;
  gboolean connection_closed;
  GInputStream *command_stream;
  GCancellable *cancellable;
  GOutputStream *reply_stream;
  int remote_fd;
  GPid actual_consumer;
  
  GVfsBackendHandle backend_handle;
  GVfsJob *current_job;
  guint32 current_job_seq_nr;

  GList *queued_requests;
  
  char reply_buffer[G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SIZE];
  int reply_buffer_pos;
  
  /* output_data is owned by the channel if output_data_free is set,
   * otherwise it is owned by the job. */
  const char *output_data;
  char *output_data_free;
  gsize output_data_size;
  gsize output_data_pos;
};

static void start_request_reader       (GVfsChannel  *channel);
static void g_vfs_channel_get_property (GObject      *object,
					guint         prop_id,
					GValue       *value,
					GParamSpec   *pspec);
static void g_vfs_channel_set_property (GObject      *object,
					guint         prop_id,
					const GValue *value,
					GParamSpec   *pspec);


static void
g_vfs_channel_finalize (GObject *object)
{
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (object);

  if (channel->priv->current_job)
    g_object_unref (channel->priv->current_job);
  channel->priv->current_job = NULL;
  
  if (channel->priv->reply_stream)
    g_object_unref (channel->priv->reply_stream);
  channel->priv->reply_stream = NULL;

  if (channel->priv->command_stream)
    g_object_unref (channel->priv->command_stream);
  channel->priv->command_stream = NULL;

  if (channel->priv->cancellable)
    g_object_unref (channel->priv->cancellable);
  channel->priv->cancellable = NULL;
  
  if (channel->priv->remote_fd != -1)
    close (channel->priv->remote_fd);

  if (channel->priv->backend)
    g_object_unref (channel->priv->backend);
  
  if (G_OBJECT_CLASS (g_vfs_channel_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_vfs_channel_parent_class)->finalize) (object);
}

static void
g_vfs_channel_job_source_iface_init (GVfsJobSourceIface *iface)
{
}

static void
g_vfs_channel_class_init (GVfsChannelClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (GVfsChannelPrivate));
  
  gobject_class->finalize = g_vfs_channel_finalize;
  gobject_class->set_property = g_vfs_channel_set_property;
  gobject_class->get_property = g_vfs_channel_get_property;

  g_object_class_install_property (gobject_class,
				   PROP_BACKEND,
				   g_param_spec_object ("backend",
							P_("Backend"),
							P_("Backend implementation to use"),
							G_VFS_TYPE_BACKEND,
							G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
							G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));

  g_object_class_install_property (gobject_class,
				   PROP_ACTUAL_CONSUMER,
				   g_param_spec_int ("actual-consumer",
                                                     P_("Actual Consumer"),
                                                     P_("The process id of the remote end"),
                                                     G_MININT,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                     G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
}

static void
g_vfs_channel_init (GVfsChannel *channel)
{
  int socket_fds[2];
  int ret;
  
  channel->priv = G_TYPE_INSTANCE_GET_PRIVATE (channel,
					       G_VFS_TYPE_CHANNEL,
					       GVfsChannelPrivate);
  channel->priv->remote_fd = -1;

  ret = socketpair (AF_UNIX, SOCK_STREAM, 0, socket_fds);
  if (ret == -1) 
    g_warning ("Error creating socket pair: %s\n", g_strerror (errno));
  else
    {
      channel->priv->command_stream = g_unix_input_stream_new (socket_fds[0], TRUE);
      channel->priv->cancellable = g_cancellable_new ();
      channel->priv->reply_stream = g_unix_output_stream_new (socket_fds[0], FALSE);
      channel->priv->remote_fd = socket_fds[1];
      
      start_request_reader (channel);
    }
}

static void
g_vfs_channel_set_property (GObject         *object,
			    guint            prop_id,
			    const GValue    *value,
			    GParamSpec      *pspec)
{
  GVfsChannel *channel = G_VFS_CHANNEL (object);
  
  switch (prop_id)
    {
    case PROP_BACKEND:
      if (channel->priv->backend)
	g_object_unref (channel->priv->backend);
      channel->priv->backend = G_VFS_BACKEND (g_value_dup_object (value));
      break;

    case PROP_ACTUAL_CONSUMER:
      channel->priv->actual_consumer = g_value_get_int (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
g_vfs_channel_get_property (GObject    *object,
			    guint       prop_id,
			    GValue     *value,
			    GParamSpec *pspec)
{
  GVfsChannel *channel = G_VFS_CHANNEL (object);
  
  switch (prop_id)
    {
    case PROP_BACKEND:
      g_value_set_object (value, channel->priv->backend);
      break;
    case PROP_ACTUAL_CONSUMER:
      g_value_set_int (value, channel->priv->actual_consumer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
g_vfs_channel_connection_closed (GVfsChannel *channel)
{
  GVfsChannelClass *class;

  if (channel->priv->connection_closed)
    return;
  channel->priv->connection_closed = TRUE;
  
  if (g_vfs_backend_get_block_requests (channel->priv->backend))
    return;

  if (channel->priv->current_job == NULL &&
      channel->priv->backend_handle != NULL)
    {
      class = G_VFS_CHANNEL_GET_CLASS (channel);
      
      channel->priv->current_job = class->close (channel);
      channel->priv->current_job_seq_nr = 0;
      g_vfs_job_source_new_job (G_VFS_JOB_SOURCE (channel), channel->priv->current_job);
    }
  /* Otherwise we'll close when current_job is finished */
}

static void
request_reader_free (RequestReader *reader)
{
  g_object_unref (reader->command_stream);
  g_object_unref (reader->cancellable);
  g_object_unref (reader->channel);
  g_free (reader->data);
  g_free (reader);
}

static gboolean
start_queued_request (GVfsChannel *channel)
{
  GVfsChannelClass *class;
  Request *req;
  GVfsJob *job;
  GError *error;
  gboolean started_job;

  started_job = FALSE;
  
  class = G_VFS_CHANNEL_GET_CLASS (channel);
  
  while (channel->priv->current_job == NULL &&
	 channel->priv->queued_requests != NULL)
    {
      req = channel->priv->queued_requests->data;

      channel->priv->queued_requests =
	g_list_delete_link (channel->priv->queued_requests,
			    channel->priv->queued_requests);

      error = NULL;
      if (!g_vfs_backend_get_block_requests (channel->priv->backend))
        {
	  /* This passes on ownership of req->data */
	  job = class->handle_request (channel,
				       req->command, req->seq_nr,
				       req->arg1, req->arg2,
				       req->data, req->data_len,
				       &error);
        }
      else
	{
	  job = NULL;
	  error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CLOSED,
	                               _("Channel blocked"));
	  g_free (req->data);
	}

      if (job != NULL && req->cancelled)
	{
	  /* Ignore the job, although we need to create it to rely
	     on handle_request side effects like seek generations, etc */
	  g_object_unref (job);
	  job = NULL;
	  error =  g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED,
					_("Operation was cancelled"));
	}

      if (job == NULL)
	{
	  job = g_vfs_job_error_new (channel, error);
	  g_error_free (error);
	}

      channel->priv->current_job = job;
      channel->priv->current_job_seq_nr = req->seq_nr;
      g_vfs_job_source_new_job (G_VFS_JOB_SOURCE (channel), channel->priv->current_job);
      started_job = TRUE;

      g_free (req);
    }

  return started_job;
}


/* Ownership of data is passed here to avoid copying it */
static void
got_request (GVfsChannel *channel,
	     GVfsDaemonSocketProtocolRequest *request,
	     gpointer data, gsize data_len)
{
  Request *req;
  guint32 command, arg1;
  GList *l;

  command = g_ntohl (request->command);
  arg1 = g_ntohl (request->arg1);

  if (command == G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_CANCEL)
    {
      if (arg1 == channel->priv->current_job_seq_nr &&
	  channel->priv->current_job != NULL)
	g_vfs_job_cancel (channel->priv->current_job);
      else
	{
	  for (l = channel->priv->queued_requests; l != NULL; l = l->next)
	    {
	      req = l->data;

	      if (req->seq_nr == 0)
		/* We're cancelling something later but this readahead might
		   be the actual operation thats replacing it */
		req->cancelled = TRUE;
		  
	      if (req->seq_nr == arg1)
		{
		  req->cancelled = TRUE;
		  break;
		}
	    }
	}

      /* Cancel ops get no return */
      g_free (data);
      return;
    }
  
  req = g_new0 (Request, 1);
  req->command = command;
  req->arg1 = arg1;
  req->arg2 = g_ntohl (request->arg2);
  req->seq_nr = g_ntohl (request->seq_nr);
  req->data_len = data_len;
  req->data = data;

  channel->priv->queued_requests =
    g_list_append (channel->priv->queued_requests,
		   req);
  
  start_queued_request (channel);
}

static void command_read_cb (GObject *source_object,
			     GAsyncResult *res,
			     gpointer user_data);


static void
finish_request (RequestReader *reader)
{
  /* Ownership of reader->data passed here */
  got_request (reader->channel, (GVfsDaemonSocketProtocolRequest *)reader->buffer,
	       reader->data, reader->data_len);
  reader->data = NULL;
  
  /* Request more commands immediately, so can get cancel requests */

  reader->buffer_size = 0;
  reader->data_len = 0;
  g_input_stream_read_async (reader->command_stream,
			     reader->buffer + reader->buffer_size,
			     G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SIZE - reader->buffer_size,
			     0, reader->cancellable,
			     command_read_cb,
			     reader);
}

static void
data_read_cb (GObject *source_object,
	      GAsyncResult *res,
	      gpointer user_data)
{
  RequestReader *reader = user_data;
  GInputStream *stream = G_INPUT_STREAM (source_object);
  gssize count_read;

  count_read = g_input_stream_read_finish (stream, res, NULL);
  
  if (count_read <= 0)
    {
      g_vfs_channel_connection_closed (reader->channel);
      request_reader_free (reader);
      return;
    }

  reader->data_pos += count_read;

  if (reader->data_pos < reader->data_len)
    {
      g_input_stream_read_async (reader->command_stream,
				 reader->data + reader->data_pos,
				 reader->data_len - reader->data_pos,
				 0, reader->cancellable,
				 data_read_cb, reader);
      return;
    }
  
  finish_request (reader);
}
  

static void
command_read_cb (GObject *source_object,
		 GAsyncResult *res,
		 gpointer user_data)
{
  GInputStream *stream = G_INPUT_STREAM (source_object);
  RequestReader *reader = user_data;
  GVfsDaemonSocketProtocolRequest *request;
  guint32 data_len;
  gssize count_read;

  count_read = g_input_stream_read_finish (stream, res, NULL);
  
  if (count_read <= 0)
    {
      g_vfs_channel_connection_closed (reader->channel);
      request_reader_free (reader);
      return;
    }

  reader->buffer_size += count_read;

  if (reader->buffer_size < G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SIZE)
    {
      g_input_stream_read_async (reader->command_stream,
				 reader->buffer + reader->buffer_size,
				 G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SIZE - reader->buffer_size,
				 0, reader->cancellable,
				 command_read_cb, reader);
      return;
    }

  request = (GVfsDaemonSocketProtocolRequest *)reader->buffer;
  data_len  = g_ntohl (request->data_len);

  if (data_len > 0)
    {
      reader->data = g_malloc (data_len);
      reader->data_len = data_len;
      reader->data_pos = 0;

      g_input_stream_read_async (reader->command_stream,
				 reader->data + reader->data_pos,
				 reader->data_len - reader->data_pos,
				 0, reader->cancellable,
				 data_read_cb, reader);
      return;
    }
  
  finish_request (reader);
}

static void
start_request_reader (GVfsChannel *channel)
{
  RequestReader *reader;

  reader = g_new0 (RequestReader, 1);
  reader->channel = g_object_ref (channel);
  reader->cancellable = g_object_ref (channel->priv->cancellable);
  reader->command_stream = g_object_ref (channel->priv->command_stream);
  
  g_input_stream_read_async (reader->command_stream,
			     reader->buffer + reader->buffer_size,
			     G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SIZE - reader->buffer_size,
			     0, reader->cancellable,
			     command_read_cb, reader);
}

static void
send_reply_cb (GObject *source_object,
	       GAsyncResult *res,
	       gpointer user_data)
{
  GOutputStream *output_stream = G_OUTPUT_STREAM (source_object);
  gssize bytes_written;
  GVfsChannel *channel = user_data;
  GVfsChannelClass *class;
  GVfsJob *job;

  bytes_written = g_output_stream_write_finish (output_stream, res, NULL);
  
  if (bytes_written <= 0)
    {
      g_vfs_channel_connection_closed (channel);
      goto error_out;
    }

  if (channel->priv->reply_buffer_pos < G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SIZE)
    {
      channel->priv->reply_buffer_pos += bytes_written;

      /* Write more of reply header if needed */
      if (channel->priv->reply_buffer_pos < G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SIZE)
	{
	  g_output_stream_write_async (channel->priv->reply_stream,
				       channel->priv->reply_buffer + channel->priv->reply_buffer_pos,
				       G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SIZE - channel->priv->reply_buffer_pos,
				       0, NULL,
				       send_reply_cb, channel);  
	  return;
	}
      bytes_written = 0;
    }

  channel->priv->output_data_pos += bytes_written;

  /* Write more of output_data if needed */
  if (channel->priv->output_data != NULL &&
      channel->priv->output_data_pos < channel->priv->output_data_size)
    {
      g_output_stream_write_async (channel->priv->reply_stream,
				   channel->priv->output_data + channel->priv->output_data_pos,
				   channel->priv->output_data_size - channel->priv->output_data_pos,
				   0, NULL,
				   send_reply_cb, channel);
      return;
    }

 error_out:
  
  /* Sent full reply */
  if (channel->priv->output_data_free)
    {
      g_free (channel->priv->output_data_free);
      channel->priv->output_data_free = NULL;
    }
  channel->priv->output_data = NULL;

  job = channel->priv->current_job;
  channel->priv->current_job = NULL;
  g_vfs_job_emit_finished (job);

  class = G_VFS_CHANNEL_GET_CLASS (channel);
  
  if (G_VFS_IS_JOB_CLOSE_READ (job) ||
      G_VFS_IS_JOB_CLOSE_WRITE (job))
    {
      /* Cancel the reader */
      g_cancellable_cancel (channel->priv->cancellable);
      g_vfs_job_source_closed (G_VFS_JOB_SOURCE (channel));
      channel->priv->backend_handle = NULL;
    }
  else if (channel->priv->connection_closed)
    {

      channel->priv->current_job = class->close (channel);
      channel->priv->current_job_seq_nr = 0;
      g_vfs_job_source_new_job (G_VFS_JOB_SOURCE (channel), channel->priv->current_job);
    }
  /* Start queued request or readahead */
  else if (!start_queued_request (channel) &&
	   class->readahead)
    {
      /* No queued requests, maybe we want to do a readahead call */
      channel->priv->current_job = class->readahead (channel, job);
      channel->priv->current_job_seq_nr = 0;
      if (channel->priv->current_job)
	g_vfs_job_source_new_job (G_VFS_JOB_SOURCE (channel), channel->priv->current_job);
    }

  g_object_unref (job);
}

/* Might be called on an i/o thread */
void
g_vfs_channel_send_reply (GVfsChannel *channel,
			  GVfsDaemonSocketProtocolReply *reply,
			  const void *data,
			  gsize data_len)
{
  
  channel->priv->output_data = data;
  channel->priv->output_data_size = data_len;
  channel->priv->output_data_pos = 0;

  if (reply != NULL)
    {
      memcpy (channel->priv->reply_buffer, reply, sizeof (GVfsDaemonSocketProtocolReply));
      channel->priv->reply_buffer_pos = 0;

      g_output_stream_write_async (channel->priv->reply_stream,
				   channel->priv->reply_buffer,
				   G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SIZE,
				   0, NULL,
				   send_reply_cb, channel);  
    }
  else
    {
      channel->priv->reply_buffer_pos = G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SIZE;
      g_output_stream_write_async (channel->priv->reply_stream,
				   channel->priv->output_data,
				   channel->priv->output_data_size,
				   0, NULL,
				   send_reply_cb, channel);  
    }
}

/* Might be called on an i/o thread */
void
g_vfs_channel_send_reply_take (GVfsChannel *channel,
                               GVfsDaemonSocketProtocolReply *reply,
                               void *data,
                               gsize data_len)
{
  channel->priv->output_data_free = data;
  g_vfs_channel_send_reply (channel, reply, data, data_len);
}

/* Might be called on an i/o thread
 */
void
g_vfs_channel_send_error (GVfsChannel *channel,
			  GError *error)
{
  char *data;
  gsize data_len;
  
  data = g_error_to_daemon_reply (error, channel->priv->current_job_seq_nr, &data_len);
  g_vfs_channel_send_reply_take (channel, NULL, data, data_len);
}

/* Might be called on an i/o thread
 */
void
g_vfs_channel_send_info (GVfsChannel *channel,
			 GFileInfo *info)
{
  GVfsDaemonSocketProtocolReply reply;
  char *data;
  gsize data_len;
  
  data = gvfs_file_info_marshal (info, &data_len);

  reply.type = g_htonl (G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_INFO);
  reply.seq_nr = g_htonl (g_vfs_channel_get_current_seq_nr (channel));
  reply.arg1 = 0;
  reply.arg2 = g_htonl (data_len);

  g_vfs_channel_send_reply_take (channel, &reply, data, data_len);
}

int
g_vfs_channel_steal_remote_fd (GVfsChannel *channel)
{
  int fd;
  fd = channel->priv->remote_fd;
  channel->priv->remote_fd = -1;
  return fd;
}

GVfsBackend *
g_vfs_channel_get_backend (GVfsChannel  *channel)
{
  return channel->priv->backend;
}

void
g_vfs_channel_set_backend_handle (GVfsChannel *channel,
				  GVfsBackendHandle backend_handle)
{
  channel->priv->backend_handle = backend_handle;
}

GVfsBackendHandle
g_vfs_channel_get_backend_handle (GVfsChannel *channel)
{
  return channel->priv->backend_handle;
}

guint32
g_vfs_channel_get_current_seq_nr (GVfsChannel *channel)
{
  return channel->priv->current_job_seq_nr;
}

GPid
g_vfs_channel_get_actual_consumer (GVfsChannel *channel)
{
  return channel->priv->actual_consumer;
}

static void
free_queued_requests (gpointer data)
{
  Request *req = (Request *) data;

  g_free (req->data);
  g_free (req);
}

void
g_vfs_channel_force_close (GVfsChannel *channel)
{
  GVfsJob *job;
  gint     fd;

  fd = g_unix_input_stream_get_fd (G_UNIX_INPUT_STREAM (channel->priv->command_stream));

  shutdown (fd, SHUT_RDWR);

  job = channel->priv->current_job;

  if (job)
    g_vfs_job_cancel (job);

  g_list_free_full (channel->priv->queued_requests, free_queued_requests);
  channel->priv->queued_requests = NULL;

  g_vfs_job_source_closed (G_VFS_JOB_SOURCE (channel));
}