In the command-line client, forbid
[svn.git] / subversion / svn / props.c
blob1c2088528c573be96c3d12258a74ae009505eedb
1 /*
2 * props.c: Utility functions for property handling
4 * ====================================================================
5 * Copyright (c) 2000-2007 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
19 /* ==================================================================== */
23 /*** Includes. ***/
25 #include <apr_hash.h>
26 #include "svn_cmdline.h"
27 #include "svn_string.h"
28 #include "svn_error.h"
29 #include "svn_subst.h"
30 #include "svn_props.h"
31 #include "svn_string.h"
32 #include "svn_opt.h"
33 #include "svn_xml.h"
34 #include "svn_base64.h"
35 #include "cl.h"
37 #include "svn_private_config.h"
41 svn_error_t *
42 svn_cl__revprop_prepare(const svn_opt_revision_t *revision,
43 apr_array_header_t *targets,
44 const char **URL,
45 apr_pool_t *pool)
47 const char *target;
49 if (revision->kind != svn_opt_revision_number
50 && revision->kind != svn_opt_revision_date
51 && revision->kind != svn_opt_revision_head)
52 return svn_error_create
53 (SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
54 _("Must specify the revision as a number, a date or 'HEAD' "
55 "when operating on a revision property"));
57 /* There must be exactly one target at this point. If it was optional and
58 unspecified by the user, the caller has already added the implicit '.'. */
59 if (targets->nelts != 1)
60 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
61 _("Wrong number of targets specified"));
63 /* (The docs say the target must be either a URL or implicit '.', but
64 explicit WC targets are also accepted.) */
65 target = APR_ARRAY_IDX(targets, 0, const char *);
66 SVN_ERR(svn_client_url_from_path(URL, target, pool));
67 if (*URL == NULL)
68 return svn_error_create
69 (SVN_ERR_UNVERSIONED_RESOURCE, NULL,
70 _("Either a URL or versioned item is required"));
72 return SVN_NO_ERROR;
76 svn_error_t *
77 svn_cl__print_prop_hash(apr_hash_t *prop_hash,
78 svn_boolean_t names_only,
79 apr_pool_t *pool)
81 apr_hash_index_t *hi;
83 for (hi = apr_hash_first(pool, prop_hash); hi; hi = apr_hash_next(hi))
85 const void *key;
86 void *val;
87 const char *pname;
88 svn_string_t *propval;
89 const char *pname_stdout;
91 apr_hash_this(hi, &key, NULL, &val);
92 pname = key;
93 propval = val;
95 if (svn_prop_needs_translation(pname))
96 SVN_ERR(svn_subst_detranslate_string(&propval, propval,
97 TRUE, pool));
99 SVN_ERR(svn_cmdline_cstring_from_utf8(&pname_stdout, pname, pool));
101 /* ### We leave these printfs for now, since if propval wasn't translated
102 * above, we don't know anything about its encoding. In fact, it
103 * might be binary data... */
104 if (names_only)
105 printf(" %s\n", pname_stdout);
106 else
107 printf(" %s : %s\n", pname_stdout, propval->data);
110 return SVN_NO_ERROR;
113 void
114 svn_cl__print_xml_prop(svn_stringbuf_t **outstr,
115 const char* propname,
116 svn_string_t *propval,
117 apr_pool_t *pool)
119 const char *xml_safe;
120 const char *encoding = NULL;
122 if (*outstr == NULL)
123 *outstr = svn_stringbuf_create("", pool);
125 if (svn_xml_is_xml_safe(propval->data, propval->len))
127 svn_stringbuf_t *xml_esc = NULL;
128 svn_xml_escape_cdata_string(&xml_esc, propval, pool);
129 xml_safe = xml_esc->data;
131 else
133 const svn_string_t *base64ed = svn_base64_encode_string(propval, pool);
134 encoding = "base64";
135 xml_safe = base64ed->data;
138 if (encoding)
139 svn_xml_make_open_tag(outstr, pool, svn_xml_protect_pcdata,
140 "property", "name", propname,
141 "encoding", encoding, NULL);
142 else
143 svn_xml_make_open_tag(outstr, pool, svn_xml_protect_pcdata,
144 "property", "name", propname, NULL);
146 svn_stringbuf_appendcstr(*outstr, xml_safe);
148 svn_xml_make_close_tag(outstr, pool, "property");
150 return;
153 svn_error_t *
154 svn_cl__print_xml_prop_hash(svn_stringbuf_t **outstr,
155 apr_hash_t *prop_hash,
156 svn_boolean_t names_only,
157 apr_pool_t *pool)
159 apr_hash_index_t *hi;
161 if (*outstr == NULL)
162 *outstr = svn_stringbuf_create("", pool);
164 for (hi = apr_hash_first(pool, prop_hash); hi; hi = apr_hash_next(hi))
166 const void *key;
167 void *val;
168 const char *pname;
169 svn_string_t *propval;
171 apr_hash_this(hi, &key, NULL, &val);
172 pname = key;
173 propval = val;
175 if (names_only)
177 svn_xml_make_open_tag(outstr, pool, svn_xml_self_closing, "property",
178 "name", pname, NULL);
180 else
182 const char *pname_out;
184 if (svn_prop_needs_translation(pname))
185 SVN_ERR(svn_subst_detranslate_string(&propval, propval,
186 TRUE, pool));
188 SVN_ERR(svn_cmdline_cstring_from_utf8(&pname_out, pname, pool));
190 svn_cl__print_xml_prop(outstr, pname_out, propval, pool);
194 return SVN_NO_ERROR;
198 void
199 svn_cl__check_boolean_prop_val(const char *propname, const char *propval,
200 apr_pool_t *pool)
202 svn_stringbuf_t *propbuf;
204 if (!svn_prop_is_boolean(propname))
205 return;
207 propbuf = svn_stringbuf_create(propval, pool);
208 svn_stringbuf_strip_whitespace(propbuf);
210 if (propbuf->data[0] == '\0'
211 || strcmp(propbuf->data, "no") == 0
212 || strcmp(propbuf->data, "off") == 0
213 || strcmp(propbuf->data, "false") == 0)
215 svn_error_t *err = svn_error_createf
216 (SVN_ERR_BAD_PROPERTY_VALUE, NULL,
217 _("To turn off the %s property, use 'svn propdel';\n"
218 "setting the property to '%s' will not turn it off."),
219 propname, propval);
220 svn_handle_warning(stderr, err);
221 svn_error_clear(err);