Import libarchive 2.1.9.
[dragonfly.git] / contrib / libarchive-2.1 / libarchive / archive_write_disk_set_standard_lookup.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_disk_set_standard_lookup.c,v 1.2 2007/04/20 15:32:13 kientzle Exp $");
28
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_GRP_H
33 #include <grp.h>
34 #endif
35 #ifdef HAVE_PWD_H
36 #include <pwd.h>
37 #endif
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44
45 #include "archive.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
48 #include "archive_write_disk_private.h"
49
50 struct bucket {
51         char    *name;
52         int      hash;
53         id_t     id;
54 };
55
56 static const size_t cache_size = 127;
57 static unsigned int     hash(const char *);
58 static gid_t    lookup_gid(void *, const char *uname, gid_t);
59 static uid_t    lookup_uid(void *, const char *uname, uid_t);
60 static void     cleanup(void *);
61
62 /*
63  * Installs functions that use getpwnam()/getgrnam()---along with
64  * a simple cache to accelerate such lookups---into the archive_write_disk
65  * object.  This is in a separate file because getpwnam()/getgrnam()
66  * can pull in a LOT of library code (including NIS/LDAP functions, which
67  * pull in DNS resolveers, etc).  This can easily top 500kB, which makes
68  * it inappropriate for some space-constrained applications.
69  *
70  * Applications that are size-sensitive may want to just use the
71  * real default functions (defined in archive_write_disk.c) that just
72  * use the uid/gid without the lookup.  Or define your own custom functions
73  * if you prefer.
74  *
75  * TODO: Replace these hash tables with simpler move-to-front LRU
76  * lists with a bounded size (128 items?).  The hash is a bit faster,
77  * but has a bad pathology in which it thrashes a single bucket.  Even
78  * walking a list of 128 items is a lot faster than calling
79  * getpwnam()!
80  */
81 int
82 archive_write_disk_set_standard_lookup(struct archive *a)
83 {
84         struct bucket *ucache = malloc(sizeof(struct bucket[cache_size]));
85         struct bucket *gcache = malloc(sizeof(struct bucket[cache_size]));
86         memset(ucache, 0, sizeof(struct bucket[cache_size]));
87         memset(gcache, 0, sizeof(struct bucket[cache_size]));
88         archive_write_disk_set_group_lookup(a, gcache, lookup_gid, cleanup);
89         archive_write_disk_set_user_lookup(a, ucache, lookup_uid, cleanup);
90         return (ARCHIVE_OK);
91 }
92
93 static gid_t
94 lookup_gid(void *private_data, const char *gname, gid_t gid)
95 {
96         int h;
97         struct bucket *b;
98         struct bucket *gcache = (struct bucket *)private_data;
99
100         /* If no gname, just use the gid provided. */
101         if (gname == NULL || *gname == '\0')
102                 return (gid);
103
104         /* Try to find gname in the cache. */
105         h = hash(gname);
106         b = &gcache[h % cache_size ];
107         if (b->name != NULL && b->hash == h && strcmp(gname, b->name) == 0)
108                 return ((gid_t)b->id);
109
110         /* Free the cache slot for a new entry. */
111         if (b->name != NULL)
112                 free(b->name);
113         b->name = strdup(gname);
114         /* Note: If strdup fails, that's okay; we just won't cache. */
115         b->hash = h;
116 #if HAVE_GRP_H
117         {
118                 struct group    *grent = getgrnam(gname);
119                 if (grent != NULL)
120                         gid = grent->gr_gid;
121         }
122 #elif _WIN32
123         /* TODO: do a gname->gid lookup for Windows. */
124 #endif
125         b->id = gid;
126
127         return (gid);
128 }
129
130 static uid_t
131 lookup_uid(void *private_data, const char *uname, uid_t uid)
132 {
133         int h;
134         struct bucket *b;
135         struct bucket *ucache = (struct bucket *)private_data;
136
137         /* If no uname, just use the uid provided. */
138         if (uname == NULL || *uname == '\0')
139                 return (uid);
140
141         /* Try to find uname in the cache. */
142         h = hash(uname);
143         b = &ucache[h % cache_size ];
144         if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
145                 return ((uid_t)b->id);
146
147         /* Free the cache slot for a new entry. */
148         if (b->name != NULL)
149                 free(b->name);
150         b->name = strdup(uname);
151         /* Note: If strdup fails, that's okay; we just won't cache. */
152         b->hash = h;
153 #if HAVE_PWD_H
154         {
155                 struct passwd   *pwent = getpwnam(uname);
156                 if (pwent != NULL)
157                         uid = pwent->pw_uid;
158         }
159 #elif _WIN32
160         /* TODO: do a uname->uid lookup for Windows. */
161 #endif
162         b->id = uid;
163
164         return (uid);
165 }
166
167 static void
168 cleanup(void *private)
169 {
170         size_t i;
171         struct bucket *cache = (struct bucket *)private;
172
173         for (i = 0; i < cache_size; i++)
174                 free(cache[i].name);
175         free(cache);
176 }
177
178
179 static unsigned int
180 hash(const char *p)
181 {
182         /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
183            as used by ELF for hashing function names. */
184         unsigned g, h = 0;
185         while (*p != '\0') {
186                 h = ( h << 4 ) + *p++;
187                 if (( g = h & 0xF0000000 )) {
188                         h ^= g >> 24;
189                         h &= 0x0FFFFFFF;
190                 }
191         }
192         return h;
193 }