2001-03-17 04:17:14 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) many different people.
|
2003-07-15 02:51:08 +05:30
|
|
|
* If you wrote this, please acknowledge your work.
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2006-05-20 00:59:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2001-12-17 20:56:36 +05:30
|
|
|
typedef struct ino_dev_hash_bucket_struct {
|
2007-03-15 03:36:01 +05:30
|
|
|
struct ino_dev_hash_bucket_struct *next;
|
|
|
|
ino_t ino;
|
|
|
|
dev_t dev;
|
|
|
|
char name[1];
|
2001-12-17 20:56:36 +05:30
|
|
|
} ino_dev_hashtable_bucket_t;
|
|
|
|
|
2007-03-15 03:36:01 +05:30
|
|
|
#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-17 04:17:14 +05:30
|
|
|
|
|
|
|
/*
|
2007-03-15 03:36:01 +05:30
|
|
|
* Return name if statbuf->st_ino && statbuf->st_dev are recorded in
|
|
|
|
* ino_dev_hashtable, else return NULL
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
|
|
|
|
2007-03-15 03:36:01 +05:30
|
|
|
if (!ino_dev_hashtable)
|
|
|
|
return NULL;
|
|
|
|
|
2001-03-17 04:17:14 +05:30
|
|
|
bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
|
|
|
|
while (bucket != NULL) {
|
2007-03-15 03:36:01 +05:30
|
|
|
if ((bucket->ino == statbuf->st_ino)
|
|
|
|
&& (bucket->dev == statbuf->st_dev)
|
|
|
|
) {
|
|
|
|
return bucket->name;
|
|
|
|
}
|
|
|
|
bucket = bucket->next;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2007-03-15 03:36:01 +05:30
|
|
|
return NULL;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Add statbuf to statbuf hash table */
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2001-03-17 04:17:14 +05:30
|
|
|
i = hash_inode(statbuf->st_ino);
|
2007-03-15 03:36:01 +05:30
|
|
|
if (!name)
|
|
|
|
name = "";
|
|
|
|
bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
|
2001-03-17 04:17:14 +05:30
|
|
|
bucket->ino = statbuf->st_ino;
|
|
|
|
bucket->dev = statbuf->st_dev;
|
2007-03-15 03:36:01 +05:30
|
|
|
strcpy(bucket->name, name);
|
|
|
|
|
|
|
|
if (!ino_dev_hashtable)
|
|
|
|
ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
|
|
|
|
|
2001-03-17 04:17:14 +05:30
|
|
|
bucket->next = ino_dev_hashtable[i];
|
|
|
|
ino_dev_hashtable[i] = bucket;
|
|
|
|
}
|
|
|
|
|
2007-03-15 03:36:01 +05:30
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
2001-03-17 04:17:14 +05:30
|
|
|
/* Clear statbuf hash table */
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC reset_ino_dev_hashtable(void)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
|
|
|
|
2007-04-05 14:51:24 +05:30
|
|
|
for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) {
|
2001-03-17 04:17:14 +05:30
|
|
|
while (ino_dev_hashtable[i] != NULL) {
|
|
|
|
bucket = ino_dev_hashtable[i]->next;
|
|
|
|
free(ino_dev_hashtable[i]);
|
|
|
|
ino_dev_hashtable[i] = bucket;
|
|
|
|
}
|
|
|
|
}
|
2007-03-15 03:36:01 +05:30
|
|
|
free(ino_dev_hashtable);
|
|
|
|
ino_dev_hashtable = NULL;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2003-06-20 14:31:58 +05:30
|
|
|
#endif
|