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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Eric Andersen | 08ff8a4 | 2001-03-23 17:02:05 +0000 | [diff] [blame] | 13 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
| 15 | |
| 16 | #define HASH_SIZE 311 /* Should be prime */ |
| 17 | #define hash_inode(i) ((i) % HASH_SIZE) |
| 18 | |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 19 | typedef struct ino_dev_hash_bucket_struct { |
| 20 | struct ino_dev_hash_bucket_struct *next; |
| 21 | ino_t ino; |
| 22 | dev_t dev; |
| 23 | char name[1]; |
| 24 | } ino_dev_hashtable_bucket_t; |
| 25 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE]; |
| 27 | |
| 28 | /* |
| 29 | * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in |
| 30 | * `ino_dev_hashtable', else return 0 |
| 31 | * |
| 32 | * If NAME is a non-NULL pointer to a character pointer, and there is |
| 33 | * a match, then set *NAME to the value of the name slot in that |
| 34 | * bucket. |
| 35 | */ |
| 36 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name) |
| 37 | { |
| 38 | ino_dev_hashtable_bucket_t *bucket; |
| 39 | |
| 40 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
| 41 | while (bucket != NULL) { |
| 42 | if ((bucket->ino == statbuf->st_ino) && |
| 43 | (bucket->dev == statbuf->st_dev)) |
| 44 | { |
| 45 | if (name) *name = bucket->name; |
| 46 | return 1; |
| 47 | } |
| 48 | bucket = bucket->next; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /* Add statbuf to statbuf hash table */ |
| 54 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
| 55 | { |
| 56 | int i; |
| 57 | size_t s; |
| 58 | ino_dev_hashtable_bucket_t *bucket; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 59 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | i = hash_inode(statbuf->st_ino); |
| 61 | s = name ? strlen(name) : 0; |
| 62 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s); |
| 63 | bucket->ino = statbuf->st_ino; |
| 64 | bucket->dev = statbuf->st_dev; |
| 65 | if (name) |
| 66 | strcpy(bucket->name, name); |
| 67 | else |
| 68 | bucket->name[0] = '\0'; |
| 69 | bucket->next = ino_dev_hashtable[i]; |
| 70 | ino_dev_hashtable[i] = bucket; |
| 71 | } |
| 72 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 73 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 74 | /* Clear statbuf hash table */ |
| 75 | void reset_ino_dev_hashtable(void) |
| 76 | { |
| 77 | int i; |
| 78 | ino_dev_hashtable_bucket_t *bucket; |
| 79 | |
| 80 | for (i = 0; i < HASH_SIZE; i++) { |
| 81 | 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 | } |
| 87 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 88 | #endif |