di-set: provide a lookup method
authorJim Meyering <meyering@redhat.com>
Mon, 7 Feb 2011 14:46:09 +0000 (15:46 +0100)
committerJim Meyering <meyering@redhat.com>
Mon, 7 Feb 2011 14:50:44 +0000 (15:50 +0100)
This is required for patch, and hence is about to move to gnulib.
* gl/lib/di-set.c (di_set_lookup): New function.
* gl/lib/di-set.h: Declare it.
* gl/tests/test-di-set.c (main): Exercise it.

The bug was introduced on 2004-12-04 via commit 7380cf79.

gl/lib/di-set.c
gl/lib/di-set.h
gl/tests/test-di-set.c

index 05d24d6..5e839a3 100644 (file)
@@ -235,3 +235,25 @@ di_set_insert (struct di_set *dis, dev_t dev, ino_t ino)
   /* Put I into the inode set.  */
   return hash_insert0 (ino_set, (void *) i, NULL);
 }
+
+/* Look up the DEV,INO pair in the set DIS.
+   If found, return 1; if not found, return 0.
+   Upon any failure return -1.  */
+int
+di_set_lookup (struct di_set *dis, dev_t dev, ino_t ino)
+{
+  hashint i;
+
+  /* Map the device number to a set of inodes.  */
+  struct hash_table *ino_set = map_device (dis, dev);
+  if (! ino_set)
+    return -1;
+
+  /* Map the inode number to a small representative I.  */
+  i = map_inode_number (dis, ino);
+  if (i == INO_MAP_INSERT_FAILURE)
+    return -1;
+
+  /* Perform the look-up.  */
+  return !!hash_lookup (ino_set, (void const *) i);
+}
index d30a31e..f05e876 100644 (file)
@@ -10,3 +10,5 @@
 struct di_set *di_set_alloc (void);
 int di_set_insert (struct di_set *, dev_t, ino_t) _ATTRIBUTE_NONNULL_ (1);
 void di_set_free (struct di_set *) _ATTRIBUTE_NONNULL_ (1);
+int di_set_lookup (struct di_set *dis, dev_t dev, ino_t ino)
+  _ATTRIBUTE_NONNULL_ (1);;
index 7fca4d4..5de8da2 100644 (file)
@@ -42,10 +42,12 @@ main (void)
   struct di_set *dis = di_set_alloc ();
   ASSERT (dis);
 
+  ASSERT (di_set_lookup (dis, 2, 5) == 0); /* initial lookup fails */
   ASSERT (di_set_insert (dis, 2, 5) == 1); /* first insertion succeeds */
   ASSERT (di_set_insert (dis, 2, 5) == 0); /* duplicate fails */
   ASSERT (di_set_insert (dis, 3, 5) == 1); /* diff dev, duplicate inode is ok */
   ASSERT (di_set_insert (dis, 2, 8) == 1); /* same dev, different inode is ok */
+  ASSERT (di_set_lookup (dis, 2, 5) == 1); /* now, the lookup succeeds */
 
   /* very large (or negative) inode number */
   ASSERT (di_set_insert (dis, 5, (ino_t) -1) == 1);