From 192d3a7db50e42ad40aea8311e1994f53a1efdc0 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 23 Jun 2009 16:27:17 +0200 Subject: Allow MetaTree open to succeed with no file You can check with meta_tree_exists if the file actually exists. This means that lookups work better for the initial set for a non-existing file. --- metadata/metatree.c | 23 +++++++++++++++++------ metadata/metatree.h | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'metadata') diff --git a/metadata/metatree.c b/metadata/metatree.c index e6fa62b1..9664fa84 100644 --- a/metadata/metatree.c +++ b/metadata/metatree.c @@ -269,7 +269,7 @@ meta_tree_clear (MetaTree *tree) } tree->len = 0; - if (tree->fd != 0) + if (tree->fd != -1) { close (tree->fd); tree->fd = 0; @@ -447,6 +447,7 @@ meta_tree_init (MetaTree *tree) } meta_builder_free (builder); } + tree->fd = -1; return FALSE; } @@ -526,12 +527,10 @@ meta_tree_open (const char *filename, tree->ref_count = 1; tree->filename = g_strdup (filename); tree->for_write = for_write; + tree->fd = -1; + + meta_tree_init (tree); - if (!meta_tree_init (tree)) - { - meta_tree_unref (tree); - return NULL; - } return tree; } @@ -541,6 +540,12 @@ meta_tree_get_filename (MetaTree *tree) return tree->filename; } +gboolean +meta_tree_exists (MetaTree *tree) +{ + return tree->fd != -1; +} + static GHashTable *cached_trees = NULL; G_LOCK_DEFINE_STATIC (cached_trees); @@ -607,6 +612,9 @@ meta_tree_unref (MetaTree *tree) static gboolean meta_tree_needs_rereading (MetaTree *tree) { + if (tree->fd == -1) + return TRUE; + if (tree->header != NULL && GUINT32_FROM_BE (tree->header->rotated) == 0) return FALSE; /* Got a valid tree and its not rotated */ @@ -734,6 +742,9 @@ meta_tree_lookup (MetaTree *tree, MetaFileDirEnt *dirent; char *path_copy; + if (tree->root == NULL) + return NULL; + path_copy = g_strdup (path); dirent = dir_lookup_path (tree, tree->root, path_copy); g_free (path_copy); diff --git a/metadata/metatree.h b/metadata/metatree.h index a5bbab72..325e6fe9 100644 --- a/metadata/metatree.h +++ b/metadata/metatree.h @@ -40,6 +40,7 @@ MetaTree * meta_tree_ref (MetaTree *tree); void meta_tree_unref (MetaTree *tree); void meta_tree_refresh (MetaTree *tree); const char *meta_tree_get_filename (MetaTree *tree); +gboolean meta_tree_exists (MetaTree *tree); MetaKeyType meta_tree_lookup_key_type (MetaTree *tree, const char *path, -- cgit v1.2.1