Revision: 2040
          http://gtkpod.svn.sourceforge.net/gtkpod/?rev=2040&view=rev
Author:   teuf
Date:     2008-07-06 07:04:33 -0700 (Sun, 06 Jul 2008)

Log Message:
-----------
Use GMappedFile instead of directly using mmap

Modified Paths:
--------------
    libgpod/trunk/ChangeLog
    libgpod/trunk/src/db-itunes-parser.h
    libgpod/trunk/src/db-parse-context.c
    libgpod/trunk/src/db-parse-context.h

Modified: libgpod/trunk/ChangeLog
===================================================================
--- libgpod/trunk/ChangeLog     2008-07-06 14:04:20 UTC (rev 2039)
+++ libgpod/trunk/ChangeLog     2008-07-06 14:04:33 UTC (rev 2040)
@@ -1,5 +1,14 @@
 2008-07-06  Christophe Fergeau <[EMAIL PROTECTED]>
 
+       * src/db-itunes-parser.h: remove unused constant
+       * src/db-parse-context.c:
+       * src/db-parse-context.h: use GMappedFile instead of directly using
+       mmap, it's needed for MSVC8 portability
+
+2008-07-05  Christophe Fergeau <[EMAIL PROTECTED]>
+
+       Patch from: Songbird (http://getsongbird.com/)
+
        * src/itdb_itunesdb.c
        * src/itdb_track.c: get rid of inner functions since it is a gcc
        specific extension and MSVC8 doesn't like that

Modified: libgpod/trunk/src/db-itunes-parser.h
===================================================================
--- libgpod/trunk/src/db-itunes-parser.h        2008-07-06 14:04:20 UTC (rev 
2039)
+++ libgpod/trunk/src/db-itunes-parser.h        2008-07-06 14:04:33 UTC (rev 
2040)
@@ -28,10 +28,7 @@
 #define DB_PARSER_H
 
 #include <glib.h>
-/*#include "ipod-db-parser.h"*/
 
-#define ITUNESDB_MAX_SIZE 10 * 1024 * 1024
-
 struct _MHeader {
        unsigned char header_id[4];
        gint32 header_len;

Modified: libgpod/trunk/src/db-parse-context.c
===================================================================
--- libgpod/trunk/src/db-parse-context.c        2008-07-06 14:04:20 UTC (rev 
2039)
+++ libgpod/trunk/src/db-parse-context.c        2008-07-06 14:04:33 UTC (rev 
2040)
@@ -25,7 +25,6 @@
 #include <config.h>
 #endif
 
-#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <errno.h>
@@ -36,6 +35,7 @@
 #endif
 
 #include <glib.h>
+#include <glib/gstdio.h>
 #include "db-parse-context.h"
 #include "db-itunes-parser.h"
 #include "itdb_endianness.h"
@@ -63,9 +63,10 @@
 {
        g_return_if_fail (ctx != NULL);
 
-       if (ctx->buffer != NULL) {
-               munmap ((void*)ctx->buffer, ctx->total_len);
+       if (ctx->mapped_file) {
+               g_mapped_file_free(ctx->mapped_file);
        }
+
        g_free (ctx);
 }
 
@@ -175,63 +176,50 @@
 DBParseContext *
 db_parse_context_new_from_file (const char *filename, Itdb_DB *db)
 {
-       int fd;
-       struct stat stat_buf;
-       int result;
-       unsigned char *buffer;
        DBParseContext *ctx;
        Itdb_Device *device;
+       GError* error;
+       GMappedFile* mapped_file;
+       struct stat stat_buf;
 
-       buffer = NULL;
        ctx = NULL;
+       error = NULL;
+       mapped_file = NULL;
 
        device = db_get_device (db);
        g_return_val_if_fail (device, NULL);
 
-       fd = open (filename, O_RDONLY);
-       if (fd == -1) {
-               g_print ("Failed to open %s: %s\n", 
-                        filename, strerror (errno));
+       if (g_stat (filename, &stat_buf) != 0) {
+               return NULL;    
+       };
+       if (stat_buf.st_size > 64 * 1024 * 1024) {
+               g_warning ("%s is too big to be mmapped (%llu bytes)\n",
+                          filename, (unsigned long long)stat_buf.st_size);
                return NULL;
        }
 
-       result = fstat (fd, &stat_buf);
-       if (result == -1) {
-               g_print ("Failed to read %s size: %s\n", 
-                        filename, strerror (errno));
-               goto error;
+       mapped_file = g_mapped_file_new(filename, FALSE, &error);
+       
+       if (mapped_file == NULL) {
+               g_print ("Error while mapping %s: %s\n", filename, 
+                    error->message);
+               g_error_free(error);
+               return NULL;
        }
 
-       if (!S_ISREG (stat_buf.st_mode)) {
-               g_print ("%s is not a regular file\n", filename);
-               goto error;
-       }
-
-       if (stat_buf.st_size > ITUNESDB_MAX_SIZE) {
-               g_print ("%s is too big to be an buffer file\n", filename);
-               goto error;
-       }
-
-       buffer = mmap (NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
-
-       if (buffer == MAP_FAILED) {
-               g_print ("Error while mmap'ing %s: %s\n", 
-                        filename, strerror (errno));
-               goto error;
-       }
-
        if (device->byte_order == 0)
            itdb_device_autodetect_endianess (device);
 
-       ctx = db_parse_context_new (buffer,
-                                   stat_buf.st_size, device->byte_order);
+       ctx = db_parse_context_new ((guchar 
*)g_mapped_file_get_contents(mapped_file),
+                                       g_mapped_file_get_length(mapped_file), 
+                                       device->byte_order);
 
        if (ctx == NULL) {
-               munmap (buffer, stat_buf.st_size);
+               g_mapped_file_free(mapped_file);
+               return NULL;
        }
        ctx->db = db;
+       ctx->mapped_file = mapped_file;
 
- error:
-       close (fd);
-       return ctx;
+        return ctx;
 }

Modified: libgpod/trunk/src/db-parse-context.h
===================================================================
--- libgpod/trunk/src/db-parse-context.h        2008-07-06 14:04:20 UTC (rev 
2039)
+++ libgpod/trunk/src/db-parse-context.h        2008-07-06 14:04:33 UTC (rev 
2040)
@@ -39,7 +39,8 @@
        off_t total_len;
        guint byte_order;
        Itdb_DB *db;
-        GList **artwork;
+       GMappedFile *mapped_file;
+       GList **artwork;
 };
 
 typedef struct _DBParseContext DBParseContext;


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

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to