summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorteuf <teuf@f01d2545-417e-4e96-918e-98f8d0dbbcb6>2008-07-06 14:04:33 +0000
committerteuf <teuf@f01d2545-417e-4e96-918e-98f8d0dbbcb6>2008-07-06 14:04:33 +0000
commit35a75d4a7a0bfa12f04d65235f14fe10de079653 (patch)
tree479902bfe57b081fc40f828fb8f4366bb5988849
parent9ab003061df4f4a84591e966412db30498df46e7 (diff)
downloadlibgpod-35a75d4a7a0bfa12f04d65235f14fe10de079653.tar.gz
libgpod-35a75d4a7a0bfa12f04d65235f14fe10de079653.tar.xz
libgpod-35a75d4a7a0bfa12f04d65235f14fe10de079653.zip
Use GMappedFile instead of directly using mmap
git-svn-id: https://gtkpod.svn.sf.net/svnroot/gtkpod/libgpod/trunk@2040 f01d2545-417e-4e96-918e-98f8d0dbbcb6
-rw-r--r--ChangeLog9
-rw-r--r--src/db-itunes-parser.h3
-rw-r--r--src/db-parse-context.c70
-rw-r--r--src/db-parse-context.h3
4 files changed, 40 insertions, 45 deletions
diff --git a/ChangeLog b/ChangeLog
index 02916e6..d55de38 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2008-07-06 Christophe Fergeau <teuf@gnome.org>
+ * 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 <teuf@gnome.org>
+
+ 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
diff --git a/src/db-itunes-parser.h b/src/db-itunes-parser.h
index 8dcf10c..ba93a57 100644
--- a/src/db-itunes-parser.h
+++ b/src/db-itunes-parser.h
@@ -28,9 +28,6 @@
#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];
diff --git a/src/db-parse-context.c b/src/db-parse-context.c
index 1388626..88009c9 100644
--- a/src/db-parse-context.c
+++ b/src/db-parse-context.c
@@ -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 @@ db_parse_context_destroy (DBParseContext *ctx)
{
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 @@ db_parse_context_get_m_header_internal (DBParseContext *ctx, const char *id, off
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;
- }
-
- 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;
+ 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 (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;
}
diff --git a/src/db-parse-context.h b/src/db-parse-context.h
index 5703ac5..43411ed 100644
--- a/src/db-parse-context.h
+++ b/src/db-parse-context.h
@@ -39,7 +39,8 @@ struct _DBParseContext {
off_t total_len;
guint byte_order;
Itdb_DB *db;
- GList **artwork;
+ GMappedFile *mapped_file;
+ GList **artwork;
};
typedef struct _DBParseContext DBParseContext;