gvfs r1118 - in trunk: . client daemon



Author: danw
Date: Mon Jan 14 00:51:54 2008
New Revision: 1118
URL: http://svn.gnome.org/viewvc/gvfs?rev=1118&view=rev

Log:
	* client/httpuri.c (http_from_uri): fix

	* daemon/soup-output-stream.c: Initial SoupOutputStream; still
	works like gnome-vfs http did, by storing all the writes in memory
	until close.

	* daemon/gvfsbackendhttp.c (try_create, try_replace): Implement.
	For now we need to do HEAD-then-PUT, but we may later be able to
	do a single PUT with "Expect: 100-continue".
	(try_write, try_close_write): Implement.


Added:
   trunk/daemon/soup-output-stream.c
   trunk/daemon/soup-output-stream.h
Modified:
   trunk/ChangeLog
   trunk/client/httpuri.c
   trunk/daemon/Makefile.am
   trunk/daemon/gvfsbackendhttp.c

Modified: trunk/client/httpuri.c
==============================================================================
--- trunk/client/httpuri.c	(original)
+++ trunk/client/httpuri.c	Mon Jan 14 00:51:54 2008
@@ -109,7 +109,6 @@
 
       path = uri->path;
       uri->path = NULL;
-      info->mount_prefix = g_strdup (path);
       g_vfs_decoded_uri_free (uri);
     }
 

Modified: trunk/daemon/Makefile.am
==============================================================================
--- trunk/daemon/Makefile.am	(original)
+++ trunk/daemon/Makefile.am	Mon Jan 14 00:51:54 2008
@@ -222,6 +222,7 @@
 
 gvfsd_http_SOURCES = \
 	soup-input-stream.c soup-input-stream.h \
+	soup-output-stream.c soup-output-stream.h \
 	gvfsbackendhttp.c gvfsbackendhttp.h \
 	daemon-main.c daemon-main.h \
 	daemon-main-generic.c 
@@ -238,6 +239,7 @@
 
 gvfsd_dav_SOURCES = \
 	soup-input-stream.c soup-input-stream.h \
+	soup-output-stream.c soup-output-stream.h \
 	gvfsbackendhttp.c gvfsbackendhttp.h \
 	gvfsbackenddav.c gvfsbackenddav.h \
 	daemon-main.c daemon-main.h \

Modified: trunk/daemon/gvfsbackendhttp.c
==============================================================================
--- trunk/daemon/gvfsbackendhttp.c	(original)
+++ trunk/daemon/gvfsbackendhttp.c	Mon Jan 14 00:51:54 2008
@@ -51,6 +51,7 @@
 #include "gvfsdaemonprotocol.h"
 
 #include "soup-input-stream.h"
+#include "soup-output-stream.h"
 
 
 
@@ -353,6 +354,257 @@
   return TRUE;
 }
 
+/* *** create () *** */
+static void
+try_create_tested_existence (SoupMessage *msg, gpointer user_data)
+{
+  GVfsJob *job = G_VFS_JOB (user_data);
+  GVfsBackendHttp *op_backend = job->backend_data;
+  GOutputStream   *stream;
+  SoupMessage     *put_msg;
+
+  if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
+    {
+      g_vfs_job_failed (job,
+                        G_IO_ERROR,
+                        G_IO_ERROR_EXISTS,
+                        _("Target file already exists"));
+      return;
+    }
+  /* FIXME: other errors */
+
+  put_msg = soup_message_new_from_uri (SOUP_METHOD_PUT,
+                                       soup_message_get_uri (msg));
+  soup_message_add_header (put_msg->request_headers, "User-Agent", "gvfs/" VERSION);
+  soup_message_add_header (put_msg->request_headers, "If-None-Match", "*");
+  stream = soup_output_stream_new (op_backend->session, put_msg, -1);
+  g_object_unref (put_msg);
+
+  g_vfs_job_open_for_write_set_handle (G_VFS_JOB_OPEN_FOR_WRITE (job), stream);
+  g_vfs_job_succeeded (job);
+}  
+
+static gboolean
+try_create (GVfsBackend *backend,
+            GVfsJobOpenForWrite *job,
+            const char *filename,
+            GFileCreateFlags flags)
+{
+  GVfsBackendHttp *op_backend;
+  SoupUri         *uri;
+  SoupMessage     *msg;
+
+  /* FIXME: if SoupOutputStream supported chunked requests, we could
+   * use a PUT with "If-None-Match: *" and "Expect: 100-continue"
+   */
+
+  op_backend = G_VFS_BACKEND_HTTP (backend);
+  uri = g_vfs_backend_uri_for_filename (backend, filename);
+
+  msg = soup_message_new_from_uri (SOUP_METHOD_HEAD, uri);
+  soup_uri_free (uri);
+  soup_message_add_header (msg->request_headers, "User-Agent", "gvfs/" VERSION);
+
+  g_vfs_job_set_backend_data (G_VFS_JOB (job), op_backend, NULL);
+  soup_session_queue_message (op_backend->session, msg,
+                              try_create_tested_existence, job);
+  return TRUE;
+}
+
+/* *** replace () *** */
+static void
+open_for_replace_succeeded (GVfsBackendHttp *op_backend, GVfsJob *job,
+                            const SoupUri *uri, const char *etag)
+{
+  SoupMessage     *put_msg;
+  GOutputStream   *stream;
+
+  put_msg = soup_message_new_from_uri (SOUP_METHOD_PUT, uri);
+  soup_message_add_header (put_msg->request_headers, "User-Agent", "gvfs/" VERSION);
+  if (etag)
+    soup_message_add_header (put_msg->request_headers, "If-Match", etag);
+
+  stream = soup_output_stream_new (op_backend->session, put_msg, -1);
+  g_object_unref (put_msg);
+
+  g_vfs_job_open_for_write_set_handle (G_VFS_JOB_OPEN_FOR_WRITE (job), stream);
+  g_vfs_job_succeeded (job);
+}
+
+static void
+try_replace_checked_etag (SoupMessage *msg, gpointer user_data)
+{
+  GVfsJob *job = G_VFS_JOB (user_data);
+  GVfsBackendHttp *op_backend = job->backend_data;
+
+  if (msg->status_code == SOUP_STATUS_PRECONDITION_FAILED)
+    {
+      g_vfs_job_failed (G_VFS_JOB (job),
+                        G_IO_ERROR,
+                        G_IO_ERROR_WRONG_ETAG,
+                        _("The file was externally modified"));
+      return;
+    }
+  /* FIXME: other errors */
+
+  open_for_replace_succeeded (op_backend, job, soup_message_get_uri (msg),
+                              soup_message_get_header (msg->request_headers, "If-Match"));
+}  
+
+static gboolean
+try_replace (GVfsBackend *backend,
+             GVfsJobOpenForWrite *job,
+             const char *filename,
+             const char *etag,
+             gboolean make_backup,
+             GFileCreateFlags flags)
+{
+  GVfsBackendHttp *op_backend;
+  SoupUri         *uri;
+
+  /* FIXME: if SoupOutputStream supported chunked requests, we could
+   * use a PUT with "If-Match: ..." and "Expect: 100-continue"
+   */
+
+  op_backend = G_VFS_BACKEND_HTTP (backend);
+
+  if (make_backup)
+    {
+      g_vfs_job_failed (G_VFS_JOB (job),
+                        G_IO_ERROR,
+                        G_IO_ERROR_CANT_CREATE_BACKUP,
+                        _("Backup file creation failed"));
+      return TRUE;
+    }
+
+  uri = g_vfs_backend_uri_for_filename (backend, filename);
+
+  if (etag)
+    {
+      SoupMessage     *msg;
+
+      msg = soup_message_new_from_uri (SOUP_METHOD_HEAD, uri);
+      soup_uri_free (uri);
+      soup_message_add_header (msg->request_headers, "User-Agent", "gvfs/" VERSION);
+      soup_message_add_header (msg->request_headers, "If-Match", etag);
+
+      g_vfs_job_set_backend_data (G_VFS_JOB (job), op_backend, NULL);
+      soup_session_queue_message (op_backend->session, msg,
+                                  try_replace_checked_etag, job);
+      return TRUE;
+    }
+
+  open_for_replace_succeeded (op_backend, G_VFS_JOB (job), uri, NULL);
+  soup_uri_free (uri);
+  return TRUE;
+}
+
+/* *** write () *** */
+static void
+write_ready (GObject      *source_object,
+             GAsyncResult *result,
+             gpointer      user_data)
+{
+  GOutputStream *stream;
+  GVfsJob       *job;
+  GError        *error;
+  gssize         nwrote;
+
+  stream = G_OUTPUT_STREAM (source_object); 
+  error  = NULL;
+  job    = G_VFS_JOB (user_data);
+
+  nwrote = g_output_stream_write_finish (stream, result, &error);
+
+  if (nwrote < 0)
+   {
+     g_vfs_job_failed (G_VFS_JOB (job),
+                       error->domain,
+                       error->code,
+                       error->message);
+
+     g_error_free (error);
+     return;
+   }
+
+  g_vfs_job_write_set_written_size (G_VFS_JOB_WRITE (job), nwrote);
+  g_vfs_job_succeeded (job);
+}
+
+static gboolean
+try_write (GVfsBackend *backend,
+           GVfsJobWrite *job,
+           GVfsBackendHandle handle,
+           char *buffer,
+           gsize buffer_size)
+{
+  GVfsBackendHttp *op_backend;
+  GOutputStream   *stream;
+
+  op_backend = G_VFS_BACKEND_HTTP (backend);
+  stream = G_OUTPUT_STREAM (handle);
+
+  g_output_stream_write_async (stream,
+                               buffer,
+                               buffer_size,
+                               G_PRIORITY_DEFAULT,
+                               G_VFS_JOB (job)->cancellable,
+                               write_ready,
+                               job);
+  return TRUE;
+}
+
+/* *** close_write () *** */
+static void
+close_write_ready (GObject      *source_object,
+                   GAsyncResult *result,
+                   gpointer      user_data)
+{
+  GOutputStream *stream;
+  GVfsJob       *job;
+  GError        *error;
+  gboolean       res;
+
+  job = G_VFS_JOB (user_data);
+  stream = G_OUTPUT_STREAM (source_object);
+  res = g_output_stream_close_finish (stream,
+                                      result,
+                                      &error);
+  if (res == FALSE)
+    {
+      g_vfs_job_failed (G_VFS_JOB (job),
+                        error->domain,
+                        error->code,
+                        error->message);
+
+      g_error_free (error);
+    }
+  else
+    g_vfs_job_succeeded (job);
+
+  g_object_unref (stream);
+}
+
+static gboolean
+try_close_write (GVfsBackend *backend,
+                 GVfsJobCloseWrite *job,
+                 GVfsBackendHandle handle)
+{
+  GVfsBackendHttp *op_backend;
+  GOutputStream   *stream;
+
+  op_backend = G_VFS_BACKEND_HTTP (backend);
+  stream = G_OUTPUT_STREAM (handle);
+
+  g_output_stream_close_async (stream,
+                               G_PRIORITY_DEFAULT,
+                               G_VFS_JOB (job)->cancellable,
+                               close_write_ready,
+                               job);
+
+  return TRUE;
+}
+
 /* *** query_info () *** */
 static gboolean
 try_query_info (GVfsBackend           *backend,
@@ -382,6 +634,10 @@
   backend_class->try_read          = try_read;
   backend_class->try_seek_on_read  = try_seek_on_read;
   backend_class->try_close_read    = try_close_read;
+  backend_class->try_create        = try_create;
+  backend_class->try_replace       = try_replace;
+  backend_class->try_write         = try_write;
+  backend_class->try_close_write   = try_close_write;
   backend_class->try_query_info    = try_query_info;
 
 }

Added: trunk/daemon/soup-output-stream.c
==============================================================================
--- (empty file)
+++ trunk/daemon/soup-output-stream.c	Mon Jan 14 00:51:54 2008
@@ -0,0 +1,417 @@
+/* soup-output-stream.c, based on gunixoutputstream.c
+ *
+ * Copyright (C) 2006-2008 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., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <libsoup/soup.h>
+
+#include "soup-output-stream.h"
+#include "soup-input-stream.h"
+
+G_DEFINE_TYPE (SoupOutputStream, soup_output_stream, G_TYPE_OUTPUT_STREAM);
+
+typedef void (*SoupOutputStreamCallback) (GOutputStream *);
+
+typedef struct {
+  SoupSession *session;
+  GMainContext *async_context;
+  SoupMessage *msg;
+
+  goffset size, offset;
+  GByteArray *ba;
+
+  GCancellable *cancellable;
+  GSource *cancel_watch;
+  SoupOutputStreamCallback finished_cb;
+  SoupOutputStreamCallback cancelled_cb;
+
+  GSimpleAsyncResult *result;
+} SoupOutputStreamPrivate;
+#define SOUP_OUTPUT_STREAM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_OUTPUT_STREAM, SoupOutputStreamPrivate))
+
+static gssize   soup_output_stream_write        (GOutputStream        *stream,
+						 const void           *buffer,
+						 gsize                 count,
+						 GCancellable         *cancellable,
+						 GError              **error);
+static gboolean soup_output_stream_close        (GOutputStream        *stream,
+						 GCancellable         *cancellable,
+						 GError              **error);
+static void     soup_output_stream_write_async  (GOutputStream        *stream,
+						 const void           *buffer,
+						 gsize                 count,
+						 int                   io_priority,
+						 GCancellable         *cancellable,
+						 GAsyncReadyCallback   callback,
+						 gpointer              data);
+static gssize   soup_output_stream_write_finish (GOutputStream        *stream,
+						 GAsyncResult         *result,
+						 GError              **error);
+static void     soup_output_stream_close_async  (GOutputStream        *stream,
+						 int                   io_priority,
+						 GCancellable         *cancellable,
+						 GAsyncReadyCallback   callback,
+						 gpointer              data);
+static gboolean soup_output_stream_close_finish (GOutputStream        *stream,
+						 GAsyncResult         *result,
+						 GError              **error);
+
+static void soup_output_stream_finished (SoupMessage *msg, gpointer stream);
+
+static void
+soup_output_stream_finalize (GObject *object)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (object);
+
+  g_object_unref (priv->session);
+
+  g_signal_handlers_disconnect_by_func (priv->msg, G_CALLBACK (soup_output_stream_finished), object);
+  g_object_unref (priv->msg);
+
+  if (priv->ba)
+    g_byte_array_free (priv->ba, TRUE);
+
+  if (G_OBJECT_CLASS (soup_output_stream_parent_class)->finalize)
+    (*G_OBJECT_CLASS (soup_output_stream_parent_class)->finalize) (object);
+}
+
+static void
+soup_output_stream_class_init (SoupOutputStreamClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
+  
+  g_type_class_add_private (klass, sizeof (SoupOutputStreamPrivate));
+  
+  gobject_class->finalize = soup_output_stream_finalize;
+
+  stream_class->write_fn = soup_output_stream_write;
+  stream_class->close_fn = soup_output_stream_close;
+  stream_class->write_async = soup_output_stream_write_async;
+  stream_class->write_finish = soup_output_stream_write_finish;
+  stream_class->close_async = soup_output_stream_close_async;
+  stream_class->close_finish = soup_output_stream_close_finish;
+}
+
+static void
+soup_output_stream_init (SoupOutputStream *stream)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  priv->ba = g_byte_array_new ();
+}
+
+
+/**
+ * soup_output_stream_new:
+ * @session: the #SoupSession to use
+ * @msg: the #SoupMessage whose request will be streamed
+ * @size: the total size of the request body, or -1 if not known
+ * 
+ * Prepares to send @msg over @session, and returns a #GOutputStream
+ * that can be used to write the response. The server's response will
+ * be available in @msg after calling soup_output_stream_close()
+ * (which will return a %SOUP_OUTPUT_STREAM_HTTP_ERROR #GError if the
+ * status is not 2xx).
+ *
+ * If you know the total number of bytes that will be written, pass
+ * that in @size. Otherwise, pass -1. (If you pass a size, you MUST
+ * write that many bytes to the stream; Trying to write more than
+ * that, or closing the stream without having written enough, will
+ * result in an error.
+ *
+ * In some situations, the request will not actually be sent until you
+ * call g_output_stream_close(). (In fact, currently this is *always*
+ * true.)
+ *
+ * Internally, #SoupOutputStream is implemented using asynchronous
+ * I/O, so if you are using the synchronous API (eg,
+ * g_output_stream_write()), you should create a new #GMainContext and
+ * set it as the %SOUP_SESSION_ASYNC_CONTEXT property on @session. (If
+ * you don't, then synchronous #GOutputStream calls will cause the
+ * main loop to be run recursively.) The async #GOutputStream API
+ * works fine with %SOUP_SESSION_ASYNC_CONTEXT either set or unset.
+ *
+ * Returns: a new #GOutputStream.
+ **/
+GOutputStream *
+soup_output_stream_new (SoupSession *session, SoupMessage *msg, goffset size)
+{
+  SoupOutputStream *stream;
+  SoupOutputStreamPrivate *priv;
+
+  g_return_val_if_fail (SOUP_IS_SESSION_ASYNC (session), NULL);
+  g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
+
+  stream = g_object_new (SOUP_TYPE_OUTPUT_STREAM, NULL);
+  priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  priv->session = g_object_ref (session);
+  priv->async_context = soup_session_get_async_context (session);
+  priv->msg = g_object_ref (msg);
+  priv->size = size;
+
+  return G_OUTPUT_STREAM (stream);
+}
+
+static gboolean
+soup_output_stream_cancelled (GIOChannel *chan, GIOCondition condition,
+			      gpointer stream)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  priv->cancel_watch = NULL;
+
+  soup_message_io_pause (priv->msg);
+  if (priv->cancelled_cb)
+    priv->cancelled_cb (stream);
+
+  return FALSE;
+}  
+
+static void
+soup_output_stream_prepare_for_io (GOutputStream *stream, GCancellable *cancellable)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+  int cancel_fd;
+
+  /* Move the buffer to the SoupMessage */
+  priv->msg->request.body = (char *)priv->ba->data;
+  priv->msg->request.length = priv->ba->len;
+  priv->msg->request.owner = SOUP_BUFFER_SYSTEM_OWNED;
+  g_byte_array_free (priv->ba, FALSE);
+  priv->ba = NULL;
+
+  /* Set up cancellation */
+  priv->cancellable = cancellable;
+  cancel_fd = g_cancellable_get_fd (cancellable);
+  if (cancel_fd != -1)
+    {
+      GIOChannel *chan = g_io_channel_unix_new (cancel_fd);
+      priv->cancel_watch = soup_add_io_watch (priv->async_context, chan,
+					      G_IO_IN | G_IO_ERR | G_IO_HUP,
+					      soup_output_stream_cancelled,
+					      stream);
+      g_io_channel_unref (chan);
+    }
+
+  /* Add an extra ref since soup_session_queue_message steals one */
+  g_object_ref (priv->msg);
+  soup_session_queue_message (priv->session, priv->msg, NULL, NULL);
+}
+
+static void
+soup_output_stream_done_io (GOutputStream *stream)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  if (priv->cancel_watch)
+    {
+      g_source_destroy (priv->cancel_watch);
+      priv->cancel_watch = NULL;
+    }
+  priv->cancellable = NULL;
+}
+
+static gboolean
+set_error_if_http_failed (SoupMessage *msg, GError **error)
+{
+  if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
+    {
+      g_set_error (error, SOUP_HTTP_ERROR,
+		   msg->status_code, "%s", msg->reason_phrase);
+      return TRUE;
+    }
+  return FALSE;
+}
+
+static gssize
+soup_output_stream_write (GOutputStream  *stream,
+			  const void     *buffer,
+			  gsize           count,
+			  GCancellable   *cancellable,
+			  GError        **error)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  if (priv->size > 0 && priv->offset + count > priv->size) {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
+		   "Write would exceed caller-defined file size");
+      return -1;
+  }
+
+  g_byte_array_append (priv->ba, buffer, count);
+  priv->offset += count;
+  return count;
+}
+
+static gboolean
+soup_output_stream_close (GOutputStream  *stream,
+			  GCancellable   *cancellable,
+			  GError        **error)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  if (priv->size > 0 && priv->offset != priv->size) {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
+		   "File is incomplete");
+      return -1;
+  }
+
+  soup_output_stream_prepare_for_io (stream, cancellable);
+  while (priv->msg->status != SOUP_MESSAGE_STATUS_FINISHED &&
+	 !g_cancellable_is_cancelled (cancellable))
+    g_main_context_iteration (priv->async_context, TRUE);
+  soup_output_stream_done_io (stream);
+
+  return !set_error_if_http_failed (priv->msg, error);
+}
+
+static void
+soup_output_stream_write_async (GOutputStream       *stream,
+				const void          *buffer,
+				gsize                count,
+				int                  io_priority,
+				GCancellable        *cancellable,
+				GAsyncReadyCallback  callback,
+				gpointer             user_data)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+  GSimpleAsyncResult *result;
+
+  result = g_simple_async_result_new (G_OBJECT (stream),
+				      callback, user_data,
+				      soup_output_stream_write_async);
+
+  if (priv->size > 0 && priv->offset + count > priv->size)
+    {
+      GError *error;
+
+      error = g_error_new (G_IO_ERROR, G_IO_ERROR_NO_SPACE,
+			   "Write would exceed caller-defined file size");
+      g_simple_async_result_set_from_error (result, error);
+      g_error_free (error);
+    }
+  else
+    {
+      g_byte_array_append (priv->ba, buffer, count);
+      priv->offset += count;
+      g_simple_async_result_set_op_res_gssize (result, count);
+    }
+
+  g_simple_async_result_complete_in_idle (result);
+}
+
+static gssize
+soup_output_stream_write_finish (GOutputStream  *stream,
+				 GAsyncResult   *result,
+				 GError        **error)
+{
+  GSimpleAsyncResult *simple;
+  gssize nwritten;
+
+  simple = G_SIMPLE_ASYNC_RESULT (result);
+  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == soup_output_stream_write_async);
+  
+  nwritten = g_simple_async_result_get_op_res_gssize (simple);
+  return nwritten;
+}
+
+static void
+close_async_done (GOutputStream *stream)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+  GSimpleAsyncResult *result;
+  GError *error = NULL;
+
+  result = priv->result;
+  priv->result = NULL;
+
+  if (g_cancellable_set_error_if_cancelled (priv->cancellable, &error) ||
+      set_error_if_http_failed (priv->msg, &error))
+    {
+      g_simple_async_result_set_from_error (result, error);
+      g_error_free (error);
+    }
+  else
+    g_simple_async_result_set_op_res_gboolean (result, TRUE);
+
+  priv->finished_cb = NULL;
+  priv->cancelled_cb = NULL;
+  soup_output_stream_done_io (stream);
+
+  g_simple_async_result_complete (result);
+}
+
+static void
+soup_output_stream_finished (SoupMessage *msg, gpointer stream)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+
+  g_signal_handlers_disconnect_by_func (priv->msg, G_CALLBACK (soup_output_stream_finished), stream);
+  close_async_done (stream);
+}
+
+static void
+soup_output_stream_close_async (GOutputStream        *stream,
+				int                  io_priority,
+				GCancellable        *cancellable,
+				GAsyncReadyCallback  callback,
+				gpointer             user_data)
+{
+  SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
+  GSimpleAsyncResult *result;
+
+  result = g_simple_async_result_new (G_OBJECT (stream),
+				      callback, user_data,
+				      soup_output_stream_close_async);
+
+  if (priv->size > 0 && priv->offset != priv->size)
+    {
+      GError *error;
+
+      error = g_error_new (G_IO_ERROR, G_IO_ERROR_NO_SPACE,
+			   "File is incomplete");
+      g_simple_async_result_set_from_error (result, error);
+      g_error_free (error);
+      g_simple_async_result_complete_in_idle (result);
+      return;
+    }
+
+  priv->result = result;
+  priv->cancelled_cb = close_async_done;
+  g_signal_connect (priv->msg, "finished",
+		    G_CALLBACK (soup_output_stream_finished), stream);
+  soup_output_stream_prepare_for_io (stream, cancellable);
+}
+
+static gboolean
+soup_output_stream_close_finish (GOutputStream  *stream,
+				 GAsyncResult   *result,
+				 GError        **error)
+{
+  /* Failures handled in generic close_finish code */
+  return TRUE;
+}

Added: trunk/daemon/soup-output-stream.h
==============================================================================
--- (empty file)
+++ trunk/daemon/soup-output-stream.h	Mon Jan 14 00:51:54 2008
@@ -0,0 +1,63 @@
+/* Copyright (C) 2006-2008 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., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __SOUP_OUTPUT_STREAM_H__
+#define __SOUP_OUTPUT_STREAM_H__
+
+#include <gio/gio.h>
+#include <libsoup/soup-types.h>
+
+G_BEGIN_DECLS
+
+#define SOUP_TYPE_OUTPUT_STREAM         (soup_output_stream_get_type ())
+#define SOUP_OUTPUT_STREAM(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), SOUP_TYPE_OUTPUT_STREAM, SoupOutputStream))
+#define SOUP_OUTPUT_STREAM_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), SOUP_TYPE_OUTPUT_STREAM, SoupOutputStreamClass))
+#define SOUP_IS_OUTPUT_STREAM(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), SOUP_TYPE_OUTPUT_STREAM))
+#define SOUP_IS_OUTPUT_STREAM_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), SOUP_TYPE_OUTPUT_STREAM))
+#define SOUP_OUTPUT_STREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), SOUP_TYPE_OUTPUT_STREAM, SoupOutputStreamClass))
+
+typedef struct SoupOutputStream         SoupOutputStream;
+typedef struct SoupOutputStreamClass    SoupOutputStreamClass;
+
+struct SoupOutputStream
+{
+  GOutputStream parent;
+
+};
+
+struct SoupOutputStreamClass
+{
+  GOutputStreamClass parent_class;
+
+  /* Padding for future expansion */
+  void (*_g_reserved1) (void);
+  void (*_g_reserved2) (void);
+  void (*_g_reserved3) (void);
+  void (*_g_reserved4) (void);
+  void (*_g_reserved5) (void);
+};
+
+GType soup_output_stream_get_type (void) G_GNUC_CONST;
+
+GOutputStream *soup_output_stream_new         (SoupSession         *session,
+					       SoupMessage         *msg,
+					       goffset              size);
+
+G_END_DECLS
+
+#endif /* __SOUP_OUTPUT_STREAM_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]