2001-03-16 22:47:14 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) many different people.
|
2003-07-14 21:21:08 +00:00
|
|
|
* If you wrote this, please acknowledge your work.
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2001-12-17 15:26:36 +00:00
|
|
|
typedef struct ino_dev_hash_bucket_struct {
|
2007-03-14 22:06:01 +00:00
|
|
|
struct ino_dev_hash_bucket_struct *next;
|
|
|
|
ino_t ino;
|
|
|
|
dev_t dev;
|
|
|
|
char name[1];
|
2001-12-17 15:26:36 +00:00
|
|
|
} ino_dev_hashtable_bucket_t;
|
|
|
|
|
2007-03-14 22:06:01 +00:00
|
|
|
#define HASH_SIZE 311 /* Should be prime */
|
|
|
|
#define hash_inode(i) ((i) % HASH_SIZE)
|
|
|
|
|
|
|
|
/* array of [HASH_SIZE] elements */
|
|
|
|
static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
|
2001-03-16 22:47:14 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-14 22:06:01 +00:00
|
|
|
* Return name if statbuf->st_ino && statbuf->st_dev are recorded in
|
|
|
|
* ino_dev_hashtable, else return NULL
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
|
|
|
|
2007-03-14 22:06:01 +00:00
|
|
|
if (!ino_dev_hashtable)
|
|
|
|
return NULL;
|
|
|
|
|
2001-03-16 22:47:14 +00:00
|
|
|
bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
|
|
|
|
while (bucket != NULL) {
|
2007-03-14 22:06:01 +00:00
|
|
|
if ((bucket->ino == statbuf->st_ino)
|
|
|
|
&& (bucket->dev == statbuf->st_dev)
|
|
|
|
) {
|
|
|
|
return bucket->name;
|
|
|
|
}
|
|
|
|
bucket = bucket->next;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2007-03-14 22:06:01 +00:00
|
|
|
return NULL;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add statbuf to statbuf hash table */
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-03-16 22:47:14 +00:00
|
|
|
i = hash_inode(statbuf->st_ino);
|
2007-03-14 22:06:01 +00:00
|
|
|
if (!name)
|
|
|
|
name = "";
|
|
|
|
bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
|
2001-03-16 22:47:14 +00:00
|
|
|
bucket->ino = statbuf->st_ino;
|
|
|
|
bucket->dev = statbuf->st_dev;
|
2007-03-14 22:06:01 +00:00
|
|
|
strcpy(bucket->name, name);
|
|
|
|
|
|
|
|
if (!ino_dev_hashtable)
|
|
|
|
ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
|
|
|
|
|
2001-03-16 22:47:14 +00:00
|
|
|
bucket->next = ino_dev_hashtable[i];
|
|
|
|
ino_dev_hashtable[i] = bucket;
|
|
|
|
}
|
|
|
|
|
2008-11-11 21:15:56 +00:00
|
|
|
#if ENABLE_DU || ENABLE_FEATURE_CLEAN_UP
|
2001-03-16 22:47:14 +00:00
|
|
|
/* Clear statbuf hash table */
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC reset_ino_dev_hashtable(void)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
|
|
|
|
2007-04-05 09:21:24 +00:00
|
|
|
for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) {
|
2001-03-16 22:47:14 +00:00
|
|
|
while (ino_dev_hashtable[i] != NULL) {
|
|
|
|
bucket = ino_dev_hashtable[i]->next;
|
|
|
|
free(ino_dev_hashtable[i]);
|
|
|
|
ino_dev_hashtable[i] = bucket;
|
|
|
|
}
|
|
|
|
}
|
2007-03-14 22:06:01 +00:00
|
|
|
free(ino_dev_hashtable);
|
|
|
|
ino_dev_hashtable = NULL;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2003-06-20 09:01:58 +00:00
|
|
|
#endif
|