blob: 377ecbc756155391be6294b08f207e01460fb641 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) many different people.
Eric Andersencb81e642003-07-14 21:21:08 +00006 * If you wrote this, please acknowledge your work.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
12
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000013typedef struct ino_dev_hash_bucket_struct {
14 struct ino_dev_hash_bucket_struct *next;
15 ino_t ino;
16 dev_t dev;
17 char name[1];
18} ino_dev_hashtable_bucket_t;
19
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020020#define HASH_SIZE 311 /* Should be prime */
21#define hash_inode(i) ((i) % HASH_SIZE)
Eric Andersenaad1a882001-03-16 22:47:14 +000022
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000023/* array of [HASH_SIZE] elements */
24static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
Eric Andersenaad1a882001-03-16 22:47:14 +000025
26/*
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000027 * Return name if statbuf->st_ino && statbuf->st_dev are recorded in
28 * ino_dev_hashtable, else return NULL
Eric Andersenaad1a882001-03-16 22:47:14 +000029 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000030char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
Eric Andersenaad1a882001-03-16 22:47:14 +000031{
32 ino_dev_hashtable_bucket_t *bucket;
33
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000034 if (!ino_dev_hashtable)
35 return NULL;
36
Eric Andersenaad1a882001-03-16 22:47:14 +000037 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
38 while (bucket != NULL) {
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000039 if ((bucket->ino == statbuf->st_ino)
40 && (bucket->dev == statbuf->st_dev)
41 ) {
42 return bucket->name;
43 }
44 bucket = bucket->next;
Eric Andersenaad1a882001-03-16 22:47:14 +000045 }
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000046 return NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000047}
48
49/* Add statbuf to statbuf hash table */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000050void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
Eric Andersenaad1a882001-03-16 22:47:14 +000051{
52 int i;
Eric Andersenaad1a882001-03-16 22:47:14 +000053 ino_dev_hashtable_bucket_t *bucket;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Eric Andersenaad1a882001-03-16 22:47:14 +000055 i = hash_inode(statbuf->st_ino);
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000056 if (!name)
57 name = "";
58 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
Eric Andersenaad1a882001-03-16 22:47:14 +000059 bucket->ino = statbuf->st_ino;
60 bucket->dev = statbuf->st_dev;
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000061 strcpy(bucket->name, name);
62
63 if (!ino_dev_hashtable)
64 ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
65
Eric Andersenaad1a882001-03-16 22:47:14 +000066 bucket->next = ino_dev_hashtable[i];
67 ino_dev_hashtable[i] = bucket;
68}
69
Denis Vlasenko618a3022008-11-11 21:15:56 +000070#if ENABLE_DU || ENABLE_FEATURE_CLEAN_UP
Eric Andersenaad1a882001-03-16 22:47:14 +000071/* Clear statbuf hash table */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000072void FAST_FUNC reset_ino_dev_hashtable(void)
Eric Andersenaad1a882001-03-16 22:47:14 +000073{
maxwen27116ba2015-08-14 21:41:28 +020074 unsigned i;
Eric Andersenaad1a882001-03-16 22:47:14 +000075 ino_dev_hashtable_bucket_t *bucket;
76
Bernhard Reutner-Fischerbdd253e2007-04-05 09:21:24 +000077 for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) {
Eric Andersenaad1a882001-03-16 22:47:14 +000078 while (ino_dev_hashtable[i] != NULL) {
79 bucket = ino_dev_hashtable[i]->next;
80 free(ino_dev_hashtable[i]);
81 ino_dev_hashtable[i] = bucket;
82 }
83 }
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000084 free(ino_dev_hashtable);
85 ino_dev_hashtable = NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000086}
Eric Andersen8876fb22003-06-20 09:01:58 +000087#endif