[easytag/wip/application-window: 25/25] Replace use of stat() in Get_File_Size()



commit 8fdb8225de575bd80c25fa7e7e8e5e0be29a5ed0
Author: David King <amigadave amigadave com>
Date:   Mon Feb 3 16:54:59 2014 +0000

    Replace use of stat() in Get_File_Size()
    
    Use g_file_info_get_size() and check for errors, instead of calling
    stat(). Rename Get_File_Size() to et_get_file_size().

 src/flac_header.c |    2 +-
 src/misc.c        |   30 +++++++++++++++++++++++-------
 src/misc.h        |    2 +-
 src/mp4_header.c  |    2 +-
 src/mpeg_header.c |    2 +-
 src/ogg_header.c  |    4 ++--
 6 files changed, 29 insertions(+), 13 deletions(-)
---
diff --git a/src/flac_header.c b/src/flac_header.c
index 996a3f1..3293b42 100644
--- a/src/flac_header.c
+++ b/src/flac_header.c
@@ -110,7 +110,7 @@ gboolean Flac_Header_Read_File_Info (gchar *filename, ET_File_Info *ETFileInfo)
     /* End of decoding FLAC file */
 
 
-    filesize = Get_File_Size(filename);
+    filesize = et_get_file_size (filename);
     duration = (gint)tmp_file_info.length_in_msec/1000;
 
     ETFileInfo->version     = 0; // Not defined in FLAC file
diff --git a/src/misc.c b/src/misc.c
index ea3d868..c39ed40 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -970,18 +970,34 @@ gchar *Convert_Duration (gulong duration)
 }
 
 /*
- * Returns the size of a file in bytes
+ * @filename: (type filename): the path to a file
+ *
+ * Gets the size of a file in bytes.
+ *
+ * Returns: the size of a file, in bytes
  */
-off_t
-Get_File_Size (const gchar *filename)
+goffset
+et_get_file_size (const gchar *filename)
 {
-    struct stat statbuf;
+    GFile *file;
+    GFileInfo *info;
+    /* TODO: Take a GError from the caller. */
+    GError *error = NULL;
+
+    g_return_if_fail (filename != NULL);
 
-    g_return_val_if_fail (filename != NULL, 0);
+    file = g_file_new_for_path (filename);
+    info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE,
+                              G_FILE_QUERY_INFO_NONE, NULL, &error);
 
-    stat (filename, &statbuf);
+    if (!info)
+    {
+        g_object_unref (file);
+        g_error_free (error);
+        return FALSE;
+    }
 
-    return statbuf.st_size;
+    return g_file_info_get_size (info);
 }
 
 gchar *
diff --git a/src/misc.h b/src/misc.h
index 0d86e6e..4aa9b8a 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -68,7 +68,7 @@ gchar *Convert_Size (gsize size);
 gchar *Convert_Size_1 (gsize size);
 gchar *Convert_Duration (gulong duration);
 
-off_t Get_File_Size (const gchar *filename);
+goffset et_get_file_size (const gchar *filename);
 
 gint Combo_Alphabetic_Sort (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer data);
 
diff --git a/src/mp4_header.c b/src/mp4_header.c
index b6bedb7..6ff90d0 100644
--- a/src/mp4_header.c
+++ b/src/mp4_header.c
@@ -54,7 +54,7 @@ gboolean Mp4_Header_Read_File_Info (gchar *filename, ET_File_Info *ETFileInfo)
     g_return_val_if_fail (filename != NULL && ETFileInfo != NULL, FALSE);
 
     /* Get size of file */
-    ETFileInfo->size = Get_File_Size(filename);
+    ETFileInfo->size = et_get_file_size (filename);
 
     if ((file = taglib_file_new_type(filename, TagLib_File_MP4)) == NULL )
     {
diff --git a/src/mpeg_header.c b/src/mpeg_header.c
index 999775d..9cd34b9 100644
--- a/src/mpeg_header.c
+++ b/src/mpeg_header.c
@@ -88,7 +88,7 @@ Mpeg_Header_Read_File_Info (gchar *filename, ET_File_Info *ETFileInfo)
     g_return_val_if_fail (filename != NULL || ETFileInfo != NULL, FALSE);
 
     /* Get size of file */
-    ETFileInfo->size = Get_File_Size (filename);
+    ETFileInfo->size = et_get_file_size (filename);
 
     /* Get data from tag */
     if ( (id3_tag = ID3Tag_New()) == NULL )
diff --git a/src/ogg_header.c b/src/ogg_header.c
index 451d269..7fbd397 100644
--- a/src/ogg_header.c
+++ b/src/ogg_header.c
@@ -295,7 +295,7 @@ Ogg_Header_Read_File_Info (const gchar *filename, ET_File_Info *ETFileInfo)
         }
     }
 
-    filesize = Get_File_Size(filename);
+    filesize = et_get_file_size (filename);
 
     ETFileInfo->version    = encoder_version;
     ETFileInfo->bitrate    = bitrate_nominal/1000;
@@ -358,7 +358,7 @@ gboolean Speex_Header_Read_File_Info (gchar *filename, ET_File_Info *ETFileInfo)
         //g_print("compressed length: %ld bytes\n",(long)(ov_raw_total(&vf,-1)));
     }
 
-    filesize = Get_File_Size(filename);
+    filesize = et_get_file_size (filename);
 
     ETFileInfo->mpc_version = g_strdup(encoder_version);
     ETFileInfo->bitrate     = bitrate/1000;


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