[easytag/wip/case-insensitive-rename: 1/2] Move et_rename_file() to misc.c



commit 97ea2d75ad1f2e6256cc9b92b8590bf48bc6af4c
Author: David King <amigadave amigadave com>
Date:   Sat Dec 27 16:50:45 2014 +0000

    Move et_rename_file() to misc.c
    
    This should make it easier to write a test.

 src/easytag.c |  127 ---------------------------------------------------------
 src/misc.c    |  126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/misc.h    |    2 +
 3 files changed, 128 insertions(+), 127 deletions(-)
---
diff --git a/src/easytag.c b/src/easytag.c
index cf24156..f5e5e6b 100644
--- a/src/easytag.c
+++ b/src/easytag.c
@@ -22,8 +22,6 @@
 #include "easytag.h"
 
 #include <glib/gi18n.h>
-#include <stdlib.h>
-#include <sys/stat.h>
 #include <unistd.h>
 #include <sys/types.h>
 
@@ -57,8 +55,6 @@ static gint Save_Selected_Files_With_Answer (gboolean force_saving_files);
 static gint Save_List_Of_Files (GList *etfilelist,
                                 gboolean force_saving_files);
 
-static gboolean et_rename_file (const char *old_filepath,
-                                const char *new_filepath, GError **error);
 static GList *read_directory_recursively (GList *file_list,
                                           GFileEnumerator *dir_enumerator,
                                           gboolean recurse);
@@ -712,129 +708,6 @@ Save_File (ET_File *ETFile, gboolean multiple_files,
 }
 
 /*
- * et_rename_file:
- * @old_filepath: path of file to be renamed
- * @new_filepath: path of renamed file
- * @error: a #GError to provide information on errors, or %NULL to ignore
- *
- * Rename @old_filepath to @new_filepath.
- *
- * Returns: %TRUE if the rename was successful, %FALSE otherwise
- */
-static gboolean
-et_rename_file (const char *old_filepath, const char *new_filepath,
-                GError **error)
-{
-    GFile *file_old;
-    GFile *file_new;
-    GFile *file_new_parent;
-
-    g_return_val_if_fail (old_filepath != NULL && new_filepath != NULL, FALSE);
-    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
-    file_old = g_file_new_for_path (old_filepath);
-    file_new = g_file_new_for_path (new_filepath);
-    file_new_parent = g_file_get_parent (file_new);
-
-    if (!g_file_make_directory_with_parents (file_new_parent, NULL, error))
-    {
-        /* Ignore an error if the directory already exists. */
-        if (!g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_EXISTS))
-        {
-            g_object_unref (file_new_parent);
-            goto err;
-        }
-
-        g_clear_error (error);
-    }
-
-    g_assert (error == NULL || *error == NULL);
-    g_object_unref (file_new_parent);
-
-    /* Move the file. */
-    if (!g_file_move (file_old, file_new, G_FILE_COPY_NONE, NULL, NULL, NULL,
-                      error))
-    {
-        if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_EXISTS))
-        {
-            /* Possibly a case change on a case-insensitive filesystem. */
-            /* TODO: casefold the paths of both files, and check to see whether
-             * they only differ by case? */
-            gchar *tmp_filename;
-            mode_t old_mode;
-            gint fd;
-            GFile *tmp_file;
-            GError *tmp_error = NULL;
-
-            tmp_filename = g_strconcat (old_filepath, ".XXXXXX", NULL);
-
-            old_mode = umask (077);
-            fd = mkstemp (tmp_filename);
-            umask (old_mode);
-
-            if (fd >= 0)
-            {
-                close (fd);
-            }
-
-            tmp_file = g_file_new_for_path (tmp_filename);
-            g_free (tmp_filename);
-
-            if (!g_file_move (file_old, tmp_file, G_FILE_COPY_OVERWRITE, NULL,
-                              NULL, NULL, &tmp_error))
-            {
-                g_file_delete (tmp_file, NULL, NULL);
-
-                g_object_unref (tmp_file);
-                g_clear_error (error);
-                g_propagate_error (error, tmp_error);
-                goto err;
-            }
-            else
-            {
-                /* Move to temporary file succeeded, now move to the real new
-                 * location. */
-                if (!g_file_move (tmp_file, file_new, G_FILE_COPY_NONE, NULL,
-                                  NULL, NULL, &tmp_error))
-                {
-                    g_file_move (tmp_file, file_old, G_FILE_COPY_NONE, NULL,
-                                 NULL, NULL, NULL);
-                    g_object_unref (tmp_file);
-                    g_clear_error (error);
-                    g_propagate_error (error, tmp_error);
-                    goto err;
-                }
-                else
-                {
-                    /* Move succeeded, so clear the original error about the
-                     * new file already existing. */
-                    g_object_unref (tmp_file);
-                    g_clear_error (error);
-                    goto out;
-                }
-            }
-        }
-        else
-        {
-            /* Error moving file. */
-            goto err;
-        }
-    }
-
-out:
-    g_object_unref (file_old);
-    g_object_unref (file_new);
-    g_assert (error == NULL || *error == NULL);
-    return TRUE;
-
-err:
-    g_object_unref (file_old);
-    g_object_unref (file_new);
-    g_assert (error == NULL || *error != NULL);
-    return FALSE;
-}
-
-/*
  * Write tag of the ETFile
  * Return TRUE => OK
  *        FALSE => error
diff --git a/src/misc.c b/src/misc.c
index c76cd2f..bcdacab 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -22,6 +22,8 @@
 #include "misc.h"
 
 #include <glib/gi18n.h>
+#include <stdlib.h>
+#include <sys/stat.h>
 
 #include "easytag.h"
 #include "id3_tag.h"
@@ -386,3 +388,127 @@ et_track_number_to_string (const guint track_number)
         return g_strdup_printf ("%d", track_number);
     }
 }
+
+/*
+ * et_rename_file:
+ * @old_filepath: path of file to be renamed
+ * @new_filepath: path of renamed file
+ * @error: a #GError to provide information on errors, or %NULL to ignore
+ *
+ * Rename @old_filepath to @new_filepath.
+ *
+ * Returns: %TRUE if the rename was successful, %FALSE otherwise
+ */
+gboolean
+et_rename_file (const char *old_filepath,
+                const char *new_filepath,
+                GError **error)
+{
+    GFile *file_old;
+    GFile *file_new;
+    GFile *file_new_parent;
+
+    g_return_val_if_fail (old_filepath != NULL && new_filepath != NULL, FALSE);
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+    file_old = g_file_new_for_path (old_filepath);
+    file_new = g_file_new_for_path (new_filepath);
+    file_new_parent = g_file_get_parent (file_new);
+
+    if (!g_file_make_directory_with_parents (file_new_parent, NULL, error))
+    {
+        /* Ignore an error if the directory already exists. */
+        if (!g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+        {
+            g_object_unref (file_new_parent);
+            goto err;
+        }
+
+        g_clear_error (error);
+    }
+
+    g_assert (error == NULL || *error == NULL);
+    g_object_unref (file_new_parent);
+
+    /* Move the file. */
+    if (!g_file_move (file_old, file_new, G_FILE_COPY_NONE, NULL, NULL, NULL,
+                      error))
+    {
+        if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+        {
+            /* Possibly a case change on a case-insensitive filesystem. */
+            /* TODO: casefold the paths of both files, and check to see whether
+             * they only differ by case? */
+            gchar *tmp_filename;
+            mode_t old_mode;
+            gint fd;
+            GFile *tmp_file;
+            GError *tmp_error = NULL;
+
+            tmp_filename = g_strconcat (old_filepath, ".XXXXXX", NULL);
+
+            old_mode = umask (077);
+            fd = mkstemp (tmp_filename);
+            umask (old_mode);
+
+            if (fd >= 0)
+            {
+                close (fd);
+            }
+
+            tmp_file = g_file_new_for_path (tmp_filename);
+            g_free (tmp_filename);
+
+            if (!g_file_move (file_old, tmp_file, G_FILE_COPY_OVERWRITE, NULL,
+                              NULL, NULL, &tmp_error))
+            {
+                g_file_delete (tmp_file, NULL, NULL);
+
+                g_object_unref (tmp_file);
+                g_clear_error (error);
+                g_propagate_error (error, tmp_error);
+                goto err;
+            }
+            else
+            {
+                /* Move to temporary file succeeded, now move to the real new
+                 * location. */
+                if (!g_file_move (tmp_file, file_new, G_FILE_COPY_NONE, NULL,
+                                  NULL, NULL, &tmp_error))
+                {
+                    g_file_move (tmp_file, file_old, G_FILE_COPY_NONE, NULL,
+                                 NULL, NULL, NULL);
+                    g_object_unref (tmp_file);
+                    g_clear_error (error);
+                    g_propagate_error (error, tmp_error);
+                    goto err;
+                }
+                else
+                {
+                    /* Move succeeded, so clear the original error about the
+                     * new file already existing. */
+                    g_object_unref (tmp_file);
+                    g_clear_error (error);
+                    goto out;
+                }
+            }
+        }
+        else
+        {
+            /* Error moving file. */
+            goto err;
+        }
+    }
+
+out:
+    g_object_unref (file_old);
+    g_object_unref (file_new);
+    g_assert (error == NULL || *error == NULL);
+    return TRUE;
+
+err:
+    g_object_unref (file_old);
+    g_object_unref (file_new);
+    g_assert (error == NULL || *error != NULL);
+    return FALSE;
+}
diff --git a/src/misc.h b/src/misc.h
index bc1a10e..58085f9 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -38,6 +38,8 @@ gboolean et_run_program (const gchar *program_name, GList *args_list, GError **e
 gchar * et_disc_number_to_string (const guint disc_number);
 gchar * et_track_number_to_string (const guint track_number);
 
+gboolean et_rename_file (const gchar *old_filename, const gchar *new_filename, GError **error);
+
 G_END_DECLS
 
 #endif /* ET_MISC_H_ */


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