archive_read_set_options.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*-
  2. * Copyright (c) 2011 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. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #include "archive_read_private.h"
  28. #include "archive_options_private.h"
  29. static int archive_set_format_option(struct archive *a,
  30. const char *m, const char *o, const char *v);
  31. static int archive_set_filter_option(struct archive *a,
  32. const char *m, const char *o, const char *v);
  33. static int archive_set_option(struct archive *a,
  34. const char *m, const char *o, const char *v);
  35. int
  36. archive_read_set_format_option(struct archive *a, const char *m, const char *o,
  37. const char *v)
  38. {
  39. return _archive_set_option(a, m, o, v,
  40. ARCHIVE_READ_MAGIC, "archive_read_set_format_option",
  41. archive_set_format_option);
  42. }
  43. int
  44. archive_read_set_filter_option(struct archive *a, const char *m, const char *o,
  45. const char *v)
  46. {
  47. return _archive_set_option(a, m, o, v,
  48. ARCHIVE_READ_MAGIC, "archive_read_set_filter_option",
  49. archive_set_filter_option);
  50. }
  51. int
  52. archive_read_set_option(struct archive *a, const char *m, const char *o,
  53. const char *v)
  54. {
  55. return _archive_set_option(a, m, o, v,
  56. ARCHIVE_READ_MAGIC, "archive_read_set_option",
  57. archive_set_option);
  58. }
  59. int
  60. archive_read_set_options(struct archive *a, const char *options)
  61. {
  62. return _archive_set_options(a, options,
  63. ARCHIVE_READ_MAGIC, "archive_read_set_options",
  64. archive_set_option);
  65. }
  66. static int
  67. archive_set_format_option(struct archive *_a, const char *m, const char *o,
  68. const char *v)
  69. {
  70. struct archive_read *a = (struct archive_read *)_a;
  71. struct archive_format_descriptor *format;
  72. size_t i;
  73. int r, rv = ARCHIVE_WARN;
  74. for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
  75. format = &a->formats[i];
  76. if (format == NULL || format->options == NULL ||
  77. format->name == NULL)
  78. /* This format does not support option. */
  79. continue;
  80. if (m != NULL && strcmp(format->name, m) != 0)
  81. continue;
  82. a->format = format;
  83. r = format->options(a, o, v);
  84. a->format = NULL;
  85. if (r == ARCHIVE_FATAL)
  86. return (ARCHIVE_FATAL);
  87. if (m != NULL)
  88. return (r);
  89. if (r == ARCHIVE_OK)
  90. rv = ARCHIVE_OK;
  91. }
  92. /* If the format name didn't match, return a special code for
  93. * _archive_set_option[s]. */
  94. if (rv == ARCHIVE_WARN && m != NULL)
  95. rv = ARCHIVE_WARN - 1;
  96. return (rv);
  97. }
  98. static int
  99. archive_set_filter_option(struct archive *_a, const char *m, const char *o,
  100. const char *v)
  101. {
  102. struct archive_read *a = (struct archive_read *)_a;
  103. struct archive_read_filter *filter;
  104. struct archive_read_filter_bidder *bidder;
  105. int r, rv = ARCHIVE_WARN;
  106. for (filter = a->filter; filter != NULL; filter = filter->upstream) {
  107. bidder = filter->bidder;
  108. if (bidder == NULL)
  109. continue;
  110. if (bidder->options == NULL)
  111. /* This bidder does not support option */
  112. continue;
  113. if (m != NULL && strcmp(filter->name, m) != 0)
  114. continue;
  115. r = bidder->options(bidder, o, v);
  116. if (r == ARCHIVE_FATAL)
  117. return (ARCHIVE_FATAL);
  118. if (m != NULL)
  119. return (r);
  120. if (r == ARCHIVE_OK)
  121. rv = ARCHIVE_OK;
  122. }
  123. /* If the filter name didn't match, return a special code for
  124. * _archive_set_option[s]. */
  125. if (rv == ARCHIVE_WARN && m != NULL)
  126. rv = ARCHIVE_WARN - 1;
  127. return (rv);
  128. }
  129. static int
  130. archive_set_option(struct archive *a, const char *m, const char *o,
  131. const char *v)
  132. {
  133. return _archive_set_either_option(a, m, o, v,
  134. archive_set_format_option,
  135. archive_set_filter_option);
  136. }