summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@linbit.com>2012-07-30 16:45:11 +0200
committerAndreas Gruenbacher <agruen@linbit.com>2012-07-31 21:58:30 +0200
commitea4212ee512edb4a8c0b8559e961d8e06855db9d (patch)
tree352a861db91dfdd10dcbc9684d69568bd6858d9b
parentb0c22022be55611cf1c60401ef794dbb94767ed9 (diff)
downloadpatch-ea4212ee512edb4a8c0b8559e961d8e06855db9d.tar.gz
Allow to remember SHA1 hashes in the file id cache
* src/util.c (file_id): New sha1 field. (__insert_file_id): Split off from insert_file_id(). Initialize sha1 field. (__lookup_file_id): Split off from lookup_file_id(). (update_sha1): Remember SHA1 hash of a file or update the remembered SHA1 hash. (lookup_sha1): Look up the SHA1 hash of a file. * src/util.h (update_sha1, lookup_sha1): Declare.
-rw-r--r--src/util.c55
-rw-r--r--src/util.h2
2 files changed, 48 insertions, 9 deletions
diff --git a/src/util.c b/src/util.c
index ae2bafa..d392ffe 100644
--- a/src/util.c
+++ b/src/util.c
@@ -58,6 +58,7 @@ typedef struct
dev_t dev;
ino_t ino;
enum file_id_type type;
+ char *sha1;
} file_id;
/* Return an index for ENTRY into a hash table of size TABLE_SIZE. */
@@ -93,11 +94,8 @@ init_backup_hash_table (void)
xalloc_die ();
}
-/* Insert a file with status ST and type TYPE into the hash table.
- The type of an existing entry can be changed by re-inserting it. */
-
-void
-insert_file_id (struct stat const *st, enum file_id_type type)
+static file_id *
+__insert_file_id (struct stat const *st, enum file_id_type type)
{
file_id *p;
static file_id *next_slot;
@@ -106,12 +104,33 @@ insert_file_id (struct stat const *st, enum file_id_type type)
next_slot = xmalloc (sizeof *next_slot);
next_slot->dev = st->st_dev;
next_slot->ino = st->st_ino;
+ next_slot->sha1 = 0;
p = hash_insert (file_id_table, next_slot);
if (!p)
xalloc_die ();
if (p == next_slot)
next_slot = NULL;
p->type = type;
+ return p;
+}
+
+static file_id *
+__lookup_file_id (struct stat const *st)
+{
+ file_id f;
+
+ f.dev = st->st_dev;
+ f.ino = st->st_ino;
+ return hash_lookup (file_id_table, &f);
+}
+
+/* Insert a file with status ST and type TYPE into the hash table.
+ The type of an existing entry can be changed by re-inserting it. */
+
+void
+insert_file_id (struct stat const *st, enum file_id_type type)
+{
+ __insert_file_id (st, type);
}
/* Has the file identified by ST already been inserted into the hash
@@ -120,14 +139,32 @@ insert_file_id (struct stat const *st, enum file_id_type type)
enum file_id_type
lookup_file_id (struct stat const *st)
{
- file_id f, *p;
+ file_id *p = __lookup_file_id (st);
- f.dev = st->st_dev;
- f.ino = st->st_ino;
- p = hash_lookup (file_id_table, &f);
return p ? p->type : UNKNOWN;
}
+void
+update_sha1 (struct stat const *st, char const *sha1)
+{
+ file_id *p = __lookup_file_id (st);
+
+ if (! p)
+ p = __insert_file_id (st, UNKNOWN);
+ else
+ if (p->sha1)
+ free (p->sha1);
+ p->sha1 = sha1 ? xstrdup (sha1) : 0;
+}
+
+char const *
+lookup_sha1 (struct stat const *st)
+{
+ file_id *p = __lookup_file_id (st);
+
+ return p ? p->sha1 : NULL;
+}
+
static bool _GL_ATTRIBUTE_PURE
contains_slash (const char *s)
{
diff --git a/src/util.h b/src/util.h
index 39d089c..2913b15 100644
--- a/src/util.h
+++ b/src/util.h
@@ -66,6 +66,8 @@ void set_signals (bool);
void write_fatal (void) __attribute__ ((noreturn));
void insert_file_id (struct stat const *, enum file_id_type);
enum file_id_type lookup_file_id (struct stat const *);
+void update_sha1(struct stat const *, char const *);
+char const *lookup_sha1 (struct stat const *);
enum file_attributes {
FA_TIMES = 1,