[gvfs] afp: add new backend GVfsBackendAfp for mounting afp-volume shares



commit e32ef900ba0bd457b746b23ba81379992ef8885d
Author: Carl-Anton Ingmarsson <ca ingmarsson gmail com>
Date:   Fri Jun 17 00:04:32 2011 +0200

    afp: add new backend GVfsBackendAfp for mounting afp-volume shares

 daemon/Makefile.am            |   25 ++++-
 daemon/afp.mount.in           |    7 +
 daemon/gvfsafpconnection.h    |   17 +++
 daemon/gvfsbackendafp.c       |  260 +++++++++++++++++++++++++++++++++++++++++
 daemon/gvfsbackendafp.h       |   47 ++++++++
 daemon/gvfsbackendafpbrowse.c |   19 +++-
 6 files changed, 370 insertions(+), 5 deletions(-)
---
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index e5fbccf..67e1d54 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -40,10 +40,10 @@ service_DATA = gvfs-daemon.service
 %.mount: %.mount.in ../config.log
 	$(AM_V_GEN) $(SED) -e "s|\ libexecdir\@|$(libexecdir)|" $< > $@
 
-libexec_PROGRAMS=gvfsd gvfsd-sftp gvfsd-trash gvfsd-computer gvfsd-burn gvfsd-localtest gvfsd-ftp gvfsd-network gvfsd-afp-browse
+libexec_PROGRAMS=gvfsd gvfsd-sftp gvfsd-trash gvfsd-computer gvfsd-burn gvfsd-localtest gvfsd-ftp gvfsd-network gvfsd-afp-browse gvfsd-afp
 
-mount_in_files = sftp.mount.in trash.mount.in computer.mount.in burn.mount.in localtest.mount.in network.mount.in afp-browse.mount.in
-mount_DATA =  sftp.mount trash.mount computer.mount burn.mount localtest.mount network.mount afp-browse.mount
+mount_in_files = sftp.mount.in trash.mount.in computer.mount.in burn.mount.in localtest.mount.in network.mount.in afp-browse.mount.in afp.mount.in
+mount_DATA =  sftp.mount trash.mount computer.mount burn.mount localtest.mount network.mount afp-browse.mount afp.mount
 
 mount_in_files += http.mount.in dav.mount.in dav+sd.mount.in ftp.mount.in 
 if HAVE_HTTP
@@ -490,6 +490,25 @@ gvfsd_afp_browse_CPPFLAGS = \
 gvfsd_afp_browse_LDADD = \
 	$(libraries) \
 	$(LIBGCRYPT_LIBS)
+	
+gvfsd_afp_SOURCES = \
+	gvfsbackendafp.c gvfsbackendafp.h \
+	daemon-main.c daemon-main.h \
+	daemon-main-generic.c \
+	gvfsafpconnection.h \
+	gvfsafpconnection.c \
+	gvfsafpserver.h \
+	gvfsafpserver.c
+
+gvfsd_afp_CPPFLAGS = \
+	-DBACKEND_HEADER=gvfsbackendafp.h \
+	-DMAX_JOB_THREADS=1 \
+	-DBACKEND_TYPES='"afp-volume", G_VFS_TYPE_BACKEND_AFP,' \
+	$(LIBGCRYPT_CFLAGS)
+
+gvfsd_afp_LDADD = \
+	$(libraries) \
+	$(LIBGCRYPT_LIBS)
 
 
 # GSettings stuff
diff --git a/daemon/afp.mount.in b/daemon/afp.mount.in
new file mode 100644
index 0000000..44c28b0
--- /dev/null
+++ b/daemon/afp.mount.in
@@ -0,0 +1,7 @@
+[Mount]
+Type=afp-volume
+Exec= libexecdir@/gvfsd-afp
+AutoMount=false
+Scheme=afp
+DefaultPort=548
+HostnameIsInetAddress=true
\ No newline at end of file
diff --git a/daemon/gvfsafpconnection.h b/daemon/gvfsafpconnection.h
index 26fa832..365464b 100644
--- a/daemon/gvfsafpconnection.h
+++ b/daemon/gvfsafpconnection.h
@@ -29,10 +29,27 @@ G_BEGIN_DECLS
 
 typedef enum
 {
+  AFP_VOLUME_BITMAP_ATTRIBUTE_BIT       = 0x1,
+  AFP_VOLUME_BITMAP_SIGNATURE_BIT       = 0x2,
+  AFP_VOLUME_BITMAP_CREATE_DATE_BIT     = 0x4,
+  AFP_VOLUME_BITMAP_MOD_DATE_BIT        = 0x8,
+  AFP_VOLUME_BITMAP_BACKUP_DATE_BIT     = 0x10,
+  AFP_VOLUME_BITMAP_VOL_ID_BIT          = 0x20,
+  AFP_VOLUME_BITMAP_BYTES_FREE_BIT      = 0x40,
+  AFP_VOLUME_BITMAP_BYTES_TOTAL_BIT     = 0x80,
+  AFP_VOLUME_BITMAP_NAME_BIT            = 0x100,
+  AFP_VOLUME_BITMAP_EXT_BYTES_FREE_BIT  = 0x200,
+  AFP_VOLUME_BITMAP_EXT_BYTES_TOTAL_BIT = 0x400,
+  AFP_VOLUME_BITMAP_BLOCK_SIZE_BIT      = 0x800  
+} AfpVolumeBitmap;
+
+typedef enum
+{
   AFP_COMMAND_GET_SRVR_INFO = 15,
   AFP_COMMAND_GET_SRVR_PARMS = 16,
   AFP_COMMAND_LOGIN = 18,
   AFP_COMMAND_LOGIN_CONT = 19,
+  AFP_COMMAND_OPEN_VOL = 24,
   AFP_COMMAND_WRITE = 33,
   AFP_COMMAND_WRITE_EXT = 61
 } AfpCommandType;
diff --git a/daemon/gvfsbackendafp.c b/daemon/gvfsbackendafp.c
new file mode 100644
index 0000000..c7f8dc4
--- /dev/null
+++ b/daemon/gvfsbackendafp.c
@@ -0,0 +1,260 @@
+ /* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) Carl-Anton Ingmarsson 2011 <ca ingmarsson gmail com>
+ *
+ * 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.
+ *
+ * Author: Carl-Anton Ingmarsson <ca ingmarsson gmail com>
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+
+#ifdef HAVE_GCRYPT
+#include <gcrypt.h>
+#endif
+
+#include "gvfsjobmount.h"
+#include "gvfsjobenumerate.h"
+#include "gvfsjobmountmountable.h"
+#include "gmounttracker.h"
+
+#include "gvfsafpserver.h"
+#include "gvfsafpconnection.h"
+
+#include "gvfsbackendafp.h"
+
+struct _GVfsBackendAfpClass
+{
+	GVfsBackendClass parent_class;
+};
+
+struct _GVfsBackendAfp
+{
+	GVfsBackend parent_instance;
+
+	GNetworkAddress    *addr;
+  char               *volume;
+	char               *user;
+
+  GVfsAfpServer      *server;
+};
+
+
+G_DEFINE_TYPE (GVfsBackendAfp, g_vfs_backend_afp, G_VFS_TYPE_BACKEND);
+
+static void
+do_mount (GVfsBackend *backend,
+          GVfsJobMount *job,
+          GMountSpec *mount_spec,
+          GMountSource *mount_source,
+          gboolean is_automount)
+{
+  GVfsBackendAfp *afp_backend = G_VFS_BACKEND_AFP (backend);
+
+  gboolean res;
+  GError *err = NULL;
+
+  GVfsAfpCommand *comm;
+  AfpVolumeBitmap vol_bitmap;
+  
+  GVfsAfpReply *reply;
+  AfpResultCode res_code;
+  
+  GMountSpec *afp_mount_spec;
+  char       *display_name;
+
+  afp_backend->server = g_vfs_afp_server_new (afp_backend->addr);
+
+  res = g_vfs_afp_server_login (afp_backend->server, afp_backend->user, mount_source,
+                                G_VFS_JOB (job)->cancellable, &err);
+  if (!res)
+    goto error;
+
+  comm = g_vfs_afp_command_new (AFP_COMMAND_OPEN_VOL);
+  /* pad byte */
+  g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (comm), 0, NULL, NULL);
+  
+  /* Volume Bitmap */
+  vol_bitmap = AFP_VOLUME_BITMAP_VOL_ID_BIT | AFP_VOLUME_BITMAP_CREATE_DATE_BIT |
+    AFP_VOLUME_BITMAP_MOD_DATE_BIT | AFP_VOLUME_BITMAP_EXT_BYTES_FREE_BIT |
+    AFP_VOLUME_BITMAP_EXT_BYTES_TOTAL_BIT;
+  g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (comm), vol_bitmap,
+                                   NULL, NULL);
+
+  /* VolumeName */
+  g_vfs_afp_command_put_pascal (comm, afp_backend->volume);
+
+  /* TODO: password? */
+
+  res = g_vfs_afp_connection_send_command_sync (afp_backend->server->conn,
+                                                comm, G_VFS_JOB (job)->cancellable,
+                                                &err);
+  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;
+
+  res_code = g_vfs_afp_reply_get_result_code (reply);
+  if (res_code != AFP_RESULT_NO_ERROR)
+  {
+    g_object_unref (reply);
+    goto generic_error;
+  }
+  
+  /* Volume Bitmap */
+  g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (reply), NULL, NULL);
+
+  /* TODO: get ID etc. */
+  g_object_unref (reply);
+  
+  /* set mount info */
+  afp_mount_spec = g_mount_spec_new ("afp-volume");
+  g_mount_spec_set (afp_mount_spec, "host",
+                    g_network_address_get_hostname (G_NETWORK_ADDRESS (afp_backend->addr)));
+  g_mount_spec_set (afp_mount_spec, "volume", afp_backend->volume);
+  if (afp_backend->user)
+    g_mount_spec_set (afp_mount_spec, "user", afp_backend->user);
+
+  g_vfs_backend_set_mount_spec (backend, afp_mount_spec);
+  g_mount_spec_unref (afp_mount_spec);
+
+  if (afp_backend->user)
+    display_name = g_strdup_printf (_("AFP volume %s for %s on %s"), 
+                                    afp_backend->volume, afp_backend->user,
+                                    afp_backend->server->server_name);
+  else
+    display_name = g_strdup_printf (_("AFP volume %s on %s"),
+                                    afp_backend->volume, afp_backend->server->server_name);
+  
+  g_vfs_backend_set_display_name (backend, display_name);
+  g_free (display_name);
+
+  g_vfs_backend_set_icon_name (backend, "folder-remote-afp");
+  g_vfs_backend_set_user_visible (backend, TRUE);
+
+  g_vfs_job_succeeded (G_VFS_JOB (job));
+  return;
+
+error:
+  g_vfs_job_failed_from_error (G_VFS_JOB (job), err);
+  return;
+
+generic_error:
+  g_vfs_job_failed (G_VFS_JOB (job), G_IO_ERROR, G_IO_ERROR_FAILED,
+                    _("Couldn't mount AFP volume %s on %s"), afp_backend->volume,
+                    afp_backend->server->server_name);
+  return;
+}
+  
+static gboolean
+try_mount (GVfsBackend *backend,
+           GVfsJobMount *job,
+           GMountSpec *mount_spec,
+           GMountSource *mount_source,
+           gboolean is_automount)
+{
+	GVfsBackendAfp *afp_backend = G_VFS_BACKEND_AFP (backend);
+	
+	const char *host, *volume, *portstr, *user;
+	guint16 port = 548;
+	
+	host = g_mount_spec_get (mount_spec, "host");
+	if (host == NULL)
+		{
+			g_vfs_job_failed (G_VFS_JOB (job),
+			                  G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
+			                  _("No hostname specified"));
+			return TRUE;
+    }
+
+  volume = g_mount_spec_get (mount_spec, "volume");
+  if (volume == NULL)
+  {
+    g_vfs_job_failed (G_VFS_JOB (job),
+                      G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
+                      _("No volume specified"));
+    return TRUE;
+  }
+  afp_backend->volume = g_strdup (volume);
+  
+	portstr = g_mount_spec_get (mount_spec, "port");
+	if (portstr != NULL)
+		{
+			port = atoi (portstr);
+		}
+
+	afp_backend->addr = G_NETWORK_ADDRESS (g_network_address_new (host, port));
+	
+	user = g_mount_spec_get (mount_spec, "user");
+	afp_backend->user = g_strdup (user);
+	
+	return FALSE;
+}
+
+static void
+g_vfs_backend_afp_init (GVfsBackendAfp *object)
+{
+   GVfsBackendAfp *afp_backend = G_VFS_BACKEND_AFP (object);
+  afp_backend->volume = NULL;
+  afp_backend->user = NULL;
+
+  afp_backend->addr = NULL;
+}
+
+static void
+g_vfs_backend_afp_finalize (GObject *object)
+{
+  GVfsBackendAfp *afp_backend = G_VFS_BACKEND_AFP (object);
+
+  g_free (afp_backend->volume);
+  g_free (afp_backend->user);
+
+  if (afp_backend->addr)
+    g_object_unref (afp_backend->addr);
+  
+	G_OBJECT_CLASS (g_vfs_backend_afp_parent_class)->finalize (object);
+}
+
+static void
+g_vfs_backend_afp_class_init (GVfsBackendAfpClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GVfsBackendClass *backend_class = G_VFS_BACKEND_CLASS (klass);
+
+	object_class->finalize = g_vfs_backend_afp_finalize;
+
+	backend_class->try_mount = try_mount;
+  backend_class->mount = do_mount;
+}
+
+void
+g_vfs_afp_daemon_init (void)
+{
+  g_set_application_name (_("Apple Filing Protocol Service"));
+
+#ifdef HAVE_GCRYPT
+  gcry_check_version (NULL);
+  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+#endif
+}
diff --git a/daemon/gvfsbackendafp.h b/daemon/gvfsbackendafp.h
new file mode 100644
index 0000000..1928a57
--- /dev/null
+++ b/daemon/gvfsbackendafp.h
@@ -0,0 +1,47 @@
+ /* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) Carl-Anton Ingmarsson 2011 <ca ingmarsson gmail com>
+ *
+ * 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.
+ *
+ * Author: Carl-Anton Ingmarsson <ca ingmarsson gmail com>
+ */
+
+#ifndef _GVFSBACKENDAFP_H_
+#define _GVFSBACKENDAFP_H_
+
+#include <gvfsbackend.h>
+
+G_BEGIN_DECLS
+
+#define G_VFS_TYPE_BACKEND_AFP             (g_vfs_backend_afp_get_type ())
+#define G_VFS_BACKEND_AFP(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_VFS_TYPE_BACKEND_AFP, GVfsBackendAfp))
+#define G_VFS_BACKEND_AFP_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), G_VFS_TYPE_BACKEND_AFP, GVfsBackendAfpClass))
+#define G_IS_VFS_BACKEND_AFP(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_VFS_TYPE_BACKEND_AFP))
+#define G_IS_VFS_BACKEND_AFP_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), G_VFS_TYPE_BACKEND_AFP))
+#define G_VFS_BACKEND_AFP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), G_VFS_TYPE_BACKEND_AFP, GVfsBackendAfpClass))
+
+typedef struct _GVfsBackendAfpClass GVfsBackendAfpClass;
+typedef struct _GVfsBackendAfp GVfsBackendAfp;
+
+GType g_vfs_backend_afp_get_type (void) G_GNUC_CONST;
+
+#define BACKEND_SETUP_FUNC g_vfs_afp_daemon_init
+void g_vfs_afp_daemon_init (void);
+
+G_END_DECLS
+
+#endif /* _GVFSBACKENDAFP_H_ */
diff --git a/daemon/gvfsbackendafpbrowse.c b/daemon/gvfsbackendafpbrowse.c
index c448fb5..4940361 100644
--- a/daemon/gvfsbackendafpbrowse.c
+++ b/daemon/gvfsbackendafpbrowse.c
@@ -277,6 +277,8 @@ get_srvr_parms_cb (GVfsAfpConnection *afp_connection,
 
     g_vfs_job_enumerate_add_info (job, info);
     g_object_unref (info);
+
+    g_free (vol_name);
   }
 
   g_vfs_job_enumerate_done (job);
@@ -371,8 +373,13 @@ do_mount (GVfsBackend *backend,
 
   g_vfs_backend_set_mount_spec (backend, afp_mount_spec);
   g_mount_spec_unref (afp_mount_spec);
-  
-  display_name = g_strdup_printf (_("AFP shares on %s"), afp_backend->server->server_name);
+
+  if (afp_backend->user)
+    display_name = g_strdup_printf (_("AFP shares for %s on %s"), afp_backend->user,
+                                    afp_backend->server->server_name);
+  else
+    display_name = g_strdup_printf (_("AFP shares on %s"),
+                                    afp_backend->server->server_name);
   g_vfs_backend_set_display_name (backend, display_name);
   g_free (display_name);
 
@@ -428,6 +435,9 @@ g_vfs_backend_afp_browse_init (GVfsBackendAfpBrowse *object)
   GVfsBackendAfpBrowse *afp_backend = G_VFS_BACKEND_AFP_BROWSE (object);
 
   afp_backend->mount_tracker = g_mount_tracker_new (NULL);
+
+  afp_backend->addr = NULL;
+  afp_backend->user = NULL;
 }
 
 static void
@@ -436,6 +446,11 @@ g_vfs_backend_afp_browse_finalize (GObject *object)
   GVfsBackendAfpBrowse *afp_backend = G_VFS_BACKEND_AFP_BROWSE (object);
 
   g_object_unref (afp_backend->mount_tracker);
+
+  if (afp_backend->addr)
+    g_object_unref (afp_backend->addr);
+  
+  g_free (afp_backend->user);
   
 	G_OBJECT_CLASS (g_vfs_backend_afp_browse_parent_class)->finalize (object);
 }



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