summaryrefslogtreecommitdiff
path: root/daemon/gvfsreadchannel.c
blob: b6b6a93495b53292d894addabc14d8a0d6982a14 (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
/* 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 <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 <gvfsdaemonprotocol.h>
#include <gvfsdaemonutils.h>
#include <gvfsjobread.h>
#include <gvfsjobseekread.h>
#include <gvfsjobqueryinforead.h>
#include <gvfsjobcloseread.h>
#include <gvfsfileinfo.h>

struct _GVfsReadChannel
{
  GVfsChannel parent_instance;

  guint read_count;
  int seek_generation;
};

G_DEFINE_TYPE (GVfsReadChannel, g_vfs_read_channel, G_VFS_TYPE_CHANNEL)

static GVfsJob *read_channel_close          (GVfsChannel  *channel);
static GVfsJob *read_channel_handle_request (GVfsChannel  *channel,
					     guint32       command,
					     guint32       seq_nr,
					     guint32       arg1,
					     guint32       arg2,
					     gpointer      data,
					     gsize         data_len,
					     GError      **error);
static GVfsJob *read_channel_readahead      (GVfsChannel  *channel,
					     GVfsJob       *job);
  
static void
g_vfs_read_channel_finalize (GObject *object)
{
  if (G_OBJECT_CLASS (g_vfs_read_channel_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_vfs_read_channel_parent_class)->finalize) (object);
}

static void
g_vfs_read_channel_class_init (GVfsReadChannelClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GVfsChannelClass *channel_class = G_VFS_CHANNEL_CLASS (klass);

  gobject_class->finalize = g_vfs_read_channel_finalize;
  channel_class->close = read_channel_close;
  channel_class->handle_request = read_channel_handle_request;
  channel_class->readahead = read_channel_readahead;
}

static void
g_vfs_read_channel_init (GVfsReadChannel *channel)
{
}

static GVfsJob *
read_channel_close (GVfsChannel *channel)
{
  return g_vfs_job_close_read_new (G_VFS_READ_CHANNEL (channel),
				   g_vfs_channel_get_backend_handle (channel),
				   g_vfs_channel_get_backend (channel));
} 

/* Always request large chunks. Its very inefficient
 * to do network requests for smaller chunks.
 *
 * gstreamer tends to do 4k reads and seeks, and
 * the first read when sniffing is also small, so
 * it makes sense to never read more that 4k
 * (one page) on the first read. It should not affect
 * long-file copy performance anyway.
 */
static guint32
modify_read_size (GVfsReadChannel *channel,
		  guint32 requested_size)
{
  guint32 real_size;

  if (channel->read_count <= 1)
    real_size = 4*1024;
  else if (channel->read_count <= 2)
    real_size = 8*1024;
  else if (channel->read_count <= 3)
    real_size = 16*1024;
  else if (channel->read_count <= 4)
    real_size = 32*1024;
  else if (channel->read_count <= 5)
    real_size = 64*1024;
  else
    real_size = 128*1024;

  if (requested_size > real_size)
      real_size = requested_size;

  /* Don't do ridicoulously large requests as this
     is just stupid on the network */
  if (real_size > 256 * 1024)
    real_size = 256 * 1024;

  return real_size;
}

static GVfsJob *
read_channel_handle_request (GVfsChannel *channel,
			     guint32 command,
			     guint32 seq_nr,
			     guint32 arg1,
			     guint32 arg2,
			     gpointer data,
			     gsize data_len,
			     GError **error)
{
  GVfsJob *job;
  GSeekType seek_type;
  GVfsBackendHandle backend_handle;
  GVfsBackend *backend;
  GVfsReadChannel *read_channel;
  char *attrs;

  read_channel = G_VFS_READ_CHANNEL (channel);
  backend_handle = g_vfs_channel_get_backend_handle (channel);
  backend = g_vfs_channel_get_backend (channel);
  
  job = NULL;
  switch (command)
    {
    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_READ:
      read_channel->read_count++;
      job = g_vfs_job_read_new (read_channel,
				backend_handle,
				modify_read_size (read_channel, arg1),
				backend);
      break;
    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_CLOSE:
      job = g_vfs_job_close_read_new (read_channel,
				      backend_handle,
				      backend);
      break;
    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SEEK_END:
    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SEEK_SET:
      seek_type = G_SEEK_SET;
      if (command == G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_SEEK_END)
	seek_type = G_SEEK_END;
      
      read_channel->read_count = 0;
      read_channel->seek_generation++;
      job = g_vfs_job_seek_read_new (read_channel,
				     backend_handle,
				     seek_type,
				     ((goffset)arg1) | (((goffset)arg2) << 32),
				     backend);
      break;

    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_QUERY_INFO:
      attrs = g_strndup (data, data_len);
      job = g_vfs_job_query_info_read_new (read_channel,
					   backend_handle,
					   attrs,
					   backend);
      
      g_free (attrs);
      break;
      
    default:
      g_set_error (error, G_IO_ERROR,
		   G_IO_ERROR_FAILED,
		   "Unknown stream command %"G_GUINT32_FORMAT, command);
      break;
    }

  /* Ownership was passed */
  g_free (data);
  return job;
}

static GVfsJob *
read_channel_readahead (GVfsChannel  *channel,
			GVfsJob       *job)
{
  GVfsJob *readahead_job;
  GVfsReadChannel *read_channel;
  GVfsJobRead *read_job;

  readahead_job = NULL;
  if (!job->failed &&
      G_VFS_IS_JOB_READ (job))
    {
      read_job = G_VFS_JOB_READ (job);
      read_channel = G_VFS_READ_CHANNEL (channel);

      /* If the last operation was a read and it succeeded then we
	 might want to start a readahead. We don't do this for the
	 first read op as we're not sure we're streaming larger
	 parts of the file yet. However, after the second read we
	 queue a readahead read. After this the reading side will
	 constantly be one read operation behind, such that by
	 the time the second read operation is done and a third
	 read() is done we will send a read request but start
	 reading the readahead data, and after that is done
	 send a new request but start reading the result of the
	 previous read request. This way the reading will be
	 fully pipelined. */
      if (read_job->data_count != 0 &&
	  read_channel->read_count == 2)
	{
	  read_channel->read_count++;
	  readahead_job = g_vfs_job_read_new (read_channel,
					      g_vfs_channel_get_backend_handle (channel),
					      modify_read_size (read_channel, 8192),
					      g_vfs_channel_get_backend (channel));
	}
    }

  return readahead_job;
}


/* Might be called on an i/o thread
 */
void
g_vfs_read_channel_send_seek_offset (GVfsReadChannel *read_channel,
				     goffset offset)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (read_channel);
  
  reply.type = g_htonl (G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_SEEK_POS);
  reply.seq_nr = g_htonl (g_vfs_channel_get_current_seq_nr (channel));
  reply.arg1 = g_htonl (offset & 0xffffffff);
  reply.arg2 = g_htonl (offset >> 32);

  g_vfs_channel_send_reply (channel, &reply, NULL, 0);
}

/* Might be called on an i/o thread
 */
void
g_vfs_read_channel_send_closed (GVfsReadChannel *read_channel)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (read_channel);
  
  reply.type = g_htonl (G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_CLOSED);
  reply.seq_nr = g_htonl (g_vfs_channel_get_current_seq_nr (channel));
  reply.arg1 = g_htonl (0);
  reply.arg2 = g_htonl (0);

  g_vfs_channel_send_reply (channel, &reply, NULL, 0);
}

/* Might be called on an i/o thread
 */
void
g_vfs_read_channel_send_data (GVfsReadChannel  *read_channel,
			      char            *buffer,
			      gsize            count)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (read_channel);

  reply.type = g_htonl (G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_DATA);
  reply.seq_nr = g_htonl (g_vfs_channel_get_current_seq_nr (channel));
  reply.arg1 = g_htonl (count);
  reply.arg2 = g_htonl (read_channel->seek_generation);

  g_vfs_channel_send_reply (channel, &reply, buffer, count);
}


GVfsReadChannel *
g_vfs_read_channel_new (GVfsBackend *backend,
                        GPid         actual_consumer)
{
  return g_object_new (G_VFS_TYPE_READ_CHANNEL,
		       "backend", backend,
                       "actual-consumer", actual_consumer,
		       NULL);
}