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
|
|
|
*
|
2006-05-19 19:29:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-03-23 17:02:05 +00:00
|
|
|
#include <string.h>
|
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
|
|
|
*/
|
2007-03-14 22:06:01 +00:00
|
|
|
char *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 */
|
|
|
|
void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-03-14 22:06:01 +00:00
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
2001-03-16 22:47:14 +00:00
|
|
|
/* Clear statbuf hash table */
|
|
|
|
void reset_ino_dev_hashtable(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ino_dev_hashtable_bucket_t *bucket;
|
|
|
|
|
|
|
|
for (i = 0; i < HASH_SIZE; i++) {
|
|
|
|
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
|