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