[easytag] Use g_output_stream_write_all() throughout



commit 8eefaf5af3329089818c1a216ae75cc7868a79d4
Author: David King <amigadave amigadave com>
Date:   Sun Jun 2 09:36:26 2013 +0100

    Use g_output_stream_write_all() throughout
    
    As with commit 6eb3d68f9afa0f44922aa275f11368a61ce79a6b, use the _all()
    variant when writing a chunk of data to a stream. Only report errors on
    failure to write the whole chunk.

 src/id3_tag.c |   12 +++--
 src/log.c     |    9 +++-
 src/misc.c    |   96 +++++++++++++++++++++++++++++++++-------------
 src/vcedit.c  |  118 +++++++++++++++++++++++++++++++++++++-------------------
 4 files changed, 161 insertions(+), 74 deletions(-)
---
diff --git a/src/id3_tag.c b/src/id3_tag.c
index eeaf2be..35d2adf 100644
--- a/src/id3_tag.c
+++ b/src/id3_tag.c
@@ -1277,8 +1277,7 @@ gboolean Id3tag_Check_If_Id3lib_Is_Bugged (void)
     gchar *result = NULL;
     ID3Frame *id3_frame;
     gboolean use_unicode;
-    gssize count;
-
+    gsize bytes_written;
 
     /* Create a temporary file. */
     file = g_file_new_tmp ("easytagXXXXXX.mp3", &iostream, &error);
@@ -1302,13 +1301,16 @@ gboolean Id3tag_Check_If_Id3lib_Is_Bugged (void)
 
     /* Set data in the file. */
     ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
-    count = g_output_stream_write (G_OUTPUT_STREAM (ostream), tmp,
-                                   sizeof (tmp), NULL, &error);
-    if (count != sizeof (tmp))
+    if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream), tmp,
+                                    sizeof (tmp), &bytes_written, NULL,
+                                    &error))
     {
         gchar *filename;
         gchar *filename_utf8;
 
+        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                 " bytes of data were written", bytes_written, sizeof (tmp));
+
         filename = g_file_get_path (file);
         filename_utf8 = filename_to_display (filename);
         Log_Print (LOG_ERROR, _("Error while writing to file: '%s' (%s)"),
diff --git a/src/log.c b/src/log.c
index 5fede0c..ef45308 100644
--- a/src/log.c
+++ b/src/log.c
@@ -336,6 +336,7 @@ void Log_Print (Log_Error_Type error_type, gchar const *format, ...)
     {
         gchar *time;
         GString *data;
+        gsize bytes_written;
 
         time = Log_Format_Date ();
         data = g_string_new (time);
@@ -347,9 +348,13 @@ void Log_Print (Log_Error_Type error_type, gchar const *format, ...)
 
         data = g_string_append_c (data, '\n');
 
-        if (g_output_stream_write (G_OUTPUT_STREAM (file_ostream), data->str,
-                                   data->len, NULL, &error) != data->len)
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (file_ostream),
+                                        data->str, data->len, &bytes_written,
+                                        NULL, &error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     "bytes of data were written", bytes_written, data->len);
+
             /* To avoid recursion of Log_Print. */
             g_warning ("Error writing to the log file '%s' ('%s')", file_path,
                        error->message);
diff --git a/src/misc.c b/src/misc.c
index 5bf1fac..8cf0fdb 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1613,12 +1613,17 @@ write_playlist (GFile *file, GError **error)
     // 1) First line of the file (if playlist content is not set to "write only list of files")
     if (!PLAYLIST_CONTENT_NONE)
     {
+        gsize bytes_written;
+
         to_write = g_string_new ("#EXTM3U\r\n");
 
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), to_write->str,
-                                   to_write->len, NULL, error)
-            != to_write->len)
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                        to_write->str, to_write->len,
+                                        &bytes_written, NULL, error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     "bytes of data were written", bytes_written,
+                     to_write->len);
             g_assert (error == NULL || *error != NULL);
             g_string_free (to_write, TRUE);
             g_object_unref (ostream);
@@ -1659,12 +1664,15 @@ write_playlist (GFile *file, GError **error)
             // Keep only files in this directory and sub-dirs
             if ( strncmp(filename,basedir,strlen(basedir))==0 )
             {
+                gsize bytes_written;
+
                 // 2) Write the header
                 if (PLAYLIST_CONTENT_NONE)
                 {
                     // No header written
                 }else if (PLAYLIST_CONTENT_FILENAME)
                 {
+
                     // Header uses only filename
                     temp = g_path_get_basename(filename);
                     to_write = g_string_new ("#EXTINF:");
@@ -1672,10 +1680,15 @@ write_playlist (GFile *file, GError **error)
                     g_string_append_printf (to_write, "%d,%s\r\n", duration,
                                             temp);
 
-                    if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                               to_write->str, to_write->len,
-                                               NULL, error) != to_write->len)
+                    if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                    to_write->str,
+                                                    to_write->len,
+                                                    &bytes_written, NULL,
+                                                    error))
                     {
+                        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                                 G_GSIZE_FORMAT "bytes of data were written",
+                                 bytes_written, to_write->len);
                         g_assert (error == NULL || *error != NULL);
                         g_string_free (to_write, TRUE);
                         g_object_unref (ostream);
@@ -1696,10 +1709,15 @@ write_playlist (GFile *file, GError **error)
                     g_string_append_printf (to_write, "%d,%s\r\n", duration,
                                             filename_generated);
 
-                    if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                               to_write->str, to_write->len,
-                                               NULL, error) != to_write->len)
+                    if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                    to_write->str,
+                                                    to_write->len,
+                                                    &bytes_written, NULL,
+                                                    error))
                     {
+                        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                                 G_GSIZE_FORMAT "bytes of data were written",
+                                 bytes_written, to_write->len);
                         g_assert (error == NULL || *error != NULL);
                         g_string_free (to_write, TRUE);
                         g_object_unref (ostream);
@@ -1720,10 +1738,15 @@ write_playlist (GFile *file, GError **error)
                     /* Must be written in system encoding (not UTF-8)*/
                     to_write = g_string_append (to_write, "\r\n");
 
-                    if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                               to_write->str, to_write->len,
-                                               NULL, error) != to_write->len)
+                    if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                    to_write->str,
+                                                    to_write->len,
+                                                    &bytes_written, NULL,
+                                                    error))
                     {
+                        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                                 G_GSIZE_FORMAT "bytes of data were written",
+                                 bytes_written, to_write->len);
                         g_assert (error == NULL || *error != NULL);
                         g_string_free (to_write, TRUE);
                         g_object_unref (ostream);
@@ -1737,10 +1760,15 @@ write_playlist (GFile *file, GError **error)
                     /* Must be written in system encoding (not UTF-8)*/
                     to_write = g_string_append (to_write, "\r\n");
 
-                    if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                               to_write->str, to_write->len,
-                                               NULL, error) != to_write->len)
+                    if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                    to_write->str,
+                                                    to_write->len,
+                                                    &bytes_written, NULL,
+                                                    error))
                     {
+                        g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                                 G_GSIZE_FORMAT "bytes of data were written",
+                                 bytes_written, to_write->len);
                         g_assert (error == NULL || *error != NULL);
                         g_string_free (to_write, TRUE);
                         g_object_unref (ostream);
@@ -1751,6 +1779,8 @@ write_playlist (GFile *file, GError **error)
             }
         }else // PLAYLIST_FULL_PATH
         {
+            gsize bytes_written;
+
             // 2) Write the header
             if (PLAYLIST_CONTENT_NONE)
             {
@@ -1764,10 +1794,13 @@ write_playlist (GFile *file, GError **error)
                 g_string_append_printf (to_write, "%d,%s\r\n", duration,
                                         temp);
 
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           to_write->str, to_write->len,
-                                           NULL, error) != to_write->len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                to_write->str, to_write->len,
+                                                &bytes_written, NULL, error))
                 {
+                    g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                             G_GSIZE_FORMAT" bytes of data were written",
+                             bytes_written, to_write->len);
                     g_assert (error == NULL || *error != NULL);
                     g_string_free (to_write, TRUE);
                     g_object_unref (ostream);
@@ -1787,10 +1820,13 @@ write_playlist (GFile *file, GError **error)
                 g_string_append_printf (to_write, "%d,%s\r\n", duration,
                                         filename_generated);
 
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           to_write->str, to_write->len,
-                                           NULL, error) != to_write->len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                to_write->str, to_write->len,
+                                                &bytes_written, NULL, error))
                 {
+                    g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                             G_GSIZE_FORMAT" bytes of data were written",
+                             bytes_written, to_write->len);
                     g_assert (error == NULL || *error != NULL);
                     g_string_free (to_write, TRUE);
                     g_object_unref (ostream);
@@ -1811,10 +1847,13 @@ write_playlist (GFile *file, GError **error)
                 /* Must be written in system encoding (not UTF-8)*/
                 to_write = g_string_append (to_write, "\r\n");
 
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           to_write->str, to_write->len,
-                                           NULL, error) != to_write->len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                to_write->str, to_write->len,
+                                                &bytes_written, NULL, error))
                 {
+                    g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                             G_GSIZE_FORMAT" bytes of data were written",
+                             bytes_written, to_write->len);
                     g_assert (error == NULL || *error != NULL);
                     g_string_free (to_write, TRUE);
                     g_object_unref (ostream);
@@ -1828,10 +1867,13 @@ write_playlist (GFile *file, GError **error)
                 /* Must be written in system encoding (not UTF-8)*/
                 to_write = g_string_append (to_write, "\r\n");
 
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           to_write->str, to_write->len,
-                                           NULL, error) != to_write->len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                to_write->str, to_write->len,
+                                                &bytes_written, NULL, error))
                 {
+                    g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                             G_GSIZE_FORMAT" bytes of data were written",
+                             bytes_written, to_write->len);
                     g_assert (error == NULL || *error != NULL);
                     g_string_free (to_write, TRUE);
                     g_object_unref (ostream);
diff --git a/src/vcedit.c b/src/vcedit.c
index 561b0c1..c514f4f 100644
--- a/src/vcedit.c
+++ b/src/vcedit.c
@@ -527,18 +527,26 @@ vcedit_write(vcedit_state *state, GFile *file, GError **error)
 
     while((result = ogg_stream_flush(&streamout, &ogout)))
     {
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.header,
-                                   ogout.header_len, NULL, error)
-            != (gssize) ogout.header_len)
+        gsize bytes_written;
+
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                        ogout.header, ogout.header_len,
+                                        &bytes_written, NULL, error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     " bytes of data were written", bytes_written,
+                     ogout.header_len);
             g_assert (error == NULL || *error != NULL);
             goto cleanup;
         }
 
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.body,
-                                   ogout.body_len, NULL, error)
-            != (gssize) ogout.body_len)
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream), ogout.body,
+                                        ogout.body_len, &bytes_written, NULL,
+                                        error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     " bytes of data were written", bytes_written,
+                     ogout.body_len);
             g_assert (error == NULL || *error != NULL);
             goto cleanup;
         }
@@ -548,18 +556,26 @@ vcedit_write(vcedit_state *state, GFile *file, GError **error)
     {
         if(needflush)
         {
-            if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.header,
-                                       ogout.header_len, NULL, error)
-                != (gssize) ogout.header_len)
+            gsize bytes_written;
+
+            if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                            ogout.header, ogout.header_len,
+                                            &bytes_written, NULL, error))
             {
+                g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                         G_GSIZE_FORMAT " bytes of data were written",
+                         bytes_written, ogout.header_len);
                 g_assert (error == NULL || *error != NULL);
                 goto cleanup;
             }
 
-            if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.body,
-                                       ogout.body_len, NULL, error)
-                != (gssize) ogout.body_len)
+            if (g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                           ogout.body, ogout.body_len,
+                                           &bytes_written, NULL, error))
             {
+                g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                         G_GSIZE_FORMAT " bytes of data were written",
+                         bytes_written, ogout.body_len);
                 g_assert (error == NULL || *error != NULL);
                 goto cleanup;
             }
@@ -568,20 +584,26 @@ vcedit_write(vcedit_state *state, GFile *file, GError **error)
         {
             if(ogg_stream_pageout(&streamout, &ogout))
             {
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           ogout.header, ogout.header_len,
-                                           NULL, error)
-                    != (gssize) ogout.header_len)
+                gsize bytes_written;
+
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                ogout.header, ogout.header_len,
+                                                &bytes_written, NULL, error))
                 {
+                    g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                             G_GSIZE_FORMAT " bytes of data were written",
+                             bytes_written, ogout.header_len);
                     g_assert (error == NULL || *error != NULL);
                     goto cleanup;
                 }
 
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           ogout.body, ogout.body_len, NULL,
-                                           error)
-                    != (gssize) ogout.body_len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                ogout.body, ogout.body_len,
+                                                &bytes_written, NULL, error))
                 {
+                    g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %"
+                             G_GSIZE_FORMAT " bytes of data were written",
+                             bytes_written, ogout.body_len);
                     g_assert (error == NULL || *error != NULL);
                     goto cleanup;
                 }
@@ -644,18 +666,26 @@ vcedit_write(vcedit_state *state, GFile *file, GError **error)
     streamout.e_o_s = 1;
     while(ogg_stream_flush(&streamout, &ogout))
     {
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.header,
-                                   ogout.header_len, NULL, error)
-            != (gssize) ogout.header_len)
+        gsize bytes_written;
+
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                        ogout.header, ogout.header_len,
+                                        &bytes_written, NULL, error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     " bytes of data were written", bytes_written,
+                     ogout.header_len);
             g_assert (error == NULL || *error != NULL);
             goto cleanup;
         }
 
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.body,
-                                   ogout.body_len, NULL, error)
-            != (gssize) ogout.body_len)
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream), ogout.body,
+                                        ogout.body_len, &bytes_written, NULL,
+                                        error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     " bytes of data were written", bytes_written,
+                     ogout.body_len);
             g_assert (error == NULL || *error != NULL);
             goto cleanup;
         }
@@ -663,18 +693,26 @@ vcedit_write(vcedit_state *state, GFile *file, GError **error)
 
     if (state->extrapage)
     {
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.header,
-                                   ogout.header_len, NULL, error)
-            != (gssize) ogout.header_len)
+        gsize bytes_written;
+
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                        ogout.header, ogout.header_len,
+                                        &bytes_written, NULL, error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     " bytes of data were written", bytes_written,
+                     ogout.header_len);
             g_assert (error == NULL || *error != NULL);
             goto cleanup;
         }
 
-        if (g_output_stream_write (G_OUTPUT_STREAM (ostream), ogout.body,
-                                   ogout.body_len, NULL, error)
-            != (gssize) ogout.body_len)
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream), ogout.body,
+                                        ogout.body_len, &bytes_written, NULL,
+                                        error))
         {
+            g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT
+                     " bytes of data were written", bytes_written,
+                     ogout.body_len);
             g_assert (error == NULL || *error != NULL);
             goto cleanup;
         }
@@ -698,21 +736,21 @@ vcedit_write(vcedit_state *state, GFile *file, GError **error)
             }
             else
             {
+                gsize bytes_written;
+
                 /* Don't bother going through the rest, we can just
                  * write the page out now */
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           ogout.header, ogout.header_len,
-                                           NULL, error)
-                    != (gssize) ogout.header_len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                ogout.header, ogout.header_len,
+                                                &bytes_written, NULL, error))
                 {
                     g_assert (error == NULL || *error != NULL);
                     goto cleanup;
                 }
 
-                if (g_output_stream_write (G_OUTPUT_STREAM (ostream),
-                                           ogout.body, ogout.body_len, NULL,
-                                           error)
-                    != (gssize) ogout.body_len)
+                if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
+                                                ogout.body, ogout.body_len,
+                                                &bytes_written, NULL, error))
                 {
                     g_assert (error == NULL || *error != NULL);
                     goto cleanup;


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