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
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
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
|
|
|
ino_t ino;
|
|
|
|
dev_t dev;
|
2014-02-25 19:57:58 +05:30
|
|
|
/*
|
|
|
|
* Above fields can be 64-bit, while pointer may be 32-bit.
|
|
|
|
* Putting "next" field here may reduce size of this struct:
|
|
|
|
*/
|
|
|
|
struct ino_dev_hash_bucket_struct *next;
|
|
|
|
/*
|
|
|
|
* Reportedly, on cramfs a file and a dir can have same ino.
|
|
|
|
* Need to also remember "file/dir" bit:
|
|
|
|
*/
|
|
|
|
char isdir; /* bool */
|
2007-03-15 03:36:01 +05:30
|
|
|
char name[1];
|
2001-12-17 20:56:36 +05:30
|
|
|
} ino_dev_hashtable_bucket_t;
|
|
|
|
|
2014-02-25 19:57:58 +05:30
|
|
|
#define HASH_SIZE 311u /* Should be prime */
|
|
|
|
#define hash_inode(i) ((unsigned)(i) % HASH_SIZE)
|
2007-03-15 03:36:01 +05:30
|
|
|
|
|
|
|
/* 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)
|
2014-02-25 19:57:58 +05:30
|
|
|
&& (bucket->isdir == !!S_ISDIR(statbuf->st_mode))
|
2007-03-15 03:36:01 +05:30
|
|
|
) {
|
|
|
|
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
|
|
|
|
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;
|
2014-02-25 19:57:58 +05:30
|
|
|
bucket->isdir = !!S_ISDIR(statbuf->st_mode);
|
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));
|
|
|
|
|
2014-02-25 19:57:58 +05:30
|
|
|
i = hash_inode(statbuf->st_ino);
|
2001-03-17 04:17:14 +05:30
|
|
|
bucket->next = ino_dev_hashtable[i];
|
|
|
|
ino_dev_hashtable[i] = bucket;
|
|
|
|
}
|
|
|
|
|
2008-11-12 02:45:56 +05:30
|
|
|
#if ENABLE_DU || 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;
|
2014-02-25 19:39:01 +05:30
|
|
|
ino_dev_hashtable_bucket_t *bucket, *next;
|
|
|
|
|
|
|
|
if (!ino_dev_hashtable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < HASH_SIZE; i++) {
|
|
|
|
bucket = ino_dev_hashtable[i];
|
2001-03-17 04:17:14 +05:30
|
|
|
|
2014-02-25 19:39:01 +05:30
|
|
|
while (bucket != NULL) {
|
|
|
|
next = bucket->next;
|
|
|
|
free(bucket);
|
|
|
|
bucket = next;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
|
|
|
}
|
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
|