# HG changeset patch
# User renatofilho
# Date 1175003631 -3600
# Node ID f76bb8743b22c15e8cd109c71c79c68990f453bd
# Parent  a806d8ad0ff0b7cc4c33559a18f4e271255719bb
[svn r459] bug fix on file_transfer

diff -r a806d8ad0ff0 -r f76bb8743b22 gmyth/configure.ac
--- a/gmyth/configure.ac	Tue Mar 27 00:13:27 2007 +0100
+++ b/gmyth/configure.ac	Tue Mar 27 14:53:51 2007 +0100
@@ -81,8 +81,8 @@
 dnl Test if --disable-debug given
 AC_ARG_ENABLE(debug,
         [AC_HELP_STRING([--disable-debug], [disable debugging mode])],
-        enable_debug="$enableval",
-        enable_debug=no)
+        enable_debug=no,
+        enable_debug=yes)
 
 if test "x$enable_debug" = "xyes"; then
     CFLAGS="$CFLAGS -g -DGMYTH_USE_DEBUG"
diff -r a806d8ad0ff0 -r f76bb8743b22 gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Tue Mar 27 00:13:27 2007 +0100
+++ b/gmyth/src/gmyth_file_transfer.c	Tue Mar 27 14:53:51 2007 +0100
@@ -638,12 +638,12 @@
  * @return The actual block size (in bytes) returned by REQUEST_BLOCK message,
  * 				or the error code. 
  */
-gint 
+gint64 
 gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited)
 {
   gint bytes_sent = 0;
   gsize bytes_read = 0;
-  gsize total_read = 0;
+  gint64 total_read = 0;
   
   GError *error = NULL;
   
@@ -697,43 +697,58 @@
     gmyth_string_list_append_int( strlist, size );
 
     // Request the block to the backend
-    gmyth_socket_write_stringlist( transfer->control_sock, strlist );
+    gmyth_socket_write_stringlist (transfer->control_sock, strlist);
 
     // Receives the backand answer    
-    gmyth_socket_read_stringlist( transfer->control_sock, strlist );
+    gmyth_socket_read_stringlist (transfer->control_sock, strlist);
 
-    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
-    { 
-	    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
+    if (strlist != NULL && gmyth_string_list_length (strlist) > 0) { 
+	    bytes_sent = gmyth_string_list_get_int (strlist,  0); // -1 on backend error
 	    gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
 
-    	if ( bytes_sent > 0 )
-    	{
-        gchar *data_buffer = g_new0 ( gchar, bytes_sent );    
-   	    while ( 0 != bytes_sent ) 
-   	    { 
-          io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
-	    				(gsize) bytes_sent,	&bytes_read, &error );
+    	if (bytes_sent > 0) {
+            gchar *data_buffer = g_new0 (gchar, bytes_sent);
+            while (bytes_sent > 0) { 
+                io_status = g_io_channel_read_chars (io_channel, 
+                                                     data_buffer,
+                                                     (gsize) bytes_sent,
+                                                     &bytes_read, 
+                                                     &error);
 
-	        total_read += bytes_read;
-          bytes_sent -= bytes_read;
+                if (io_status != G_IO_STATUS_NORMAL) {
+                    gmyth_debug ("Error on io_channel");
+                    g_free (data_buffer);
+                    total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
+                    g_object_unref (strlist);
+                    break;
+                }
 
-            /* append new data to the increasing byte array */
-	    		data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
-	        gmyth_debug ("Total transfer data read: %d\n", total_read);
-        }
-        g_free (data_buffer);
-      } else if ( bytes_sent <= 0 && !read_unlimited ) {
-      	total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
+                total_read += bytes_read;
+                bytes_sent -= bytes_read;
+
+                /* append new data to the increasing byte array */
+                data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
+                gmyth_debug ("Total transfer data read: %"G_GINT64_FORMAT"\n", total_read);
+            }
+            g_free (data_buffer);
+        } else if (!read_unlimited) {
+      	    total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
     		g_object_unref (strlist);
     		strlist = NULL;
     		break;
-    	}
-    } else if ( !read_unlimited || !(transfer->priv != NULL && transfer->priv->recorder != NULL &&
+        } else {
+            /* wait for more data */
+            g_usleep (300);
+        }
+    } else { 
+        /*if (!(transfer->priv != NULL && transfer->priv->recorder != NULL &&
 					transfer->priv->do_next_program_chain) ) {
+        */                    
     	total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
-    	g_object_unref (strlist);
-    	strlist = NULL;
+        if (strlist != NULL) {
+        	g_object_unref (strlist);
+        	strlist = NULL;
+        }
     	break;
     }
     if ( strlist!=NULL )
@@ -743,19 +758,15 @@
     }
   } /* while - iterates through bytes until reaches the end of stream */
 
-  if ( read_unlimited && ( bytes_sent == 0 ) && ( max_tries == 0 ) )
-	{
+  if (read_unlimited && (bytes_sent == 0) && (max_tries == 0) ) {
 		gmyth_debug( "Trying to move to the next program chain..." );
 		transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
 		
 		if ( transfer->priv != NULL && transfer->priv->recorder != NULL &&
 					transfer->priv->do_next_program_chain )
 		{
-		
-			  total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
-		
+		    total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
 	  		//g_mutex_lock( transfer->mutex );
-	  		
 	  		//ret = gmyth_livetv_next_program_chain( transfer->priv->recorder );
 	  		GMythProgramInfo *prog_info = gmyth_recorder_get_current_program_info( transfer->priv->recorder );
 	  		
@@ -775,7 +786,7 @@
 										transfer->filename );						
 		}
 
-	} /* if */
+  } /* if */
   
   myth_control_release_context( transfer );
   g_string_free (query, TRUE);
diff -r a806d8ad0ff0 -r f76bb8743b22 gmyth/src/gmyth_file_transfer.h
--- a/gmyth/src/gmyth_file_transfer.h	Tue Mar 27 00:13:27 2007 +0100
+++ b/gmyth/src/gmyth_file_transfer.h	Tue Mar 27 14:53:51 2007 +0100
@@ -102,7 +102,7 @@
                                 					     const gchar* filename);
 void                gmyth_file_transfer_close           (GMythFileTransfer *transfer);
 gboolean            gmyth_file_transfer_is_open         (GMythFileTransfer *transfer);
-gint                gmyth_file_transfer_read            (GMythFileTransfer *transfer, 
+gint64              gmyth_file_transfer_read            (GMythFileTransfer *transfer, 
                                                          GByteArray *data, 
                                                          gint size, 
                                                          gboolean read_unlimited);
diff -r a806d8ad0ff0 -r f76bb8743b22 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Tue Mar 27 00:13:27 2007 +0100
+++ b/gmyth/src/gmyth_livetv.c	Tue Mar 27 14:53:51 2007 +0100
@@ -470,7 +470,7 @@
 	
 		} /* if - changes the channel number */
 		
-    //sleep (6);                  /* FIXME: this is evil (tpm) */
+    sleep (6);                  /* FIXME: this is evil (tpm) */
   }
   
   /* DEBUG message */