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

Log Message:
-----------
Rework itdb_cp for MacOSX/Windows portability

Modified Paths:
--------------
    libgpod/trunk/ChangeLog
    libgpod/trunk/src/itdb_itunesdb.c

Modified: libgpod/trunk/ChangeLog
===================================================================
--- libgpod/trunk/ChangeLog     2008-01-27 23:38:25 UTC (rev 1938)
+++ libgpod/trunk/ChangeLog     2008-01-29 23:14:04 UTC (rev 1939)
@@ -1,3 +1,8 @@
+2007-11-16  Christophe Fergeau <[EMAIL PROTECTED]>
+
+       * src/itdb_itunesdb.c: rework itdb_cp for MacOSX/Windows
+       portability
+
 2008-01-26  Michael Tiffany <tiffman at users.sourceforge.net>
 
         * src/itdb_chapterdata.c: file missed being added in

Modified: libgpod/trunk/src/itdb_itunesdb.c
===================================================================
--- libgpod/trunk/src/itdb_itunesdb.c   2008-01-27 23:38:25 UTC (rev 1938)
+++ libgpod/trunk/src/itdb_itunesdb.c   2008-01-29 23:14:04 UTC (rev 1939)
@@ -6037,6 +6037,7 @@
 }
 
 
+/* Use open instead of fopen.  fwrite is really slow on the Mac. */
 /**
  * itdb_cp:
  * @from_file: source file
@@ -6051,10 +6052,13 @@
 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;
-    FILE *file_in = NULL;
-    FILE *file_out = NULL;
+    int file_in = -1;
+    int file_out = -1;
 
 #if ITUNESDB_DEBUG
     fprintf(stderr, "Entered itunesdb_cp: '%s', '%s'\n", from_file, to_file);
@@ -6065,8 +6069,8 @@
 
     data = g_malloc (ITUNESDB_COPYBLK);
 
-    file_in = fopen (from_file, "r");
-    if (file_in == NULL)
+    file_in = g_open (from_file, O_RDONLY | O_BINARY, 0);
+    if (file_in < 0)
     {
        g_set_error (error,
                     G_FILE_ERROR,
@@ -6076,8 +6080,9 @@
        goto err_out;
     }
 
-    file_out = fopen (to_file, "w");
-    if (file_out == NULL)
+    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,
@@ -6088,25 +6093,23 @@
     }
 
     do {
-       bread = fread (data, 1, ITUNESDB_COPYBLK, file_in);
+       bread = read (file_in, data, ITUNESDB_COPYBLK);
 #if ITUNESDB_DEBUG
        fprintf(stderr, "itunesdb_cp: read %ld bytes\n", bread);
 #endif
-       if (bread == 0)
+       if (bread < 0)
        {
-           if (feof (file_in) == 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;
-           }
+           /* 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 = fwrite (data, 1, bread, file_out);
+           bwrite = write (file_out, data, bread);
 #if ITUNESDB_DEBUG
            fprintf(stderr, "itunesdb_cp: wrote %ld bytes\n", bwrite);
 #endif
@@ -6122,9 +6125,9 @@
        }
     } while (bread != 0);
 
-    if (fclose (file_in) != 0)
+    if (close (file_in) != 0)
     {
-       file_in = NULL;
+       file_in = -1;
        g_set_error (error,
                     G_FILE_ERROR,
                     g_file_error_from_errno (errno),
@@ -6132,9 +6135,9 @@
                     from_file, g_strerror (errno));
        goto err_out;
     }
-    if (fclose (file_out) != 0)
+    if (close (file_out) != 0)
     {
-       file_out = NULL;
+       file_out = -1;
        g_set_error (error,
                     G_FILE_ERROR,
                     g_file_error_from_errno (errno),
@@ -6146,9 +6149,9 @@
     return TRUE;
 
   err_out:
-    if (file_in)  fclose (file_in);
-    if (file_out) fclose (file_out);
-    remove (to_file);
+    if (file_in >= 0)  close (file_in);
+    if (file_out >= 0) close (file_out);
+    g_unlink (to_file);
     g_free (data);
     return FALSE;
 }


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