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: src/lib/libarchive/archive_write_disk_set_standard_lookup.c,v 1.1 2007/03/03 07:37:36 kientzle Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
35 #ifdef HAVE_ATTR_XATTR_H
36 #include <attr/xattr.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_STAT_H
44 #ifdef HAVE_SYS_TIME_H
65 #include "archive_private.h"
66 #include "archive_read_private.h"
67 #include "archive_write_disk_private.h"
75 static unsigned int hash(const char *);
76 static gid_t
lookup_gid(void *, const char *uname
, gid_t
);
77 static uid_t
lookup_uid(void *, const char *uname
, uid_t
);
78 static void cleanup(void *);
81 * Installs functions that use getpwnam()/getgrnam()---along with
82 * a simple cache to accelerate such lookups---into the archive_write_disk
83 * object. This is in a separate file because getpwnam()/getgrnam()
84 * can pull in a LOT of library code (including NIS/LDAP functions, which
85 * pull in DNS resolveers, etc). This can easily top 500kB, which makes
86 * it inappropriate for some space-constrained applications.
88 * Applications that are size-sensitive may want to just use the
89 * real default functions (defined in archive_write_disk.c) that just
90 * use the uid/gid without the lookup. Or define your own custom functions
93 * TODO: Replace these hash tables with simpler move-to-front LRU
94 * lists with a bounded size (128 items?). The hash is a bit faster,
95 * but has a bad pathology in which it thrashes a single bucket. Even
96 * walking a list of 128 items is a lot faster than calling
100 archive_write_disk_set_standard_lookup(struct archive
*a
)
102 struct bucket
*ucache
= malloc(sizeof(struct bucket
[127]));
103 struct bucket
*gcache
= malloc(sizeof(struct bucket
[127]));
104 memset(ucache
, 0, sizeof(struct bucket
[127]));
105 memset(gcache
, 0, sizeof(struct bucket
[127]));
106 archive_write_disk_set_group_lookup(a
, gcache
, lookup_gid
, cleanup
);
107 archive_write_disk_set_user_lookup(a
, ucache
, lookup_uid
, cleanup
);
112 lookup_gid(void *private_data
, const char *gname
, gid_t gid
)
117 struct bucket
*gcache
= (struct bucket
*)private_data
;
121 /* If no gname, just use the gid provided. */
122 if (gname
== NULL
|| *gname
== '\0')
125 /* Try to find gname in the cache. */
127 b
= &gcache
[h
% cache_size
];
128 if (b
->name
!= NULL
&& b
->hash
== h
&& strcmp(gname
, b
->name
) == 0)
129 return ((gid_t
)b
->id
);
131 /* Free the cache slot for a new entry. */
134 b
->name
= strdup(gname
);
135 /* Note: If strdup fails, that's okay; we just won't cache. */
139 struct group
*grent
= getgrnam(gname
);
144 /* TODO: do a gname->gid lookup for Windows. */
152 lookup_uid(void *private_data
, const char *uname
, uid_t uid
)
157 struct bucket
*ucache
= (struct bucket
*)private_data
;
161 /* If no uname, just use the uid provided. */
162 if (uname
== NULL
|| *uname
== '\0')
165 /* Try to find uname in the cache. */
167 b
= &ucache
[h
% cache_size
];
168 if (b
->name
!= NULL
&& b
->hash
== h
&& strcmp(uname
, b
->name
) == 0)
169 return ((uid_t
)b
->id
);
171 /* Free the cache slot for a new entry. */
174 b
->name
= strdup(uname
);
175 /* Note: If strdup fails, that's okay; we just won't cache. */
179 struct passwd
*pwent
= getpwnam(uname
);
184 /* TODO: do a uname->uid lookup for Windows. */
192 cleanup(void *private)
201 /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
202 as used by ELF for hashing function names. */
205 h
= ( h
<< 4 ) + *p
++;
206 if (( g
= h
& 0xF0000000 )) {