From 16d1e13f652e54936236a38e6d6b7003eeaf2768 Mon Sep 17 00:00:00 2001
From: Nikias Bassen <nikias@gmx.li>
Date: Tue, 25 Aug 2009 14:32:40 +0200
Subject: [PATCH 11/14] Use gio in itdb_cp and itdb_file_set_contents.

---
 src/itdb_itunesdb.c |  117 +++++++--------------------------------------------
 1 files changed, 16 insertions(+), 101 deletions(-)

diff --git a/src/itdb_itunesdb.c b/src/itdb_itunesdb.c
index 5b4b260..5575c09 100644
--- a/src/itdb_itunesdb.c
+++ b/src/itdb_itunesdb.c
@@ -7538,108 +7538,15 @@ gchar *itdb_filename_on_ipod (Itdb_Track *track)
 gboolean itdb_cp (const gchar *from_file, const gchar *to_file,
 		  GError **error)
 {
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-    gchar *data;
-    glong bread, bwrite;
-    int file_in = -1;
-    int file_out = -1;
-
-#if ITUNESDB_DEBUG
-    fprintf(stderr, "Entered itunesdb_cp: '%s', '%s'\n", from_file, to_file);
-#endif
+    GFile *src;
+    GFile *dst;
 
-    g_return_val_if_fail (from_file, FALSE);
-    g_return_val_if_fail (to_file, FALSE);
-
-    data = g_malloc (ITUNESDB_COPYBLK);
-
-    file_in = g_open (from_file, O_RDONLY | O_BINARY, 0);
-    if (file_in < 0)
-    {
-	g_set_error (error,
-		     G_FILE_ERROR,
-		     g_file_error_from_errno (errno),
-		     _("Error opening '%s' for reading (%s)."),
-		     from_file, g_strerror (errno));
-	goto err_out;
-    }
+    src = g_file_new_for_path (from_file);
+    dst = g_file_new_for_path (to_file);
 
-    file_out =  g_open (to_file, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,
-                        S_IRWXU|S_IRWXG|S_IRWXO);
-    if (file_out < 0)
-    {
-	g_set_error (error,
-		     G_FILE_ERROR,
-		     g_file_error_from_errno (errno),
-		     _("Error opening '%s' for writing (%s)."),
-		     to_file, g_strerror (errno));
-	goto err_out;
-    }
-
-    do {
-	bread = read (file_in, data, ITUNESDB_COPYBLK);
-#if ITUNESDB_DEBUG
-	fprintf(stderr, "itunesdb_cp: read %ld bytes\n", bread);
-#endif
-	if (bread < 0)
-	{
-	    /* error -- not end of file! */
-	    g_set_error (error,
-			 G_FILE_ERROR,
-			 g_file_error_from_errno (errno),
-			 _("Error while reading from '%s' (%s)."),
-			 from_file, g_strerror (errno));
-	    goto err_out;
-	}
-	else
-	{
-	    bwrite = write (file_out, data, bread);
-#if ITUNESDB_DEBUG
-	    fprintf(stderr, "itunesdb_cp: wrote %ld bytes\n", bwrite);
-#endif
-	    if (bwrite != bread)
-	    {
-		g_set_error (error,
-			     G_FILE_ERROR,
-			     g_file_error_from_errno (errno),
-			     _("Error while writing to '%s' (%s)."),
-			     to_file, g_strerror (errno));
-		goto err_out;
-	    }
-	}
-    } while (bread != 0);
-
-    if (close (file_in) != 0)
-    {
-	file_in = -1;
-	g_set_error (error,
-		     G_FILE_ERROR,
-		     g_file_error_from_errno (errno),
-		     _("Error when closing '%s' (%s)."),
-		     from_file, g_strerror (errno));
-	goto err_out;
-    }
-    if (close (file_out) != 0)
-    {
-	file_out = -1;
-	g_set_error (error,
-		     G_FILE_ERROR,
-		     g_file_error_from_errno (errno),
-		     _("Error when closing '%s' (%s)."),
-		     to_file, g_strerror (errno));
-	goto err_out;
-    }
-    g_free (data);
-    return TRUE;
-
-  err_out:
-    if (file_in >= 0)  close (file_in);
-    if (file_out >= 0) close (file_out);
-    g_unlink (to_file);
-    g_free (data);
-    return FALSE;
+    return g_file_copy (src, dst, G_FILE_COPY_OVERWRITE, NULL,
+		NULL, /* no callback function used */
+		NULL, error);
 }
 
 
@@ -7648,9 +7555,16 @@ itdb_file_set_contents (const char *filename,
                         const char *data, gssize len, 
                         GError **error)
 {
+    GFile *gf;
+
+    gf = g_file_new_for_path (filename);
+
+    return g_file_replace_contents(gf, data, len, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, error);
+
+#if 0
     gchar *backup;
     gboolean success;
-
+    /* TODO: CHECK THIS */
     /* sshfs (which is used to access iPhones/iTouches) can't successfully
      * rename a file if the destination file already exist.
      * We first move away the existing file to workaround that limitation
@@ -7685,6 +7599,7 @@ itdb_file_set_contents (const char *filename,
     }
 
     return TRUE;
+#endif
 }
 
 /**
-- 
1.6.4.4

