[easytag] Use g_open() instead of open()



commit 5e74546aec8ce4721d52480b1c55bd231e2bc39f
Author: David King <amigadave amigadave com>
Date:   Tue Jan 19 16:49:57 2016 +0000

    Use g_open() instead of open()
    
    Ensure that filenames are kept in the GLib filename encoding.
    Additionally, use id3_file_fdopen() rather than id3_file_open() to
    ensure that the GLib filename encoding is used when reading ID3 tags.

 src/tags/id3v24_tag.c         |   27 ++++++++++++++++++++++++---
 src/tags/libapetag/info_mac.c |    3 ++-
 src/tags/libapetag/info_mpc.c |    3 ++-
 src/win32/win32dep.c          |    3 ++-
 4 files changed, 30 insertions(+), 6 deletions(-)
---
diff --git a/src/tags/id3v24_tag.c b/src/tags/id3v24_tag.c
index 348f59e..1226cc8 100644
--- a/src/tags/id3v24_tag.c
+++ b/src/tags/id3v24_tag.c
@@ -1,5 +1,5 @@
 /* EasyTAG - Tag editor for audio files
- * Copyright (C) 2014-2015  David King <amigadave amigadave com>
+ * Copyright (C) 2014-2016  David King <amigadave amigadave com>
  * Copyright (C) 2001-2003  Jerome Couderc <easytag gmail com>
  * Copyright (C) 2006-2007  Alexey Illarionov <littlesavage rambler ru>
  *
@@ -21,9 +21,13 @@
 #include "config.h"
 
 #include <glib/gi18n.h>
+#include <glib/gstdio.h>
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/fcntl.h>
 
 #include "id3_tag.h"
 #include "picture.h"
@@ -92,6 +96,7 @@ id3tag_read_file_tag (GFile *gfile,
     gsize bytes_read;
     GSeekable *seekable;
     gchar *filename;
+    int fd;
     struct id3_file *file;
     struct id3_tag *tag;
     struct id3_frame *frame;
@@ -217,7 +222,7 @@ id3tag_read_file_tag (GFile *gfile,
 
     filename = g_file_get_path (gfile);
 
-    if ((file = id3_file_open (filename, ID3_FILE_MODE_READONLY)) == NULL)
+    if ((fd = g_open (filename, O_RDONLY, 0)) == -1)
     {
         g_free (filename);
         g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
@@ -227,6 +232,14 @@ id3tag_read_file_tag (GFile *gfile,
 
     g_free (filename);
 
+    /* The fd ownership is transferred to id3tag. */
+    if ((file = id3_file_fdopen (fd, ID3_FILE_MODE_READONLY)) == NULL)
+    {
+        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
+                     _("Error reading tags from file"));
+        return FALSE;
+    }
+
     if ( ((tag = id3_file_tag(file)) == NULL)
     ||   (tag->nframes == 0))
     {
@@ -939,13 +952,21 @@ id3tag_write_file_v24tag (const ET_File *ETFile,
     /* Write ID3v2 tag. */
     if (g_settings_get_boolean (MainSettings, "id3v2-enabled"))
     {
+        int fd;
         struct id3_file *file;
         struct id3_tag *tmptag;
         unsigned tagsize;
         id3_byte_t *tmpbuf = NULL;
 
+        if ((fd = g_open (filename, O_RDONLY, 0)) == -1)
+        {
+            g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
+                         _("Error reading tags from file"));
+            return FALSE;
+        }
+
         /* Read old v2 tag */
-        if ((file = id3_file_open(filename, ID3_FILE_MODE_READWRITE)) == NULL)
+        if ((file = id3_file_fdopen (fd, ID3_FILE_MODE_READONLY)) == NULL)
         {
             g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
                          _("Error reading tags from file"));
diff --git a/src/tags/libapetag/info_mac.c b/src/tags/libapetag/info_mac.c
index 2105545..00b7f47 100644
--- a/src/tags/libapetag/info_mac.c
+++ b/src/tags/libapetag/info_mac.c
@@ -17,6 +17,7 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#include <glib/gstdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -93,7 +94,7 @@ info_mac_read(const char *fn, StreamInfoMac * Info)
     struct macHeader * header;
     
     // load file
-    tmpFile = fopen(fn, "rb");
+    tmpFile = g_fopen (fn, "rb");
         
     if (tmpFile == NULL) 
         return 1;    // file not found or read-protected
diff --git a/src/tags/libapetag/info_mpc.c b/src/tags/libapetag/info_mpc.c
index 712cb16..240b1dd 100644
--- a/src/tags/libapetag/info_mpc.c
+++ b/src/tags/libapetag/info_mpc.c
@@ -26,6 +26,7 @@
     -Thomas Juerges <thomas juerges astro ruhr-uni-bochum de> 
 */
 
+#include <glib/gstdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -68,7 +69,7 @@ info_mpc_read(const char *fn, StreamInfoMpc * Info)
     long SkipSizeID3;
     
     // load file
-    tmpFile = fopen(fn, "rb");
+    tmpFile = g_fopen (fn, "rb");
         
     if (tmpFile == NULL) 
         return 1;    // file not found or read-protected
diff --git a/src/win32/win32dep.c b/src/win32/win32dep.c
index 6e1a0e9..e7f7f9c 100644
--- a/src/win32/win32dep.c
+++ b/src/win32/win32dep.c
@@ -33,6 +33,7 @@
 #include <windows.h>
 #include <io.h>
 #include <stdlib.h>
+#include <glib/gstdio.h>
 #include <stdio.h>
 #include <winuser.h>
 #include <sys/types.h>
@@ -118,7 +119,7 @@ et_w32_mkstemp (gchar *template)
     char *str = mktemp(template);
     if(str != NULL)
     {
-        fd  = open(str, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+        fd  = g_open (str, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
     }
 
     return fd;


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