[tracker/binary-log-2: 14/48] libtracker-db: Use open/close instead of fopen and fclose
- From: Jürg Billeter <juergbi src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [tracker/binary-log-2: 14/48] libtracker-db: Use open/close instead of fopen and fclose
- Date: Mon, 11 Jan 2010 15:59:21 +0000 (UTC)
commit 63cf51db065b44c0804bf8ab1ecb862b7b6d287e
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 | 98 ++++++++++++++++++++++++--------
1 files changed, 75 insertions(+), 23 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index e4ff221..b2c403e 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,18 @@ tracker_db_journal_init (const gchar *filename)
NULL);
}
- journal = g_fopen (journal_filename, "a");
+ mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
+ flags = O_WRONLY | O_APPEND | O_CREAT;
+ 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 +225,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 +241,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 +260,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 +268,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 +278,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 +304,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 +331,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 +358,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 +385,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 +411,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 +432,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 +447,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 +467,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 +481,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]