Import libarchive-2.2.5 which fixes a forgotten 'break'. Without this,
[dragonfly.git] / contrib / libarchive-2.1 / libarchive / archive_read_support_format_cpio.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_read_support_format_cpio.c,v 1.23 2007/04/13 16:07:25 kientzle Exp $");
28
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 /* #include <stdint.h> */ /* See archive_platform.h */
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39
40 #include "archive.h"
41 #include "archive_entry.h"
42 #include "archive_private.h"
43 #include "archive_read_private.h"
44
45 struct cpio_bin_header {
46         unsigned char   c_magic[2];
47         unsigned char   c_dev[2];
48         unsigned char   c_ino[2];
49         unsigned char   c_mode[2];
50         unsigned char   c_uid[2];
51         unsigned char   c_gid[2];
52         unsigned char   c_nlink[2];
53         unsigned char   c_rdev[2];
54         unsigned char   c_mtime[4];
55         unsigned char   c_namesize[2];
56         unsigned char   c_filesize[4];
57 };
58
59 struct cpio_odc_header {
60         char    c_magic[6];
61         char    c_dev[6];
62         char    c_ino[6];
63         char    c_mode[6];
64         char    c_uid[6];
65         char    c_gid[6];
66         char    c_nlink[6];
67         char    c_rdev[6];
68         char    c_mtime[11];
69         char    c_namesize[6];
70         char    c_filesize[11];
71 };
72
73 struct cpio_newc_header {
74         char    c_magic[6];
75         char    c_ino[8];
76         char    c_mode[8];
77         char    c_uid[8];
78         char    c_gid[8];
79         char    c_nlink[8];
80         char    c_mtime[8];
81         char    c_filesize[8];
82         char    c_devmajor[8];
83         char    c_devminor[8];
84         char    c_rdevmajor[8];
85         char    c_rdevminor[8];
86         char    c_namesize[8];
87         char    c_crc[8];
88 };
89
90 struct links_entry {
91         struct links_entry      *next;
92         struct links_entry      *previous;
93         int                      links;
94         dev_t                    dev;
95         ino_t                    ino;
96         char                    *name;
97 };
98
99 #define CPIO_MAGIC   0x13141516
100 struct cpio {
101         int                       magic;
102         int                     (*read_header)(struct archive_read *, struct cpio *,
103                                      struct archive_entry *, size_t *, size_t *);
104         struct links_entry       *links_head;
105         struct archive_string     entry_name;
106         struct archive_string     entry_linkname;
107         off_t                     entry_bytes_remaining;
108         off_t                     entry_offset;
109         off_t                     entry_padding;
110 };
111
112 static int64_t  atol16(const char *, unsigned);
113 static int64_t  atol8(const char *, unsigned);
114 static int      archive_read_format_cpio_bid(struct archive_read *);
115 static int      archive_read_format_cpio_cleanup(struct archive_read *);
116 static int      archive_read_format_cpio_read_data(struct archive_read *,
117                     const void **, size_t *, off_t *);
118 static int      archive_read_format_cpio_read_header(struct archive_read *,
119                     struct archive_entry *);
120 static int      be4(const unsigned char *);
121 static int      header_bin_be(struct archive_read *, struct cpio *,
122                     struct archive_entry *, size_t *, size_t *);
123 static int      header_bin_le(struct archive_read *, struct cpio *,
124                     struct archive_entry *, size_t *, size_t *);
125 static int      header_newc(struct archive_read *, struct cpio *,
126                     struct archive_entry *, size_t *, size_t *);
127 static int      header_odc(struct archive_read *, struct cpio *,
128                     struct archive_entry *, size_t *, size_t *);
129 static int      le4(const unsigned char *);
130 static void     record_hardlink(struct cpio *cpio, struct archive_entry *entry);
131
132 int
133 archive_read_support_format_cpio(struct archive *_a)
134 {
135         struct archive_read *a = (struct archive_read *)_a;
136         struct cpio *cpio;
137         int r;
138
139         cpio = (struct cpio *)malloc(sizeof(*cpio));
140         if (cpio == NULL) {
141                 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
142                 return (ARCHIVE_FATAL);
143         }
144         memset(cpio, 0, sizeof(*cpio));
145         cpio->magic = CPIO_MAGIC;
146
147         r = __archive_read_register_format(a,
148             cpio,
149             archive_read_format_cpio_bid,
150             archive_read_format_cpio_read_header,
151             archive_read_format_cpio_read_data,
152             NULL,
153             archive_read_format_cpio_cleanup);
154
155         if (r != ARCHIVE_OK)
156                 free(cpio);
157         return (ARCHIVE_OK);
158 }
159
160
161 static int
162 archive_read_format_cpio_bid(struct archive_read *a)
163 {
164         int bid, bytes_read;
165         const void *h;
166         const unsigned char *p;
167         struct cpio *cpio;
168
169         cpio = (struct cpio *)(a->format->data);
170         bid = 0;
171         bytes_read = (a->decompressor->read_ahead)(a, &h, 6);
172         /* Convert error code into error return. */
173         if (bytes_read < 0)
174                 return ((int)bytes_read);
175         if (bytes_read < 6)
176                 return (-1);
177
178         p = (const unsigned char *)h;
179         if (memcmp(p, "070707", 6) == 0) {
180                 /* ASCII cpio archive (odc, POSIX.1) */
181                 cpio->read_header = header_odc;
182                 bid += 48;
183                 /*
184                  * XXX TODO:  More verification; Could check that only octal
185                  * digits appear in appropriate header locations. XXX
186                  */
187         } else if (memcmp(p, "070701", 6) == 0) {
188                 /* ASCII cpio archive (SVR4 without CRC) */
189                 cpio->read_header = header_newc;
190                 bid += 48;
191                 /*
192                  * XXX TODO:  More verification; Could check that only hex
193                  * digits appear in appropriate header locations. XXX
194                  */
195         } else if (memcmp(p, "070702", 6) == 0) {
196                 /* ASCII cpio archive (SVR4 with CRC) */
197                 /* XXX TODO: Flag that we should check the CRC. XXX */
198                 cpio->read_header = header_newc;
199                 bid += 48;
200                 /*
201                  * XXX TODO:  More verification; Could check that only hex
202                  * digits appear in appropriate header locations. XXX
203                  */
204         } else if (p[0] * 256 + p[1] == 070707) {
205                 /* big-endian binary cpio archives */
206                 cpio->read_header = header_bin_be;
207                 bid += 16;
208                 /* Is more verification possible here? */
209         } else if (p[0] + p[1] * 256 == 070707) {
210                 /* little-endian binary cpio archives */
211                 cpio->read_header = header_bin_le;
212                 bid += 16;
213                 /* Is more verification possible here? */
214         } else
215                 return (ARCHIVE_WARN);
216
217         return (bid);
218 }
219
220 static int
221 archive_read_format_cpio_read_header(struct archive_read *a,
222     struct archive_entry *entry)
223 {
224         struct cpio *cpio;
225         size_t bytes;
226         const void *h;
227         size_t namelength;
228         size_t name_pad;
229         int r;
230
231         cpio = (struct cpio *)(a->format->data);
232         r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
233
234         if (r != ARCHIVE_OK)
235                 return (r);
236
237         /* Read name from buffer. */
238         bytes = (a->decompressor->read_ahead)(a, &h, namelength + name_pad);
239         if (bytes < namelength + name_pad)
240             return (ARCHIVE_FATAL);
241         (a->decompressor->consume)(a, namelength + name_pad);
242         archive_strncpy(&cpio->entry_name, (const char *)h, namelength);
243         archive_entry_set_pathname(entry, cpio->entry_name.s);
244         cpio->entry_offset = 0;
245
246         /* If this is a symlink, read the link contents. */
247         if (archive_entry_filetype(entry) == AE_IFLNK) {
248                 bytes = (a->decompressor->read_ahead)(a, &h,
249                     cpio->entry_bytes_remaining);
250                 if ((off_t)bytes < cpio->entry_bytes_remaining)
251                         return (ARCHIVE_FATAL);
252                 (a->decompressor->consume)(a, cpio->entry_bytes_remaining);
253                 archive_strncpy(&cpio->entry_linkname, (const char *)h,
254                     cpio->entry_bytes_remaining);
255                 archive_entry_set_symlink(entry, cpio->entry_linkname.s);
256                 cpio->entry_bytes_remaining = 0;
257         }
258
259         /* Compare name to "TRAILER!!!" to test for end-of-archive. */
260         if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
261             /* TODO: Store file location of start of block. */
262             archive_set_error(&a->archive, 0, NULL);
263             return (ARCHIVE_EOF);
264         }
265
266         /* Detect and record hardlinks to previously-extracted entries. */
267         record_hardlink(cpio, entry);
268
269         return (ARCHIVE_OK);
270 }
271
272 static int
273 archive_read_format_cpio_read_data(struct archive_read *a,
274     const void **buff, size_t *size, off_t *offset)
275 {
276         ssize_t bytes_read;
277         struct cpio *cpio;
278
279         cpio = (struct cpio *)(a->format->data);
280         if (cpio->entry_bytes_remaining > 0) {
281                 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
282                 if (bytes_read <= 0)
283                         return (ARCHIVE_FATAL);
284                 if (bytes_read > cpio->entry_bytes_remaining)
285                         bytes_read = cpio->entry_bytes_remaining;
286                 *size = bytes_read;
287                 *offset = cpio->entry_offset;
288                 cpio->entry_offset += bytes_read;
289                 cpio->entry_bytes_remaining -= bytes_read;
290                 (a->decompressor->consume)(a, bytes_read);
291                 return (ARCHIVE_OK);
292         } else {
293                 while (cpio->entry_padding > 0) {
294                         bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
295                         if (bytes_read <= 0)
296                                 return (ARCHIVE_FATAL);
297                         if (bytes_read > cpio->entry_padding)
298                                 bytes_read = cpio->entry_padding;
299                         (a->decompressor->consume)(a, bytes_read);
300                         cpio->entry_padding -= bytes_read;
301                 }
302                 *buff = NULL;
303                 *size = 0;
304                 *offset = cpio->entry_offset;
305                 return (ARCHIVE_EOF);
306         }
307 }
308
309 static int
310 header_newc(struct archive_read *a, struct cpio *cpio,
311     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
312 {
313         const void *h;
314         const struct cpio_newc_header *header;
315         size_t bytes;
316
317         /* Read fixed-size portion of header. */
318         bytes = (a->decompressor->read_ahead)(a, &h, sizeof(struct cpio_newc_header));
319         if (bytes < sizeof(struct cpio_newc_header))
320             return (ARCHIVE_FATAL);
321         (a->decompressor->consume)(a, sizeof(struct cpio_newc_header));
322
323         /* Parse out hex fields. */
324         header = (const struct cpio_newc_header *)h;
325
326         if (memcmp(header->c_magic, "070701", 6) == 0) {
327                 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
328                 a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
329         } else if (memcmp(header->c_magic, "070702", 6) == 0) {
330                 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
331                 a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
332         } else {
333                 /* TODO: Abort here? */
334         }
335
336         archive_entry_set_devmajor(entry, atol16(header->c_devmajor, sizeof(header->c_devmajor)));
337         archive_entry_set_devminor(entry, atol16(header->c_devminor, sizeof(header->c_devminor)));
338         archive_entry_set_ino(entry, atol16(header->c_ino, sizeof(header->c_ino)));
339         archive_entry_set_mode(entry, atol16(header->c_mode, sizeof(header->c_mode)));
340         archive_entry_set_uid(entry, atol16(header->c_uid, sizeof(header->c_uid)));
341         archive_entry_set_gid(entry, atol16(header->c_gid, sizeof(header->c_gid)));
342         archive_entry_set_nlink(entry, atol16(header->c_nlink, sizeof(header->c_nlink)));
343         archive_entry_set_rdevmajor(entry, atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)));
344         archive_entry_set_rdevminor(entry, atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
345         archive_entry_set_mtime(entry, atol16(header->c_mtime, sizeof(header->c_mtime)), 0);
346         *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
347         /* Pad name to 2 more than a multiple of 4. */
348         *name_pad = (2 - *namelength) & 3;
349
350         /*
351          * Note: entry_bytes_remaining is at least 64 bits and
352          * therefore guaranteed to be big enough for a 33-bit file
353          * size.
354          */
355         cpio->entry_bytes_remaining =
356             atol16(header->c_filesize, sizeof(header->c_filesize));
357         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
358         /* Pad file contents to a multiple of 4. */
359         cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
360         return (ARCHIVE_OK);
361 }
362
363 static int
364 header_odc(struct archive_read *a, struct cpio *cpio,
365     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
366 {
367         const void *h;
368         const struct cpio_odc_header *header;
369         size_t bytes;
370
371         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
372         a->archive.archive_format_name = "POSIX octet-oriented cpio";
373
374         /* Read fixed-size portion of header. */
375         bytes = (a->decompressor->read_ahead)(a, &h, sizeof(struct cpio_odc_header));
376         if (bytes < sizeof(struct cpio_odc_header))
377             return (ARCHIVE_FATAL);
378         (a->decompressor->consume)(a, sizeof(struct cpio_odc_header));
379
380         /* Parse out octal fields. */
381         header = (const struct cpio_odc_header *)h;
382
383         archive_entry_set_dev(entry, atol8(header->c_dev, sizeof(header->c_dev)));
384         archive_entry_set_ino(entry, atol8(header->c_ino, sizeof(header->c_ino)));
385         archive_entry_set_mode(entry, atol8(header->c_mode, sizeof(header->c_mode)));
386         archive_entry_set_uid(entry, atol8(header->c_uid, sizeof(header->c_uid)));
387         archive_entry_set_gid(entry, atol8(header->c_gid, sizeof(header->c_gid)));
388         archive_entry_set_nlink(entry, atol8(header->c_nlink, sizeof(header->c_nlink)));
389         archive_entry_set_rdev(entry, atol8(header->c_rdev, sizeof(header->c_rdev)));
390         archive_entry_set_mtime(entry, atol8(header->c_mtime, sizeof(header->c_mtime)), 0);
391         *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
392         *name_pad = 0; /* No padding of filename. */
393
394         /*
395          * Note: entry_bytes_remaining is at least 64 bits and
396          * therefore guaranteed to be big enough for a 33-bit file
397          * size.
398          */
399         cpio->entry_bytes_remaining =
400             atol8(header->c_filesize, sizeof(header->c_filesize));
401         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
402         cpio->entry_padding = 0;
403         return (ARCHIVE_OK);
404 }
405
406 static int
407 header_bin_le(struct archive_read *a, struct cpio *cpio,
408     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
409 {
410         const void *h;
411         const struct cpio_bin_header *header;
412         size_t bytes;
413
414         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
415         a->archive.archive_format_name = "cpio (little-endian binary)";
416
417         /* Read fixed-size portion of header. */
418         bytes = (a->decompressor->read_ahead)(a, &h, sizeof(struct cpio_bin_header));
419         if (bytes < sizeof(struct cpio_bin_header))
420             return (ARCHIVE_FATAL);
421         (a->decompressor->consume)(a, sizeof(struct cpio_bin_header));
422
423         /* Parse out binary fields. */
424         header = (const struct cpio_bin_header *)h;
425
426         archive_entry_set_dev(entry, header->c_dev[0] + header->c_dev[1] * 256);
427         archive_entry_set_ino(entry, header->c_ino[0] + header->c_ino[1] * 256);
428         archive_entry_set_mode(entry, header->c_mode[0] + header->c_mode[1] * 256);
429         archive_entry_set_uid(entry, header->c_uid[0] + header->c_uid[1] * 256);
430         archive_entry_set_gid(entry, header->c_gid[0] + header->c_gid[1] * 256);
431         archive_entry_set_nlink(entry, header->c_nlink[0] + header->c_nlink[1] * 256);
432         archive_entry_set_rdev(entry, header->c_rdev[0] + header->c_rdev[1] * 256);
433         archive_entry_set_mtime(entry, le4(header->c_mtime), 0);
434         *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
435         *name_pad = *namelength & 1; /* Pad to even. */
436
437         cpio->entry_bytes_remaining = le4(header->c_filesize);
438         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
439         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
440         return (ARCHIVE_OK);
441 }
442
443 static int
444 header_bin_be(struct archive_read *a, struct cpio *cpio,
445     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
446 {
447         const void *h;
448         const struct cpio_bin_header *header;
449         size_t bytes;
450
451         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
452         a->archive.archive_format_name = "cpio (big-endian binary)";
453
454         /* Read fixed-size portion of header. */
455         bytes = (a->decompressor->read_ahead)(a, &h,
456             sizeof(struct cpio_bin_header));
457         if (bytes < sizeof(struct cpio_bin_header))
458             return (ARCHIVE_FATAL);
459         (a->decompressor->consume)(a, sizeof(struct cpio_bin_header));
460
461         /* Parse out binary fields. */
462         header = (const struct cpio_bin_header *)h;
463         archive_entry_set_dev(entry, header->c_dev[0] * 256 + header->c_dev[1]);
464         archive_entry_set_ino(entry, header->c_ino[0] * 256 + header->c_ino[1]);
465         archive_entry_set_mode(entry, header->c_mode[0] * 256 + header->c_mode[1]);
466         archive_entry_set_uid(entry, header->c_uid[0] * 256 + header->c_uid[1]);
467         archive_entry_set_gid(entry, header->c_gid[0] * 256 + header->c_gid[1]);
468         archive_entry_set_nlink(entry, header->c_nlink[0] * 256 + header->c_nlink[1]);
469         archive_entry_set_rdev(entry, header->c_rdev[0] * 256 + header->c_rdev[1]);
470         archive_entry_set_mtime(entry, be4(header->c_mtime), 0);
471         *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
472         *name_pad = *namelength & 1; /* Pad to even. */
473
474         cpio->entry_bytes_remaining = be4(header->c_filesize);
475         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
476         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
477         return (ARCHIVE_OK);
478 }
479
480 static int
481 archive_read_format_cpio_cleanup(struct archive_read *a)
482 {
483         struct cpio *cpio;
484
485         cpio = (struct cpio *)(a->format->data);
486         /* Free inode->name map */
487         while (cpio->links_head != NULL) {
488                 struct links_entry *lp = cpio->links_head->next;
489
490                 if (cpio->links_head->name)
491                         free(cpio->links_head->name);
492                 free(cpio->links_head);
493                 cpio->links_head = lp;
494         }
495         archive_string_free(&cpio->entry_name);
496         free(cpio);
497         (a->format->data) = NULL;
498         return (ARCHIVE_OK);
499 }
500
501 static int
502 le4(const unsigned char *p)
503 {
504         return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
505 }
506
507
508 static int
509 be4(const unsigned char *p)
510 {
511         return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
512 }
513
514 /*
515  * Note that this implementation does not (and should not!) obey
516  * locale settings; you cannot simply substitute strtol here, since
517  * it does obey locale.
518  */
519 static int64_t
520 atol8(const char *p, unsigned char_cnt)
521 {
522         int64_t l;
523         int digit;
524
525         l = 0;
526         while (char_cnt-- > 0) {
527                 if (*p >= '0' && *p <= '7')
528                         digit = *p - '0';
529                 else
530                         return (l);
531                 p++;
532                 l <<= 3;
533                 l |= digit;
534         }
535         return (l);
536 }
537
538 static int64_t
539 atol16(const char *p, unsigned char_cnt)
540 {
541         int64_t l;
542         int digit;
543
544         l = 0;
545         while (char_cnt-- > 0) {
546                 if (*p >= 'a' && *p <= 'f')
547                         digit = *p - 'a' + 10;
548                 else if (*p >= 'A' && *p <= 'F')
549                         digit = *p - 'A' + 10;
550                 else if (*p >= '0' && *p <= '9')
551                         digit = *p - '0';
552                 else
553                         return (l);
554                 p++;
555                 l <<= 4;
556                 l |= digit;
557         }
558         return (l);
559 }
560
561 static void
562 record_hardlink(struct cpio *cpio, struct archive_entry *entry)
563 {
564         struct links_entry      *le;
565         dev_t dev;
566         ino_t ino;
567
568         dev = archive_entry_dev(entry);
569         ino = archive_entry_ino(entry);
570
571         /*
572          * First look in the list of multiply-linked files.  If we've
573          * already dumped it, convert this entry to a hard link entry.
574          */
575         for (le = cpio->links_head; le; le = le->next) {
576                 if (le->dev == dev && le->ino == ino) {
577                         archive_entry_set_hardlink(entry, le->name);
578
579                         if (--le->links <= 0) {
580                                 if (le->previous != NULL)
581                                         le->previous->next = le->next;
582                                 if (le->next != NULL)
583                                         le->next->previous = le->previous;
584                                 if (cpio->links_head == le)
585                                         cpio->links_head = le->next;
586                                 free(le);
587                         }
588
589                         return;
590                 }
591         }
592
593         le = (struct links_entry *)malloc(sizeof(struct links_entry));
594         if (le == NULL)
595                 __archive_errx(1, "Out of memory adding file to list");
596         if (cpio->links_head != NULL)
597                 cpio->links_head->previous = le;
598         le->next = cpio->links_head;
599         le->previous = NULL;
600         cpio->links_head = le;
601         le->dev = dev;
602         le->ino = ino;
603         le->links = archive_entry_nlink(entry) - 1;
604         le->name = strdup(archive_entry_pathname(entry));
605         if (le->name == NULL)
606                 __archive_errx(1, "Out of memory adding file to list");
607 }