File: gvfswritechannel.c

package info (click to toggle)
gvfs 1.30.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,580 kB
  • ctags: 12,211
  • sloc: ansic: 113,948; sh: 4,957; xml: 2,353; makefile: 1,601; python: 1,448; sed: 16
file content (252 lines) | stat: -rw-r--r-- 7,271 bytes parent folder | download | duplicates (5)
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
/* 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 <strings.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 <gvfswritechannel.h>
#include <gvfsdaemonprotocol.h>
#include <gvfsdaemonutils.h>
#include <gvfsjobwrite.h>
#include <gvfsjobseekwrite.h>
#include <gvfsjobtruncate.h>
#include <gvfsjobclosewrite.h>
#include <gvfsjobqueryinfowrite.h>

struct _GVfsWriteChannel
{
  GVfsChannel parent_instance;
};

G_DEFINE_TYPE (GVfsWriteChannel, g_vfs_write_channel, G_VFS_TYPE_CHANNEL)

static GVfsJob *write_channel_close          (GVfsChannel  *channel);
static GVfsJob *write_channel_handle_request (GVfsChannel  *channel,
					      guint32       command,
					      guint32       seq_nr,
					      guint32       arg1,
					      guint32       arg2,
					      gpointer      data,
					      gsize         data_len,
					      GError      **error);
  
static void
g_vfs_write_channel_finalize (GObject *object)
{
  if (G_OBJECT_CLASS (g_vfs_write_channel_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_vfs_write_channel_parent_class)->finalize) (object);
}

static void
g_vfs_write_channel_class_init (GVfsWriteChannelClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GVfsChannelClass *channel_class = G_VFS_CHANNEL_CLASS (klass);

  gobject_class->finalize = g_vfs_write_channel_finalize;
  channel_class->close = write_channel_close;
  channel_class->handle_request = write_channel_handle_request;
}

static void
g_vfs_write_channel_init (GVfsWriteChannel *channel)
{
}

static GVfsJob *
write_channel_close (GVfsChannel *channel)
{
  return g_vfs_job_close_write_new (G_VFS_WRITE_CHANNEL (channel),
				    g_vfs_channel_get_backend_handle (channel),
				    g_vfs_channel_get_backend (channel));
} 

static GVfsJob *
write_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;
  GVfsWriteChannel *write_channel;
  char *attrs;

  write_channel = G_VFS_WRITE_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_WRITE:
      job = g_vfs_job_write_new (write_channel,
				 backend_handle,
				 data, data_len,
				 backend);
      data = NULL; /* Pass ownership */
      break;
    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_CLOSE:
      job = g_vfs_job_close_write_new (write_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;
      
      job = g_vfs_job_seek_write_new (write_channel,
				      backend_handle,
				      seek_type,
				      ((goffset)arg1) | (((goffset)arg2) << 32),
				      backend);
      break;
    case G_VFS_DAEMON_SOCKET_PROTOCOL_REQUEST_TRUNCATE:
      job = g_vfs_job_truncate_new (write_channel,
                                    backend_handle,
                                    ((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_write_new (write_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;
}

/* Might be called on an i/o thread
 */
void
g_vfs_write_channel_send_seek_offset (GVfsWriteChannel *write_channel,
				     goffset offset)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (write_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_write_channel_send_truncated (GVfsWriteChannel *write_channel)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (write_channel);

  reply.type = g_htonl (G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_TRUNCATED);
  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_write_channel_send_closed (GVfsWriteChannel *write_channel,
				 const char       *etag)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (write_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 (strlen (etag));

  g_vfs_channel_send_reply (channel, &reply, etag, strlen (etag));
}

/* Might be called on an i/o thread
 */
void
g_vfs_write_channel_send_written (GVfsWriteChannel  *write_channel,
				  gsize bytes_written)
{
  GVfsDaemonSocketProtocolReply reply;
  GVfsChannel *channel;

  channel = G_VFS_CHANNEL (write_channel);

  reply.type = g_htonl (G_VFS_DAEMON_SOCKET_PROTOCOL_REPLY_WRITTEN);
  reply.seq_nr = g_htonl (g_vfs_channel_get_current_seq_nr (channel));
  reply.arg1 = g_htonl (bytes_written);
  reply.arg2 = 0;

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


GVfsWriteChannel *
g_vfs_write_channel_new (GVfsBackend *backend,
                         GPid         actual_consumer)
{
  return g_object_new (G_VFS_TYPE_WRITE_CHANNEL,
		       "backend", backend,
                       "actual-consumer", actual_consumer,
		       NULL);
}