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_entry_link_resolver.c,v 1.1 2007/12/30 04:58:21 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
43 #include "archive_entry.h"
46 * This is mostly a pretty straightforward hash table implementation.
47 * The only interesting bit is the different strategies used to
48 * match up links. These strategies match those used by various
50 * tar - content stored with first link, remainder refer back to it.
51 * This requires us to match each subsequent link up with the
53 * cpio - Old cpio just stored body with each link, match-ups were
54 * implicit. This is trivial.
55 * new cpio - New cpio only stores body with last link, match-ups
56 * are implicit. This is actually quite tricky; see the notes
60 /* Initial size of link cache. */
61 #define links_cache_initial_size 1024
64 struct links_entry *next;
65 struct links_entry *previous;
66 int links; /* # links not yet seen */
68 struct archive_entry *entry;
71 struct archive_entry_linkresolver {
72 struct links_entry **buckets;
73 struct links_entry *spare;
74 unsigned long number_entries;
75 size_t number_buckets;
79 static struct links_entry *find_entry(struct archive_entry_linkresolver *,
80 struct archive_entry *);
81 static void grow_hash(struct archive_entry_linkresolver *);
82 static void insert_entry(struct archive_entry_linkresolver *,
83 struct archive_entry *);
85 struct archive_entry_linkresolver *
86 archive_entry_linkresolver_new(void)
88 struct archive_entry_linkresolver *res;
91 res = malloc(sizeof(struct archive_entry_linkresolver));
94 memset(res, 0, sizeof(struct archive_entry_linkresolver));
95 res->number_buckets = links_cache_initial_size;
96 res->buckets = malloc(res->number_buckets *
97 sizeof(res->buckets[0]));
98 if (res->buckets == NULL) {
102 for (i = 0; i < res->number_buckets; i++)
103 res->buckets[i] = NULL;
108 archive_entry_linkresolver_set_strategy(struct archive_entry_linkresolver *res,
111 res->strategy = strategy;
115 archive_entry_linkresolver_free(struct archive_entry_linkresolver *res)
119 if (res->buckets == NULL)
122 for (i = 0; i < res->number_buckets; i++) {
123 while (res->buckets[i] != NULL) {
124 struct links_entry *lp = res->buckets[i]->next;
125 archive_entry_free(res->buckets[i]->entry);
126 free(res->buckets[i]);
127 res->buckets[i] = lp;
134 /* Always uses tar-like semantics. */
136 archive_entry_linkresolve(struct archive_entry_linkresolver *res,
137 struct archive_entry *entry)
139 struct links_entry *le;
141 /* If it has only one link, then we're done. */
142 if (archive_entry_nlink(entry) == 1)
145 /* Look it up in the hash. */
146 le = find_entry(res, entry);
148 return (archive_entry_pathname(le->entry));
149 /* If it's not there, insert it. */
150 insert_entry(res, entry);
155 archive_entry_linkify(struct archive_entry_linkresolver *res,
156 struct archive_entry **e, struct archive_entry **f)
158 struct links_entry *le;
159 struct archive_entry *t;
161 *f = NULL; /* Default: Don't return a second entry. */
163 /* If it has only one link, then we're done. */
164 if (archive_entry_nlink(*e) == 1)
167 switch (res->strategy) {
168 case ARCHIVE_ENTRY_LINKIFY_LIKE_TAR:
169 le = find_entry(res, *e);
171 archive_entry_set_size(*e, 0);
172 archive_entry_set_hardlink(*e,
173 archive_entry_pathname(le->entry));
175 insert_entry(res, *e);
177 case ARCHIVE_ENTRY_LINKIFY_LIKE_OLD_CPIO:
178 /* This one is trivial. */
180 case ARCHIVE_ENTRY_LINKIFY_LIKE_NEW_CPIO:
181 le = find_entry(res, *e);
186 archive_entry_set_size(*e, 0);
187 archive_entry_set_hardlink(*e,
188 archive_entry_pathname(le->entry));
189 if (le->links == 0) {
193 insert_entry(res, *e);
203 static struct links_entry *
204 find_entry(struct archive_entry_linkresolver *res,
205 struct archive_entry *entry)
207 struct links_entry *le;
212 /* Free a held entry. */
213 if (res->spare != NULL) {
214 archive_entry_free(res->spare->entry);
219 /* If the links cache overflowed and got flushed, don't bother. */
220 if (res->buckets == NULL)
223 dev = archive_entry_dev(entry);
224 ino = archive_entry_ino(entry);
227 /* Try to locate this entry in the links cache. */
228 bucket = hash % res->number_buckets;
229 for (le = res->buckets[bucket]; le != NULL; le = le->next) {
231 && dev == archive_entry_dev(le->entry)
232 && ino == archive_entry_ino(le->entry)) {
234 * Decrement link count each time and release
235 * the entry if it hits zero. This saves
236 * memory and is necessary for detecting
242 /* Remove it from this hash bucket. */
243 if (le->previous != NULL)
244 le->previous->next = le->next;
245 if (le->next != NULL)
246 le->next->previous = le->previous;
247 if (res->buckets[bucket] == le)
248 res->buckets[bucket] = le->next;
249 res->number_entries--;
250 /* Defer freeing this entry. */
259 insert_entry(struct archive_entry_linkresolver *res,
260 struct archive_entry *entry)
262 struct links_entry *le;
265 /* Add this entry to the links cache. */
266 le = malloc(sizeof(struct links_entry));
269 le->entry = archive_entry_clone(entry);
270 if (le->entry == NULL) {
275 /* If the links cache is getting too full, enlarge the hash table. */
276 if (res->number_entries > res->number_buckets * 2)
279 hash = archive_entry_dev(entry) ^ archive_entry_ino(entry);
280 bucket = hash % res->number_buckets;
282 /* If we could allocate the entry, record it. */
283 if (res->buckets[bucket] != NULL)
284 res->buckets[bucket]->previous = le;
285 res->number_entries++;
286 le->next = res->buckets[bucket];
288 res->buckets[bucket] = le;
290 le->links = archive_entry_nlink(entry) - 1;
294 grow_hash(struct archive_entry_linkresolver *res)
296 struct links_entry *le, **new_buckets;
300 /* Try to enlarge the bucket list. */
301 new_size = res->number_buckets * 2;
302 new_buckets = malloc(new_size * sizeof(struct links_entry *));
304 if (new_buckets != NULL) {
305 memset(new_buckets, 0,
306 new_size * sizeof(struct links_entry *));
307 for (i = 0; i < res->number_buckets; i++) {
308 while (res->buckets[i] != NULL) {
309 /* Remove entry from old bucket. */
310 le = res->buckets[i];
311 res->buckets[i] = le->next;
313 /* Add entry to new bucket. */
314 bucket = le->hash % new_size;
316 if (new_buckets[bucket] != NULL)
317 new_buckets[bucket]->previous =
319 le->next = new_buckets[bucket];
321 new_buckets[bucket] = le;
325 res->buckets = new_buckets;
326 res->number_buckets = new_size;