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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 12 | typedef struct ino_dev_hash_bucket_struct { |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 13 | ino_t ino; |
| 14 | dev_t dev; |
Denys Vlasenko | 83bc433 | 2014-02-25 15:27:58 +0100 | [diff] [blame] | 15 | /* |
| 16 | * Above fields can be 64-bit, while pointer may be 32-bit. |
| 17 | * Putting "next" field here may reduce size of this struct: |
| 18 | */ |
| 19 | struct ino_dev_hash_bucket_struct *next; |
| 20 | /* |
| 21 | * Reportedly, on cramfs a file and a dir can have same ino. |
| 22 | * Need to also remember "file/dir" bit: |
| 23 | */ |
| 24 | char isdir; /* bool */ |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 25 | char name[1]; |
| 26 | } ino_dev_hashtable_bucket_t; |
| 27 | |
Denys Vlasenko | 83bc433 | 2014-02-25 15:27:58 +0100 | [diff] [blame] | 28 | #define HASH_SIZE 311u /* Should be prime */ |
| 29 | #define hash_inode(i) ((unsigned)(i) % HASH_SIZE) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 31 | /* array of [HASH_SIZE] elements */ |
| 32 | static ino_dev_hashtable_bucket_t **ino_dev_hashtable; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 35 | * Return name if statbuf->st_ino && statbuf->st_dev are recorded in |
| 36 | * ino_dev_hashtable, else return NULL |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 38 | char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | { |
| 40 | ino_dev_hashtable_bucket_t *bucket; |
| 41 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 42 | if (!ino_dev_hashtable) |
| 43 | return NULL; |
| 44 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
| 46 | while (bucket != NULL) { |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 47 | if ((bucket->ino == statbuf->st_ino) |
| 48 | && (bucket->dev == statbuf->st_dev) |
Denys Vlasenko | 83bc433 | 2014-02-25 15:27:58 +0100 | [diff] [blame] | 49 | && (bucket->isdir == !!S_ISDIR(statbuf->st_mode)) |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 50 | ) { |
| 51 | return bucket->name; |
| 52 | } |
| 53 | bucket = bucket->next; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 54 | } |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 55 | return NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Add statbuf to statbuf hash table */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 59 | void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | { |
| 61 | int i; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 62 | ino_dev_hashtable_bucket_t *bucket; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 64 | if (!name) |
| 65 | name = ""; |
| 66 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name)); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 67 | bucket->ino = statbuf->st_ino; |
| 68 | bucket->dev = statbuf->st_dev; |
Denys Vlasenko | 83bc433 | 2014-02-25 15:27:58 +0100 | [diff] [blame] | 69 | bucket->isdir = !!S_ISDIR(statbuf->st_mode); |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 70 | strcpy(bucket->name, name); |
| 71 | |
| 72 | if (!ino_dev_hashtable) |
| 73 | ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable)); |
| 74 | |
Denys Vlasenko | 83bc433 | 2014-02-25 15:27:58 +0100 | [diff] [blame] | 75 | i = hash_inode(statbuf->st_ino); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 76 | bucket->next = ino_dev_hashtable[i]; |
| 77 | ino_dev_hashtable[i] = bucket; |
| 78 | } |
| 79 | |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 80 | #if ENABLE_DU || ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | /* Clear statbuf hash table */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 82 | void FAST_FUNC reset_ino_dev_hashtable(void) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 83 | { |
| 84 | int i; |
Denys Vlasenko | 12916b9 | 2014-02-25 15:09:01 +0100 | [diff] [blame] | 85 | ino_dev_hashtable_bucket_t *bucket, *next; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | |
Denys Vlasenko | 12916b9 | 2014-02-25 15:09:01 +0100 | [diff] [blame] | 87 | if (!ino_dev_hashtable) |
| 88 | return; |
| 89 | |
| 90 | for (i = 0; i < HASH_SIZE; i++) { |
| 91 | bucket = ino_dev_hashtable[i]; |
| 92 | |
| 93 | while (bucket != NULL) { |
| 94 | next = bucket->next; |
| 95 | free(bucket); |
| 96 | bucket = next; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 99 | free(ino_dev_hashtable); |
| 100 | ino_dev_hashtable = NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 102 | #endif |