File: locking_commands.c

package info (click to toggle)
subversion 1.8.10-6+deb8u6
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 62,080 kB
  • sloc: ansic: 795,684; python: 115,859; java: 17,742; sh: 13,590; ruby: 12,397; cpp: 11,206; lisp: 7,540; perl: 5,649; sql: 1,466; makefile: 1,110; xml: 577
file content (552 lines) | stat: -rw-r--r-- 19,978 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * locking_commands.c:  Implementation of lock and unlock.
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

/* ==================================================================== */



/*** Includes. ***/

#include "svn_client.h"
#include "svn_hash.h"
#include "client.h"
#include "svn_dirent_uri.h"
#include "svn_path.h"
#include "svn_xml.h"
#include "svn_pools.h"

#include "svn_private_config.h"
#include "private/svn_client_private.h"
#include "private/svn_wc_private.h"


/*** Code. ***/

/* For use with store_locks_callback, below. */
struct lock_baton
{
  const char *base_dir_abspath;
  apr_hash_t *urls_to_paths;
  svn_client_ctx_t *ctx;
  apr_pool_t *pool;
};


/* This callback is called by the ra_layer for each path locked.
 * BATON is a 'struct lock_baton *', PATH is the path being locked,
 * and LOCK is the lock itself.
 *
 * If BATON->base_dir_abspath is not null, then this function either
 * stores the LOCK on REL_URL or removes any lock tokens from REL_URL
 * (depending on whether DO_LOCK is true or false respectively), but
 * only if RA_ERR is null, or (in the unlock case) is something other
 * than SVN_ERR_FS_LOCK_OWNER_MISMATCH.
 *
 * Implements svn_ra_lock_callback_t.
 */
static svn_error_t *
store_locks_callback(void *baton,
                     const char *rel_url,
                     svn_boolean_t do_lock,
                     const svn_lock_t *lock,
                     svn_error_t *ra_err, apr_pool_t *pool)
{
  struct lock_baton *lb = baton;
  svn_wc_notify_t *notify;

  /* Create the notify struct first, so we can tweak it below. */
  notify = svn_wc_create_notify(rel_url,
                                do_lock
                                ? (ra_err
                                   ? svn_wc_notify_failed_lock
                                   : svn_wc_notify_locked)
                                : (ra_err
                                   ? svn_wc_notify_failed_unlock
                                   : svn_wc_notify_unlocked),
                                pool);
  notify->lock = lock;
  notify->err = ra_err;

  if (lb->base_dir_abspath)
    {
      char *path = svn_hash_gets(lb->urls_to_paths, rel_url);
      const char *local_abspath;

      local_abspath = svn_dirent_join(lb->base_dir_abspath, path, pool);

      /* Notify a valid working copy path */
      notify->path = local_abspath;
      notify->path_prefix = lb->base_dir_abspath;

      if (do_lock)
        {
          if (!ra_err)
            {
              SVN_ERR(svn_wc_add_lock2(lb->ctx->wc_ctx, local_abspath, lock,
                                       lb->pool));
              notify->lock_state = svn_wc_notify_lock_state_locked;
            }
          else
            notify->lock_state = svn_wc_notify_lock_state_unchanged;
        }
      else /* unlocking */
        {
          /* Remove our wc lock token either a) if we got no error, or b) if
             we got any error except for owner mismatch.  Note that the only
             errors that are handed to this callback will be locking-related
             errors. */

          if (!ra_err ||
              (ra_err && (ra_err->apr_err != SVN_ERR_FS_LOCK_OWNER_MISMATCH)))
            {
              SVN_ERR(svn_wc_remove_lock2(lb->ctx->wc_ctx, local_abspath,
                                          lb->pool));
              notify->lock_state = svn_wc_notify_lock_state_unlocked;
            }
          else
            notify->lock_state = svn_wc_notify_lock_state_unchanged;
        }
    }
  else
    notify->url = rel_url; /* Notify that path is actually a url  */

  if (lb->ctx->notify_func2)
    lb->ctx->notify_func2(lb->ctx->notify_baton2, notify, pool);

  return SVN_NO_ERROR;
}


/* This is a wrapper around svn_uri_condense_targets() and
 * svn_dirent_condense_targets() (the choice of which is made based on
 * the value of TARGETS_ARE_URIS) which takes care of the
 * single-target special case.
 *
 * Callers are expected to check for an empty *COMMON_PARENT (which
 * means, "there was nothing common") for themselves.
 */
static svn_error_t *
condense_targets(const char **common_parent,
                 apr_array_header_t **target_relpaths,
                 const apr_array_header_t *targets,
                 svn_boolean_t targets_are_uris,
                 svn_boolean_t remove_redundancies,
                 apr_pool_t *result_pool,
                 apr_pool_t *scratch_pool)
{
  if (targets_are_uris)
    {
      SVN_ERR(svn_uri_condense_targets(common_parent, target_relpaths,
                                       targets, remove_redundancies,
                                       result_pool, scratch_pool));
    }
  else
    {
      SVN_ERR(svn_dirent_condense_targets(common_parent, target_relpaths,
                                          targets, remove_redundancies,
                                          result_pool, scratch_pool));
    }

  /* svn_*_condense_targets leaves *TARGET_RELPATHS empty if TARGETS only
     had 1 member, so we special case that. */
  if (apr_is_empty_array(*target_relpaths))
    {
      const char *base_name;

      if (targets_are_uris)
        {
          svn_uri_split(common_parent, &base_name,
                        *common_parent, result_pool);
        }
      else
        {
          svn_dirent_split(common_parent, &base_name,
                           *common_parent, result_pool);
        }
      APR_ARRAY_PUSH(*target_relpaths, const char *) = base_name;
    }

  return SVN_NO_ERROR;
}

/* Lock info. Used in organize_lock_targets.
   ### Maybe return this instead of the ugly hashes? */
struct wc_lock_item_t
{
  svn_revnum_t revision;
  const char *lock_token;
};

/* Set *COMMON_PARENT_URL to the nearest common parent URL of all TARGETS.
 * If TARGETS are local paths, then the entry for each path is examined
 * and *COMMON_PARENT is set to the common parent URL for all the
 * targets (as opposed to the common local path).
 *
 * If there is no common parent, either because the targets are a
 * mixture of URLs and local paths, or because they simply do not
 * share a common parent, then return SVN_ERR_UNSUPPORTED_FEATURE.
 *
 * DO_LOCK is TRUE for locking TARGETS, and FALSE for unlocking them.
 * FORCE is TRUE for breaking or stealing locks, and FALSE otherwise.
 *
 * Each key stored in *REL_TARGETS_P is a path relative to
 * *COMMON_PARENT.  If TARGETS are local paths, then: if DO_LOCK is
 * true, the value is a pointer to the corresponding base_revision
 * (allocated in POOL) for the path, else the value is the lock token
 * (or "" if no token found in the wc).
 *
 * If TARGETS is an array of urls, REL_FS_PATHS_P is set to NULL.
 * Otherwise each key in REL_FS_PATHS_P is an repository path (relative to
 * COMMON_PARENT) mapped to the target path for TARGET (relative to
 * the common parent WC path). working copy targets that they "belong" to.
 *
 * If *COMMON_PARENT is a URL, then the values are a pointer to
 * SVN_INVALID_REVNUM (allocated in pool) if DO_LOCK, else "".
 *
 * TARGETS may not be empty.
 */
static svn_error_t *
organize_lock_targets(const char **common_parent_url,
                      const char **base_dir,
                      apr_hash_t **rel_targets_p,
                      apr_hash_t **rel_fs_paths_p,
                      const apr_array_header_t *targets,
                      svn_boolean_t do_lock,
                      svn_boolean_t force,
                      svn_wc_context_t *wc_ctx,
                      apr_pool_t *result_pool,
                      apr_pool_t *scratch_pool)
{
  const char *common_url = NULL;
  const char *common_dirent = NULL;
  apr_hash_t *rel_targets_ret = apr_hash_make(result_pool);
  apr_hash_t *rel_fs_paths = NULL;
  apr_array_header_t *rel_targets;
  apr_hash_t *wc_info = apr_hash_make(scratch_pool);
  svn_boolean_t url_mode;
  int i;

  SVN_ERR_ASSERT(targets->nelts);
  SVN_ERR(svn_client__assert_homogeneous_target_type(targets));

  url_mode = svn_path_is_url(APR_ARRAY_IDX(targets, 0, const char *));

  if (url_mode)
    {
      svn_revnum_t *invalid_revnum =
        apr_palloc(result_pool, sizeof(*invalid_revnum));

      *invalid_revnum = SVN_INVALID_REVNUM;

      /* Get the common parent URL and a bunch of relpaths, one per target. */
      SVN_ERR(condense_targets(&common_url, &rel_targets, targets,
                               TRUE, TRUE, result_pool, scratch_pool));
      if (! (common_url && *common_url))
        return svn_error_create(SVN_ERR_UNSUPPORTED_FEATURE, NULL,
                                _("No common parent found, unable to operate "
                                  "on disjoint arguments"));

      /* Create mapping of the target relpaths to either
         SVN_INVALID_REVNUM (if our caller is locking) or to an empty
         lock token string (if the caller is unlocking). */
      for (i = 0; i < rel_targets->nelts; i++)
        {
          svn_hash_sets(rel_targets_ret,
                        APR_ARRAY_IDX(rel_targets, i, const char *),
                        do_lock
                        ? (const void *)invalid_revnum
                        : (const void *)"");
        }
    }
  else
    {
      apr_array_header_t *rel_urls, *target_urls;
      apr_pool_t *iterpool = svn_pool_create(scratch_pool);

      /* Get the common parent dirent and a bunch of relpaths, one per
         target. */
      SVN_ERR(condense_targets(&common_dirent, &rel_targets, targets,
                               FALSE, TRUE, result_pool, scratch_pool));
      if (! (common_dirent && *common_dirent))
        return svn_error_create(SVN_ERR_UNSUPPORTED_FEATURE, NULL,
                                _("No common parent found, unable to operate "
                                  "on disjoint arguments"));

      /* Get the URL for each target (which also serves to verify that
         the dirent targets are sane).  */
      target_urls = apr_array_make(scratch_pool, rel_targets->nelts,
                                   sizeof(const char *));
      for (i = 0; i < rel_targets->nelts; i++)
        {
          const char *rel_target;
          const char *repos_relpath;
          const char *repos_root_url;
          const char *target_url;
          struct wc_lock_item_t *wli;
          const char *local_abspath;
          svn_node_kind_t kind;

          svn_pool_clear(iterpool);

          rel_target = APR_ARRAY_IDX(rel_targets, i, const char *);
          local_abspath = svn_dirent_join(common_dirent, rel_target, scratch_pool);
          wli = apr_pcalloc(scratch_pool, sizeof(*wli));

          SVN_ERR(svn_wc__node_get_base(&kind, &wli->revision, &repos_relpath,
                                        &repos_root_url, NULL,
                                        &wli->lock_token,
                                        wc_ctx, local_abspath,
                                        FALSE /* ignore_enoent */,
                                        FALSE /* show_hidden */,
                                        result_pool, iterpool));

          if (kind != svn_node_file)
            return svn_error_createf(SVN_ERR_WC_NOT_FILE, NULL,
                                     _("The node '%s' is not a file"),
                                     svn_dirent_local_style(local_abspath,
                                                            iterpool));

          svn_hash_sets(wc_info, local_abspath, wli);

          target_url = svn_path_url_add_component2(repos_root_url,
                                                   repos_relpath,
                                                   scratch_pool);

          APR_ARRAY_PUSH(target_urls, const char *) = target_url;
        }

      /* Now that we have a bunch of URLs for our dirent targets,
         condense those into a single common parent URL and a bunch of
         paths relative to that. */
      SVN_ERR(condense_targets(&common_url, &rel_urls, target_urls,
                               TRUE, FALSE, result_pool, scratch_pool));
      if (! (common_url && *common_url))
        return svn_error_create(SVN_ERR_UNSUPPORTED_FEATURE, NULL,
                                _("Unable to lock/unlock across multiple "
                                  "repositories"));

      /* Now we need to create a couple of different hash mappings. */
      rel_fs_paths = apr_hash_make(result_pool);
      for (i = 0; i < rel_targets->nelts; i++)
        {
          const char *rel_target, *rel_url;
          const char *local_abspath;

          svn_pool_clear(iterpool);

          /* First, we need to map our REL_URL (which is relative to
             COMMON_URL) to our REL_TARGET (which is relative to
             COMMON_DIRENT). */
          rel_target = APR_ARRAY_IDX(rel_targets, i, const char *);
          rel_url = APR_ARRAY_IDX(rel_urls, i, const char *);
          svn_hash_sets(rel_fs_paths, rel_url,
                        apr_pstrdup(result_pool, rel_target));

          /* Then, we map our REL_URL (again) to either the base
             revision of the dirent target with which it is associated
             (if our caller is locking) or to a (possible empty) lock
             token string (if the caller is unlocking). */
          local_abspath = svn_dirent_join(common_dirent, rel_target, iterpool);

          if (do_lock) /* Lock. */
            {
              svn_revnum_t *revnum;
              struct wc_lock_item_t *wli;
              revnum = apr_palloc(result_pool, sizeof(* revnum));

              wli = svn_hash_gets(wc_info, local_abspath);

              SVN_ERR_ASSERT(wli != NULL);

              *revnum = wli->revision;

              svn_hash_sets(rel_targets_ret, rel_url, revnum);
            }
          else /* Unlock. */
            {
              const char *lock_token;
              struct wc_lock_item_t *wli;

              /* If not forcing the unlock, get the lock token. */
              if (! force)
                {
                  wli = svn_hash_gets(wc_info, local_abspath);

                  SVN_ERR_ASSERT(wli != NULL);

                  if (! wli->lock_token)
                    return svn_error_createf(
                               SVN_ERR_CLIENT_MISSING_LOCK_TOKEN, NULL,
                               _("'%s' is not locked in this working copy"),
                               svn_dirent_local_style(local_abspath,
                                                      scratch_pool));

                  lock_token = wli->lock_token
                                ? apr_pstrdup(result_pool, wli->lock_token)
                                : NULL;
                }
              else
                lock_token = NULL;

              /* If breaking a lock, we shouldn't pass any lock token. */
              svn_hash_sets(rel_targets_ret, rel_url,
                            lock_token ? lock_token : "");
            }
        }

      svn_pool_destroy(iterpool);
    }

  /* Set our return variables. */
  *common_parent_url = common_url;
  *base_dir = common_dirent;
  *rel_targets_p = rel_targets_ret;
  *rel_fs_paths_p = rel_fs_paths;

  return SVN_NO_ERROR;
}

/* Fetch lock tokens from the repository for the paths in PATH_TOKENS,
   setting the values to the fetched tokens, allocated in pool. */
static svn_error_t *
fetch_tokens(svn_ra_session_t *ra_session, apr_hash_t *path_tokens,
             apr_pool_t *pool)
{
  apr_hash_index_t *hi;
  apr_pool_t *iterpool = svn_pool_create(pool);

  for (hi = apr_hash_first(pool, path_tokens); hi; hi = apr_hash_next(hi))
    {
      const char *path = svn__apr_hash_index_key(hi);
      svn_lock_t *lock;

      svn_pool_clear(iterpool);

      SVN_ERR(svn_ra_get_lock(ra_session, &lock, path, iterpool));

      if (! lock)
        return svn_error_createf
          (SVN_ERR_CLIENT_MISSING_LOCK_TOKEN, NULL,
           _("'%s' is not locked"), path);

      svn_hash_sets(path_tokens, path, apr_pstrdup(pool, lock->token));
    }

  svn_pool_destroy(iterpool);
  return SVN_NO_ERROR;
}


svn_error_t *
svn_client_lock(const apr_array_header_t *targets,
                const char *comment,
                svn_boolean_t steal_lock,
                svn_client_ctx_t *ctx,
                apr_pool_t *pool)
{
  const char *base_dir;
  const char *base_dir_abspath = NULL;
  const char *common_parent_url;
  svn_ra_session_t *ra_session;
  apr_hash_t *path_revs, *urls_to_paths;
  struct lock_baton cb;

  if (apr_is_empty_array(targets))
    return SVN_NO_ERROR;

  /* Enforce that the comment be xml-escapable. */
  if (comment)
    {
      if (! svn_xml_is_xml_safe(comment, strlen(comment)))
        return svn_error_create
          (SVN_ERR_XML_UNESCAPABLE_DATA, NULL,
           _("Lock comment contains illegal characters"));
    }

  SVN_ERR(organize_lock_targets(&common_parent_url, &base_dir, &path_revs,
                                &urls_to_paths, targets, TRUE, steal_lock,
                                ctx->wc_ctx, pool, pool));

  /* Open an RA session to the common parent of TARGETS. */
  if (base_dir)
    SVN_ERR(svn_dirent_get_absolute(&base_dir_abspath, base_dir, pool));
  SVN_ERR(svn_client_open_ra_session2(&ra_session, common_parent_url,
                                      base_dir_abspath, ctx, pool, pool));

  cb.base_dir_abspath = base_dir_abspath;
  cb.urls_to_paths = urls_to_paths;
  cb.ctx = ctx;
  cb.pool = pool;

  /* Lock the paths. */
  SVN_ERR(svn_ra_lock(ra_session, path_revs, comment,
                      steal_lock, store_locks_callback, &cb, pool));

  return SVN_NO_ERROR;
}

svn_error_t *
svn_client_unlock(const apr_array_header_t *targets,
                  svn_boolean_t break_lock,
                  svn_client_ctx_t *ctx,
                  apr_pool_t *pool)
{
  const char *base_dir;
  const char *base_dir_abspath = NULL;
  const char *common_parent_url;
  svn_ra_session_t *ra_session;
  apr_hash_t *path_tokens, *urls_to_paths;
  struct lock_baton cb;

  if (apr_is_empty_array(targets))
    return SVN_NO_ERROR;

  SVN_ERR(organize_lock_targets(&common_parent_url, &base_dir, &path_tokens,
                                &urls_to_paths, targets, FALSE, break_lock,
                                ctx->wc_ctx, pool, pool));

  /* Open an RA session. */
  if (base_dir)
    SVN_ERR(svn_dirent_get_absolute(&base_dir_abspath, base_dir, pool));
  SVN_ERR(svn_client_open_ra_session2(&ra_session, common_parent_url,
                                      base_dir_abspath, ctx, pool, pool));

  /* If break_lock is not set, lock tokens are required by the server.
     If the targets were all URLs, ensure that we provide lock tokens,
     so the repository will only check that the user owns the
     locks. */
  if (! base_dir && !break_lock)
    SVN_ERR(fetch_tokens(ra_session, path_tokens, pool));

  cb.base_dir_abspath = base_dir_abspath;
  cb.urls_to_paths = urls_to_paths;
  cb.ctx = ctx;
  cb.pool = pool;

  /* Unlock the paths. */
  SVN_ERR(svn_ra_unlock(ra_session, path_tokens, break_lock,
                        store_locks_callback, &cb, pool));

  return SVN_NO_ERROR;
}