archive_write_set_options.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*-
  2. * Copyright (c) 2003-2010 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_write_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_write_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_WRITE_MAGIC, "archive_write_set_format_option",
  41. archive_set_format_option);
  42. }
  43. int
  44. archive_write_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_WRITE_MAGIC, "archive_write_set_filter_option",
  49. archive_set_filter_option);
  50. }
  51. int
  52. archive_write_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_WRITE_MAGIC, "archive_write_set_option",
  57. archive_set_option);
  58. }
  59. int
  60. archive_write_set_options(struct archive *a, const char *options)
  61. {
  62. return _archive_set_options(a, options,
  63. ARCHIVE_WRITE_MAGIC, "archive_write_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_write *a = (struct archive_write *)_a;
  71. if (a->format_name == NULL)
  72. return (m == NULL)?ARCHIVE_FAILED:ARCHIVE_WARN - 1;
  73. /* If the format name didn't match, return a special code for
  74. * _archive_set_option[s]. */
  75. if (m != NULL && strcmp(m, a->format_name) != 0)
  76. return (ARCHIVE_WARN - 1);
  77. if (a->format_options == NULL)
  78. return (ARCHIVE_WARN);
  79. return a->format_options(a, o, v);
  80. }
  81. static int
  82. archive_set_filter_option(struct archive *_a, const char *m, const char *o,
  83. const char *v)
  84. {
  85. struct archive_write *a = (struct archive_write *)_a;
  86. struct archive_write_filter *filter;
  87. int r, rv = ARCHIVE_WARN;
  88. for (filter = a->filter_first; filter != NULL; filter = filter->next_filter) {
  89. if (filter->options == NULL)
  90. continue;
  91. if (m != NULL && strcmp(filter->name, m) != 0)
  92. continue;
  93. r = filter->options(filter, o, v);
  94. if (r == ARCHIVE_FATAL)
  95. return (ARCHIVE_FATAL);
  96. if (m != NULL)
  97. return (r);
  98. if (r == ARCHIVE_OK)
  99. rv = ARCHIVE_OK;
  100. }
  101. /* If the filter name didn't match, return a special code for
  102. * _archive_set_option[s]. */
  103. if (rv == ARCHIVE_WARN && m != NULL)
  104. rv = ARCHIVE_WARN - 1;
  105. return (rv);
  106. }
  107. static int
  108. archive_set_option(struct archive *a, const char *m, const char *o,
  109. const char *v)
  110. {
  111. return _archive_set_either_option(a, m, o, v,
  112. archive_set_format_option,
  113. archive_set_filter_option);
  114. }