archive_read_set_options.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. size_t i;
  72. int r, rv = ARCHIVE_WARN, matched_modules = 0;
  73. for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
  74. struct archive_format_descriptor *format = &a->formats[i];
  75. if (format->options == NULL || format->name == NULL)
  76. /* This format does not support option. */
  77. continue;
  78. if (m != NULL) {
  79. if (strcmp(format->name, m) != 0)
  80. continue;
  81. ++matched_modules;
  82. }
  83. a->format = format;
  84. r = format->options(a, o, v);
  85. a->format = NULL;
  86. if (r == ARCHIVE_FATAL)
  87. return (ARCHIVE_FATAL);
  88. if (r == ARCHIVE_OK)
  89. rv = ARCHIVE_OK;
  90. }
  91. /* If the format name didn't match, return a special code for
  92. * _archive_set_option[s]. */
  93. if (m != NULL && matched_modules == 0)
  94. return ARCHIVE_WARN - 1;
  95. return (rv);
  96. }
  97. static int
  98. archive_set_filter_option(struct archive *_a, const char *m, const char *o,
  99. const char *v)
  100. {
  101. struct archive_read *a = (struct archive_read *)_a;
  102. struct archive_read_filter *filter;
  103. struct archive_read_filter_bidder *bidder;
  104. int r, rv = ARCHIVE_WARN, matched_modules = 0;
  105. for (filter = a->filter; filter != NULL; filter = filter->upstream) {
  106. bidder = filter->bidder;
  107. if (bidder == NULL)
  108. continue;
  109. if (bidder->options == NULL)
  110. /* This bidder does not support option */
  111. continue;
  112. if (m != NULL) {
  113. if (strcmp(filter->name, m) != 0)
  114. continue;
  115. ++matched_modules;
  116. }
  117. r = bidder->options(bidder, o, v);
  118. if (r == ARCHIVE_FATAL)
  119. return (ARCHIVE_FATAL);
  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 (m != NULL && matched_modules == 0)
  126. return 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. }