1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22 
23 #include <config.h>
24 
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include "gvfsjobstartmountable.h"
33 
34 G_DEFINE_TYPE (GVfsJobStartMountable, g_vfs_job_start_mountable, G_VFS_TYPE_JOB_DBUS)
35 
36 static void         run          (GVfsJob        *job);
37 static gboolean     try          (GVfsJob        *job);
38 static void         create_reply (GVfsJob               *job,
39                                   GVfsDBusMount         *object,
40                                   GDBusMethodInvocation *invocation);
41 
42 static void
g_vfs_job_start_mountable_finalize(GObject * object)43 g_vfs_job_start_mountable_finalize (GObject *object)
44 {
45   GVfsJobStartMountable *job;
46 
47   job = G_VFS_JOB_START_MOUNTABLE (object);
48 
49   if (job->mount_source)
50     g_object_unref (job->mount_source);
51 
52   g_free (job->filename);
53 
54   if (G_OBJECT_CLASS (g_vfs_job_start_mountable_parent_class)->finalize)
55     (*G_OBJECT_CLASS (g_vfs_job_start_mountable_parent_class)->finalize) (object);
56 }
57 
58 static void
g_vfs_job_start_mountable_class_init(GVfsJobStartMountableClass * klass)59 g_vfs_job_start_mountable_class_init (GVfsJobStartMountableClass *klass)
60 {
61   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
62   GVfsJobClass *job_class = G_VFS_JOB_CLASS (klass);
63   GVfsJobDBusClass *job_dbus_class = G_VFS_JOB_DBUS_CLASS (klass);
64 
65   gobject_class->finalize = g_vfs_job_start_mountable_finalize;
66   job_class->run = run;
67   job_class->try = try;
68   job_dbus_class->create_reply = create_reply;
69 }
70 
71 static void
g_vfs_job_start_mountable_init(GVfsJobStartMountable * job)72 g_vfs_job_start_mountable_init (GVfsJobStartMountable *job)
73 {
74 }
75 
76 gboolean
g_vfs_job_start_mountable_new_handle(GVfsDBusMount * object,GDBusMethodInvocation * invocation,const gchar * arg_path_data,const gchar * arg_dbus_id,const gchar * arg_obj_path,GVfsBackend * backend)77 g_vfs_job_start_mountable_new_handle (GVfsDBusMount *object,
78                                       GDBusMethodInvocation *invocation,
79                                       const gchar *arg_path_data,
80                                       const gchar *arg_dbus_id,
81                                       const gchar *arg_obj_path,
82                                       GVfsBackend *backend)
83 {
84   GVfsJobStartMountable *job;
85 
86   if (g_vfs_backend_invocation_first_handler (object, invocation, backend))
87     return TRUE;
88 
89   job = g_object_new (G_VFS_TYPE_JOB_START_MOUNTABLE,
90                       "object", object,
91                       "invocation", invocation,
92                       NULL);
93 
94   job->filename = g_strdup (arg_path_data);
95   job->backend = backend;
96   job->mount_source = g_mount_source_new (arg_dbus_id, arg_obj_path);
97 
98   g_vfs_job_source_new_job (G_VFS_JOB_SOURCE (backend), G_VFS_JOB (job));
99   g_object_unref (job);
100 
101   return TRUE;
102 }
103 
104 static void
run(GVfsJob * job)105 run (GVfsJob *job)
106 {
107   GVfsJobStartMountable *op_job = G_VFS_JOB_START_MOUNTABLE (job);
108   GVfsBackendClass *class = G_VFS_BACKEND_GET_CLASS (op_job->backend);
109 
110   if (class->start_mountable == NULL)
111     {
112       g_vfs_job_failed (job, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
113 			_("Operation not supported"));
114       return;
115     }
116 
117   class->start_mountable (op_job->backend,
118 			  op_job,
119 			  op_job->filename,
120 			  op_job->mount_source);
121 }
122 
123 static gboolean
try(GVfsJob * job)124 try (GVfsJob *job)
125 {
126   GVfsJobStartMountable *op_job = G_VFS_JOB_START_MOUNTABLE (job);
127   GVfsBackendClass *class = G_VFS_BACKEND_GET_CLASS (op_job->backend);
128 
129   if (class->try_start_mountable == NULL)
130     return FALSE;
131 
132   return class->try_start_mountable (op_job->backend,
133 				     op_job,
134 				     op_job->filename,
135 				     op_job->mount_source);
136 }
137 
138 /* Might be called on an i/o thread */
139 static void
create_reply(GVfsJob * job,GVfsDBusMount * object,GDBusMethodInvocation * invocation)140 create_reply (GVfsJob *job,
141               GVfsDBusMount *object,
142               GDBusMethodInvocation *invocation)
143 {
144   gvfs_dbus_mount_complete_start_mountable (object, invocation);
145 }
146