diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-24 14:57:31 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-24 14:57:31 +0000 |
commit | aa2fdd67f50302aa6d6f3422f57ce5d42333c1f8 (patch) | |
tree | 45516e35a895fa4eccfb36ff0d8c1e29714a2c41 /libbb | |
parent | 708d74b5fe4e29c55f76b79bc71aa6ae532d38fe (diff) | |
download | busybox-w32-aa2fdd67f50302aa6d6f3422f57ce5d42333c1f8.tar.gz busybox-w32-aa2fdd67f50302aa6d6f3422f57ce5d42333c1f8.tar.bz2 busybox-w32-aa2fdd67f50302aa6d6f3422f57ce5d42333c1f8.zip |
tar: cache [ug]id->username/groupname mappings. Cuts down amount
of open/read/close of /etc/passwd and /etc/group
dramatically (we were rereading those for each untarred file!!!)
git-svn-id: svn://busybox.net/trunk/busybox@16654 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/procps.c | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/libbb/procps.c b/libbb/procps.c index dee5638e4..52203ee9a 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -11,32 +11,62 @@ | |||
11 | #include "libbb.h" | 11 | #include "libbb.h" |
12 | 12 | ||
13 | 13 | ||
14 | typedef struct { | 14 | typedef struct unsigned_to_name_map_t { |
15 | uid_t uid; | 15 | unsigned id; |
16 | char username[12]; | 16 | char name[12]; |
17 | } user_map_t; | 17 | } unsigned_to_name_map_t; |
18 | 18 | ||
19 | static user_map_t *username_cache; | 19 | typedef struct cache_t { |
20 | static int username_cache_size; | 20 | unsigned_to_name_map_t *cache; |
21 | int size; | ||
22 | } cache_t; | ||
21 | 23 | ||
24 | static cache_t username, groupname; | ||
25 | |||
26 | static void clear_cache(cache_t *cp) | ||
27 | { | ||
28 | free(cp->cache); | ||
29 | cp->cache = NULL; | ||
30 | cp->size = 0; | ||
31 | } | ||
22 | void clear_username_cache(void) | 32 | void clear_username_cache(void) |
23 | { | 33 | { |
24 | free(username_cache); | 34 | clear_cache(&username); |
25 | username_cache = NULL; | 35 | clear_cache(&groupname); |
26 | username_cache_size = 0; | ||
27 | } | 36 | } |
28 | 37 | ||
29 | const char* get_cached_username(uid_t uid) | 38 | /* Returns -N-1 if not found. */ |
39 | /* cp->cache[N] is allocated and must be filled in this case */ | ||
40 | static int get_cached(cache_t *cp, unsigned id) | ||
30 | { | 41 | { |
31 | int i; | 42 | int i; |
32 | for (i = 0; i < username_cache_size; i++) | 43 | for (i = 0; i < cp->size; i++) |
33 | if (username_cache[i].uid == uid) | 44 | if (cp->cache[i].id == id) |
34 | return username_cache[i].username; | 45 | return i; |
35 | i = username_cache_size++; | 46 | i = cp->size++; |
36 | username_cache = xrealloc(username_cache, username_cache_size * sizeof(*username_cache)); | 47 | cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); |
37 | username_cache[i].uid = uid; | 48 | cp->cache[i++].id = id; |
38 | bb_getpwuid(username_cache[i].username, uid, sizeof(username_cache[i].username)); | 49 | return -i; |
39 | return username_cache[i].username; | 50 | } |
51 | const char* get_cached_username(uid_t uid) | ||
52 | { | ||
53 | int i = get_cached(&username, uid); | ||
54 | if (i < 0) { | ||
55 | i = -i - 1; | ||
56 | bb_getpwuid(username.cache[i].name, uid, | ||
57 | sizeof(username.cache[i].name)); | ||
58 | } | ||
59 | return username.cache[i].name; | ||
60 | } | ||
61 | const char* get_cached_groupname(uid_t uid) | ||
62 | { | ||
63 | int i = get_cached(&groupname, uid); | ||
64 | if (i < 0) { | ||
65 | i = -i - 1; | ||
66 | bb_getgrgid(groupname.cache[i].name, uid, | ||
67 | sizeof(groupname.cache[i].name)); | ||
68 | } | ||
69 | return username.cache[i].name; | ||
40 | } | 70 | } |
41 | 71 | ||
42 | 72 | ||