3
0

inode_hash.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please acknowledge your work.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. typedef struct ino_dev_hash_bucket_struct {
  12. struct ino_dev_hash_bucket_struct *next;
  13. ino_t ino;
  14. dev_t dev;
  15. char name[1];
  16. } ino_dev_hashtable_bucket_t;
  17. #define HASH_SIZE 311 /* Should be prime */
  18. #define hash_inode(i) ((i) % HASH_SIZE)
  19. /* array of [HASH_SIZE] elements */
  20. static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
  21. /*
  22. * Return name if statbuf->st_ino && statbuf->st_dev are recorded in
  23. * ino_dev_hashtable, else return NULL
  24. */
  25. char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
  26. {
  27. ino_dev_hashtable_bucket_t *bucket;
  28. if (!ino_dev_hashtable)
  29. return NULL;
  30. bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
  31. while (bucket != NULL) {
  32. if ((bucket->ino == statbuf->st_ino)
  33. && (bucket->dev == statbuf->st_dev)
  34. ) {
  35. return bucket->name;
  36. }
  37. bucket = bucket->next;
  38. }
  39. return NULL;
  40. }
  41. /* Add statbuf to statbuf hash table */
  42. void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
  43. {
  44. int i;
  45. ino_dev_hashtable_bucket_t *bucket;
  46. i = hash_inode(statbuf->st_ino);
  47. if (!name)
  48. name = "";
  49. bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
  50. bucket->ino = statbuf->st_ino;
  51. bucket->dev = statbuf->st_dev;
  52. strcpy(bucket->name, name);
  53. if (!ino_dev_hashtable)
  54. ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
  55. bucket->next = ino_dev_hashtable[i];
  56. ino_dev_hashtable[i] = bucket;
  57. }
  58. #if ENABLE_DU || ENABLE_FEATURE_CLEAN_UP
  59. /* Clear statbuf hash table */
  60. void FAST_FUNC reset_ino_dev_hashtable(void)
  61. {
  62. int i;
  63. ino_dev_hashtable_bucket_t *bucket;
  64. for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) {
  65. while (ino_dev_hashtable[i] != NULL) {
  66. bucket = ino_dev_hashtable[i]->next;
  67. free(ino_dev_hashtable[i]);
  68. ino_dev_hashtable[i] = bucket;
  69. }
  70. }
  71. free(ino_dev_hashtable);
  72. ino_dev_hashtable = NULL;
  73. }
  74. #endif