[svn r158] Fixes seg fault when getting URI fields.
2 * @author Hallyson Melo <hallyson.melo@indt.org.br>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <libgnomevfs/gnome-vfs-module.h>
28 #include <libgnomevfs/gnome-vfs-utils.h>
30 #include "gmyth_file_transfer.h"
32 #define GST_MYTHTV_ID_NUM 1
33 #define MYTHTV_VERSION_DEFAULT 30
35 #define MYTHTV_BUFFER_SIZE 1024*64
37 static GnomeVFSResult do_read (GnomeVFSMethod *method,
38 GnomeVFSMethodHandle *method_handle,
40 GnomeVFSFileSize num_bytes,
41 GnomeVFSFileSize *bytes_read,
42 GnomeVFSContext *context);
45 GMythFileTransfer *file_transfer;
58 do_open (GnomeVFSMethod *method,
59 GnomeVFSMethodHandle **method_handle,
61 GnomeVFSOpenMode mode,
62 GnomeVFSContext *context)
66 MythtvHandle *myth_handle;
71 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
72 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
74 //g_debug ("[%s] Calling do_open function", __FUNCTION__);
76 myth_handle = g_new0 (MythtvHandle, 1);
78 if (mode & GNOME_VFS_OPEN_WRITE) {
79 return GNOME_VFS_ERROR_NOT_PERMITTED;
82 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
83 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
86 transfer_uri = gnome_vfs_uri_to_string (uri,
87 GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
89 /* Initialize mythtv handler*/
90 myth_handle->file_transfer = NULL;
91 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
92 myth_handle->bytes_read = 0;
93 myth_handle->content_size = -1;
95 /* Connect to the backend */
96 myth_handle->file_transfer = gmyth_file_transfer_new ();
98 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
100 /* sets the Playback monitor connection */
101 uri_str = g_string_new (transfer_uri);
102 g_free (transfer_uri);
104 ret = gmyth_file_transfer_open (myth_handle->file_transfer, uri_str);
106 g_printerr ("Mythtv FileTransfer open error\n");
107 return GNOME_VFS_ERROR_NOT_OPEN;
109 g_string_free (uri_str, TRUE);
111 // TODO: Verify if file exists in the backend
113 myth_handle->content_size = myth_handle->file_transfer->filesize;
115 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
116 myth_handle->buffer_remain = 0;
118 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
123 static GnomeVFSResult
124 do_read (GnomeVFSMethod *method,
125 GnomeVFSMethodHandle *method_handle,
127 GnomeVFSFileSize num_bytes,
128 GnomeVFSFileSize *bytes_read,
129 GnomeVFSContext *context)
131 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
132 GnomeVFSFileSize bytes_to_read;
134 //g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_remain: %du",
135 // myth_handle->content_size, myth_handle->bytes_read, num_bytes,
136 // myth_handle->buffer_remain);
140 if (myth_handle->bytes_read >= myth_handle->content_size)
141 return GNOME_VFS_ERROR_EOF;
143 // fixme: change this to min math function
144 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
145 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
147 bytes_to_read = num_bytes;
149 /* Loop sending the Myth File Transfer request:
150 * Retry whilst authentication fails and we supply it. */
152 if ( bytes_to_read > myth_handle->buffer_remain ) {
153 GByteArray *tmp_buffer = g_byte_array_new();
155 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
156 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
159 g_byte_array_free (tmp_buffer, TRUE);
160 return GNOME_VFS_ERROR_IO;
163 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
164 tmp_buffer->data, len);
166 myth_handle->buffer_remain += len;
168 //g_debug ( "Reading %d data from backend", MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain );
170 g_byte_array_free (tmp_buffer, TRUE);
174 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
176 //g_debug ("Returning %du bytes to gnomevfs", (gint) bytes_to_read);
177 /* gets the first buffer_size bytes from the byte array buffer variable */
179 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
181 myth_handle->bytes_read += bytes_to_read;
182 myth_handle->buffer_remain -= bytes_to_read;
184 /* flushs the newly buffer got from byte array */
185 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
186 *bytes_read = bytes_to_read;
191 static GnomeVFSResult
192 do_close (GnomeVFSMethod *method,
193 GnomeVFSMethodHandle *method_handle,
194 GnomeVFSContext *context)
197 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
199 if (myth_handle->file_transfer) {
200 gmyth_file_transfer_close (myth_handle->file_transfer);
201 g_object_unref (myth_handle->file_transfer);
202 myth_handle->file_transfer = NULL;
205 if (myth_handle->buffer) {
206 g_byte_array_free (myth_handle->buffer, TRUE);
207 myth_handle->buffer = NULL;
210 g_free (myth_handle);
215 static GnomeVFSResult
216 do_get_file_info (GnomeVFSMethod *method,
218 GnomeVFSFileInfo *file_info,
219 GnomeVFSFileInfoOptions options,
220 GnomeVFSContext *context)
222 file_info->name = g_strdup ("fixme.txt");
223 file_info->valid_fields = file_info->valid_fields
224 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
225 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
226 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
227 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
228 // fixme: get from file extension?
229 file_info->mime_type = g_strdup ("video/x-nuv");
230 file_info->permissions =
231 GNOME_VFS_PERM_USER_READ |
232 GNOME_VFS_PERM_OTHER_READ |
233 GNOME_VFS_PERM_GROUP_READ;
239 do_is_local (GnomeVFSMethod *method,
240 const GnomeVFSURI *uri)
245 static GnomeVFSMethod method = {
246 sizeof (GnomeVFSMethod),
277 vfs_module_init (const char *method_name, const char *args)
283 vfs_module_shutdown (GnomeVFSMethod *method)