[easytag] Move et_rename_file() to misc.c



commit 22547cbf8d92f50a4a083ca886a9674953d2a89b
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    |  128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/misc.h    |    3 +-
 3 files changed, 129 insertions(+), 129 deletions(-)
---
diff --git a/src/easytag.c b/src/easytag.c
index 02bbe2d..3ed743e 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>
 
@@ -64,8 +62,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);
@@ -714,129 +710,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 139680e..027ec4f 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1,5 +1,5 @@
 /* EasyTAG - Tag editor for audio files
- * Copyright (C) 2014  David King <amigadave amigadave com>
+ * Copyright (C) 2014-2015  David King <amigadave amigadave com>
  * Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -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"
@@ -370,6 +372,130 @@ et_track_number_to_string (const guint 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;
+}
+
+/*
  * et_filename_prepare:
  * @filename_utf8: UTF8-encoded basename
  * @replace_illegal: whether to replace illegal characters in the file name
diff --git a/src/misc.h b/src/misc.h
index 72a25f7..abf1add 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -1,5 +1,5 @@
 /* EasyTAG - Tag editor for audio files
- * Copyright (C) 2014  David King <amigadave amigadave com>
+ * Copyright (C) 2014-2015  David King <amigadave amigadave com>
  * Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -38,6 +38,7 @@ gchar * et_disc_number_to_string (const guint disc_number);
 gchar * et_track_number_to_string (const guint track_number);
 
 void et_filename_prepare (gchar *filename_utf8, gboolean replace_illegal);
+gboolean et_rename_file (const gchar *old_filename, const gchar *new_filename, GError **error);
 
 guint et_undo_key_new (void);
 gint et_normalized_strcmp0 (const gchar *str1, const gchar *str2);


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