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



commit acb91508fe8d5b10412f7b967ea8867ad3f01c3d
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 |  106 ++++++++++++++++++++++++--------
 1 files changed, 79 insertions(+), 27 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index 0a88a38..f318590 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>
 
@@ -58,7 +59,7 @@ static struct {
 
 static struct {
 	gchar *journal_filename;
-	FILE *journal;
+	int journal;
 	gsize cur_size;
 	guint cur_block_len;
 	guint cur_block_alloc;
@@ -132,6 +133,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)
 {
@@ -142,8 +176,10 @@ gboolean
 tracker_db_journal_init (const gchar *filename)
 {
 	struct stat st;
+	int flags;
+	int mode;
 
-	g_return_val_if_fail (writer.journal == NULL, FALSE);
+	g_return_val_if_fail (writer.journal == 0, FALSE);
 
 	writer.cur_block_len = 0;
 	writer.cur_pos = 0;
@@ -162,7 +198,18 @@ tracker_db_journal_init (const gchar *filename)
 		                                            NULL);
 	}
 
-	writer.journal = g_fopen (writer.journal_filename, "a");
+	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
+	flags = O_WRONLY | O_APPEND | O_CREAT;
+	writer.journal = g_open (writer.journal_filename, flags, mode);
+
+	if (writer.journal == -1) {
+		g_critical ("Could not open journal for writing, %s", 
+		            g_strerror (errno));
+
+		g_free (writer.journal_filename);
+		writer.journal_filename = NULL;
+		return FALSE;
+	}
 
 	if (g_stat (writer.journal_filename, &st) == 0) {
 		writer.cur_size = (gsize) st.st_size;
@@ -185,10 +232,13 @@ tracker_db_journal_init (const gchar *filename)
 		writer.cur_block[6] = '0';
 		writer.cur_block[7] = '1';
 
-		write (fileno (writer.journal), writer.cur_block, 8);
+		if (!write_all_data (writer.journal, writer.cur_block, 8)) {
+			g_free (writer.journal_filename);
+			writer.journal_filename = NULL;
+			return FALSE;
+		}
 
 		writer.cur_size += 8;
-
 		cur_block_kill ();
 	}
 
@@ -198,15 +248,18 @@ 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 (writer.journal > 0, FALSE);
 
-	if (journal) {
-		fclose (journal);
-		journal = NULL;
+	if (close (writer.journal) != 0) {
+		g_warning ("Could not close journal, %s", 
+		           g_strerror (errno));
+		return FALSE;
 	}
 
-	g_free (journal_filename);
-	journal_filename = NULL;
+	writer.journal = 0;
+
+	g_free (writer.journal_filename);
+	writer.journal_filename = NULL;
 
 	return TRUE;
 }
@@ -214,17 +267,17 @@ 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 (writer.journal > 0, FALSE);
 
-	return cur_size;
+	return writer.cur_size;
 }
 
 const gchar *
 tracker_db_journal_get_filename (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
-	return (const gchar*) journal_filename;
+	return (const gchar*) writer.journal_filename;
 }
 
 gboolean
@@ -232,7 +285,7 @@ tracker_db_journal_start_transaction (void)
 {
 	guint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	size = sizeof (guint32) * 3;
 	cur_block_maybe_expand (size);
@@ -258,7 +311,7 @@ tracker_db_journal_append_delete_statement (guint32      s_id,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x04;
@@ -285,7 +338,7 @@ tracker_db_journal_append_delete_statement_id (guint32 s_id,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	data_format = 0x06;
 	size = sizeof (guint32) * 4;
@@ -312,7 +365,7 @@ tracker_db_journal_append_insert_statement (guint32      s_id,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x00;
@@ -339,7 +392,7 @@ tracker_db_journal_append_insert_statement_id (guint32 s_id,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	data_format = 0x02;
 	size = sizeof (guint32) * 4;
@@ -365,7 +418,7 @@ tracker_db_journal_append_resource (guint32      s_id,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	o_len = strlen (uri);
 	data_format = 0x01;
@@ -386,7 +439,7 @@ tracker_db_journal_append_resource (guint32      s_id,
 gboolean
 tracker_db_journal_rollback_transaction (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	cur_block_kill ();
 
@@ -401,7 +454,7 @@ tracker_db_journal_commit_transaction (void)
 	guint size;
 	guint offset;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	begin_pos = 0;
 	size = sizeof (guint32);
@@ -421,8 +474,7 @@ tracker_db_journal_commit_transaction (void)
 	crc = tracker_crc32 (writer.cur_block + offset, writer.cur_block_len - offset);
 	cur_setnum (writer.cur_block, &begin_pos, crc);
 
-	/* FIXME: What if we don't write all of len, needs improving. */
-	if (write (fileno (writer.journal), writer.cur_block, writer.cur_block_len) == -1) {
+	if (!write_all_data (writer.journal, writer.cur_block, writer.cur_block_len)) {
 		g_critical ("Could not write to journal, %s", g_strerror (errno));
 		return FALSE;
 	}
@@ -436,9 +488,9 @@ tracker_db_journal_commit_transaction (void)
 gboolean
 tracker_db_journal_fsync (void)
 {
-	g_return_val_if_fail (writer.journal != NULL, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
-	return fsync (fileno (writer.journal)) == 0;
+	return fsync (writer.journal) == 0;
 }
 
 /*



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