]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_write_disk_set_standard_lookup.c
This commit was generated by cvs2svn to compensate for changes in r169765,
[FreeBSD/FreeBSD.git] / lib / 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$");
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_ACL_H
33 #include <sys/acl.h>
34 #endif
35 #ifdef HAVE_ATTR_XATTR_H
36 #include <attr/xattr.h>
37 #endif
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 #include <sys/stat.h>
43 #endif
44 #ifdef HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47
48 #ifdef HAVE_ERRNO_H
49 #include <errno.h>
50 #endif
51 #ifdef HAVE_GRP_H
52 #include <grp.h>
53 #endif
54 #ifdef HAVE_PWD_H
55 #include <pwd.h>
56 #endif
57 #ifdef HAVE_STDLIB_H
58 #include <stdlib.h>
59 #endif
60 #ifdef HAVE_STRING_H
61 #include <string.h>
62 #endif
63
64 #include "archive.h"
65 #include "archive_private.h"
66 #include "archive_read_private.h"
67 #include "archive_write_disk_private.h"
68
69 struct bucket {
70         char    *name;
71         int      hash;
72         id_t     id;
73 };
74
75 static const size_t cache_size = 127;
76 static unsigned int     hash(const char *);
77 static gid_t    lookup_gid(void *, const char *uname, gid_t);
78 static uid_t    lookup_uid(void *, const char *uname, uid_t);
79 static void     cleanup(void *);
80
81 /*
82  * Installs functions that use getpwnam()/getgrnam()---along with
83  * a simple cache to accelerate such lookups---into the archive_write_disk
84  * object.  This is in a separate file because getpwnam()/getgrnam()
85  * can pull in a LOT of library code (including NIS/LDAP functions, which
86  * pull in DNS resolveers, etc).  This can easily top 500kB, which makes
87  * it inappropriate for some space-constrained applications.
88  *
89  * Applications that are size-sensitive may want to just use the
90  * real default functions (defined in archive_write_disk.c) that just
91  * use the uid/gid without the lookup.  Or define your own custom functions
92  * if you prefer.
93  *
94  * TODO: Replace these hash tables with simpler move-to-front LRU
95  * lists with a bounded size (128 items?).  The hash is a bit faster,
96  * but has a bad pathology in which it thrashes a single bucket.  Even
97  * walking a list of 128 items is a lot faster than calling
98  * getpwnam()!
99  */
100 int
101 archive_write_disk_set_standard_lookup(struct archive *a)
102 {
103         struct bucket *ucache = malloc(sizeof(struct bucket[cache_size]));
104         struct bucket *gcache = malloc(sizeof(struct bucket[cache_size]));
105         memset(ucache, 0, sizeof(struct bucket[cache_size]));
106         memset(gcache, 0, sizeof(struct bucket[cache_size]));
107         archive_write_disk_set_group_lookup(a, gcache, lookup_gid, cleanup);
108         archive_write_disk_set_user_lookup(a, ucache, lookup_uid, cleanup);
109         return (ARCHIVE_OK);
110 }
111
112 static gid_t
113 lookup_gid(void *private_data, const char *gname, gid_t gid)
114 {
115         int h;
116         struct bucket *b;
117         struct bucket *gcache = (struct bucket *)private_data;
118
119         /* If no gname, just use the gid provided. */
120         if (gname == NULL || *gname == '\0')
121                 return (gid);
122
123         /* Try to find gname in the cache. */
124         h = hash(gname);
125         b = &gcache[h % cache_size ];
126         if (b->name != NULL && b->hash == h && strcmp(gname, b->name) == 0)
127                 return ((gid_t)b->id);
128
129         /* Free the cache slot for a new entry. */
130         if (b->name != NULL)
131                 free(b->name);
132         b->name = strdup(gname);
133         /* Note: If strdup fails, that's okay; we just won't cache. */
134         b->hash = h;
135 #if HAVE_GRP_H
136         {
137                 struct group    *grent = getgrnam(gname);
138                 if (grent != NULL)
139                         gid = grent->gr_gid;
140         }
141 #elif _WIN32
142         /* TODO: do a gname->gid lookup for Windows. */
143 #endif
144         b->id = gid;
145
146         return (gid);
147 }
148
149 static uid_t
150 lookup_uid(void *private_data, const char *uname, uid_t uid)
151 {
152         int h;
153         struct bucket *b;
154         struct bucket *ucache = (struct bucket *)private_data;
155
156         /* If no uname, just use the uid provided. */
157         if (uname == NULL || *uname == '\0')
158                 return (uid);
159
160         /* Try to find uname in the cache. */
161         h = hash(uname);
162         b = &ucache[h % cache_size ];
163         if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
164                 return ((uid_t)b->id);
165
166         /* Free the cache slot for a new entry. */
167         if (b->name != NULL)
168                 free(b->name);
169         b->name = strdup(uname);
170         /* Note: If strdup fails, that's okay; we just won't cache. */
171         b->hash = h;
172 #if HAVE_PWD_H
173         {
174                 struct passwd   *pwent = getpwnam(uname);
175                 if (pwent != NULL)
176                         uid = pwent->pw_uid;
177         }
178 #elif _WIN32
179         /* TODO: do a uname->uid lookup for Windows. */
180 #endif
181         b->id = uid;
182
183         return (uid);
184 }
185
186 static void
187 cleanup(void *private)
188 {
189         size_t i;
190         struct bucket *cache = (struct bucket *)private;
191
192         for (i = 0; i < cache_size; i++)
193                 free(cache[i].name);
194         free(cache);
195 }
196
197
198 static unsigned int
199 hash(const char *p)
200 {
201         /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
202            as used by ELF for hashing function names. */
203         unsigned g, h = 0;
204         while (*p != '\0') {
205                 h = ( h << 4 ) + *p++;
206                 if (( g = h & 0xF0000000 )) {
207                         h ^= g >> 24;
208                         h &= 0x0FFFFFFF;
209                 }
210         }
211         return h;
212 }