Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement functions to manually set the format and filters used.

  • Loading branch information...
commit 82b5d8dfb3c358df513d2fb89aa6a5b90188b4c2 1 parent 5fbef6a
Andres Mejia amejia1 authored
1  Makefile.am
@@ -409,6 +409,7 @@ libarchive_test_SOURCES= \
409 409 libarchive/test/test_read_large.c \
410 410 libarchive/test/test_read_pax_truncated.c \
411 411 libarchive/test/test_read_position.c \
  412 + libarchive/test/test_read_set_format.c \
412 413 libarchive/test/test_read_truncated.c \
413 414 libarchive/test/test_read_truncated_filter.c \
414 415 libarchive/test/test_sparse_basic.c \
11 libarchive/archive.h
@@ -372,6 +372,17 @@ __LA_DECL int archive_read_support_format_tar(struct archive *);
372 372 __LA_DECL int archive_read_support_format_xar(struct archive *);
373 373 __LA_DECL int archive_read_support_format_zip(struct archive *);
374 374
  375 +/* Functions to manually set the format and filters to be used. This is
  376 + * useful to bypass the bidding process when the format and filters to use
  377 + * is known in advance.
  378 + */
  379 +__LA_DECL int archive_read_set_format(struct archive *, int);
  380 +__LA_DECL int archive_read_append_filter(struct archive *, int);
  381 +__LA_DECL int archive_read_append_filter_program(struct archive *,
  382 + const char *);
  383 +__LA_DECL int archive_read_append_filter_program_signature
  384 + (struct archive *, const char *, const void * /* match */, size_t);
  385 +
375 386 /* Set various callbacks. */
376 387 __LA_DECL int archive_read_set_open_callback(struct archive *,
377 388 archive_open_callback *);
275 libarchive/archive_read.c
@@ -455,7 +455,7 @@ int
455 455 archive_read_open1(struct archive *_a)
456 456 {
457 457 struct archive_read *a = (struct archive_read *)_a;
458   - struct archive_read_filter *filter;
  458 + struct archive_read_filter *filter, *tmp;
459 459 int slot, e;
460 460 unsigned int i;
461 461
@@ -499,25 +499,37 @@ archive_read_open1(struct archive *_a)
499 499 filter->sswitch = client_switch_proxy;
500 500 filter->name = "none";
501 501 filter->code = ARCHIVE_FILTER_NONE;
502   - a->filter = filter;
503 502
504   - client_switch_proxy(a->filter, 0);
505 503 a->client.dataset[0].begin_position = 0;
506   -
507   - /* Build out the input pipeline. */
508   - e = choose_filters(a);
509   - if (e < ARCHIVE_WARN) {
510   - a->archive.state = ARCHIVE_STATE_FATAL;
511   - return (ARCHIVE_FATAL);
  504 + if (!a->filter || !a->bypass_filter_bidding)
  505 + {
  506 + a->filter = filter;
  507 + /* Build out the input pipeline. */
  508 + e = choose_filters(a);
  509 + if (e < ARCHIVE_WARN) {
  510 + a->archive.state = ARCHIVE_STATE_FATAL;
  511 + return (ARCHIVE_FATAL);
  512 + }
  513 + }
  514 + else
  515 + {
  516 + /* Need to add "NONE" type filter at the end of the filter chain */
  517 + tmp = a->filter;
  518 + while (tmp->upstream)
  519 + tmp = tmp->upstream;
  520 + tmp->upstream = filter;
512 521 }
513 522
514   - slot = choose_format(a);
515   - if (slot < 0) {
516   - close_filters(a);
517   - a->archive.state = ARCHIVE_STATE_FATAL;
518   - return (ARCHIVE_FATAL);
  523 + if (!a->format)
  524 + {
  525 + slot = choose_format(a);
  526 + if (slot < 0) {
  527 + close_filters(a);
  528 + a->archive.state = ARCHIVE_STATE_FATAL;
  529 + return (ARCHIVE_FATAL);
  530 + }
  531 + a->format = &(a->formats[slot]);
519 532 }
520   - a->format = &(a->formats[slot]);
521 533
522 534 a->archive.state = ARCHIVE_STATE_HEADER;
523 535
@@ -1622,3 +1634,236 @@ __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1622 1634 }
1623 1635 return r;
1624 1636 }
  1637 +
  1638 +int
  1639 +archive_read_set_format(struct archive *_a, int code)
  1640 +{
  1641 + int r1, r2, slots, i;
  1642 + char str[10];
  1643 + struct archive_read *a = (struct archive_read *)_a;
  1644 +
  1645 + if ((r1 = archive_read_support_format_by_code(_a, code)) < (ARCHIVE_OK))
  1646 + return r1;
  1647 +
  1648 + r1 = r2 = (ARCHIVE_OK);
  1649 + if (a->format)
  1650 + r2 = (ARCHIVE_WARN);
  1651 + switch (code & ARCHIVE_FORMAT_BASE_MASK)
  1652 + {
  1653 + case ARCHIVE_FORMAT_7ZIP:
  1654 + strcpy(str, "7zip");
  1655 + break;
  1656 + case ARCHIVE_FORMAT_AR:
  1657 + strcpy(str, "ar");
  1658 + break;
  1659 + case ARCHIVE_FORMAT_CAB:
  1660 + strcpy(str, "cab");
  1661 + break;
  1662 + case ARCHIVE_FORMAT_CPIO:
  1663 + strcpy(str, "cpio");
  1664 + break;
  1665 + case ARCHIVE_FORMAT_ISO9660:
  1666 + strcpy(str, "iso9660");
  1667 + break;
  1668 + case ARCHIVE_FORMAT_LHA:
  1669 + strcpy(str, "lha");
  1670 + break;
  1671 + case ARCHIVE_FORMAT_MTREE:
  1672 + strcpy(str, "mtree");
  1673 + break;
  1674 + case ARCHIVE_FORMAT_RAR:
  1675 + strcpy(str, "rar");
  1676 + break;
  1677 + case ARCHIVE_FORMAT_TAR:
  1678 + strcpy(str, "tar");
  1679 + break;
  1680 + case ARCHIVE_FORMAT_XAR:
  1681 + strcpy(str, "xar");
  1682 + break;
  1683 + case ARCHIVE_FORMAT_ZIP:
  1684 + strcpy(str, "zip");
  1685 + break;
  1686 + default:
  1687 + archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1688 + "Invalid format code specified");
  1689 + return (ARCHIVE_FATAL);
  1690 + }
  1691 +
  1692 + slots = sizeof(a->formats) / sizeof(a->formats[0]);
  1693 + a->format = &(a->formats[0]);
  1694 + for (i = 0; i < slots; i++, a->format++) {
  1695 + if (!a->format->name || !strcmp(a->format->name, str))
  1696 + break;
  1697 + }
  1698 + if (!a->format->name || strcmp(a->format->name, str))
  1699 + {
  1700 + archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1701 + "Internal error: Unable to set format");
  1702 + r1 = (ARCHIVE_FATAL);
  1703 + }
  1704 +
  1705 + return (r1 < r2) ? r1 : r2;
  1706 +}
  1707 +
  1708 +int
  1709 +archive_read_append_filter(struct archive *_a, int code)
  1710 +{
  1711 + int r1, r2, number_bidders, i;
  1712 + char str[10];
  1713 + struct archive_read_filter_bidder *bidder;
  1714 + struct archive_read_filter *filter;
  1715 + struct archive_read *a = (struct archive_read *)_a;
  1716 +
  1717 + r1 = r2 = (ARCHIVE_OK);
  1718 + switch (code)
  1719 + {
  1720 + case ARCHIVE_FILTER_NONE:
  1721 + /* No filter to add, so do nothing.
  1722 + * NOTE: An initial "NONE" type filter is always set at the end of the
  1723 + * filter chain.
  1724 + */
  1725 + r1 = (ARCHIVE_OK);
  1726 + break;
  1727 + case ARCHIVE_FILTER_GZIP:
  1728 + strcpy(str, "gzip");
  1729 + r1 = archive_read_support_filter_gzip(_a);
  1730 + break;
  1731 + case ARCHIVE_FILTER_BZIP2:
  1732 + strcpy(str, "bzip2");
  1733 + r1 = archive_read_support_filter_bzip2(_a);
  1734 + break;
  1735 + case ARCHIVE_FILTER_COMPRESS:
  1736 + strcpy(str, "compress (.Z)");
  1737 + r1 = archive_read_support_filter_compress(_a);
  1738 + break;
  1739 + case ARCHIVE_FILTER_PROGRAM:
  1740 + archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1741 + "Cannot append program filter using archive_read_append_filter");
  1742 + return (ARCHIVE_FATAL);
  1743 + case ARCHIVE_FILTER_LZMA:
  1744 + strcpy(str, "lzma");
  1745 + r1 = archive_read_support_filter_lzma(_a);
  1746 + break;
  1747 + case ARCHIVE_FILTER_XZ:
  1748 + strcpy(str, "xz");
  1749 + r1 = archive_read_support_filter_xz(_a);
  1750 + break;
  1751 + case ARCHIVE_FILTER_UU:
  1752 + strcpy(str, "uu");
  1753 + r1 = archive_read_support_filter_uu(_a);
  1754 + break;
  1755 + case ARCHIVE_FILTER_RPM:
  1756 + strcpy(str, "rpm");
  1757 + r1 = archive_read_support_filter_rpm(_a);
  1758 + break;
  1759 + case ARCHIVE_FILTER_LZIP:
  1760 + strcpy(str, "lzip");
  1761 + r1 = archive_read_support_filter_lzip(_a);
  1762 + break;
  1763 + case ARCHIVE_FILTER_LRZIP:
  1764 + strcpy(str, "lrzip");
  1765 + r1 = archive_read_support_filter_lrzip(_a);
  1766 + break;
  1767 + default:
  1768 + archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1769 + "Invalid filter code specified");
  1770 + return (ARCHIVE_FATAL);
  1771 + }
  1772 +
  1773 + if (code != ARCHIVE_FILTER_NONE)
  1774 + {
  1775 + number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
  1776 +
  1777 + bidder = a->bidders;
  1778 + for (i = 0; i < number_bidders; i++, bidder++)
  1779 + {
  1780 + if (!bidder->name || !strcmp(bidder->name, str))
  1781 + break;
  1782 + }
  1783 + if (!bidder->name || strcmp(bidder->name, str))
  1784 + {
  1785 + archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1786 + "Internal error: Unable to append filter");
  1787 + return (ARCHIVE_FATAL);
  1788 + }
  1789 +
  1790 + filter
  1791 + = (struct archive_read_filter *)calloc(1, sizeof(*filter));
  1792 + if (filter == NULL)
  1793 + {
  1794 + archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1795 + return (ARCHIVE_FATAL);
  1796 + }
  1797 + filter->bidder = bidder;
  1798 + filter->archive = a;
  1799 + filter->upstream = a->filter;
  1800 + a->filter = filter;
  1801 + r2 = (bidder->init)(a->filter);
  1802 + if (r2 != ARCHIVE_OK) {
  1803 + close_filters(a);
  1804 + free_filters(a);
  1805 + return (ARCHIVE_FATAL);
  1806 + }
  1807 + }
  1808 +
  1809 + a->bypass_filter_bidding = 1;
  1810 + return (r1 < r2) ? r1 : r2;
  1811 +}
  1812 +
  1813 +int
  1814 +archive_read_append_filter_program(struct archive *_a, const char *cmd)
  1815 +{
  1816 + return (archive_read_append_filter_program_signature(_a, cmd, NULL, 0));
  1817 +}
  1818 +
  1819 +int
  1820 +archive_read_append_filter_program_signature(struct archive *_a,
  1821 + const char *cmd, const void *signature, size_t signature_len)
  1822 +{
  1823 + int r, number_bidders, i;
  1824 + struct archive_read_filter_bidder *bidder;
  1825 + struct archive_read_filter *filter;
  1826 + struct archive_read *a = (struct archive_read *)_a;
  1827 +
  1828 + if (archive_read_support_filter_program_signature(_a, cmd, signature,
  1829 + signature_len) != (ARCHIVE_OK))
  1830 + return (ARCHIVE_FATAL);
  1831 +
  1832 + number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
  1833 +
  1834 + bidder = a->bidders;
  1835 + for (i = 0; i < number_bidders; i++, bidder++)
  1836 + {
  1837 + /* Program bidder name set to filter name after initialization */
  1838 + if (bidder->data && !bidder->name)
  1839 + break;
  1840 + }
  1841 + if (!bidder->data)
  1842 + {
  1843 + archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1844 + "Internal error: Unable to append program filter");
  1845 + return (ARCHIVE_FATAL);
  1846 + }
  1847 +
  1848 + filter
  1849 + = (struct archive_read_filter *)calloc(1, sizeof(*filter));
  1850 + if (filter == NULL)
  1851 + {
  1852 + archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1853 + return (ARCHIVE_FATAL);
  1854 + }
  1855 + filter->bidder = bidder;
  1856 + filter->archive = a;
  1857 + filter->upstream = a->filter;
  1858 + a->filter = filter;
  1859 + r = (bidder->init)(a->filter);
  1860 + if (r != ARCHIVE_OK) {
  1861 + close_filters(a);
  1862 + free_filters(a);
  1863 + return (ARCHIVE_FATAL);
  1864 + }
  1865 + bidder->name = a->filter->name;
  1866 +
  1867 + a->bypass_filter_bidding = 1;
  1868 + return r;
  1869 +}
5 libarchive/archive_read_private.h
@@ -58,6 +58,8 @@ struct archive_read_filter;
58 58 struct archive_read_filter_bidder {
59 59 /* Configuration data for the bidder. */
60 60 void *data;
  61 + /* Name of the filter */
  62 + const char *name;
61 63 /* Taste the upstream filter to see if we handle this. */
62 64 int (*bid)(struct archive_read_filter_bidder *,
63 65 struct archive_read_filter *);
@@ -168,6 +170,9 @@ struct archive_read {
168 170 /* Last filter in chain */
169 171 struct archive_read_filter *filter;
170 172
  173 + /* Whether to bypass filter bidding process */
  174 + int bypass_filter_bidding;
  175 +
171 176 /* File offset of beginning of most recently-read header. */
172 177 int64_t header_position;
173 178
1  libarchive/archive_read_support_filter_bzip2.c
@@ -94,6 +94,7 @@ archive_read_support_filter_bzip2(struct archive *_a)
94 94 return (ARCHIVE_FATAL);
95 95
96 96 reader->data = NULL;
  97 + reader->name = "bzip2";
97 98 reader->bid = bzip2_reader_bid;
98 99 reader->init = bzip2_reader_init;
99 100 reader->options = NULL;
1  libarchive/archive_read_support_filter_compress.c
@@ -163,6 +163,7 @@ archive_read_support_filter_compress(struct archive *_a)
163 163 return (ARCHIVE_FATAL);
164 164
165 165 bidder->data = NULL;
  166 + bidder->name = "compress (.Z)";
166 167 bidder->bid = compress_bidder_bid;
167 168 bidder->init = compress_bidder_init;
168 169 bidder->options = NULL;
1  libarchive/archive_read_support_filter_gzip.c
@@ -100,6 +100,7 @@ archive_read_support_filter_gzip(struct archive *_a)
100 100 return (ARCHIVE_FATAL);
101 101
102 102 bidder->data = NULL;
  103 + bidder->name = "gzip";
103 104 bidder->bid = gzip_bidder_bid;
104 105 bidder->init = gzip_bidder_init;
105 106 bidder->options = NULL;
1  libarchive/archive_read_support_filter_lrzip.c
@@ -73,6 +73,7 @@ archive_read_support_filter_lrzip(struct archive *_a)
73 73 return (ARCHIVE_FATAL);
74 74
75 75 reader->data = NULL;
  76 + reader->name = "lrzip";
76 77 reader->bid = lrzip_bidder_bid;
77 78 reader->init = lrzip_bidder_init;
78 79 reader->options = NULL;
1  libarchive/archive_read_support_filter_program.c
@@ -88,6 +88,7 @@ archive_read_support_filter_program(struct archive *a, const char *cmd)
88 88 * bid twice in the same pipeline.
89 89 */
90 90 struct program_bidder {
  91 + char *description;
91 92 char *cmd;
92 93 void *signature;
93 94 size_t signature_len;
1  libarchive/archive_read_support_filter_rpm.c
@@ -85,6 +85,7 @@ archive_read_support_filter_rpm(struct archive *_a)
85 85 return (ARCHIVE_FATAL);
86 86
87 87 bidder->data = NULL;
  88 + bidder->name = "rpm";
88 89 bidder->bid = rpm_bidder_bid;
89 90 bidder->init = rpm_bidder_init;
90 91 bidder->options = NULL;
1  libarchive/archive_read_support_filter_uu.c
@@ -89,6 +89,7 @@ archive_read_support_filter_uu(struct archive *_a)
89 89 return (ARCHIVE_FATAL);
90 90
91 91 bidder->data = NULL;
  92 + bidder->name = "uu";
92 93 bidder->bid = uudecode_bidder_bid;
93 94 bidder->init = uudecode_bidder_init;
94 95 bidder->options = NULL;
3  libarchive/archive_read_support_filter_xz.c
@@ -136,6 +136,7 @@ archive_read_support_filter_xz(struct archive *_a)
136 136 return (ARCHIVE_FATAL);
137 137
138 138 bidder->data = NULL;
  139 + bidder->name = "xz";
139 140 bidder->bid = xz_bidder_bid;
140 141 bidder->init = xz_bidder_init;
141 142 bidder->options = NULL;
@@ -170,6 +171,7 @@ archive_read_support_filter_lzma(struct archive *_a)
170 171 return (ARCHIVE_FATAL);
171 172
172 173 bidder->data = NULL;
  174 + bidder->name = "lzma";
173 175 bidder->bid = lzma_bidder_bid;
174 176 bidder->init = lzma_bidder_init;
175 177 bidder->options = NULL;
@@ -207,6 +209,7 @@ archive_read_support_filter_lzip(struct archive *_a)
207 209 return (ARCHIVE_FATAL);
208 210
209 211 bidder->data = NULL;
  212 + bidder->name = "lzip";
210 213 bidder->bid = lzip_bidder_bid;
211 214 bidder->init = lzip_bidder_init;
212 215 bidder->options = NULL;
1  libarchive/test/CMakeLists.txt
@@ -144,6 +144,7 @@ IF(ENABLE_TEST)
144 144 test_read_large.c
145 145 test_read_pax_truncated.c
146 146 test_read_position.c
  147 + test_read_set_format.c
147 148 test_read_truncated.c
148 149 test_read_truncated_filter.c
149 150 test_sparse_basic.c
219 libarchive/test/test_read_set_format.c
... ... @@ -0,0 +1,219 @@
  1 +/*-
  2 + * Copyright (c) 2003-2007 Tim Kientzle
  3 + * Copyright (c) 2012 Andres Mejia
  4 + * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  5 + * All rights reserved.
  6 + *
  7 + * Redistribution and use in source and binary forms, with or without
  8 + * modification, are permitted provided that the following conditions
  9 + * are met:
  10 + * 1. Redistributions of source code must retain the above copyright
  11 + * notice, this list of conditions and the following disclaimer.
  12 + * 2. Redistributions in binary form must reproduce the above copyright
  13 + * notice, this list of conditions and the following disclaimer in the
  14 + * documentation and/or other materials provided with the distribution.
  15 + *
  16 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19 + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26 + */
  27 +#include "test.h"
  28 +
  29 +DEFINE_TEST(test_read_set_format)
  30 +{
  31 + char buff[64];
  32 + const char reffile[] = "test_read_format_rar.rar";
  33 + const char test_txt[] = "test text document\r\n";
  34 + int size = sizeof(test_txt)-1;
  35 + struct archive_entry *ae;
  36 + struct archive *a;
  37 +
  38 + extract_reference_file(reffile);
  39 + assert((a = archive_read_new()) != NULL);
  40 + assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR));
  41 + assertA(0 == archive_read_append_filter(a, ARCHIVE_FILTER_NONE));
  42 + assertA(0 == archive_read_open_filename(a, reffile, 10240));
  43 +
  44 + /* First header. */
  45 + assertA(0 == archive_read_next_header(a, &ae));
  46 + assertEqualString("test.txt", archive_entry_pathname(ae));
  47 + assertA((int)archive_entry_mtime(ae));
  48 + assertA((int)archive_entry_ctime(ae));
  49 + assertA((int)archive_entry_atime(ae));
  50 + assertEqualInt(20, archive_entry_size(ae));
  51 + assertEqualInt(33188, archive_entry_mode(ae));
  52 + assertA(size == archive_read_data(a, buff, size));
  53 + assertEqualMem(buff, test_txt, size);
  54 +
  55 + /* Second header. */
  56 + assertA(0 == archive_read_next_header(a, &ae));
  57 + assertEqualString("testlink", archive_entry_pathname(ae));
  58 + assertA((int)archive_entry_mtime(ae));
  59 + assertA((int)archive_entry_ctime(ae));
  60 + assertA((int)archive_entry_atime(ae));
  61 + assertEqualInt(0, archive_entry_size(ae));
  62 + assertEqualInt(41471, archive_entry_mode(ae));
  63 + assertEqualString("test.txt", archive_entry_symlink(ae));
  64 + assertEqualIntA(a, 0, archive_read_data(a, buff, sizeof(buff)));
  65 +
  66 + /* Third header. */
  67 + assertA(0 == archive_read_next_header(a, &ae));
  68 + assertEqualString("testdir/test.txt", archive_entry_pathname(ae));
  69 + assertA((int)archive_entry_mtime(ae));
  70 + assertA((int)archive_entry_ctime(ae));
  71 + assertA((int)archive_entry_atime(ae));
  72 + assertEqualInt(20, archive_entry_size(ae));
  73 + assertEqualInt(33188, archive_entry_mode(ae));
  74 + assertA(size == archive_read_data(a, buff, size));
  75 + assertEqualMem(buff, test_txt, size);
  76 +
  77 + /* Fourth header. */
  78 + assertA(0 == archive_read_next_header(a, &ae));
  79 + assertEqualString("testdir", archive_entry_pathname(ae));
  80 + assertA((int)archive_entry_mtime(ae));
  81 + assertA((int)archive_entry_ctime(ae));
  82 + assertA((int)archive_entry_atime(ae));
  83 + assertEqualInt(0, archive_entry_size(ae));
  84 + assertEqualInt(16877, archive_entry_mode(ae));
  85 +
  86 + /* Fifth header. */
  87 + assertA(0 == archive_read_next_header(a, &ae));
  88 + assertEqualString("testemptydir", archive_entry_pathname(ae));
  89 + assertA((int)archive_entry_mtime(ae));
  90 + assertA((int)archive_entry_ctime(ae));
  91 + assertA((int)archive_entry_atime(ae));
  92 + assertEqualInt(0, archive_entry_size(ae));
  93 + assertEqualInt(16877, archive_entry_mode(ae));
  94 +
  95 + /* Test EOF */
  96 + assertA(1 == archive_read_next_header(a, &ae));
  97 + assertEqualInt(5, archive_file_count(a));
  98 + assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
  99 + assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  100 +}
  101 +
  102 +DEFINE_TEST(test_read_set_wrong_format)
  103 +{
  104 + const char reffile[] = "test_read_format_zip.zip";
  105 + struct archive_entry *ae;
  106 + struct archive *a;
  107 +
  108 + extract_reference_file(reffile);
  109 + assert((a = archive_read_new()) != NULL);
  110 + assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR));
  111 + assertA(0 == archive_read_append_filter(a, ARCHIVE_FILTER_NONE));
  112 + assertA(0 == archive_read_open_filename(a, reffile, 10240));
  113 +
  114 + /* Check that this actually fails, then close the archive. */
  115 + assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN));
  116 + assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
  117 + assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  118 +}
  119 +
  120 +static unsigned char archive[] = {
  121 +31,139,8,0,222,'C','p','C',0,3,211,'c',160,'=','0','0','0','0','7','5','U',
  122 +0,210,134,230,166,6,200,'4',28,'(',24,26,24,27,155,24,152,24,154,27,155,')',
  123 +24,24,26,152,154,25,'2','(',152,210,193,'m',12,165,197,'%',137,'E','@',167,
  124 +148,'d',230,226,'U','G','H',30,234,15,'8','=',10,'F',193,'(',24,5,131,28,
  125 +0,0,29,172,5,240,0,6,0,0};
  126 +
  127 +DEFINE_TEST(test_read_append_filter)
  128 +{
  129 + struct archive_entry *ae;
  130 + struct archive *a;
  131 + int r;
  132 +
  133 + assert((a = archive_read_new()) != NULL);
  134 + assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
  135 + r = archive_read_append_filter(a, ARCHIVE_FILTER_GZIP);
  136 + if (r == ARCHIVE_WARN) {
  137 + skipping("gzip reading not fully supported on this platform");
  138 + assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  139 + return;
  140 + }
  141 + assertEqualInt(ARCHIVE_OK,
  142 + archive_read_open_memory(a, archive, sizeof(archive)));
  143 + assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &ae));
  144 + assertEqualInt(1, archive_file_count(a));
  145 + assertEqualInt(archive_filter_code(a, 0), ARCHIVE_COMPRESSION_GZIP);
  146 + assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
  147 + assertEqualInt(ARCHIVE_OK, archive_read_close(a));
  148 + assertEqualInt(ARCHIVE_OK,archive_read_free(a));
  149 +}
  150 +
  151 +DEFINE_TEST(test_read_append_wrong_filter)
  152 +{
  153 + struct archive_entry *ae;
  154 + struct archive *a;
  155 + int r;
  156 +
  157 + assert((a = archive_read_new()) != NULL);
  158 + assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
  159 + r = archive_read_append_filter(a, ARCHIVE_FILTER_XZ);
  160 + if (r == ARCHIVE_WARN) {
  161 + skipping("gzip reading not fully supported on this platform");
  162 + assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  163 + return;
  164 + }
  165 + assertEqualInt(ARCHIVE_OK,
  166 + archive_read_open_memory(a, archive, sizeof(archive)));
  167 + assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN));
  168 + assertEqualInt(ARCHIVE_OK, archive_read_close(a));
  169 + assertEqualInt(ARCHIVE_OK,archive_read_free(a));
  170 +}
  171 +
  172 +DEFINE_TEST(test_read_append_filter_program)
  173 +{
  174 + struct archive_entry *ae;
  175 + struct archive *a;
  176 +
  177 + /*
  178 + * If we have "gzip -d", try using that.
  179 + */
  180 + if (!canGzip()) {
  181 + skipping("Can't run gunzip program on this platform");
  182 + return;
  183 + }
  184 + assert((a = archive_read_new()) != NULL);
  185 + assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
  186 + assertEqualIntA(a, ARCHIVE_OK,
  187 + archive_read_append_filter_program(a, "gunzip"));
  188 + assertEqualIntA(a, ARCHIVE_OK,
  189 + archive_read_open_memory(a, archive, sizeof(archive)));
  190 + assertEqualIntA(a, ARCHIVE_OK,
  191 + archive_read_next_header(a, &ae));
  192 + assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_PROGRAM);
  193 + assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
  194 + assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
  195 + assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  196 +}
  197 +
  198 +DEFINE_TEST(test_read_append_filter_wrong_program)
  199 +{
  200 + struct archive_entry *ae;
  201 + struct archive *a;
  202 +
  203 + /*
  204 + * If we have "bunzip2 -q", try using that.
  205 + */
  206 + if (!canRunCommand("bunzip2 -V")) {
  207 + skipping("Can't run bunzip2 program on this platform");
  208 + return;
  209 + }
  210 + assert((a = archive_read_new()) != NULL);
  211 + assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
  212 + assertEqualIntA(a, ARCHIVE_OK,
  213 + archive_read_append_filter_program(a, "bunzip2 -q"));
  214 + assertEqualIntA(a, ARCHIVE_OK,
  215 + archive_read_open_memory(a, archive, sizeof(archive)));
  216 + assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN));
  217 + assertEqualIntA(a, ARCHIVE_WARN, archive_read_close(a));
  218 + assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  219 +}

0 comments on commit 82b5d8d

Please sign in to comment.
Something went wrong with that request. Please try again.