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
26#include "archive_platform.h"
27__FBSDID("$FreeBSD$");
28
29#include "archive_write_private.h"
30#include "archive_options_private.h"
31
32static int	archive_set_format_option(struct archive *a,
33		    const char *m, const char *o, const char *v);
34static int	archive_set_filter_option(struct archive *a,
35		    const char *m, const char *o, const char *v);
36static int	archive_set_option(struct archive *a,
37		    const char *m, const char *o, const char *v);
38
39int
40archive_write_set_format_option(struct archive *a, const char *m, const char *o,
41    const char *v)
42{
43	return _archive_set_option(a, m, o, v,
44	    ARCHIVE_WRITE_MAGIC, "archive_write_set_format_option",
45	    archive_set_format_option);
46}
47
48int
49archive_write_set_filter_option(struct archive *a, const char *m, const char *o,
50    const char *v)
51{
52	return _archive_set_option(a, m, o, v,
53	    ARCHIVE_WRITE_MAGIC, "archive_write_set_filter_option",
54	    archive_set_filter_option);
55}
56
57int
58archive_write_set_option(struct archive *a, const char *m, const char *o,
59    const char *v)
60{
61	return _archive_set_option(a, m, o, v,
62	    ARCHIVE_WRITE_MAGIC, "archive_write_set_option",
63	    archive_set_option);
64}
65
66int
67archive_write_set_options(struct archive *a, const char *options)
68{
69	return _archive_set_options(a, options,
70	    ARCHIVE_WRITE_MAGIC, "archive_write_set_options",
71	    archive_set_option);
72}
73
74static int
75archive_set_format_option(struct archive *_a, const char *m, const char *o,
76    const char *v)
77{
78	struct archive_write *a = (struct archive_write *)_a;
79
80	if (a->format_name == NULL)
81		return (m == NULL)?ARCHIVE_FAILED:ARCHIVE_WARN - 1;
82	/* If the format name didn't match, return a special code for
83	 * _archive_set_option[s]. */
84	if (m != NULL && strcmp(m, a->format_name) != 0)
85		return (ARCHIVE_WARN - 1);
86	if (a->format_options == NULL)
87		return (ARCHIVE_WARN);
88	return a->format_options(a, o, v);
89}
90
91static int
92archive_set_filter_option(struct archive *_a, const char *m, const char *o,
93    const char *v)
94{
95	struct archive_write *a = (struct archive_write *)_a;
96	struct archive_write_filter *filter;
97	int r, rv = ARCHIVE_WARN;
98
99	for (filter = a->filter_first; filter != NULL; filter = filter->next_filter) {
100		if (filter->options == NULL)
101			continue;
102		if (m != NULL && strcmp(filter->name, m) != 0)
103			continue;
104
105		r = filter->options(filter, o, v);
106
107		if (r == ARCHIVE_FATAL)
108			return (ARCHIVE_FATAL);
109
110		if (m != NULL)
111			return (r);
112
113		if (r == ARCHIVE_OK)
114			rv = ARCHIVE_OK;
115	}
116	/* If the filter name didn't match, return a special code for
117	 * _archive_set_option[s]. */
118	if (rv == ARCHIVE_WARN && m != NULL)
119		rv = ARCHIVE_WARN - 1;
120	return (rv);
121}
122
123static int
124archive_set_option(struct archive *a, const char *m, const char *o,
125    const char *v)
126{
127	return _archive_set_either_option(a, m, o, v,
128	    archive_set_format_option,
129	    archive_set_filter_option);
130}
131