1 | /* |
---|
2 | * cmdline.c: command-line processing |
---|
3 | * |
---|
4 | * ==================================================================== |
---|
5 | * Copyright (c) 2008 CollabNet. All rights reserved. |
---|
6 | * |
---|
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. |
---|
12 | * |
---|
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 | * ==================================================================== |
---|
17 | */ |
---|
18 | |
---|
19 | /* ==================================================================== */ |
---|
20 | |
---|
21 | |
---|
22 | /*** Includes. ***/ |
---|
23 | #include "svn_client.h" |
---|
24 | #include "svn_error.h" |
---|
25 | #include "svn_path.h" |
---|
26 | #include "svn_opt.h" |
---|
27 | #include "svn_utf.h" |
---|
28 | |
---|
29 | #include "client.h" |
---|
30 | |
---|
31 | #include "private/svn_opt_private.h" |
---|
32 | |
---|
33 | #include "svn_private_config.h" |
---|
34 | |
---|
35 | |
---|
36 | /*** Code. ***/ |
---|
37 | |
---|
38 | #define DEFAULT_ARRAY_SIZE 5 |
---|
39 | |
---|
40 | /* Return true iff ARG is a repository-relative URL: specifically that |
---|
41 | * it starts with the characters "^/". |
---|
42 | * ARG is in UTF-8 encoding. |
---|
43 | * Do not check whether ARG is properly URI-encoded, canonical, or valid |
---|
44 | * in any other way. */ |
---|
45 | static svn_boolean_t |
---|
46 | arg_is_repos_relative_url(const char *arg) |
---|
47 | { |
---|
48 | return (0 == strncmp("^/", arg, 2)); |
---|
49 | } |
---|
50 | |
---|
51 | /* Set *ABSOLUTE_URL to the absolute URL represented by RELATIVE_URL |
---|
52 | * relative to REPOS_ROOT_URL. |
---|
53 | * *ABSOLUTE_URL will end with a peg revision specifier if RELATIVE_URL did. |
---|
54 | * RELATIVE_URL is in repository-relative syntax: |
---|
55 | * "^/[REL-URL][@PEG]", |
---|
56 | * REPOS_ROOT_URL is the absolute URL of the repository root. |
---|
57 | * All strings are in UTF-8 encoding. |
---|
58 | * Allocate *ABSOLUTE_URL in POOL. |
---|
59 | * |
---|
60 | * REPOS_ROOT_URL and RELATIVE_URL do not have to be properly URI-encoded, |
---|
61 | * canonical, or valid in any other way. The caller is expected to perform |
---|
62 | * canonicalization on *ABSOLUTE_URL after the call to the function. |
---|
63 | */ |
---|
64 | static svn_error_t * |
---|
65 | resolve_repos_relative_url(const char **absolute_url, |
---|
66 | const char *relative_url, |
---|
67 | const char *repos_root_url, |
---|
68 | apr_pool_t *pool) |
---|
69 | { |
---|
70 | if (! arg_is_repos_relative_url(relative_url)) |
---|
71 | return svn_error_createf(SVN_ERR_BAD_URL, NULL, |
---|
72 | _("Improper relative URL '%s'"), |
---|
73 | relative_url); |
---|
74 | |
---|
75 | /* No assumptions are made about the canonicalization of the input |
---|
76 | * arguments, it is presumed that the output will be canonicalized after |
---|
77 | * this function, which will remove any duplicate path separator. |
---|
78 | */ |
---|
79 | *absolute_url = apr_pstrcat(pool, repos_root_url, relative_url + 1, NULL); |
---|
80 | |
---|
81 | return SVN_NO_ERROR; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /* Attempt to find the repository root url for TARGET, possibly using CTX for |
---|
86 | * authentication. If one is found and *ROOT_URL is not NULL, then just check |
---|
87 | * that the root url for TARGET matches the value given in *ROOT_URL and |
---|
88 | * return an error if it does not. If one is found and *ROOT_URL is NULL then |
---|
89 | * set *ROOT_URL to the root url for TARGET, allocated from POOL. |
---|
90 | * If a root url is not found for TARGET because it does not exist in the |
---|
91 | * repository, then return with no error. |
---|
92 | * |
---|
93 | * TARGET is a UTF-8 encoded string that is fully canonicalized and escaped. |
---|
94 | */ |
---|
95 | static svn_error_t * |
---|
96 | check_root_url_of_target(const char **root_url, |
---|
97 | const char *target, |
---|
98 | svn_client_ctx_t *ctx, |
---|
99 | apr_pool_t *pool) |
---|
100 | { |
---|
101 | svn_error_t *error; |
---|
102 | const char *tmp_root_url; |
---|
103 | const char *truepath; |
---|
104 | svn_opt_revision_t opt_rev; |
---|
105 | |
---|
106 | SVN_ERR(svn_opt_parse_path(&opt_rev, &truepath, target, pool)); |
---|
107 | |
---|
108 | if ((error = svn_client__get_repos_root(&tmp_root_url, |
---|
109 | truepath, |
---|
110 | &opt_rev, |
---|
111 | NULL, ctx, pool))) |
---|
112 | { |
---|
113 | /* It is OK if the given target does not exist, it just means |
---|
114 | * we will not be able to determine the root url from this particular |
---|
115 | * argument. |
---|
116 | */ |
---|
117 | if ((error->apr_err == SVN_ERR_ENTRY_NOT_FOUND) |
---|
118 | || (error->apr_err == SVN_ERR_WC_NOT_DIRECTORY)) |
---|
119 | { |
---|
120 | svn_error_clear(error); |
---|
121 | return SVN_NO_ERROR; |
---|
122 | } |
---|
123 | else |
---|
124 | return error; |
---|
125 | } |
---|
126 | else if (*root_url != NULL) |
---|
127 | { |
---|
128 | if (strcmp(*root_url, tmp_root_url) != 0) |
---|
129 | return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL, |
---|
130 | _("All non-relative targets must have " |
---|
131 | "the same root URL")); |
---|
132 | } |
---|
133 | else |
---|
134 | *root_url = tmp_root_url; |
---|
135 | |
---|
136 | return SVN_NO_ERROR; |
---|
137 | } |
---|
138 | |
---|
139 | /* Note: This is substantially copied from svn_opt__args_to_target_array() in |
---|
140 | * order to move to libsvn_client while maintaining backward compatibility. */ |
---|
141 | svn_error_t * |
---|
142 | svn_client_args_to_target_array(apr_array_header_t **targets_p, |
---|
143 | apr_getopt_t *os, |
---|
144 | const apr_array_header_t *known_targets, |
---|
145 | svn_client_ctx_t *ctx, |
---|
146 | apr_pool_t *pool) |
---|
147 | { |
---|
148 | int i; |
---|
149 | svn_boolean_t rel_url_found = FALSE; |
---|
150 | const char *root_url = NULL; |
---|
151 | svn_error_t *err = SVN_NO_ERROR; |
---|
152 | apr_array_header_t *input_targets = |
---|
153 | apr_array_make(pool, DEFAULT_ARRAY_SIZE, sizeof(const char *)); |
---|
154 | apr_array_header_t *output_targets = |
---|
155 | apr_array_make(pool, DEFAULT_ARRAY_SIZE, sizeof(const char *)); |
---|
156 | |
---|
157 | /* Step 1: create a master array of targets that are in UTF-8 |
---|
158 | encoding, and come from concatenating the targets left by apr_getopt, |
---|
159 | plus any extra targets (e.g., from the --targets switch.) |
---|
160 | If any of the targets are relative urls, then set the rel_url_found |
---|
161 | flag.*/ |
---|
162 | |
---|
163 | for (; os->ind < os->argc; os->ind++) |
---|
164 | { |
---|
165 | /* The apr_getopt targets are still in native encoding. */ |
---|
166 | const char *raw_target = os->argv[os->ind]; |
---|
167 | const char *utf8_target; |
---|
168 | |
---|
169 | SVN_ERR(svn_utf_cstring_to_utf8(&utf8_target, |
---|
170 | raw_target, pool)); |
---|
171 | |
---|
172 | if (arg_is_repos_relative_url(utf8_target)) |
---|
173 | rel_url_found = TRUE; |
---|
174 | |
---|
175 | APR_ARRAY_PUSH(input_targets, const char *) = utf8_target; |
---|
176 | } |
---|
177 | |
---|
178 | if (known_targets) |
---|
179 | { |
---|
180 | for (i = 0; i < known_targets->nelts; i++) |
---|
181 | { |
---|
182 | /* The --targets array have already been converted to UTF-8, |
---|
183 | because we needed to split up the list with svn_cstring_split. */ |
---|
184 | const char *utf8_target = APR_ARRAY_IDX(known_targets, |
---|
185 | i, const char *); |
---|
186 | |
---|
187 | if (arg_is_repos_relative_url(utf8_target)) |
---|
188 | rel_url_found = TRUE; |
---|
189 | |
---|
190 | APR_ARRAY_PUSH(input_targets, const char *) = utf8_target; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | /* Step 2: process each target. */ |
---|
195 | |
---|
196 | for (i = 0; i < input_targets->nelts; i++) |
---|
197 | { |
---|
198 | const char *utf8_target = APR_ARRAY_IDX(input_targets, i, const char *); |
---|
199 | |
---|
200 | /* Relative urls will be canonicalized when they are resolved later in |
---|
201 | * the function |
---|
202 | */ |
---|
203 | if (arg_is_repos_relative_url(utf8_target)) |
---|
204 | { |
---|
205 | APR_ARRAY_PUSH(output_targets, const char *) = utf8_target; |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | const char *true_target; |
---|
210 | const char *peg_rev; |
---|
211 | const char *target; |
---|
212 | |
---|
213 | /* |
---|
214 | * This is needed so that the target can be properly canonicalized, |
---|
215 | * otherwise the canonicalization does not treat a ".@BASE" as a "." |
---|
216 | * with a BASE peg revision, and it is not canonicalized to "@BASE". |
---|
217 | * If any peg revision exists, it is appended to the final |
---|
218 | * canonicalized path or URL. Do not use svn_opt_parse_path() |
---|
219 | * because the resulting peg revision is a structure that would have |
---|
220 | * to be converted back into a string. Converting from a string date |
---|
221 | * to the apr_time_t field in the svn_opt_revision_value_t and back to |
---|
222 | * a string would not necessarily preserve the exact bytes of the |
---|
223 | * input date, so its easier just to keep it in string form. |
---|
224 | */ |
---|
225 | SVN_ERR(svn_opt__split_arg_at_peg_revision(&true_target, &peg_rev, |
---|
226 | utf8_target, pool)); |
---|
227 | |
---|
228 | /* URLs and wc-paths get treated differently. */ |
---|
229 | if (svn_path_is_url(true_target)) |
---|
230 | { |
---|
231 | SVN_ERR(svn_opt__arg_canonicalize_url(&true_target, |
---|
232 | true_target, pool)); |
---|
233 | } |
---|
234 | else /* not a url, so treat as a path */ |
---|
235 | { |
---|
236 | char *base_name; |
---|
237 | |
---|
238 | SVN_ERR(svn_opt__arg_canonicalize_path(&true_target, |
---|
239 | true_target, pool)); |
---|
240 | |
---|
241 | /* If the target has the same name as a Subversion |
---|
242 | working copy administrative dir, skip it. */ |
---|
243 | base_name = svn_path_basename(true_target, pool); |
---|
244 | |
---|
245 | if (svn_wc_is_adm_dir(base_name, pool)) |
---|
246 | { |
---|
247 | err = svn_error_createf(SVN_ERR_RESERVED_FILENAME_SPECIFIED, |
---|
248 | err, |
---|
249 | _("'%s' ends in a reserved name"), |
---|
250 | utf8_target); |
---|
251 | continue; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | target = apr_pstrcat(pool, true_target, peg_rev, NULL); |
---|
256 | |
---|
257 | if (rel_url_found) |
---|
258 | { |
---|
259 | SVN_ERR(check_root_url_of_target(&root_url, target, |
---|
260 | ctx, pool)); |
---|
261 | } |
---|
262 | |
---|
263 | APR_ARRAY_PUSH(output_targets, const char *) = target; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /* Only resolve relative urls if there were some actually found earlier. */ |
---|
268 | if (rel_url_found) |
---|
269 | { |
---|
270 | /* |
---|
271 | * Use the current directory's root url if one wasn't found using the |
---|
272 | * arguments. |
---|
273 | */ |
---|
274 | if (root_url == NULL) |
---|
275 | SVN_ERR(svn_client_root_url_from_path(&root_url, "", ctx, pool)); |
---|
276 | |
---|
277 | *targets_p = apr_array_make(pool, output_targets->nelts, |
---|
278 | sizeof(const char *)); |
---|
279 | |
---|
280 | for (i = 0; i < output_targets->nelts; i++) |
---|
281 | { |
---|
282 | const char *target = APR_ARRAY_IDX(output_targets, i, |
---|
283 | const char *); |
---|
284 | |
---|
285 | if (arg_is_repos_relative_url(target)) |
---|
286 | { |
---|
287 | const char *abs_target; |
---|
288 | const char *true_target; |
---|
289 | const char *peg_rev; |
---|
290 | |
---|
291 | SVN_ERR(svn_opt__split_arg_at_peg_revision(&true_target, &peg_rev, |
---|
292 | target, pool)); |
---|
293 | |
---|
294 | SVN_ERR(resolve_repos_relative_url(&abs_target, true_target, |
---|
295 | root_url, pool)); |
---|
296 | |
---|
297 | SVN_ERR(svn_opt__arg_canonicalize_url(&true_target, abs_target, |
---|
298 | pool)); |
---|
299 | |
---|
300 | target = apr_pstrcat(pool, true_target, peg_rev, NULL); |
---|
301 | } |
---|
302 | |
---|
303 | APR_ARRAY_PUSH(*targets_p, const char *) = target; |
---|
304 | } |
---|
305 | } |
---|
306 | else |
---|
307 | *targets_p = output_targets; |
---|
308 | |
---|
309 | return err; |
---|
310 | } |
---|