[easytag/wip/easytag-next: 8/11] Use g_file_new_tmp() for creating temporary file



commit 21f9575b19f34e7327776724021fd698e45c3275
Author: David King <amigadave amigadave com>
Date:   Wed Dec 5 22:08:46 2012 +0000

    Use g_file_new_tmp() for creating temporary file
    
    Add a requirement on GIO 2.32 in configure.ac.

 configure.ac  |    3 ++-
 src/id3_tag.c |   47 ++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 38 insertions(+), 12 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 226ec5a..c347e81 100644
--- a/configure.ac
+++ b/configure.ac
@@ -220,7 +220,8 @@ AS_IF([test "x$have_wavpack" != "xno"],
              [AC_MSG_ERROR([Wavpack support requested but required dependencies ($WAVPACK_DEPS) not found])])])
 
 dnl Check the pkg-config dependencies
-PKG_CHECK_MODULES([EASYTAG], [$GTK_DEPS $OGG_DEPS $SPEEX_DEPS $FLAC_DEPS $ID3TAG_DEPS $TAGLIB_DEPS $WAVPACK_DEPS])
+GIO_DEPS="gio-2.0 >= 2.32.0" dnl For g_file_new_tmp()
+PKG_CHECK_MODULES([EASYTAG], [$GIO_DEPS $GTK_DEPS $OGG_DEPS $SPEEX_DEPS $FLAC_DEPS $ID3TAG_DEPS $TAGLIB_DEPS $WAVPACK_DEPS])
 
 dnl Check that the compiler accepts the given warning flags.
 warning_flags="-Wall -Wstrict-prototypes -Wnested-externs -Werror=missing-prototypes -Werror=implicit-function-declaration -Werror=pointer-arith -Werror=init-self -Werror=format-security -Werror=format=2 -Werror=missing-include-dirs"
diff --git a/src/id3_tag.c b/src/id3_tag.c
index 5575951..a9f3081 100644
--- a/src/id3_tag.c
+++ b/src/id3_tag.c
@@ -1253,35 +1253,58 @@ gboolean Id3tag_Check_If_File_Is_Corrupted (gchar *filename)
  */
 gboolean Id3tag_Check_If_Id3lib_Is_Bugged (void)
 {
-    FILE *file;
+    GFile *file;
+    GFileIOStream *ostream;
+    GError *error = NULL;
     guchar tmp[16] = {0xFF, 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     ID3Tag *id3_tag = NULL;
-    gchar *filename;
     gchar *result = NULL;
     ID3Frame *id3_frame;
     gboolean use_unicode;
+    gssize count;
 
 
-    // Create a temporary file
-    if ((file = g_mkstemp ("easytagXXXXXX.mp3")) == NULL)
+    /* Create a temporary file. */
+    file = g_file_new_tmp ("easytagXXXXXX.mp3", &ostream, &error);
+    if (file)
     {
-        gchar *filename_utf8 = filename_to_display(filename);
+        gchar *filename_utf8 = filename_to_display (g_file_get_path (file));
         Log_Print (LOG_ERROR, _("Error while opening file: '%s' (%s)"),
-                   filename_utf8, g_strerror(errno));
+                   filename_utf8, error->message);
         g_free (filename_utf8);
+        g_clear_error (&error);
+        g_object_unref (file);
+
         return FALSE;
     }
+
     // Set data in the file
-    fwrite(&tmp,16,1,file);
-    fclose(file);
+    count = g_output_stream_write (G_OUTPUT_STREAM (ostream), tmp,
+                                   sizeof (tmp), NULL, &error);
+    if (count != sizeof (tmp))
+    {
+        gchar *filename_utf8 = filename_to_display (g_file_get_path (file));
+        Log_Print (LOG_ERROR, _("Error while writing to file: '%s' (%s)"),
+                   filename_utf8, error->message);
+
+        g_free (filename_utf8);
+        g_clear_error (&error);
+        g_object_unref (file);
+        g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, NULL);
+
+        return FALSE;
+    }
+
+    g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, NULL);
+    g_object_unref (ostream);
 
     // Save state of switches as we must force to Unicode before writting
     use_unicode = FILE_WRITING_ID3V2_USE_UNICODE_CHARACTER_SET;
     FILE_WRITING_ID3V2_USE_UNICODE_CHARACTER_SET = TRUE;
 
     id3_tag = ID3Tag_New();
-    ID3Tag_Link_1(id3_tag,filename);
+    ID3Tag_Link_1 (id3_tag, g_file_get_path (file));
 
     // Create a new 'title' field for testing
     id3_frame = ID3Frame_NewID(ID3FID_TITLE);
@@ -1299,7 +1322,7 @@ gboolean Id3tag_Check_If_Id3lib_Is_Bugged (void)
 
 
     id3_tag = ID3Tag_New();
-    ID3Tag_Link_1(id3_tag,filename);
+    ID3Tag_Link_1 (id3_tag, g_file_get_path (file));
     // Read the written field
     if ( (id3_frame = ID3Tag_FindFrameWithID(id3_tag,ID3FID_TITLE)) )
     {
@@ -1307,7 +1330,9 @@ gboolean Id3tag_Check_If_Id3lib_Is_Bugged (void)
     }
 
     ID3Tag_Delete(id3_tag);
-    remove(filename);
+    g_file_delete (file, NULL, NULL);
+
+    g_object_unref (file);
 
     // Same string found? if yes => not bugged
     //if ( result && strcmp(result,"Ã")!=0 )



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