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 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 16 | typedef 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 Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | #define HASH_SIZE 311 /* Should be prime */ |
| 24 | #define hash_inode(i) ((i) % HASH_SIZE) |
| 25 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 26 | /* array of [HASH_SIZE] elements */ |
| 27 | static ino_dev_hashtable_bucket_t **ino_dev_hashtable; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 30 | * Return name if statbuf->st_ino && statbuf->st_dev are recorded in |
| 31 | * ino_dev_hashtable, else return NULL |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 32 | */ |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 33 | char *is_in_ino_dev_hashtable(const struct stat *statbuf) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | { |
| 35 | ino_dev_hashtable_bucket_t *bucket; |
| 36 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 37 | if (!ino_dev_hashtable) |
| 38 | return NULL; |
| 39 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
| 41 | while (bucket != NULL) { |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 42 | if ((bucket->ino == statbuf->st_ino) |
| 43 | && (bucket->dev == statbuf->st_dev) |
| 44 | ) { |
| 45 | return bucket->name; |
| 46 | } |
| 47 | bucket = bucket->next; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 48 | } |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 49 | return NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* Add statbuf to statbuf hash table */ |
| 53 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
| 54 | { |
| 55 | int i; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 56 | ino_dev_hashtable_bucket_t *bucket; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 57 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 58 | i = hash_inode(statbuf->st_ino); |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 59 | if (!name) |
| 60 | name = ""; |
| 61 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name)); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 62 | bucket->ino = statbuf->st_ino; |
| 63 | bucket->dev = statbuf->st_dev; |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 64 | strcpy(bucket->name, name); |
| 65 | |
| 66 | if (!ino_dev_hashtable) |
| 67 | ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable)); |
| 68 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 69 | bucket->next = ino_dev_hashtable[i]; |
| 70 | ino_dev_hashtable[i] = bucket; |
| 71 | } |
| 72 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 73 | #if ENABLE_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 | |
Bernhard Reutner-Fischer | bdd253e | 2007-04-05 09:21:24 +0000 | [diff] [blame^] | 80 | for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 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 | } |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 87 | free(ino_dev_hashtable); |
| 88 | ino_dev_hashtable = NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | } |
Bernhard Reutner-Fischer | bdd253e | 2007-04-05 09:21:24 +0000 | [diff] [blame^] | 90 | #else |
| 91 | void reset_ino_dev_hashtable(void); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 92 | #endif |