aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archival/tar.c9
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/procps.c66
3 files changed, 52 insertions, 24 deletions
diff --git a/archival/tar.c b/archival/tar.c
index 99c4adb6a..51d34ea19 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -211,12 +211,9 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
211 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); 211 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
212 strcpy(header.magic, "ustar "); 212 strcpy(header.magic, "ustar ");
213 213
214 /* Enter the user and group names (default to root if it fails) */ 214 /* Enter the user and group names */
215//cache!!! 215 safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
216 if (bb_getpwuid(header.uname, statbuf->st_uid, sizeof(header.uname)) == NULL) 216 safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname));
217 strcpy(header.uname, "root");
218 if (bb_getgrgid(header.gname, statbuf->st_gid, sizeof(header.gname)) == NULL)
219 strcpy(header.gname, "root");
220 217
221 if (tbInfo->hlInfo) { 218 if (tbInfo->hlInfo) {
222 /* This is a hard link */ 219 /* This is a hard link */
diff --git a/include/libbb.h b/include/libbb.h
index 582e34f52..99a1928df 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -567,6 +567,7 @@ pid_t *find_pid_by_name(const char* procName);
567pid_t *pidlist_reverse(pid_t *pidList); 567pid_t *pidlist_reverse(pid_t *pidList);
568void clear_username_cache(void); 568void clear_username_cache(void);
569const char* get_cached_username(uid_t uid); 569const char* get_cached_username(uid_t uid);
570const char* get_cached_groupname(gid_t gid);
570 571
571 572
572extern const char bb_uuenc_tbl_base64[]; 573extern const char bb_uuenc_tbl_base64[];
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
14typedef struct { 14typedef 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
19static user_map_t *username_cache; 19typedef struct cache_t {
20static int username_cache_size; 20 unsigned_to_name_map_t *cache;
21 int size;
22} cache_t;
21 23
24static cache_t username, groupname;
25
26static void clear_cache(cache_t *cp)
27{
28 free(cp->cache);
29 cp->cache = NULL;
30 cp->size = 0;
31}
22void clear_username_cache(void) 32void 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
29const 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 */
40static 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}
51const 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}
61const 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