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.
13 typedef struct ino_dev_hash_bucket_struct {
17 * Above fields can be 64-bit, while pointer may be 32-bit.
18 * Putting "next" field here may reduce size of this struct:
20 struct ino_dev_hash_bucket_struct *next;
22 * Reportedly, on cramfs a file and a dir can have same ino.
23 * Need to also remember "file/dir" bit:
25 char isdir; /* bool */
27 } ino_dev_hashtable_bucket_t;
29 #define HASH_SIZE 311u /* Should be prime */
30 #define hash_inode(i) ((unsigned)(i) % HASH_SIZE)
32 /* array of [HASH_SIZE] elements */
33 static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
36 * Return name if statbuf->st_ino && statbuf->st_dev are recorded in
37 * ino_dev_hashtable, else return NULL
39 char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
41 ino_dev_hashtable_bucket_t *bucket;
43 if (!ino_dev_hashtable)
46 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
47 while (bucket != NULL) {
48 if ((bucket->ino == statbuf->st_ino)
49 && (bucket->dev == statbuf->st_dev)
50 && (bucket->isdir == !!S_ISDIR(statbuf->st_mode))
54 bucket = bucket->next;
59 /* Add statbuf to statbuf hash table */
60 void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
63 ino_dev_hashtable_bucket_t *bucket;
67 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
68 bucket->ino = statbuf->st_ino;
69 bucket->dev = statbuf->st_dev;
70 bucket->isdir = !!S_ISDIR(statbuf->st_mode);
71 strcpy(bucket->name, name);
73 if (!ino_dev_hashtable)
74 ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
76 i = hash_inode(statbuf->st_ino);
77 bucket->next = ino_dev_hashtable[i];
78 ino_dev_hashtable[i] = bucket;
81 #if ENABLE_DU || ENABLE_FEATURE_CLEAN_UP
82 /* Clear statbuf hash table */
83 void FAST_FUNC reset_ino_dev_hashtable(void)
86 ino_dev_hashtable_bucket_t *bucket, *next;
88 if (!ino_dev_hashtable)
91 for (i = 0; i < HASH_SIZE; i++) {
92 bucket = ino_dev_hashtable[i];
94 while (bucket != NULL) {
100 free(ino_dev_hashtable);
101 ino_dev_hashtable = NULL;