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 | |
| 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
| -rw-r--r-- | archival/tar.c | 9 | ||||
| -rw-r--r-- | include/libbb.h | 1 | ||||
| -rw-r--r-- | libbb/procps.c | 66 |
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); | |||
| 567 | pid_t *pidlist_reverse(pid_t *pidList); | 567 | pid_t *pidlist_reverse(pid_t *pidList); |
| 568 | void clear_username_cache(void); | 568 | void clear_username_cache(void); |
| 569 | const char* get_cached_username(uid_t uid); | 569 | const char* get_cached_username(uid_t uid); |
| 570 | const char* get_cached_groupname(gid_t gid); | ||
| 570 | 571 | ||
| 571 | 572 | ||
| 572 | extern const char bb_uuenc_tbl_base64[]; | 573 | extern 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 | ||
| 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 | ||
