blob: 78c1b0f9e929865ea9c720d3c5f30a0d9ac8fbb5 [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
11#include <stdio.h>
12#include <stdlib.h>
Eric Andersen08ff8a42001-03-23 17:02:05 +000013#include <string.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000014#include "libbb.h"
15
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000016typedef struct ino_dev_hash_bucket_struct {
17 struct ino_dev_hash_bucket_struct *next;
18 ino_t ino;
19 dev_t dev;
20 char name[1];
21} ino_dev_hashtable_bucket_t;
22
Eric Andersenaad1a882001-03-16 22:47:14 +000023#define HASH_SIZE 311 /* Should be prime */
24#define hash_inode(i) ((i) % HASH_SIZE)
25
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000026/* array of [HASH_SIZE] elements */
27static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
Eric Andersenaad1a882001-03-16 22:47:14 +000028
29/*
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000030 * Return name if statbuf->st_ino && statbuf->st_dev are recorded in
31 * ino_dev_hashtable, else return NULL
Eric Andersenaad1a882001-03-16 22:47:14 +000032 */
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000033char *is_in_ino_dev_hashtable(const struct stat *statbuf)
Eric Andersenaad1a882001-03-16 22:47:14 +000034{
35 ino_dev_hashtable_bucket_t *bucket;
36
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000037 if (!ino_dev_hashtable)
38 return NULL;
39
Eric Andersenaad1a882001-03-16 22:47:14 +000040 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
41 while (bucket != NULL) {
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000042 if ((bucket->ino == statbuf->st_ino)
43 && (bucket->dev == statbuf->st_dev)
44 ) {
45 return bucket->name;
46 }
47 bucket = bucket->next;
Eric Andersenaad1a882001-03-16 22:47:14 +000048 }
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000049 return NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000050}
51
52/* Add statbuf to statbuf hash table */
53void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
54{
55 int i;
Eric Andersenaad1a882001-03-16 22:47:14 +000056 ino_dev_hashtable_bucket_t *bucket;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057
Eric Andersenaad1a882001-03-16 22:47:14 +000058 i = hash_inode(statbuf->st_ino);
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000059 if (!name)
60 name = "";
61 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
Eric Andersenaad1a882001-03-16 22:47:14 +000062 bucket->ino = statbuf->st_ino;
63 bucket->dev = statbuf->st_dev;
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000064 strcpy(bucket->name, name);
65
66 if (!ino_dev_hashtable)
67 ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
68
Eric Andersenaad1a882001-03-16 22:47:14 +000069 bucket->next = ino_dev_hashtable[i];
70 ino_dev_hashtable[i] = bucket;
71}
72
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000073#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaad1a882001-03-16 22:47:14 +000074/* Clear statbuf hash table */
75void reset_ino_dev_hashtable(void)
76{
77 int i;
78 ino_dev_hashtable_bucket_t *bucket;
79
Bernhard Reutner-Fischerbdd253e2007-04-05 09:21:24 +000080 for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) {
Eric Andersenaad1a882001-03-16 22:47:14 +000081 while (ino_dev_hashtable[i] != NULL) {
82 bucket = ino_dev_hashtable[i]->next;
83 free(ino_dev_hashtable[i]);
84 ino_dev_hashtable[i] = bucket;
85 }
86 }
Denis Vlasenko6ef06ee2007-03-14 22:06:01 +000087 free(ino_dev_hashtable);
88 ino_dev_hashtable = NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000089}
Bernhard Reutner-Fischerbdd253e2007-04-05 09:21:24 +000090#else
91void reset_ino_dev_hashtable(void);
Eric Andersen8876fb22003-06-20 09:01:58 +000092#endif