1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) many different people.
6 * If you wrote this, please acknowledge your work.
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 typedef struct ino_dev_hash_bucket_struct {
16 * Above fields can be 64-bit, while pointer may be 32-bit.
17 * Putting "next" field here may reduce size of this struct:
19 struct ino_dev_hash_bucket_struct *next;
21 * Reportedly, on cramfs a file and a dir can have same ino.
22 * Need to also remember "file/dir" bit:
24 char isdir; /* bool */
26 } ino_dev_hashtable_bucket_t;
28 #define HASH_SIZE 311u /* Should be prime */
29 #define hash_inode(i) ((unsigned)(i) % HASH_SIZE)
31 /* array of [HASH_SIZE] elements */
32 static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
35 * Return name if statbuf->st_ino && statbuf->st_dev are recorded in
36 * ino_dev_hashtable, else return NULL
38 char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
40 ino_dev_hashtable_bucket_t *bucket;
42 if (!ino_dev_hashtable)
45 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
46 while (bucket != NULL) {
47 if ((bucket->ino == statbuf->st_ino)
48 && (bucket->dev == statbuf->st_dev)
49 && (bucket->isdir == !!S_ISDIR(statbuf->st_mode))
53 bucket = bucket->next;
58 /* Add statbuf to statbuf hash table */
59 void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
62 ino_dev_hashtable_bucket_t *bucket;
66 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
67 bucket->ino = statbuf->st_ino;
68 bucket->dev = statbuf->st_dev;
69 bucket->isdir = !!S_ISDIR(statbuf->st_mode);
70 strcpy(bucket->name, name);
72 if (!ino_dev_hashtable)
73 ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
75 i = hash_inode(statbuf->st_ino);
76 bucket->next = ino_dev_hashtable[i];
77 ino_dev_hashtable[i] = bucket;
80 #if ENABLE_DU || ENABLE_FEATURE_CLEAN_UP
81 /* Clear statbuf hash table */
82 void FAST_FUNC reset_ino_dev_hashtable(void)
85 ino_dev_hashtable_bucket_t *bucket, *next;
87 if (!ino_dev_hashtable)
90 for (i = 0; i < HASH_SIZE; i++) {
91 bucket = ino_dev_hashtable[i];
93 while (bucket != NULL) {
99 free(ino_dev_hashtable);
100 ino_dev_hashtable = NULL;