Revision: 1941
          http://gtkpod.svn.sourceforge.net/gtkpod/?rev=1941&view=rev
Author:   teuf
Date:     2008-01-29 15:14:34 -0800 (Tue, 29 Jan 2008)

Log Message:
-----------
Add and use itdb_file_set_contents to workaround a rename issue on sshfs

Modified Paths:
--------------
    libgpod/trunk/ChangeLog
    libgpod/trunk/src/db-artwork-writer.c
    libgpod/trunk/src/itdb_itunesdb.c
    libgpod/trunk/src/itdb_private.h

Modified: libgpod/trunk/ChangeLog
===================================================================
--- libgpod/trunk/ChangeLog     2008-01-29 23:14:21 UTC (rev 1940)
+++ libgpod/trunk/ChangeLog     2008-01-29 23:14:34 UTC (rev 1941)
@@ -1,4 +1,13 @@
 2007-11-15  Christophe Fergeau  <[EMAIL PROTECTED]>
+
+       * src/itdb_private.h:
+       * src/itdb_itunesdb.c: add itdb_file_set_contents to workaround a
+       rename issue on sshfs (existing files on the FS can't be
+       atomatically erased during a rename). Use it in wcontents_write
+       * src/db-artwork-writer.c: use itdb_file_set_contents to write the
+       ArtworkDB
+
+2007-11-15  Christophe Fergeau  <[EMAIL PROTECTED]>
        
        * src/db-artwork-writer.c:
        * src/db-parse-context.c:

Modified: libgpod/trunk/src/db-artwork-writer.c
===================================================================
--- libgpod/trunk/src/db-artwork-writer.c       2008-01-29 23:14:21 UTC (rev 
1940)
+++ libgpod/trunk/src/db-artwork-writer.c       2008-01-29 23:14:34 UTC (rev 
1941)
@@ -34,6 +34,8 @@
 #include "db-image-parser.h"
 #include "itdb_endianness.h"
 
+#include <glib/gstdio.h>
+
 #include <errno.h>
 #include <fcntl.h>
 #include <string.h>
@@ -65,13 +67,13 @@
 static gboolean
 ipod_gstring_flush (struct iPodSharedDataBuffer *shared, GError **error)
 {
-       gboolean result;
+       gboolean success;
 
-       result = g_file_set_contents (shared->filename, 
-                                     shared->data->str, shared->data->len,
-                                     error);
-       if (!result) {
-               return FALSE;
+       success = itdb_file_set_contents (shared->filename, 
+                                         shared->data->str, shared->data->len,
+                                         error);
+       if (!success) {
+                return FALSE;
        }
        g_string_free (shared->data, TRUE);
        g_free (shared->filename);

Modified: libgpod/trunk/src/itdb_itunesdb.c
===================================================================
--- libgpod/trunk/src/itdb_itunesdb.c   2008-01-29 23:14:21 UTC (rev 1940)
+++ libgpod/trunk/src/itdb_itunesdb.c   2008-01-29 23:14:34 UTC (rev 1941)
@@ -4842,44 +4842,12 @@
  * cts->error accordingly. */
 static gboolean wcontents_write (WContents *cts)
 {
-    int fd;
-
     g_return_val_if_fail (cts, FALSE);
     g_return_val_if_fail (cts->filename, FALSE);
 
-    fd = creat (cts->filename, S_IRWXU|S_IRWXG|S_IRWXO);
-
-    if (fd == -1)
-    {
-       cts->error = g_error_new (G_FILE_ERROR,
-                                 g_file_error_from_errno (errno),
-                                 _("Opening of '%s' for writing failed (%s)."),
-                                 cts->filename, g_strerror (errno));
-       return FALSE;
-    }
-    if (cts->contents && cts->pos)
-    {
-       ssize_t written = write (fd, cts->contents, cts->pos);
-       if (written == -1)
-       {
-           cts->error = g_error_new (G_FILE_ERROR,
-                                     g_file_error_from_errno (errno),
-                                     _("Writing to '%s' failed (%s)."),
-                                     cts->filename, g_strerror (errno));
-           close (fd);
-           return FALSE;
-       }
-    }
-    fd = close (fd);
-    if (fd == -1)
-    {
-       cts->error = g_error_new (G_FILE_ERROR,
-                                 g_file_error_from_errno (errno),
-                                 _("Writing to '%s' failed (%s)."),
-                                 cts->filename, g_strerror (errno));
-       return FALSE;
-    }
-    return TRUE;
+    cts->error = NULL;
+    return itdb_file_set_contents (cts->filename, cts->contents, 
+                                   cts->pos, &cts->error);
 }
 
 
@@ -6159,8 +6127,51 @@
 }
 
 
+G_GNUC_INTERNAL gboolean
+itdb_file_set_contents (const char *filename, 
+                        const char *data, gssize len, 
+                        GError **error)
+{
+    gchar *backup;
+    gboolean success;
 
+    /* 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
+     *                            */
+    if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
+        gint result;
+        backup = g_strdup_printf ("%sXXXXXX", filename);
+        result = g_rename (filename, backup);
+        if (result != 0) {
+            g_free (backup);
+            return FALSE;
+        }
+    } else {
+        backup = NULL;
+    }
 
+    success = g_file_set_contents (filename, data, len, error);
+    if (!success) {
+        if (backup != NULL) {
+            g_rename (backup, filename);
+            g_free (backup);
+        }
+        return FALSE;
+    }
+
+    /* File saving was
+     * ok, clean up our
+     * mess */
+    if (backup != NULL) {
+        g_unlink (backup);
+        g_free (backup);
+    }
+
+    return TRUE;
+}
+
+
 /**
  * itdb_get_control_dir:
  * @mountpoint: the iPod mountpoint

Modified: libgpod/trunk/src/itdb_private.h
===================================================================
--- libgpod/trunk/src/itdb_private.h    2008-01-29 23:14:21 UTC (rev 1940)
+++ libgpod/trunk/src/itdb_private.h    2008-01-29 23:14:34 UTC (rev 1941)
@@ -156,4 +156,7 @@
                                                 time_t timet);
 G_GNUC_INTERNAL gint itdb_musicdirs_number_by_mountpoint (const gchar 
*mountpoint);
 G_GNUC_INTERNAL gboolean itdb_device_requires_checksum (Itdb_Device *device);
+G_GNUC_INTERNAL gboolean itdb_file_set_contents (const char *filename, 
+                                                 const char *data, gssize len, 
+                                                 GError **error);
 #endif


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to