blob: 52c54cdc17422e9f91153e57e31013aa97dcff6e [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) many different people. If you wrote this, please
6 * acknowledge your work.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +000013 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
Eric Andersenaad1a882001-03-16 22:47:14 +000015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Eric Andersenbdfd0d72001-10-24 05:00:29 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
Eric Andersenaad1a882001-03-16 22:47:14 +000022 */
23
24#include <stdio.h>
25#include <stdlib.h>
Eric Andersen08ff8a42001-03-23 17:02:05 +000026#include <string.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000027#include "libbb.h"
28
29#define HASH_SIZE 311 /* Should be prime */
30#define hash_inode(i) ((i) % HASH_SIZE)
31
32static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
33
34/*
35 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
36 * `ino_dev_hashtable', else return 0
37 *
38 * If NAME is a non-NULL pointer to a character pointer, and there is
39 * a match, then set *NAME to the value of the name slot in that
40 * bucket.
41 */
42int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
43{
44 ino_dev_hashtable_bucket_t *bucket;
45
46 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
47 while (bucket != NULL) {
48 if ((bucket->ino == statbuf->st_ino) &&
49 (bucket->dev == statbuf->st_dev))
50 {
51 if (name) *name = bucket->name;
52 return 1;
53 }
54 bucket = bucket->next;
55 }
56 return 0;
57}
58
59/* Add statbuf to statbuf hash table */
60void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
61{
62 int i;
63 size_t s;
64 ino_dev_hashtable_bucket_t *bucket;
65
66 i = hash_inode(statbuf->st_ino);
67 s = name ? strlen(name) : 0;
68 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
69 bucket->ino = statbuf->st_ino;
70 bucket->dev = statbuf->st_dev;
71 if (name)
72 strcpy(bucket->name, name);
73 else
74 bucket->name[0] = '\0';
75 bucket->next = ino_dev_hashtable[i];
76 ino_dev_hashtable[i] = bucket;
77}
78
79/* Clear statbuf hash table */
80void reset_ino_dev_hashtable(void)
81{
82 int i;
83 ino_dev_hashtable_bucket_t *bucket;
84
85 for (i = 0; i < HASH_SIZE; i++) {
86 while (ino_dev_hashtable[i] != NULL) {
87 bucket = ino_dev_hashtable[i]->next;
88 free(ino_dev_hashtable[i]);
89 ino_dev_hashtable[i] = bucket;
90 }
91 }
92}
93
94
95/* END CODE */
96/*
97Local Variables:
98c-file-style: "linux"
99c-basic-offset: 4
100tab-width: 4
101End:
102*/