Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 5 | * Copyright (C) many different people. |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * If you wrote this, please acknowledge your work. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
| 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 Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | * 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 Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | * USA |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Eric Andersen | 08ff8a4 | 2001-03-23 17:02:05 +0000 | [diff] [blame] | 26 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
| 28 | |
| 29 | #define HASH_SIZE 311 /* Should be prime */ |
| 30 | #define hash_inode(i) ((i) % HASH_SIZE) |
| 31 | |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 32 | typedef struct ino_dev_hash_bucket_struct { |
| 33 | struct ino_dev_hash_bucket_struct *next; |
| 34 | ino_t ino; |
| 35 | dev_t dev; |
| 36 | char name[1]; |
| 37 | } ino_dev_hashtable_bucket_t; |
| 38 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE]; |
| 40 | |
| 41 | /* |
| 42 | * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in |
| 43 | * `ino_dev_hashtable', else return 0 |
| 44 | * |
| 45 | * If NAME is a non-NULL pointer to a character pointer, and there is |
| 46 | * a match, then set *NAME to the value of the name slot in that |
| 47 | * bucket. |
| 48 | */ |
| 49 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name) |
| 50 | { |
| 51 | ino_dev_hashtable_bucket_t *bucket; |
| 52 | |
| 53 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
| 54 | while (bucket != NULL) { |
| 55 | if ((bucket->ino == statbuf->st_ino) && |
| 56 | (bucket->dev == statbuf->st_dev)) |
| 57 | { |
| 58 | if (name) *name = bucket->name; |
| 59 | return 1; |
| 60 | } |
| 61 | bucket = bucket->next; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* Add statbuf to statbuf hash table */ |
| 67 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
| 68 | { |
| 69 | int i; |
| 70 | size_t s; |
| 71 | ino_dev_hashtable_bucket_t *bucket; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 72 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 73 | i = hash_inode(statbuf->st_ino); |
| 74 | s = name ? strlen(name) : 0; |
| 75 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s); |
| 76 | bucket->ino = statbuf->st_ino; |
| 77 | bucket->dev = statbuf->st_dev; |
| 78 | if (name) |
| 79 | strcpy(bucket->name, name); |
| 80 | else |
| 81 | bucket->name[0] = '\0'; |
| 82 | bucket->next = ino_dev_hashtable[i]; |
| 83 | ino_dev_hashtable[i] = bucket; |
| 84 | } |
| 85 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 87 | /* Clear statbuf hash table */ |
| 88 | void reset_ino_dev_hashtable(void) |
| 89 | { |
| 90 | int i; |
| 91 | ino_dev_hashtable_bucket_t *bucket; |
| 92 | |
| 93 | for (i = 0; i < HASH_SIZE; i++) { |
| 94 | while (ino_dev_hashtable[i] != NULL) { |
| 95 | bucket = ino_dev_hashtable[i]->next; |
| 96 | free(ino_dev_hashtable[i]); |
| 97 | ino_dev_hashtable[i] = bucket; |
| 98 | } |
| 99 | } |
| 100 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 101 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /* END CODE */ |
| 105 | /* |
| 106 | Local Variables: |
| 107 | c-file-style: "linux" |
| 108 | c-basic-offset: 4 |
| 109 | tab-width: 4 |
| 110 | End: |
| 111 | */ |