[easytag] Use _all() variants with streams when appropriate



commit 6eb3d68f9afa0f44922aa275f11368a61ce79a6b
Author: David King <amigadave amigadave com>
Date:   Sat Jun 1 23:29:24 2013 +0100

    Use _all() variants with streams when appropriate
    
    Use the _all() variants of g_input_stream_read() and
    g_output_stream_write() when accessing a required chunk from the stream.
    Only report errors on failure to read or write the whole chunk.

 src/crc32.c   |   19 +++++++++++++------
 src/ogg_tag.c |   19 +++++++++++--------
 src/picture.c |   45 ++++++++++++++++++---------------------------
 3 files changed, 42 insertions(+), 41 deletions(-)
---
diff --git a/src/crc32.c b/src/crc32.c
index aeea50d..88469cf 100644
--- a/src/crc32.c
+++ b/src/crc32.c
@@ -115,6 +115,7 @@ crc32_file_with_ID3_tag (const gchar *filename, guint32 *crc32, GError **err)
     GFileInfo *info;
     GFileInputStream *istream;
     goffset size;
+    gsize bytes_read;
     gboolean has_id3v1 = FALSE;
 
     g_return_val_if_fail (filename != NULL, FALSE);
@@ -148,9 +149,11 @@ crc32_file_with_ID3_tag (const gchar *filename, guint32 *crc32, GError **err)
         goto error;
     }
 
-    if (g_input_stream_read (G_INPUT_STREAM (istream), tmp_id3, 3,
-                             NULL, err) != 3)
+    if (!g_input_stream_read_all (G_INPUT_STREAM (istream), tmp_id3, 3,
+                                  &bytes_read, NULL, err))
     {
+        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of 3 bytes of data were "
+                 "read", bytes_read);
         goto error;
     }
 
@@ -165,9 +168,11 @@ crc32_file_with_ID3_tag (const gchar *filename, guint32 *crc32, GError **err)
         goto error;
     }
 
-    if (g_input_stream_read (G_INPUT_STREAM (istream), tmp_id3, 4,
-                             NULL, err) != 4)
+    if (!g_input_stream_read_all (G_INPUT_STREAM (istream), tmp_id3, 4,
+                                  &bytes_read, NULL, err))
     {
+        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of 4 bytes of data were "
+                 "read", bytes_read);
         goto error;
     }
 
@@ -184,9 +189,11 @@ crc32_file_with_ID3_tag (const gchar *filename, guint32 *crc32, GError **err)
             goto error;
         }
 
-        if (g_input_stream_read (G_INPUT_STREAM (istream), tmp_id3, 4,
-                                 NULL, err) != 4)
+        if (!g_input_stream_read_all (G_INPUT_STREAM (istream), tmp_id3, 4,
+                                      &bytes_read, NULL, err))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of 4 bytes of data "
+                     "were read", bytes_read);
             goto error;
         }
 
diff --git a/src/ogg_tag.c b/src/ogg_tag.c
index 89532aa..39511e7 100644
--- a/src/ogg_tag.c
+++ b/src/ogg_tag.c
@@ -694,6 +694,7 @@ ogg_tag_write_file_tag (ET_File *ETFile, GError **error)
 
     {
     // Skip the id3v2 tag
+    gsize bytes_read;
     guchar tmp_id3[4];
     gulong id3v2size;
 
@@ -703,8 +704,8 @@ ogg_tag_write_file_tag (ET_File *ETFile, GError **error)
         goto err;
     }
 
-    if (g_input_stream_read (G_INPUT_STREAM (istream), tmp_id3, 4, NULL,
-                             error) == 4)
+    if (g_input_stream_read_all (G_INPUT_STREAM (istream), tmp_id3, 4,
+                                 &bytes_read, NULL, error))
     {
         // Calculate ID3v2 length
         if (tmp_id3[0] == 'I' && tmp_id3[1] == 'D' && tmp_id3[2] == '3' && tmp_id3[3] < 0xFF)
@@ -717,8 +718,8 @@ ogg_tag_write_file_tag (ET_File *ETFile, GError **error)
                 goto err;
             }
 
-            if (g_input_stream_read (G_INPUT_STREAM (istream), tmp_id3, 4,
-                                     NULL, error) == 4)
+            if (g_input_stream_read_all (G_INPUT_STREAM (istream), tmp_id3, 4,
+                                         &bytes_read, NULL, error))
             {
                 id3v2size = 10 + ( (long)(tmp_id3[3])        | ((long)(tmp_id3[2]) << 7)
                                 | ((long)(tmp_id3[1]) << 14) | ((long)(tmp_id3[0]) << 21) );
@@ -731,9 +732,10 @@ ogg_tag_write_file_tag (ET_File *ETFile, GError **error)
 
                 Log_Print(LOG_ERROR,_("Warning: The Ogg Vorbis file '%s' contains an ID3v2 
tag."),filename_utf8);
             }
-            else if (!g_seekable_seek (G_SEEKABLE (istream), 0L, G_SEEK_SET,
-                                       NULL, error))
+            else
             {
+                g_debug ("Only %" G_GSIZE_FORMAT " bytes out of 4 bytes of "
+                         "data were read", bytes_read);
                 goto err;
             }
 
@@ -745,9 +747,10 @@ ogg_tag_write_file_tag (ET_File *ETFile, GError **error)
         }
 
     }
-    else if (!g_seekable_seek (G_SEEKABLE (istream), 0L, G_SEEK_SET,
-                               NULL, error))
+    else
     {
+        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of 4 bytes of data were "
+                 "read", bytes_read);
         goto err;
     }
 
diff --git a/src/picture.c b/src/picture.c
index 91cfee7..57d41e0 100644
--- a/src/picture.c
+++ b/src/picture.c
@@ -1103,6 +1103,7 @@ void Picture_Free (Picture *pic)
 /*
  * et_picture_load_file_data:
  * @file: the GFile from which to load an image
+ * @error: a #GError to provide information on errors, or %NULL to ignore
  *
  * Load an image from the supplied @file.
  *
@@ -1112,9 +1113,9 @@ static Picture *
 et_picture_load_file_data (GFile *file, GError **error)
 {
     Picture *pic;
-    gchar *buffer = 0;
+    gchar *buffer = NULL;
     gsize size;
-    gssize read_size;
+    gsize bytes_read;
     GFileInfo *info;
     GFileInputStream *file_istream;
 
@@ -1131,37 +1132,24 @@ et_picture_load_file_data (GFile *file, GError **error)
 
     if (!file_istream)
     {
-        g_free (buffer);
         g_assert (error == NULL || *error != NULL);
         return NULL;
     }
 
     size = g_file_info_get_size (info);
-    buffer = g_malloc(size);
+    buffer = g_malloc (size);
+    g_object_unref (info);
 
-    read_size = g_input_stream_read (G_INPUT_STREAM (file_istream), buffer,
-                                     size, NULL, error);
-    if (read_size == -1)
+    if (!g_input_stream_read_all (G_INPUT_STREAM (file_istream), buffer, size,
+                                  &bytes_read, NULL, error))
     {
-        /* Error on reading*/
-        if (buffer)
-            g_free (buffer);
+        g_free (buffer);
 
-        g_object_unref (info);
-        g_object_unref (file_istream);
-        g_assert (error == NULL || *error != NULL);
-        return NULL;
-    }
-    else if (read_size != size)
-    {
-        /* Did not read whole file. */
-        if (buffer)
-            g_free (buffer);
+        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                 " bytes of picture data were read", bytes_read, size);
 
-        g_object_unref (info);
         g_object_unref (file_istream);
-        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
-                             _("The whole image file could not be read"));
+        g_assert (error == NULL || *error != NULL);
         return NULL;
     }
     else
@@ -1171,7 +1159,6 @@ et_picture_load_file_data (GFile *file, GError **error)
         pic->size = size;
         pic->data = (guchar *)buffer;
 
-        g_object_unref (info);
         g_object_unref (file_istream);
         g_assert (error == NULL || *error == NULL);
         return pic;
@@ -1182,7 +1169,7 @@ et_picture_load_file_data (GFile *file, GError **error)
  * et_picture_save_file_data:
  * @pic: the #Picture from which to take an image
  * @file: the #GFile for which to save an image
- * @error: a #GError to provide information on erros, or %NULL to ignore
+ * @error: a #GError to provide information on errors, or %NULL to ignore
  *
  * Saves an image from @pic to the supplied @file.
  *
@@ -1192,6 +1179,7 @@ static gboolean
 et_picture_save_file_data (const Picture *pic, GFile *file, GError **error)
 {
     GFileOutputStream *file_ostream;
+    gsize bytes_written;
 
     g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
@@ -1204,9 +1192,12 @@ et_picture_save_file_data (const Picture *pic, GFile *file, GError **error)
         return FALSE;
     }
 
-    if (g_output_stream_write (G_OUTPUT_STREAM (file_ostream), pic->data,
-                               pic->size, NULL, error) != pic->size)
+    if (!g_output_stream_write_all (G_OUTPUT_STREAM (file_ostream), pic->data,
+                                    pic->size, &bytes_written, NULL, error))
     {
+        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                 " bytes of picture data were written", bytes_written,
+                 pic->size);
         g_object_unref (file_ostream);
         g_assert (error == NULL || *error != NULL);
         return FALSE;


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