[tracker/binary-log-2: 15/44] libtracker-db: Use open/close instead of fopen and fclose



commit 415818e8c9b707c4425cc988d8f53d989c6c13b7
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Jan 5 11:53:34 2010 +0000

    libtracker-db: Use open/close instead of fopen and fclose
    
    Also added loop to make sure write writes all the buffer

 src/libtracker-db/tracker-db-journal.c |   99 ++++++++++++++++++++++++-------
 1 files changed, 76 insertions(+), 23 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index e4ff221..affdda4 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -30,6 +30,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <errno.h>
+#include <fcntl.h>
 
 #include <glib/gstdio.h>
 
@@ -57,7 +58,7 @@ static struct {
 } reader;
 
 static gchar *journal_filename = NULL;
-static FILE *journal = NULL;
+static int journal = 0;
 
 static gsize cur_size = 0;
 static guint cur_block_len = 0;
@@ -131,6 +132,39 @@ cur_setstr (gchar       *dest,
 	memset (dest + (*pos)++, 0 & 0xff, 1);
 }
 
+static gboolean
+write_all_data (int    fd, 
+                gchar *data, 
+                gsize  len)
+{
+	gssize written;
+	gboolean result;
+	
+	result = FALSE;
+	
+	while (len > 0) {
+		written = write (fd, data, len);
+		
+		if (written < 0) {
+			if (errno == EAGAIN) {
+				continue;
+			}
+			goto out;
+		} else if (written == 0) {
+			goto out; /* WTH? Don't loop forever*/
+		}
+		
+		len -= written;
+		data += written;
+	}
+	
+	result = TRUE; /* Succeeded! */
+	
+out:
+	
+	return result;
+}
+
 GQuark
 tracker_db_journal_error_quark (void)
 {
@@ -141,8 +175,10 @@ gboolean
 tracker_db_journal_init (const gchar *filename)
 {
 	struct stat st;
+	int flags;
+	int mode;
 
-	g_return_val_if_fail (journal == NULL, FALSE);
+	g_return_val_if_fail (journal == 0, FALSE);
 
 	/* Used mostly for testing */
 	if (G_UNLIKELY (filename)) {
@@ -155,7 +191,19 @@ tracker_db_journal_init (const gchar *filename)
 		                                     NULL);
 	}
 
-	journal = g_fopen (journal_filename, "a");
+	/* FIXME: Should we use O_SYNC? and O_DIRECT? */
+	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
+	flags = O_WRONLY | O_APPEND | O_CREAT | O_SYNC;
+	journal = g_open (journal_filename, flags, mode);
+
+	if (journal == -1) {
+		g_critical ("Could not open journal for writing, %s", 
+		            g_strerror (errno));
+
+		g_free (journal_filename);
+		journal_filename = NULL;
+		return FALSE;
+	}
 
 	if (g_stat (journal_filename, &st) == 0) {
 		cur_size = (gsize) st.st_size;
@@ -178,10 +226,13 @@ tracker_db_journal_init (const gchar *filename)
 		cur_block[6] = '0';
 		cur_block[7] = '1';
 
-		write (fileno (journal), cur_block, 8);
+		if (!write_all_data (journal, cur_block, 8)) {
+			g_free (journal_filename);
+			journal_filename = NULL;
+			return FALSE;
+		}
 
 		cur_size += 8;
-
 		cur_block_kill ();
 	}
 
@@ -191,12 +242,15 @@ tracker_db_journal_init (const gchar *filename)
 gboolean
 tracker_db_journal_shutdown (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
-	if (journal) {
-		fclose (journal);
-		journal = NULL;
+	if (close (journal) != 0) {
+		g_warning ("Could not close journal, %s", 
+		           g_strerror (errno));
+		return FALSE;
 	}
+	
+	journal = 0;
 
 	g_free (journal_filename);
 	journal_filename = NULL;
@@ -207,7 +261,7 @@ tracker_db_journal_shutdown (void)
 gsize
 tracker_db_journal_get_size (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	return cur_size;
 }
@@ -215,7 +269,7 @@ tracker_db_journal_get_size (void)
 const gchar *
 tracker_db_journal_get_filename (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	return (const gchar*) journal_filename;
 }
@@ -225,7 +279,7 @@ tracker_db_journal_start_transaction (void)
 {
 	guint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	size = sizeof (guint32) * 3;
 	cur_block_maybe_expand (size);
@@ -251,7 +305,7 @@ tracker_db_journal_append_delete_statement (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x04;
@@ -278,7 +332,7 @@ tracker_db_journal_append_delete_statement_code (guint32 s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	data_format = 0x06;
 	size = sizeof (guint32) * 4;
@@ -305,7 +359,7 @@ tracker_db_journal_append_insert_statement (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x00;
@@ -332,7 +386,7 @@ tracker_db_journal_append_insert_statement_code (guint32 s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	data_format = 0x02;
 	size = sizeof (guint32) * 4;
@@ -358,7 +412,7 @@ tracker_db_journal_append_resource (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	o_len = strlen (uri);
 	data_format = 0x01;
@@ -379,7 +433,7 @@ tracker_db_journal_append_resource (guint32      s_code,
 gboolean
 tracker_db_journal_rollback_transaction (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	cur_block_kill ();
 
@@ -394,7 +448,7 @@ tracker_db_journal_commit_transaction (void)
 	guint size;
 	guint offset;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
 	begin_pos = 0;
 	size = sizeof (guint32);
@@ -414,8 +468,7 @@ tracker_db_journal_commit_transaction (void)
 	crc = tracker_crc32 (cur_block + offset, cur_block_len - offset);
 	cur_setnum (cur_block, &begin_pos, crc);
 
-	/* FIXME: What if we don't write all of len, needs improving. */
-	if (write (fileno (journal), cur_block, cur_block_len) == -1) {
+	if (!write_all_data (journal, cur_block, cur_block_len)) {
 		g_critical ("Could not write to journal, %s", g_strerror (errno));
 		return FALSE;
 	}
@@ -429,9 +482,9 @@ tracker_db_journal_commit_transaction (void)
 gboolean
 tracker_db_journal_fsync (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal > 0, FALSE);
 
-	return fsync (fileno (journal)) == 0;
+	return fsync (journal) == 0;
 }
 
 /*



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]