summaryrefslogtreecommitdiff
path: root/subversion/libsvn_wc/delete.c
blob: 82ae9382d6dcd5bf3ce0a01313d9126610573ba5 (plain)
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
/*
 * delete.c: Handling of the in-wc side of the delete operation
 *
 * ====================================================================
 *    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.
 * ====================================================================
 */



#include <string.h>
#include <stdlib.h>

#include <apr_pools.h>

#include "svn_types.h"
#include "svn_pools.h"
#include "svn_string.h"
#include "svn_error.h"
#include "svn_dirent_uri.h"
#include "svn_wc.h"
#include "svn_io.h"

#include "wc.h"
#include "adm_files.h"
#include "conflicts.h"
#include "workqueue.h"

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


/* Remove/erase PATH from the working copy. This involves deleting PATH
 * from the physical filesystem. PATH is assumed to be an unversioned file
 * or directory.
 *
 * If ignore_enoent is TRUE, ignore missing targets.
 *
 * If CANCEL_FUNC is non-null, invoke it with CANCEL_BATON at various
 * points, return any error immediately.
 */
static svn_error_t *
erase_unversioned_from_wc(const char *path,
                          svn_boolean_t ignore_enoent,
                          svn_cancel_func_t cancel_func,
                          void *cancel_baton,
                          apr_pool_t *scratch_pool)
{
  svn_error_t *err;

  /* Optimize the common case: try to delete the file */
  err = svn_io_remove_file2(path, ignore_enoent, scratch_pool);
  if (err)
    {
      /* Then maybe it was a directory? */
      svn_error_clear(err);

      err = svn_io_remove_dir2(path, ignore_enoent, cancel_func, cancel_baton,
                               scratch_pool);

      if (err)
        {
          /* We're unlikely to end up here. But we need this fallback
             to make sure we report the right error *and* try the
             correct deletion at least once. */
          svn_node_kind_t kind;

          svn_error_clear(err);
          SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
          if (kind == svn_node_file)
            SVN_ERR(svn_io_remove_file2(path, ignore_enoent, scratch_pool));
          else if (kind == svn_node_dir)
            SVN_ERR(svn_io_remove_dir2(path, ignore_enoent,
                                       cancel_func, cancel_baton,
                                       scratch_pool));
          else if (kind == svn_node_none)
            return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
                                     _("'%s' does not exist"),
                                     svn_dirent_local_style(path,
                                                            scratch_pool));
          else
            return svn_error_createf(SVN_ERR_UNSUPPORTED_FEATURE, NULL,
                                     _("Unsupported node kind for path '%s'"),
                                     svn_dirent_local_style(path,
                                                            scratch_pool));

        }
    }

  return SVN_NO_ERROR;
}

/* Helper for svn_wc__delete and svn_wc__delete_many */
static svn_error_t *
create_delete_wq_items(svn_skel_t **work_items,
                       svn_wc__db_t *db,
                       const char *local_abspath,
                       svn_node_kind_t kind,
                       svn_boolean_t conflicted,
                       apr_pool_t *result_pool,
                       apr_pool_t *scratch_pool)
{
  *work_items = NULL;

  /* Schedule the on-disk delete */
  if (kind == svn_node_dir)
    SVN_ERR(svn_wc__wq_build_dir_remove(work_items, db, local_abspath,
                                        local_abspath,
                                        TRUE /* recursive */,
                                        result_pool, scratch_pool));
  else
    SVN_ERR(svn_wc__wq_build_file_remove(work_items, db, local_abspath,
                                         local_abspath,
                                         result_pool, scratch_pool));

  /* Read conflicts, to allow deleting the markers after updating the DB */
  if (conflicted)
    {
      svn_skel_t *conflict;
      const apr_array_header_t *markers;
      int i;

      SVN_ERR(svn_wc__db_read_conflict(&conflict, NULL, NULL,
                                       db, local_abspath,
                                       scratch_pool, scratch_pool));

      SVN_ERR(svn_wc__conflict_read_markers(&markers, db, local_abspath,
                                            conflict,
                                            scratch_pool, scratch_pool));

      /* Maximum number of markers is 4, so no iterpool */
      for (i = 0; markers && i < markers->nelts; i++)
        {
          const char *marker_abspath;
          svn_node_kind_t marker_kind;

          marker_abspath = APR_ARRAY_IDX(markers, i, const char *);
          SVN_ERR(svn_io_check_path(marker_abspath, &marker_kind,
                                    scratch_pool));

          if (marker_kind == svn_node_file)
            {
              svn_skel_t *work_item;

              SVN_ERR(svn_wc__wq_build_file_remove(&work_item, db,
                                                   local_abspath,
                                                   marker_abspath,
                                                   result_pool,
                                                   scratch_pool));

              *work_items = svn_wc__wq_merge(*work_items, work_item,
                                             result_pool);
            }
        }
    }

  return SVN_NO_ERROR;
}

svn_error_t *
svn_wc__delete_many(svn_wc_context_t *wc_ctx,
                    const apr_array_header_t *targets,
                    svn_boolean_t keep_local,
                    svn_boolean_t delete_unversioned_target,
                    svn_cancel_func_t cancel_func,
                    void *cancel_baton,
                    svn_wc_notify_func2_t notify_func,
                    void *notify_baton,
                    apr_pool_t *scratch_pool)
{
  svn_wc__db_t *db = wc_ctx->db;
  svn_error_t *err;
  svn_wc__db_status_t status;
  svn_node_kind_t kind;
  svn_skel_t *work_items = NULL;
  apr_array_header_t *versioned_targets;
  const char *local_abspath;
  int i;
  apr_pool_t *iterpool;

  iterpool = svn_pool_create(scratch_pool);
  versioned_targets = apr_array_make(scratch_pool, targets->nelts,
                                     sizeof(const char *));
  for (i = 0; i < targets->nelts; i++)
    {
      svn_boolean_t conflicted = FALSE;
      const char *repos_relpath;

      svn_pool_clear(iterpool);

      local_abspath = APR_ARRAY_IDX(targets, i, const char *);
      err = svn_wc__db_read_info(&status, &kind, NULL, &repos_relpath, NULL,
                                 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                 NULL, &conflicted,
                                 NULL, NULL, NULL, NULL, NULL, NULL,
                                 db, local_abspath, iterpool, iterpool);

      if (err)
        {
          if (err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
            {
              svn_error_clear(err);
              if (delete_unversioned_target && !keep_local)
                SVN_ERR(erase_unversioned_from_wc(local_abspath, FALSE,
                                                  cancel_func, cancel_baton,
                                                  iterpool));
              continue;
            }
         else
          return svn_error_trace(err);
        }

      APR_ARRAY_PUSH(versioned_targets, const char *) = local_abspath;

      switch (status)
        {
          /* svn_wc__db_status_server_excluded handled by
           * svn_wc__db_op_delete_many */
          case svn_wc__db_status_excluded:
          case svn_wc__db_status_not_present:
            return svn_error_createf(SVN_ERR_WC_PATH_NOT_FOUND, NULL,
                                     _("'%s' cannot be deleted"),
                                     svn_dirent_local_style(local_abspath,
                                                            iterpool));

          /* Explicitly ignore other statii */
          default:
            break;
        }

      if (status == svn_wc__db_status_normal
          && kind == svn_node_dir)
        {
          svn_boolean_t is_wcroot;
          SVN_ERR(svn_wc__db_is_wcroot(&is_wcroot, db, local_abspath,
                                       iterpool));

          if (is_wcroot)
            return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
                                     _("'%s' is the root of a working copy and "
                                       "cannot be deleted"),
                                     svn_dirent_local_style(local_abspath,
                                                            iterpool));
        }
      if (repos_relpath && !repos_relpath[0])
        return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
                                     _("'%s' represents the repository root "
                                       "and cannot be deleted"),
                                     svn_dirent_local_style(local_abspath,
                                                            iterpool));

      /* Verify if we have a write lock on the parent of this node as we might
         be changing the childlist of that directory. */
      SVN_ERR(svn_wc__write_check(db, svn_dirent_dirname(local_abspath,
                                                         iterpool),
                                  iterpool));

      /* Prepare the on-disk delete */
      if (!keep_local)
        {
          svn_skel_t *work_item;

          SVN_ERR(create_delete_wq_items(&work_item, db, local_abspath, kind,
                                         conflicted,
                                         scratch_pool, iterpool));

          work_items = svn_wc__wq_merge(work_items, work_item,
                                        scratch_pool);
        }
    }

  if (versioned_targets->nelts == 0)
    return SVN_NO_ERROR;

  SVN_ERR(svn_wc__db_op_delete_many(db, versioned_targets,
                                    !keep_local /* delete_dir_externals */,
                                    work_items,
                                    cancel_func, cancel_baton,
                                    notify_func, notify_baton,
                                    iterpool));

  if (work_items != NULL)
    {
      /* Our only caller locked the wc, so for now assume it only passed
         nodes from a single wc (asserted in svn_wc__db_op_delete_many) */
      local_abspath = APR_ARRAY_IDX(versioned_targets, 0, const char *);

      SVN_ERR(svn_wc__wq_run(db, local_abspath, cancel_func, cancel_baton,
                             iterpool));
    }
  svn_pool_destroy(iterpool);

  return SVN_NO_ERROR;
}

svn_error_t *
svn_wc_delete4(svn_wc_context_t *wc_ctx,
               const char *local_abspath,
               svn_boolean_t keep_local,
               svn_boolean_t delete_unversioned_target,
               svn_cancel_func_t cancel_func,
               void *cancel_baton,
               svn_wc_notify_func2_t notify_func,
               void *notify_baton,
               apr_pool_t *scratch_pool)
{
  apr_pool_t *pool = scratch_pool;
  svn_wc__db_t *db = wc_ctx->db;
  svn_error_t *err;
  svn_wc__db_status_t status;
  svn_node_kind_t kind;
  svn_boolean_t conflicted;
  svn_skel_t *work_items = NULL;
  const char *repos_relpath;

  err = svn_wc__db_read_info(&status, &kind, NULL, &repos_relpath, NULL, NULL,
                             NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                             NULL, NULL, NULL, NULL, NULL, NULL, &conflicted,
                             NULL, NULL, NULL, NULL, NULL, NULL,
                             db, local_abspath, pool, pool);

  if (delete_unversioned_target &&
      err != NULL && err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
    {
      svn_error_clear(err);

      if (!keep_local)
        SVN_ERR(erase_unversioned_from_wc(local_abspath, FALSE,
                                          cancel_func, cancel_baton,
                                          pool));
      return SVN_NO_ERROR;
    }
  else
    SVN_ERR(err);

  switch (status)
    {
      /* svn_wc__db_status_server_excluded handled by svn_wc__db_op_delete */
      case svn_wc__db_status_excluded:
      case svn_wc__db_status_not_present:
        return svn_error_createf(SVN_ERR_WC_PATH_NOT_FOUND, NULL,
                                 _("'%s' cannot be deleted"),
                                 svn_dirent_local_style(local_abspath, pool));

      /* Explicitly ignore other statii */
      default:
        break;
    }

  if (status == svn_wc__db_status_normal
      && kind == svn_node_dir)
    {
      svn_boolean_t is_wcroot;
      SVN_ERR(svn_wc__db_is_wcroot(&is_wcroot, db, local_abspath, pool));

      if (is_wcroot)
        return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
                                 _("'%s' is the root of a working copy and "
                                   "cannot be deleted"),
                                 svn_dirent_local_style(local_abspath, pool));
    }
  if (repos_relpath && !repos_relpath[0])
    return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
                             _("'%s' represents the repository root "
                               "and cannot be deleted"),
                               svn_dirent_local_style(local_abspath, pool));

  /* Verify if we have a write lock on the parent of this node as we might
     be changing the childlist of that directory. */
  SVN_ERR(svn_wc__write_check(db, svn_dirent_dirname(local_abspath, pool),
                              pool));

  /* Prepare the on-disk delete */
      if (!keep_local)
        {
          SVN_ERR(create_delete_wq_items(&work_items, db, local_abspath, kind,
                                         conflicted,
                                         scratch_pool, scratch_pool));
        }

  SVN_ERR(svn_wc__db_op_delete(db, local_abspath,
                               NULL /*moved_to_abspath */,
                               !keep_local /* delete_dir_externals */,
                               NULL, work_items,
                               cancel_func, cancel_baton,
                               notify_func, notify_baton,
                               pool));

  if (work_items)
    SVN_ERR(svn_wc__wq_run(db, local_abspath, cancel_func, cancel_baton,
                           scratch_pool));

  return SVN_NO_ERROR;
}

svn_error_t *
svn_wc__internal_remove_from_revision_control(svn_wc__db_t *db,
                                              const char *local_abspath,
                                              svn_boolean_t destroy_wf,
                                              svn_cancel_func_t cancel_func,
                                              void *cancel_baton,
                                              apr_pool_t *scratch_pool)
{
  svn_boolean_t left_something = FALSE;
  svn_boolean_t is_root;
  svn_error_t *err = NULL;

  SVN_ERR(svn_wc__db_is_wcroot(&is_root, db, local_abspath, scratch_pool));

  SVN_ERR(svn_wc__write_check(db, is_root ? local_abspath
                                          : svn_dirent_dirname(local_abspath,
                                                               scratch_pool),
                              scratch_pool));

  SVN_ERR(svn_wc__db_op_remove_node(&left_something,
                                    db, local_abspath,
                                    destroy_wf /* destroy_wc */,
                                    destroy_wf /* destroy_changes */,
                                    NULL, NULL,
                                    cancel_func, cancel_baton,
                                    scratch_pool));

  SVN_ERR(svn_wc__wq_run(db, local_abspath,
                         cancel_func, cancel_baton,
                         scratch_pool));

  if (is_root)
    {
      /* Destroy the administrative area */
      SVN_ERR(svn_wc__adm_destroy(db, local_abspath, cancel_func, cancel_baton,
                                  scratch_pool));

      /* And if we didn't leave something interesting, remove the directory */
      if (!left_something && destroy_wf)
        err = svn_io_dir_remove_nonrecursive(local_abspath, scratch_pool);
    }

  if (left_something || err)
    return svn_error_create(SVN_ERR_WC_LEFT_LOCAL_MOD, err, NULL);

  return SVN_NO_ERROR;
}

/* Implements svn_wc_status_func4_t for svn_wc_remove_from_revision_control2 */
static svn_error_t *
remove_from_revision_status_callback(void *baton,
                                     const char *local_abspath,
                                     const svn_wc_status3_t *status,
                                     apr_pool_t *scratch_pool)
{
  /* For legacy reasons we only check the file contents for changes */
  if (status->versioned
      && status->kind == svn_node_file
      && (status->text_status == svn_wc_status_modified
          || status->text_status == svn_wc_status_conflicted))
    {
      return svn_error_createf(SVN_ERR_WC_LEFT_LOCAL_MOD, NULL,
                               _("File '%s' has local modifications"),
                               svn_dirent_local_style(local_abspath,
                                                      scratch_pool));
    }
  return SVN_NO_ERROR;
}

svn_error_t *
svn_wc_remove_from_revision_control2(svn_wc_context_t *wc_ctx,
                                    const char *local_abspath,
                                    svn_boolean_t destroy_wf,
                                    svn_boolean_t instant_error,
                                    svn_cancel_func_t cancel_func,
                                    void *cancel_baton,
                                    apr_pool_t *scratch_pool)
{
  if (instant_error)
    {
      SVN_ERR(svn_wc_walk_status(wc_ctx, local_abspath, svn_depth_infinity,
                                 FALSE, FALSE, FALSE, NULL,
                                 remove_from_revision_status_callback, NULL,
                                 cancel_func, cancel_baton,
                                 scratch_pool));
    }
  return svn_error_trace(
      svn_wc__internal_remove_from_revision_control(wc_ctx->db,
                                                    local_abspath,
                                                    destroy_wf,
                                                    cancel_func,
                                                    cancel_baton,
                                                    scratch_pool));
}