util.c revision 299742
1/*
2 * util.c:  general routines defying categorization; eventually I
3 *          suspect they'll end up in libsvn_subr, but don't want to
4 *          pollute that right now.  Note that nothing in here is
5 *          specific to working copies.
6 *
7 * ====================================================================
8 *    Licensed to the Apache Software Foundation (ASF) under one
9 *    or more contributor license agreements.  See the NOTICE file
10 *    distributed with this work for additional information
11 *    regarding copyright ownership.  The ASF licenses this file
12 *    to you under the Apache License, Version 2.0 (the
13 *    "License"); you may not use this file except in compliance
14 *    with the License.  You may obtain a copy of the License at
15 *
16 *      http://www.apache.org/licenses/LICENSE-2.0
17 *
18 *    Unless required by applicable law or agreed to in writing,
19 *    software distributed under the License is distributed on an
20 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 *    KIND, either express or implied.  See the License for the
22 *    specific language governing permissions and limitations
23 *    under the License.
24 * ====================================================================
25 */
26
27#include <apr_pools.h>
28#include <apr_file_io.h>
29
30#include "svn_io.h"
31#include "svn_types.h"
32#include "svn_error.h"
33#include "svn_dirent_uri.h"
34#include "svn_path.h"
35#include "svn_props.h"
36#include "svn_version.h"
37
38#include "wc.h"   /* just for prototypes of things in this .c file */
39#include "entries.h"
40#include "private/svn_wc_private.h"
41
42#include "svn_private_config.h"
43
44
45svn_error_t *
46svn_wc__ensure_directory(const char *path,
47                         apr_pool_t *pool)
48{
49  svn_node_kind_t kind;
50
51  SVN_ERR(svn_io_check_path(path, &kind, pool));
52
53  if (kind != svn_node_none && kind != svn_node_dir)
54    {
55      /* If got an error other than dir non-existence, then we can't
56         ensure this directory's existence, so just return the error.
57         Might happen if there's a file in the way, for example. */
58      return svn_error_createf(APR_ENOTDIR, NULL,
59                               _("'%s' is not a directory"),
60                               svn_dirent_local_style(path, pool));
61    }
62  else if (kind == svn_node_none)
63    {
64      /* The dir doesn't exist, and it's our job to change that. */
65      SVN_ERR(svn_io_make_dir_recursively(path, pool));
66    }
67  else  /* No problem, the dir already existed, so just leave. */
68    SVN_ERR_ASSERT(kind == svn_node_dir);
69
70  return SVN_NO_ERROR;
71}
72
73/* Return the library version number. */
74const svn_version_t *
75svn_wc_version(void)
76{
77  SVN_VERSION_BODY;
78}
79
80svn_wc_notify_t *
81svn_wc_create_notify(const char *path,
82                     svn_wc_notify_action_t action,
83                     apr_pool_t *pool)
84{
85  svn_wc_notify_t *ret = apr_pcalloc(pool, sizeof(*ret));
86  ret->path = path;
87  ret->action = action;
88  ret->kind = svn_node_unknown;
89  ret->content_state = ret->prop_state = svn_wc_notify_state_unknown;
90  ret->lock_state = svn_wc_notify_lock_state_unknown;
91  ret->revision = SVN_INVALID_REVNUM;
92  ret->old_revision = SVN_INVALID_REVNUM;
93
94  return ret;
95}
96
97svn_wc_notify_t *
98svn_wc_create_notify_url(const char *url,
99                         svn_wc_notify_action_t action,
100                         apr_pool_t *pool)
101{
102  svn_wc_notify_t *ret = svn_wc_create_notify(".", action, pool);
103  ret->url = url;
104
105  return ret;
106}
107
108/* Pool cleanup function to clear an svn_error_t *. */
109static apr_status_t err_cleanup(void *data)
110{
111  svn_error_clear(data);
112
113  return APR_SUCCESS;
114}
115
116svn_wc_notify_t *
117svn_wc_dup_notify(const svn_wc_notify_t *notify,
118                  apr_pool_t *pool)
119{
120  svn_wc_notify_t *ret = apr_palloc(pool, sizeof(*ret));
121
122  *ret = *notify;
123
124  if (ret->path)
125    ret->path = apr_pstrdup(pool, ret->path);
126  if (ret->mime_type)
127    ret->mime_type = apr_pstrdup(pool, ret->mime_type);
128  if (ret->lock)
129    ret->lock = svn_lock_dup(ret->lock, pool);
130  if (ret->err)
131    {
132      ret->err = svn_error_dup(ret->err);
133      apr_pool_cleanup_register(pool, ret->err, err_cleanup,
134                                apr_pool_cleanup_null);
135    }
136  if (ret->changelist_name)
137    ret->changelist_name = apr_pstrdup(pool, ret->changelist_name);
138  if (ret->merge_range)
139    ret->merge_range = svn_merge_range_dup(ret->merge_range, pool);
140  if (ret->url)
141    ret->url = apr_pstrdup(pool, ret->url);
142  if (ret->path_prefix)
143    ret->path_prefix = apr_pstrdup(pool, ret->path_prefix);
144  if (ret->prop_name)
145    ret->prop_name = apr_pstrdup(pool, ret->prop_name);
146  if (ret->rev_props)
147    ret->rev_props = svn_prop_hash_dup(ret->rev_props, pool);
148
149  return ret;
150}
151
152svn_error_t *
153svn_wc_external_item2_create(svn_wc_external_item2_t **item,
154                             apr_pool_t *pool)
155{
156  *item = apr_pcalloc(pool, sizeof(svn_wc_external_item2_t));
157  return SVN_NO_ERROR;
158}
159
160
161svn_wc_external_item2_t *
162svn_wc_external_item2_dup(const svn_wc_external_item2_t *item,
163                          apr_pool_t *pool)
164{
165  svn_wc_external_item2_t *new_item = apr_palloc(pool, sizeof(*new_item));
166
167  *new_item = *item;
168
169  if (new_item->target_dir)
170    new_item->target_dir = apr_pstrdup(pool, new_item->target_dir);
171
172  if (new_item->url)
173    new_item->url = apr_pstrdup(pool, new_item->url);
174
175  return new_item;
176}
177
178
179svn_boolean_t
180svn_wc_match_ignore_list(const char *str, const apr_array_header_t *list,
181                         apr_pool_t *pool)
182{
183  /* For now, we simply forward to svn_cstring_match_glob_list. In the
184     future, if we support more complex ignore patterns, we would iterate
185     over 'list' ourselves, and decide for each pattern how to handle
186     it. */
187
188  return svn_cstring_match_glob_list(str, list);
189}
190
191svn_wc_conflict_description2_t *
192svn_wc_conflict_description_create_text2(const char *local_abspath,
193                                         apr_pool_t *result_pool)
194{
195  svn_wc_conflict_description2_t *conflict;
196
197  SVN_ERR_ASSERT_NO_RETURN(svn_dirent_is_absolute(local_abspath));
198
199  conflict = apr_pcalloc(result_pool, sizeof(*conflict));
200  conflict->local_abspath = apr_pstrdup(result_pool, local_abspath);
201  conflict->node_kind = svn_node_file;
202  conflict->kind = svn_wc_conflict_kind_text;
203  conflict->action = svn_wc_conflict_action_edit;
204  conflict->reason = svn_wc_conflict_reason_edited;
205  return conflict;
206}
207
208svn_wc_conflict_description2_t *
209svn_wc_conflict_description_create_prop2(const char *local_abspath,
210                                         svn_node_kind_t node_kind,
211                                         const char *property_name,
212                                         apr_pool_t *result_pool)
213{
214  svn_wc_conflict_description2_t *conflict;
215
216  SVN_ERR_ASSERT_NO_RETURN(svn_dirent_is_absolute(local_abspath));
217
218  conflict = apr_pcalloc(result_pool, sizeof(*conflict));
219  conflict->local_abspath = apr_pstrdup(result_pool, local_abspath);
220  conflict->node_kind = node_kind;
221  conflict->kind = svn_wc_conflict_kind_property;
222  conflict->property_name = apr_pstrdup(result_pool, property_name);
223  return conflict;
224}
225
226svn_wc_conflict_description2_t *
227svn_wc_conflict_description_create_tree2(
228  const char *local_abspath,
229  svn_node_kind_t node_kind,
230  svn_wc_operation_t operation,
231  const svn_wc_conflict_version_t *src_left_version,
232  const svn_wc_conflict_version_t *src_right_version,
233  apr_pool_t *result_pool)
234{
235  svn_wc_conflict_description2_t *conflict;
236
237  SVN_ERR_ASSERT_NO_RETURN(svn_dirent_is_absolute(local_abspath));
238
239  conflict = apr_pcalloc(result_pool, sizeof(*conflict));
240  conflict->local_abspath = apr_pstrdup(result_pool, local_abspath);
241  conflict->node_kind = node_kind;
242  conflict->kind = svn_wc_conflict_kind_tree;
243  conflict->operation = operation;
244  conflict->src_left_version = svn_wc_conflict_version_dup(src_left_version,
245                                                           result_pool);
246  conflict->src_right_version = svn_wc_conflict_version_dup(src_right_version,
247                                                            result_pool);
248  return conflict;
249}
250
251svn_wc_conflict_version_t *
252svn_wc_conflict_version_create2(const char *repos_url,
253                                const char *repos_uuid,
254                                const char *repos_relpath,
255                                svn_revnum_t revision,
256                                svn_node_kind_t kind,
257                                apr_pool_t *result_pool)
258{
259  svn_wc_conflict_version_t *version;
260
261  version = apr_pcalloc(result_pool, sizeof(*version));
262
263    SVN_ERR_ASSERT_NO_RETURN(svn_uri_is_canonical(repos_url, result_pool)
264                             && svn_relpath_is_canonical(repos_relpath)
265                             && SVN_IS_VALID_REVNUM(revision)
266                             /* ### repos_uuid can be NULL :( */);
267
268  version->repos_url = repos_url;
269  version->peg_rev = revision;
270  version->path_in_repos = repos_relpath;
271  version->node_kind = kind;
272  version->repos_uuid = repos_uuid;
273
274  return version;
275}
276
277svn_wc_conflict_description2_t *
278svn_wc_conflict_description2_dup(const svn_wc_conflict_description2_t *conflict,
279                                  apr_pool_t *pool)
280{
281  svn_wc_conflict_description2_t *new_conflict;
282
283  new_conflict = apr_pcalloc(pool, sizeof(*new_conflict));
284
285  /* Shallow copy all members. */
286  *new_conflict = *conflict;
287
288  if (conflict->local_abspath)
289    new_conflict->local_abspath = apr_pstrdup(pool, conflict->local_abspath);
290  if (conflict->property_name)
291    new_conflict->property_name = apr_pstrdup(pool, conflict->property_name);
292  if (conflict->mime_type)
293    new_conflict->mime_type = apr_pstrdup(pool, conflict->mime_type);
294  if (conflict->base_abspath)
295    new_conflict->base_abspath = apr_pstrdup(pool, conflict->base_abspath);
296  if (conflict->their_abspath)
297    new_conflict->their_abspath = apr_pstrdup(pool, conflict->their_abspath);
298  if (conflict->my_abspath)
299    new_conflict->my_abspath = apr_pstrdup(pool, conflict->my_abspath);
300  if (conflict->merged_file)
301    new_conflict->merged_file = apr_pstrdup(pool, conflict->merged_file);
302  if (conflict->src_left_version)
303    new_conflict->src_left_version =
304      svn_wc_conflict_version_dup(conflict->src_left_version, pool);
305  if (conflict->src_right_version)
306    new_conflict->src_right_version =
307      svn_wc_conflict_version_dup(conflict->src_right_version, pool);
308
309  /* ### For property conflicts, cd2 stores prop_reject_abspath in
310   * ### their_abspath, and stores theirs_abspath in merged_file. */
311  if (conflict->prop_reject_abspath)
312    new_conflict->prop_reject_abspath = new_conflict->their_abspath;
313
314  if (conflict->prop_value_base)
315    new_conflict->prop_value_base =
316      svn_string_dup(conflict->prop_value_base, pool);
317  if (conflict->prop_value_working)
318    new_conflict->prop_value_working =
319      svn_string_dup(conflict->prop_value_working, pool);
320  if (conflict->prop_value_incoming_old)
321    new_conflict->prop_value_incoming_old =
322      svn_string_dup(conflict->prop_value_incoming_old, pool);
323  if (conflict->prop_value_incoming_new)
324    new_conflict->prop_value_incoming_new =
325      svn_string_dup(conflict->prop_value_incoming_new, pool);
326
327  return new_conflict;
328}
329
330svn_wc_conflict_version_t *
331svn_wc_conflict_version_dup(const svn_wc_conflict_version_t *version,
332                            apr_pool_t *result_pool)
333{
334
335  svn_wc_conflict_version_t *new_version;
336
337  if (version == NULL)
338    return NULL;
339
340  new_version = apr_pcalloc(result_pool, sizeof(*new_version));
341
342  /* Shallow copy all members. */
343  *new_version = *version;
344
345  if (version->repos_url)
346    new_version->repos_url = apr_pstrdup(result_pool, version->repos_url);
347
348  if (version->path_in_repos)
349    new_version->path_in_repos = apr_pstrdup(result_pool,
350                                             version->path_in_repos);
351
352  if (version->repos_uuid)
353    new_version->repos_uuid = apr_pstrdup(result_pool, version->repos_uuid);
354
355  return new_version;
356}
357
358svn_wc_conflict_description_t *
359svn_wc__cd2_to_cd(const svn_wc_conflict_description2_t *conflict,
360                  apr_pool_t *result_pool)
361{
362  svn_wc_conflict_description_t *new_conflict;
363
364  if (conflict == NULL)
365    return NULL;
366
367  new_conflict = apr_pcalloc(result_pool, sizeof(*new_conflict));
368
369  new_conflict->path = apr_pstrdup(result_pool, conflict->local_abspath);
370  new_conflict->node_kind = conflict->node_kind;
371  new_conflict->kind = conflict->kind;
372  new_conflict->action = conflict->action;
373  new_conflict->reason = conflict->reason;
374  if (conflict->src_left_version)
375    new_conflict->src_left_version =
376          svn_wc_conflict_version_dup(conflict->src_left_version, result_pool);
377  if (conflict->src_right_version)
378    new_conflict->src_right_version =
379          svn_wc_conflict_version_dup(conflict->src_right_version, result_pool);
380
381  switch (conflict->kind)
382    {
383
384      case svn_wc_conflict_kind_property:
385        new_conflict->property_name = apr_pstrdup(result_pool,
386                                                  conflict->property_name);
387        /* Falling through. */
388
389      case svn_wc_conflict_kind_text:
390        new_conflict->is_binary = conflict->is_binary;
391        if (conflict->mime_type)
392          new_conflict->mime_type = apr_pstrdup(result_pool,
393                                                conflict->mime_type);
394        if (conflict->base_abspath)
395          new_conflict->base_file = apr_pstrdup(result_pool,
396                                                conflict->base_abspath);
397        if (conflict->their_abspath)
398          new_conflict->their_file = apr_pstrdup(result_pool,
399                                                 conflict->their_abspath);
400        if (conflict->my_abspath)
401          new_conflict->my_file = apr_pstrdup(result_pool,
402                                              conflict->my_abspath);
403        if (conflict->merged_file)
404          new_conflict->merged_file = apr_pstrdup(result_pool,
405                                                  conflict->merged_file);
406        break;
407
408      case svn_wc_conflict_kind_tree:
409        new_conflict->operation = conflict->operation;
410        break;
411    }
412
413  /* A NULL access baton is allowable by the API. */
414  new_conflict->access = NULL;
415
416  return new_conflict;
417}
418
419
420svn_error_t *
421svn_wc__fetch_kind_func(svn_node_kind_t *kind,
422                        void *baton,
423                        const char *path,
424                        svn_revnum_t base_revision,
425                        apr_pool_t *scratch_pool)
426{
427  struct svn_wc__shim_fetch_baton_t *sfb = baton;
428  const char *local_abspath = svn_dirent_join(sfb->base_abspath, path,
429                                              scratch_pool);
430
431  SVN_ERR(svn_wc__db_read_kind(kind, sfb->db, local_abspath,
432                               FALSE /* allow_missing */,
433                               TRUE /* show_deleted */,
434                               FALSE /* show_hidden */,
435                               scratch_pool));
436
437  return SVN_NO_ERROR;
438}
439
440
441svn_error_t *
442svn_wc__fetch_props_func(apr_hash_t **props,
443                         void *baton,
444                         const char *path,
445                         svn_revnum_t base_revision,
446                         apr_pool_t *result_pool,
447                         apr_pool_t *scratch_pool)
448{
449  struct svn_wc__shim_fetch_baton_t *sfb = baton;
450  const char *local_abspath = svn_dirent_join(sfb->base_abspath, path,
451                                              scratch_pool);
452  svn_error_t *err;
453
454  if (sfb->fetch_base)
455    err = svn_wc__db_base_get_props(props, sfb->db, local_abspath, result_pool,
456                                    scratch_pool);
457  else
458    err = svn_wc__db_read_props(props, sfb->db, local_abspath,
459                                result_pool, scratch_pool);
460
461  /* If the path doesn't exist, just return an empty set of props. */
462  if (err && err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
463    {
464      svn_error_clear(err);
465      *props = apr_hash_make(result_pool);
466    }
467  else if (err)
468    return svn_error_trace(err);
469
470  return SVN_NO_ERROR;
471}
472
473
474svn_error_t *
475svn_wc__fetch_base_func(const char **filename,
476                        void *baton,
477                        const char *path,
478                        svn_revnum_t base_revision,
479                        apr_pool_t *result_pool,
480                        apr_pool_t *scratch_pool)
481{
482  struct svn_wc__shim_fetch_baton_t *sfb = baton;
483  const svn_checksum_t *checksum;
484  svn_error_t *err;
485  const char *local_abspath = svn_dirent_join(sfb->base_abspath, path,
486                                              scratch_pool);
487
488  err = svn_wc__db_base_get_info(NULL, NULL, NULL, NULL, NULL, NULL,
489                                 NULL, NULL, NULL, NULL, &checksum,
490                                 NULL, NULL, NULL, NULL, NULL,
491                                 sfb->db, local_abspath,
492                                 scratch_pool, scratch_pool);
493  if (err && err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
494    {
495      svn_error_clear(err);
496      *filename = NULL;
497      return SVN_NO_ERROR;
498    }
499  else if (err)
500    return svn_error_trace(err);
501
502  if (checksum == NULL)
503    {
504      *filename = NULL;
505      return SVN_NO_ERROR;
506    }
507
508  SVN_ERR(svn_wc__db_pristine_get_path(filename, sfb->db, local_abspath,
509                                       checksum, scratch_pool, scratch_pool));
510
511  return SVN_NO_ERROR;
512}
513