2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_disk_set_standard_lookup.c 201083 2009-12-28 02:09:57Z kientzle $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
49 #include "archive_private.h"
50 #include "archive_read_private.h"
51 #include "archive_write_disk_private.h"
59 static const size_t cache_size
= 127;
60 static unsigned int hash(const char *);
61 static int64_t lookup_gid(void *, const char *uname
, int64_t);
62 static int64_t lookup_uid(void *, const char *uname
, int64_t);
63 static void cleanup(void *);
66 * Installs functions that use getpwnam()/getgrnam()---along with
67 * a simple cache to accelerate such lookups---into the archive_write_disk
68 * object. This is in a separate file because getpwnam()/getgrnam()
69 * can pull in a LOT of library code (including NIS/LDAP functions, which
70 * pull in DNS resolvers, etc). This can easily top 500kB, which makes
71 * it inappropriate for some space-constrained applications.
73 * Applications that are size-sensitive may want to just use the
74 * real default functions (defined in archive_write_disk.c) that just
75 * use the uid/gid without the lookup. Or define your own custom functions
78 * TODO: Replace these hash tables with simpler move-to-front LRU
79 * lists with a bounded size (128 items?). The hash is a bit faster,
80 * but has a bad pathology in which it thrashes a single bucket. Even
81 * walking a list of 128 items is a lot faster than calling
85 archive_write_disk_set_standard_lookup(struct archive
*a
)
87 struct bucket
*ucache
= malloc(cache_size
* sizeof(struct bucket
));
88 struct bucket
*gcache
= malloc(cache_size
* sizeof(struct bucket
));
89 if (ucache
== NULL
|| gcache
== NULL
) {
92 return (ARCHIVE_FATAL
);
94 memset(ucache
, 0, cache_size
* sizeof(struct bucket
));
95 memset(gcache
, 0, cache_size
* sizeof(struct bucket
));
96 archive_write_disk_set_group_lookup(a
, gcache
, lookup_gid
, cleanup
);
97 archive_write_disk_set_user_lookup(a
, ucache
, lookup_uid
, cleanup
);
102 lookup_gid(void *private_data
, const char *gname
, int64_t gid
)
106 struct bucket
*gcache
= (struct bucket
*)private_data
;
108 /* If no gname, just use the gid provided. */
109 if (gname
== NULL
|| *gname
== '\0')
112 /* Try to find gname in the cache. */
114 b
= &gcache
[h
% cache_size
];
115 if (b
->name
!= NULL
&& b
->hash
== h
&& strcmp(gname
, b
->name
) == 0)
116 return ((gid_t
)b
->id
);
118 /* Free the cache slot for a new entry. */
121 b
->name
= strdup(gname
);
122 /* Note: If strdup fails, that's okay; we just won't cache. */
128 size_t bufsize
= 128;
129 char *buffer
= _buffer
;
130 char *allocated
= NULL
;
131 struct group grent
, *result
;
135 result
= &grent
; /* Old getgrnam_r ignores last arg. */
136 r
= getgrnam_r(gname
, &grent
, buffer
, bufsize
, &result
);
143 allocated
= malloc(bufsize
);
144 if (allocated
== NULL
)
149 gid
= result
->gr_gid
;
152 # else /* HAVE_GETGRNAM_R */
154 struct group
*result
;
156 result
= getgrnam(gname
);
158 gid
= result
->gr_gid
;
160 # endif /* HAVE_GETGRNAM_R */
161 #elif defined(_WIN32) && !defined(__CYGWIN__)
162 /* TODO: do a gname->gid lookup for Windows. */
164 #error No way to perform gid lookups on this platform
172 lookup_uid(void *private_data
, const char *uname
, int64_t uid
)
176 struct bucket
*ucache
= (struct bucket
*)private_data
;
178 /* If no uname, just use the uid provided. */
179 if (uname
== NULL
|| *uname
== '\0')
182 /* Try to find uname in the cache. */
184 b
= &ucache
[h
% cache_size
];
185 if (b
->name
!= NULL
&& b
->hash
== h
&& strcmp(uname
, b
->name
) == 0)
186 return ((uid_t
)b
->id
);
188 /* Free the cache slot for a new entry. */
191 b
->name
= strdup(uname
);
192 /* Note: If strdup fails, that's okay; we just won't cache. */
198 size_t bufsize
= 128;
199 char *buffer
= _buffer
;
200 char *allocated
= NULL
;
201 struct passwd pwent
, *result
;
205 result
= &pwent
; /* Old getpwnam_r ignores last arg. */
206 r
= getpwnam_r(uname
, &pwent
, buffer
, bufsize
, &result
);
213 allocated
= malloc(bufsize
);
214 if (allocated
== NULL
)
219 uid
= result
->pw_uid
;
222 # else /* HAVE_GETPWNAM_R */
224 struct passwd
*result
;
226 result
= getpwnam(uname
);
228 uid
= result
->pw_uid
;
230 #endif /* HAVE_GETPWNAM_R */
231 #elif defined(_WIN32) && !defined(__CYGWIN__)
232 /* TODO: do a uname->uid lookup for Windows. */
234 #error No way to look up uids on this platform
242 cleanup(void *private)
245 struct bucket
*cache
= (struct bucket
*)private;
247 for (i
= 0; i
< cache_size
; i
++)
256 /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
257 as used by ELF for hashing function names. */
261 if ((g
= h
& 0xF0000000) != 0) {