[gvfs] afp: reuse g_vfs_afp_connection_send_command in the sync version



commit ecc1730d3bfdc11fa07f15893267a7ea054328f1
Author: Carl-Anton Ingmarsson <ca ingmarsson gmail com>
Date:   Thu Aug 25 18:03:23 2011 +0200

    afp: reuse g_vfs_afp_connection_send_command in the sync version

 daemon/gvfsafpconnection.c |  127 ++++++++++++++++++++++++--------------------
 daemon/gvfsafpconnection.h |   14 ++---
 daemon/gvfsafpserver.c     |   50 ++++-------------
 daemon/gvfsbackendafp.c    |   32 +++--------
 4 files changed, 95 insertions(+), 128 deletions(-)
---
diff --git a/daemon/gvfsafpconnection.c b/daemon/gvfsafpconnection.c
index b725f54..1551d82 100644
--- a/daemon/gvfsafpconnection.c
+++ b/daemon/gvfsafpconnection.c
@@ -1281,6 +1281,76 @@ g_vfs_afp_connection_send_command_finish (GVfsAfpConnection *afp_connection,
   return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
 }
 
+typedef struct {
+
+  /* For sync calls */
+  GMutex *mutex;
+  GCond *cond;
+
+  /* results: */
+  GAsyncResult *result;
+} SyncData;
+
+static void
+init_sync_data (SyncData *data)
+{
+  data->mutex = g_mutex_new ();
+  data->cond = g_cond_new ();
+}
+
+static void
+clear_sync_data (SyncData *data)
+{
+  g_mutex_free (data->mutex);
+  g_cond_free (data->cond);
+  g_object_unref (data->result);
+}
+
+static void
+sync_data_wait (SyncData *data)
+{
+  g_mutex_lock (data->mutex);
+  g_cond_wait (data->cond, data->mutex);
+  g_mutex_unlock (data->mutex);
+}
+
+static void
+reply_sync  (GObject *source_object,
+             GAsyncResult *res,
+             gpointer user_data)
+{
+  SyncData *data;
+
+  data = (SyncData *) user_data;
+
+  data->result = g_object_ref (res);
+
+  /* Wake up sync call thread */
+  g_mutex_lock (data->mutex);
+  g_cond_signal (data->cond);
+  g_mutex_unlock (data->mutex);
+}
+
+GVfsAfpReply *
+g_vfs_afp_connection_send_command_sync (GVfsAfpConnection *afp_connection,
+                                        GVfsAfpCommand    *command,
+                                        GCancellable      *cancellable,
+                                        GError            **error)
+{
+  SyncData data;
+  GVfsAfpReply *reply;
+
+  init_sync_data (&data);
+  g_vfs_afp_connection_send_command (afp_connection, command, NULL, reply_sync,
+                                     cancellable, &data);
+  sync_data_wait (&data);
+
+  reply = g_vfs_afp_connection_send_command_finish (afp_connection, data.result,
+                                                    error);
+  clear_sync_data (&data);
+  return reply;
+}
+
 static gboolean
 read_reply_sync (GInputStream      *input,
                  DSIHeader         *dsi_header,
@@ -1336,25 +1406,6 @@ read_reply_sync (GInputStream      *input,
   return TRUE;
 }
 
-GVfsAfpReply *
-g_vfs_afp_connection_read_reply_sync (GVfsAfpConnection *afp_connection,
-                                      GCancellable *cancellable,
-                                      GError **error)
-{
-  GVfsAfpConnectionPrivate *priv = afp_connection->priv;
-  
-  gboolean res;
-  char *data;
-  DSIHeader dsi_header;
-
-  res = read_reply_sync (g_io_stream_get_input_stream (priv->conn), &dsi_header,
-                         &data, cancellable, error);
-  if (!res)
-    return NULL;
-
-  return g_vfs_afp_reply_new (dsi_header.errorCode, data, dsi_header.totalDataLength, TRUE);
-}
-
 static gboolean
 send_request_sync (GOutputStream     *output,
                    DsiCommand        command,
@@ -1395,44 +1446,6 @@ send_request_sync (GOutputStream     *output,
 }
 
 gboolean
-g_vfs_afp_connection_send_command_sync (GVfsAfpConnection *afp_connection,
-                                        GVfsAfpCommand    *afp_command,
-                                        GCancellable      *cancellable,
-                                        GError            **error)
-{
-  GVfsAfpConnectionPrivate *priv = afp_connection->priv;
-  
-  DsiCommand dsi_command;
-  guint16 req_id;
-  guint32 writeOffset;
-
-  /* set dsi_command */
-  switch (afp_command->type)
-  {
-    case AFP_COMMAND_WRITE:
-      writeOffset = 8;
-      dsi_command = DSI_WRITE;
-      break;
-    case AFP_COMMAND_WRITE_EXT:
-      writeOffset = 20;
-      dsi_command = DSI_WRITE;
-      break;
-
-    default:
-      writeOffset = 0;
-      dsi_command = DSI_COMMAND;
-      break;
-  }
-
-  req_id = get_request_id (afp_connection);
-  return send_request_sync (g_io_stream_get_output_stream (priv->conn),
-                            dsi_command, req_id, writeOffset,
-                            g_vfs_afp_command_get_size (afp_command),
-                            g_vfs_afp_command_get_data (afp_command),
-                            cancellable, error);
-}
-
-gboolean
 g_vfs_afp_connection_close (GVfsAfpConnection *afp_connection,
                             GCancellable      *cancellable,
                             GError            **error)
diff --git a/daemon/gvfsafpconnection.h b/daemon/gvfsafpconnection.h
index f5cde13..57badd2 100644
--- a/daemon/gvfsafpconnection.h
+++ b/daemon/gvfsafpconnection.h
@@ -400,15 +400,6 @@ gboolean           g_vfs_afp_connection_close             (GVfsAfpConnection *af
                                                            GCancellable      *cancellable,
                                                            GError            **error);
 
-gboolean           g_vfs_afp_connection_send_command_sync (GVfsAfpConnection *afp_connection,
-                                                           GVfsAfpCommand    *afp_command,
-                                                           GCancellable      *cancellable,
-                                                           GError            **error);
-
-GVfsAfpReply*      g_vfs_afp_connection_read_reply_sync   (GVfsAfpConnection *afp_connection,
-                                                           GCancellable *cancellable,
-                                                           GError **error);
-
 GVfsAfpReply*      g_vfs_afp_connection_send_command_finish (GVfsAfpConnection *afp_connnection,
                                                              GAsyncResult      *res,
                                                              GError           **error);
@@ -419,6 +410,11 @@ void               g_vfs_afp_connection_send_command     (GVfsAfpConnection   *a
                                                           GAsyncReadyCallback  callback,
                                                           GCancellable        *cancellable,                                                           
                                                           gpointer             user_data);
+
+GVfsAfpReply *     g_vfs_afp_connection_send_command_sync (GVfsAfpConnection *afp_connection,
+                                                           GVfsAfpCommand    *command,
+                                                           GCancellable      *cancellable,
+                                                           GError            **error);
 G_END_DECLS
 
 #endif /* _GVFSAFPCONNECTION_H_ */
diff --git a/daemon/gvfsafpserver.c b/daemon/gvfsafpserver.c
index ca1ff20..c2ec489 100644
--- a/daemon/gvfsafpserver.c
+++ b/daemon/gvfsafpserver.c
@@ -208,13 +208,9 @@ dhx2_login (GVfsAfpServer *afp_serv,
   g_vfs_afp_command_put_pascal (comm, username);
   g_vfs_afp_command_pad_to_even (comm);
 
-  res = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
-                                                cancellable, error);
+  reply = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
+                                                  cancellable, error);
   g_object_unref (comm);
-  if (!res)
-    goto error;
-
-  reply = g_vfs_afp_connection_read_reply_sync (afp_serv->conn, cancellable, error);
   if (!reply)
     goto error;
 
@@ -316,14 +312,9 @@ dhx2_login (GVfsAfpServer *afp_serv,
   /* clientNonce */
   g_output_stream_write_all (G_OUTPUT_STREAM (comm), clientNonce_buf, 16, NULL, NULL, NULL);
 
-  res = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
-                                                cancellable, error);
+  reply = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
+                                                  cancellable, error);
   g_object_unref (comm);
-  if (!res)
-    goto error;
-
-  
-  reply = g_vfs_afp_connection_read_reply_sync (afp_serv->conn, cancellable, error);
   if (!reply)
     goto error;
 
@@ -387,13 +378,9 @@ dhx2_login (GVfsAfpServer *afp_serv,
                              G_N_ELEMENTS (answer_buf), NULL, NULL, NULL);
 
 
-  res = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
-                                                cancellable, error);
+  reply = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
+                                                  cancellable, error);
   g_object_unref (comm);
-  if (!res)
-    goto error;
-  
-  reply = g_vfs_afp_connection_read_reply_sync (afp_serv->conn, cancellable, error);
   if (!reply)
     goto error;
 
@@ -522,13 +509,9 @@ dhx_login (GVfsAfpServer *afp_serv,
   g_output_stream_write_all (G_OUTPUT_STREAM(comm), ma_buf, G_N_ELEMENTS (ma_buf),
                              NULL, NULL, NULL);
 
-  res = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
-                                                cancellable, error);
+  reply = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
+                                                  cancellable, error);
   g_object_unref (comm);
-  if (!res)
-    goto done;
-
-  reply = g_vfs_afp_connection_read_reply_sync (afp_serv->conn, cancellable, error);
   if (!reply)
     goto error;
 
@@ -616,13 +599,9 @@ dhx_login (GVfsAfpServer *afp_serv,
                              G_N_ELEMENTS (answer_buf), NULL, NULL, NULL);
 
 
-  res = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
-                                                cancellable, error);
+  reply = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
+                                                  cancellable, error);
   g_object_unref (comm);
-  if (!res)
-    goto done;
-
-  reply = g_vfs_afp_connection_read_reply_sync (afp_serv->conn, cancellable, error);
   if (!reply)
     goto error;
 
@@ -673,7 +652,6 @@ do_login (GVfsAfpServer *afp_serv,
   if (anonymous)
   {
     GVfsAfpCommand *comm;
-    gboolean res;
     GVfsAfpReply *reply;
     AfpResultCode res_code;
     
@@ -689,13 +667,9 @@ do_login (GVfsAfpServer *afp_serv,
 
     g_vfs_afp_command_put_pascal (comm, afp_version_to_string (afp_serv->version));
     g_vfs_afp_command_put_pascal (comm, AFP_UAM_NO_USER);
-    res = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
-                                                  cancellable, error);
+    reply = g_vfs_afp_connection_send_command_sync (afp_serv->conn, comm,
+                                                    cancellable, error);
     g_object_unref (comm);
-    if (!res)
-      return FALSE;
-
-    reply = g_vfs_afp_connection_read_reply_sync (afp_serv->conn, cancellable, error);
     if (!reply)
       return FALSE;
 
diff --git a/daemon/gvfsbackendafp.c b/daemon/gvfsbackendafp.c
index 26175bb..fdea031 100644
--- a/daemon/gvfsbackendafp.c
+++ b/daemon/gvfsbackendafp.c
@@ -4017,7 +4017,6 @@ get_userinfo (GVfsBackendAfp *afp_backend,
 {
   GVfsAfpCommand *comm;
   guint16 bitmap;
-  gboolean res;
 
   GVfsAfpReply *reply;
   AfpResultCode res_code;
@@ -4031,14 +4030,9 @@ get_userinfo (GVfsBackendAfp *afp_backend,
   bitmap = AFP_GET_USER_INFO_BITMAP_GET_UID_BIT | AFP_GET_USER_INFO_BITMAP_GET_GID_BIT;
   g_vfs_afp_command_put_uint16 (comm, bitmap);
 
-  res = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
-                                                comm, cancellable, error);
+  reply = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
+                                                  comm, cancellable, error);
   g_object_unref (comm);
-  if (!res)
-    return FALSE;
-
-  reply = g_vfs_afp_connection_read_reply_sync (afp_backend->server->conn,
-                                                cancellable, error);
   if (!reply)
     return FALSE;
 
@@ -4128,15 +4122,10 @@ do_mount (GVfsBackend *backend,
   /* pad byte */
   g_vfs_afp_command_put_byte (comm, 0);
 
-  res = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
-                                                comm, G_VFS_JOB (job)->cancellable,
-                                                &err);
+  reply = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
+                                                  comm, G_VFS_JOB (job)->cancellable,
+                                                  &err);
   g_object_unref (comm);
-  if (!res)
-    goto error;
-
-  reply = g_vfs_afp_connection_read_reply_sync (afp_backend->server->conn,
-                                                G_VFS_JOB (job)->cancellable, &err);
   if (!reply)
     goto error;
 
@@ -4170,15 +4159,10 @@ do_mount (GVfsBackend *backend,
 
   /* TODO: password? */
 
-  res = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
-                                                comm, G_VFS_JOB (job)->cancellable,
-                                                &err);
+  reply = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
+                                                  comm, G_VFS_JOB (job)->cancellable,
+                                                  &err);
   g_object_unref (comm);
-  if (!res)
-    goto error;
-
-  reply = g_vfs_afp_connection_read_reply_sync (afp_backend->server->conn,
-                                                G_VFS_JOB (job)->cancellable, &err);
   if (!reply)
     goto error;
 



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