From 7292355283b90f1da3a86713b9f85074da22bb84 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Fri, 18 May 2007 00:02:35 -0400 Subject: [PATCH] Add new function blkid_gc_cache() New function which performs a garbage collection pass on the /etc/blkid.tab file. Signed-off-by: "Theodore Ts'o" --- lib/blkid/ChangeLog | 5 +++++ lib/blkid/blkid.h | 1 + lib/blkid/cache.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/lib/blkid/ChangeLog b/lib/blkid/ChangeLog index 4433ef4..303f7cf 100644 --- a/lib/blkid/ChangeLog +++ b/lib/blkid/ChangeLog @@ -1,3 +1,8 @@ +2007-05-17 Theodore Tso + + * cache.c (blkid_gc_cache): New function which removes any devices + from the blkid cache if the device node does not exist. + 2007-03-23 Theodore Tso * read.c (parse_dev): Fix memory leak on error path. diff --git a/lib/blkid/blkid.h b/lib/blkid/blkid.h index 892b798..f38a235 100644 --- a/lib/blkid/blkid.h +++ b/lib/blkid/blkid.h @@ -50,6 +50,7 @@ typedef struct blkid_struct_dev_iterate *blkid_dev_iterate; /* cache.c */ extern void blkid_put_cache(blkid_cache cache); extern int blkid_get_cache(blkid_cache *cache, const char *filename); +extern void blkid_gc_cache(blkid_cache cache); /* dev.c */ extern const char *blkid_dev_devname(blkid_dev dev); diff --git a/lib/blkid/cache.c b/lib/blkid/cache.c index b47b488..1508d0f 100644 --- a/lib/blkid/cache.c +++ b/lib/blkid/cache.c @@ -26,6 +26,9 @@ #if (!defined(HAVE_PRCTL) && defined(linux)) #include #endif +#ifdef HAVE_SYS_STAT_H +#include +#endif #include "blkidP.h" int blkid_debug_mask = 0; @@ -149,6 +152,31 @@ void blkid_put_cache(blkid_cache cache) free(cache); } +void blkid_gc_cache(blkid_cache cache) +{ + struct list_head *p; + struct stat st; + + if (!cache) + return; + + list_for_each(p, &cache->bic_devs) { + blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs); + if (!p) + break; + if (stat(dev->bid_name, &st) < 0) { + DBG(DEBUG_CACHE, + printf("freeing %s\n", dev->bid_name)); + blkid_free_dev(dev); + cache->bic_flags |= BLKID_BIC_FL_CHANGED; + } else { + DBG(DEBUG_CACHE, + printf("Device %s exists\n", dev->bid_name)); + } + } +} + + #ifdef TEST_PROGRAM int main(int argc, char** argv) { -- 1.8.3.1