util.c revision 299742
14Srgrimes/*
21690Sdg * util.c:  general routines defying categorization; eventually I
31690Sdg *          suspect they'll end up in libsvn_subr, but don't want to
41690Sdg *          pollute that right now.  Note that nothing in here is
5174395Sjkoshy *          specific to working copies.
64Srgrimes *
74Srgrimes * ====================================================================
84Srgrimes *    Licensed to the Apache Software Foundation (ASF) under one
94Srgrimes *    or more contributor license agreements.  See the NOTICE file
10174395Sjkoshy *    distributed with this work for additional information
11174395Sjkoshy *    regarding copyright ownership.  The ASF licenses this file
12174395Sjkoshy *    to you under the Apache License, Version 2.0 (the
134Srgrimes *    "License"); you may not use this file except in compliance
144Srgrimes *    with the License.  You may obtain a copy of the License at
154Srgrimes *
164Srgrimes *      http://www.apache.org/licenses/LICENSE-2.0
174Srgrimes *
184Srgrimes *    Unless required by applicable law or agreed to in writing,
194Srgrimes *    software distributed under the License is distributed on an
204Srgrimes *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
214Srgrimes *    KIND, either express or implied.  See the License for the
224Srgrimes *    specific language governing permissions and limitations
234Srgrimes *    under the License.
244Srgrimes * ====================================================================
254Srgrimes */
264Srgrimes
274Srgrimes#include <apr_pools.h>
284Srgrimes#include <apr_file_io.h>
294Srgrimes
304Srgrimes#include "svn_io.h"
314Srgrimes#include "svn_types.h"
324Srgrimes#include "svn_error.h"
334Srgrimes#include "svn_dirent_uri.h"
344Srgrimes#include "svn_path.h"
354Srgrimes#include "svn_props.h"
364Srgrimes#include "svn_version.h"
374Srgrimes
384Srgrimes#include "wc.h"   /* just for prototypes of things in this .c file */
394Srgrimes#include "entries.h"
404Srgrimes#include "private/svn_wc_private.h"
41608Srgrimes
424Srgrimes#include "svn_private_config.h"
434Srgrimes
44116182Sobrien
45116182Sobriensvn_error_t *
46116182Sobriensvn_wc__ensure_directory(const char *path,
47118240Speter                         apr_pool_t *pool)
4878983Sjhb{
4971257Speter  svn_node_kind_t kind;
5078983Sjhb
51170640Sjeff  SVN_ERR(svn_io_check_path(path, &kind, pool));
5213203Swollman
531549Srgrimes  if (kind != svn_node_none && kind != svn_node_dir)
5465557Sjasone    {
551549Srgrimes      /* If got an error other than dir non-existence, then we can't
5678983Sjhb         ensure this directory's existence, so just return the error.
5767365Sjhb         Might happen if there's a file in the way, for example. */
58174395Sjkoshy      return svn_error_createf(APR_ENOTDIR, NULL,
5978983Sjhb                               _("'%s' is not a directory"),
6099072Sjulian                               svn_dirent_local_style(path, pool));
6131389Sbde    }
62104964Sjeff  else if (kind == svn_node_none)
6331389Sbde    {
6478983Sjhb      /* The dir doesn't exist, and it's our job to change that. */
6512662Sdg      SVN_ERR(svn_io_make_dir_recursively(path, pool));
66118240Speter    }
67118240Speter  else  /* No problem, the dir already existed, so just leave. */
68118240Speter    SVN_ERR_ASSERT(kind == svn_node_dir);
69118240Speter
70118240Speter  return SVN_NO_ERROR;
711549Srgrimes}
7231389Sbde
731549Srgrimes/* Return the library version number. */
74184042Skmacyconst svn_version_t *
75184042Skmacysvn_wc_version(void)
76184042Skmacy{
77184042Skmacy  SVN_VERSION_BODY;
78184042Skmacy}
79184042Skmacy
80163606Srwatsonsvn_wc_notify_t *
81163606Srwatsonsvn_wc_create_notify(const char *path,
8278983Sjhb                     svn_wc_notify_action_t action,
83167211Srwatson                     apr_pool_t *pool)
84167211Srwatson{
8578983Sjhb  svn_wc_notify_t *ret = apr_pcalloc(pool, sizeof(*ret));
8671527Sjhb  ret->path = path;
87155455Sphk  ret->action = action;
881690Sdg  ret->kind = svn_node_unknown;
8983366Sjulian  ret->content_state = ret->prop_state = svn_wc_notify_state_unknown;
90757Sdg  ret->lock_state = svn_wc_notify_lock_state_unknown;
9199072Sjulian  ret->revision = SVN_INVALID_REVNUM;
92173601Sjulian  ret->old_revision = SVN_INVALID_REVNUM;
93126661Srwatson
94110190Sjulian  return ret;
9578636Sjhb}
96170307Sjeff
97112888Sjeffsvn_wc_notify_t *
98111032Sjuliansvn_wc_create_notify_url(const char *url,
99102266Srwatson                         svn_wc_notify_action_t action,
100170307Sjeff                         apr_pool_t *pool)
10182585Sdillon{
10293793Sbde  svn_wc_notify_t *ret = svn_wc_create_notify(".", action, pool);
103152376Srwatson  ret->url = url;
104152376Srwatson
105152376Srwatson  return ret;
10693793Sbde}
107136837Sphk
108136837Sphk/* Pool cleanup function to clear an svn_error_t *. */
109136837Sphkstatic apr_status_t err_cleanup(void *data)
110136837Sphk{
111136837Sphk  svn_error_clear(data);
112136837Sphk
113136837Sphk  return APR_SUCCESS;
114110190Sjulian}
115110190Sjulian
116113874Sjhbsvn_wc_notify_t *
117155455Sphksvn_wc_dup_notify(const svn_wc_notify_t *notify,
118110190Sjulian                  apr_pool_t *pool)
119139324Sjeff{
120139324Sjeff  svn_wc_notify_t *ret = apr_palloc(pool, sizeof(*ret));
121139324Sjeff
122139324Sjeff  *ret = *notify;
123144061Sjeff
124144061Sjeff  if (ret->path)
125184042Skmacy    ret->path = apr_pstrdup(pool, ret->path);
126184042Skmacy  if (ret->mime_type)
127184042Skmacy    ret->mime_type = apr_pstrdup(pool, ret->mime_type);
1281690Sdg  if (ret->lock)
1291690Sdg    ret->lock = svn_lock_dup(ret->lock, pool);
1304Srgrimes  if (ret->err)
13178983Sjhb    {
13278983Sjhb      ret->err = svn_error_dup(ret->err);
13381493Sjhb      apr_pool_cleanup_register(pool, ret->err, err_cleanup,
1344Srgrimes                                apr_pool_cleanup_null);
135798Swollman    }
13699072Sjulian  if (ret->changelist_name)
13765557Sjasone    ret->changelist_name = apr_pstrdup(pool, ret->changelist_name);
138104297Sjhb  if (ret->merge_range)
139104297Sjhb    ret->merge_range = svn_merge_range_dup(ret->merge_range, pool);
14083366Sjulian  if (ret->url)
14193793Sbde    ret->url = apr_pstrdup(pool, ret->url);
14277015Sbde  if (ret->path_prefix)
14377015Sbde    ret->path_prefix = apr_pstrdup(pool, ret->path_prefix);
144151316Sdavidxu  if (ret->prop_name)
14577015Sbde    ret->prop_name = apr_pstrdup(pool, ret->prop_name);
14665557Sjasone  if (ret->rev_props)
147104297Sjhb    ret->rev_props = svn_prop_hash_dup(ret->rev_props, pool);
148104297Sjhb
149104378Sjmallett  return ret;
15099072Sjulian}
15199072Sjulian
15272911Sjhbsvn_error_t *
153111883Sjhbsvn_wc_external_item2_create(svn_wc_external_item2_t **item,
15481493Sjhb                             apr_pool_t *pool)
155170307Sjeff{
15693390Sjake  *item = apr_pcalloc(pool, sizeof(svn_wc_external_item2_t));
157155455Sphk  return SVN_NO_ERROR;
158104297Sjhb}
15993390Sjake
160172207Sjeff
16193390Sjakesvn_wc_external_item2_t *
16293390Sjakesvn_wc_external_item2_dup(const svn_wc_external_item2_t *item,
163172207Sjeff                          apr_pool_t *pool)
16493390Sjake{
16593390Sjake  svn_wc_external_item2_t *new_item = apr_palloc(pool, sizeof(*new_item));
166170307Sjeff
167170307Sjeff  *new_item = *item;
168177471Sjeff
169177471Sjeff  if (new_item->target_dir)
170170307Sjeff    new_item->target_dir = apr_pstrdup(pool, new_item->target_dir);
171170292Sattilio
172135573Sjhb  if (new_item->url)
17393390Sjake    new_item->url = apr_pstrdup(pool, new_item->url);
17493390Sjake
175132266Sjhb  return new_item;
176132266Sjhb}
177132266Sjhb
178132266Sjhb
179131437Sjhbsvn_boolean_t
180172207Sjeffsvn_wc_match_ignore_list(const char *str, const apr_array_header_t *list,
18193390Sjake                         apr_pool_t *pool)
18293390Sjake{
18393390Sjake  /* For now, we simply forward to svn_cstring_match_glob_list. In the
18493390Sjake     future, if we support more complex ignore patterns, we would iterate
18577015Sbde     over 'list' ourselves, and decide for each pattern how to handle
18693390Sjake     it. */
18793390Sjake
18893390Sjake  return svn_cstring_match_glob_list(str, list);
18993390Sjake}
19093390Sjake
191151316Sdavidxusvn_wc_conflict_description2_t *
192151316Sdavidxusvn_wc_conflict_description_create_text2(const char *local_abspath,
193151316Sdavidxu                                         apr_pool_t *result_pool)
194151316Sdavidxu{
19577015Sbde  svn_wc_conflict_description2_t *conflict;
19693390Sjake
19777015Sbde  SVN_ERR_ASSERT_NO_RETURN(svn_dirent_is_absolute(local_abspath));
198172207Sjeff
19993390Sjake  conflict = apr_pcalloc(result_pool, sizeof(*conflict));
20093390Sjake  conflict->local_abspath = apr_pstrdup(result_pool, local_abspath);
20193390Sjake  conflict->node_kind = svn_node_file;
20293390Sjake  conflict->kind = svn_wc_conflict_kind_text;
203106655Srwatson  conflict->action = svn_wc_conflict_action_edit;
204172207Sjeff  conflict->reason = svn_wc_conflict_reason_edited;
205106655Srwatson  return conflict;
206106655Srwatson}
207111032Sjulian
208118240Spetersvn_wc_conflict_description2_t *
209118240Spetersvn_wc_conflict_description_create_prop2(const char *local_abspath,
210119781Speter                                         svn_node_kind_t node_kind,
211118240Speter                                         const char *property_name,
212170307Sjeff                                         apr_pool_t *result_pool)
213163709Sjb{
214178272Sjeff  svn_wc_conflict_description2_t *conflict;
215170307Sjeff
216118240Speter  SVN_ERR_ASSERT_NO_RETURN(svn_dirent_is_absolute(local_abspath));
217118240Speter
218119781Speter  conflict = apr_pcalloc(result_pool, sizeof(*conflict));
219118240Speter  conflict->local_abspath = apr_pstrdup(result_pool, local_abspath);
22093793Sbde  conflict->node_kind = node_kind;
221112888Sjeff  conflict->kind = svn_wc_conflict_kind_property;
22293793Sbde  conflict->property_name = apr_pstrdup(result_pool, property_name);
223114983Sjhb  return conflict;
224195702Skib}
22593793Sbde
226114983Sjhbsvn_wc_conflict_description2_t *
22793793Sbdesvn_wc_conflict_description_create_tree2(
22893793Sbde  const char *local_abspath,
229177471Sjeff  svn_node_kind_t node_kind,
230177471Sjeff  svn_wc_operation_t operation,
231177471Sjeff  const svn_wc_conflict_version_t *src_left_version,
232177471Sjeff  const svn_wc_conflict_version_t *src_right_version,
233177471Sjeff  apr_pool_t *result_pool)
234177471Sjeff{
235177471Sjeff  svn_wc_conflict_description2_t *conflict;
236177471Sjeff
237177471Sjeff  SVN_ERR_ASSERT_NO_RETURN(svn_dirent_is_absolute(local_abspath));
23865557Sjasone
239155455Sphk  conflict = apr_pcalloc(result_pool, sizeof(*conflict));
24081493Sjhb  conflict->local_abspath = apr_pstrdup(result_pool, local_abspath);
24124691Speter  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