1d25dac7peter/*
2d25dac7peter * cleanup.c:  wrapper around wc cleanup functionality.
3d25dac7peter *
4d25dac7peter * ====================================================================
5d25dac7peter *    Licensed to the Apache Software Foundation (ASF) under one
6d25dac7peter *    or more contributor license agreements.  See the NOTICE file
7d25dac7peter *    distributed with this work for additional information
8d25dac7peter *    regarding copyright ownership.  The ASF licenses this file
9d25dac7peter *    to you under the Apache License, Version 2.0 (the
10d25dac7peter *    "License"); you may not use this file except in compliance
11d25dac7peter *    with the License.  You may obtain a copy of the License at
12d25dac7peter *
13d25dac7peter *      http://www.apache.org/licenses/LICENSE-2.0
14d25dac7peter *
15d25dac7peter *    Unless required by applicable law or agreed to in writing,
16d25dac7peter *    software distributed under the License is distributed on an
17d25dac7peter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18d25dac7peter *    KIND, either express or implied.  See the License for the
19d25dac7peter *    specific language governing permissions and limitations
20d25dac7peter *    under the License.
21d25dac7peter * ====================================================================
22d25dac7peter */
23d25dac7peter
24d25dac7peter/* ==================================================================== */
25d25dac7peter
26d25dac7peter
27d25dac7peter
28d25dac7peter/*** Includes. ***/
29d25dac7peter
30d25dac7peter#include "svn_time.h"
31d25dac7peter#include "svn_wc.h"
32d25dac7peter#include "svn_client.h"
33d25dac7peter#include "svn_config.h"
34d25dac7peter#include "svn_dirent_uri.h"
35c18b5d7peter#include "svn_hash.h"
36d25dac7peter#include "svn_path.h"
37d25dac7peter#include "svn_pools.h"
38d25dac7peter#include "client.h"
39d25dac7peter#include "svn_props.h"
40d25dac7peter
41d25dac7peter#include "svn_private_config.h"
42c18b5d7peter#include "private/svn_wc_private.h"
43d25dac7peter
44d25dac7peter
45d25dac7peter/*** Code. ***/
46d25dac7peter
47c18b5d7peterstruct cleanup_status_walk_baton
48c18b5d7peter{
49c18b5d7peter  svn_boolean_t break_locks;
50c18b5d7peter  svn_boolean_t fix_timestamps;
51c18b5d7peter  svn_boolean_t clear_dav_cache;
52c18b5d7peter  svn_boolean_t vacuum_pristines;
53c18b5d7peter  svn_boolean_t remove_unversioned_items;
54c18b5d7peter  svn_boolean_t remove_ignored_items;
55c18b5d7peter  svn_boolean_t include_externals;
56c18b5d7peter  svn_client_ctx_t *ctx;
57c18b5d7peter};
58c18b5d7peter
59c18b5d7peter/* Forward declararion. */
60c18b5d7peterstatic svn_error_t *
61c18b5d7petercleanup_status_walk(void *baton,
62c18b5d7peter                    const char *local_abspath,
63c18b5d7peter                    const svn_wc_status3_t *status,
64c18b5d7peter                    apr_pool_t *scratch_pool);
65c18b5d7peter
66c18b5d7peterstatic svn_error_t *
67c18b5d7peterdo_cleanup(const char *local_abspath,
68c18b5d7peter           svn_boolean_t break_locks,
69c18b5d7peter           svn_boolean_t fix_timestamps,
70c18b5d7peter           svn_boolean_t clear_dav_cache,
71c18b5d7peter           svn_boolean_t vacuum_pristines,
72c18b5d7peter           svn_boolean_t remove_unversioned_items,
73c18b5d7peter           svn_boolean_t remove_ignored_items,
74c18b5d7peter           svn_boolean_t include_externals,
75c18b5d7peter           svn_client_ctx_t *ctx,
76c18b5d7peter           apr_pool_t *scratch_pool)
77c18b5d7peter{
78c18b5d7peter  SVN_ERR(svn_wc_cleanup4(ctx->wc_ctx,
79c18b5d7peter                          local_abspath,
80c18b5d7peter                          break_locks,
81c18b5d7peter                          fix_timestamps,
82c18b5d7peter                          clear_dav_cache,
83c18b5d7peter                          vacuum_pristines,
84c18b5d7peter                          ctx->cancel_func, ctx->cancel_baton,
85c18b5d7peter                          ctx->notify_func2, ctx->notify_baton2,
86c18b5d7peter                          scratch_pool));
87c18b5d7peter
88c18b5d7peter  if (fix_timestamps)
89c18b5d7peter    svn_io_sleep_for_timestamps(local_abspath, scratch_pool);
90c18b5d7peter
91c18b5d7peter  if (remove_unversioned_items || remove_ignored_items || include_externals)
92c18b5d7peter    {
93c18b5d7peter      struct cleanup_status_walk_baton b;
94c18b5d7peter      apr_array_header_t *ignores;
95c18b5d7peter
96c18b5d7peter      b.break_locks = break_locks;
97c18b5d7peter      b.fix_timestamps = fix_timestamps;
98c18b5d7peter      b.clear_dav_cache = clear_dav_cache;
99c18b5d7peter      b.vacuum_pristines = vacuum_pristines;
100c18b5d7peter      b.remove_unversioned_items = remove_unversioned_items;
101c18b5d7peter      b.remove_ignored_items = remove_ignored_items;
102c18b5d7peter      b.include_externals = include_externals;
103c18b5d7peter      b.ctx = ctx;
104c18b5d7peter
105c18b5d7peter      SVN_ERR(svn_wc_get_default_ignores(&ignores, ctx->config, scratch_pool));
106c18b5d7peter
107c18b5d7peter      SVN_WC__CALL_WITH_WRITE_LOCK(
108c18b5d7peter              svn_wc_walk_status(ctx->wc_ctx, local_abspath,
109c18b5d7peter                                 svn_depth_infinity,
110c18b5d7peter                                 TRUE,  /* get all */
111c18b5d7peter                                 remove_ignored_items,
112c18b5d7peter                                 TRUE,  /* ignore textmods */
113c18b5d7peter                                 ignores,
114c18b5d7peter                                 cleanup_status_walk, &b,
115c18b5d7peter                                 ctx->cancel_func,
116c18b5d7peter                                 ctx->cancel_baton,
117c18b5d7peter                                 scratch_pool),
118c18b5d7peter              ctx->wc_ctx,
119c18b5d7peter              local_abspath,
120c18b5d7peter              FALSE /* lock_anchor */,
121c18b5d7peter              scratch_pool);
122c18b5d7peter    }
123c18b5d7peter
124c18b5d7peter  return SVN_NO_ERROR;
125c18b5d7peter}
126c18b5d7peter
127c18b5d7peter
128c18b5d7peter/* An implementation of svn_wc_status_func4_t. */
129c18b5d7peterstatic svn_error_t *
130c18b5d7petercleanup_status_walk(void *baton,
131c18b5d7peter                    const char *local_abspath,
132c18b5d7peter                    const svn_wc_status3_t *status,
133c18b5d7peter                    apr_pool_t *scratch_pool)
134c18b5d7peter{
135c18b5d7peter  struct cleanup_status_walk_baton *b = baton;
136c18b5d7peter  svn_node_kind_t kind_on_disk;
137c18b5d7peter  svn_wc_notify_t *notify;
138c18b5d7peter
139c18b5d7peter  if (status->node_status == svn_wc_status_external && b->include_externals)
140c18b5d7peter    {
141c18b5d7peter      svn_error_t *err;
142c18b5d7peter
143c18b5d7peter      SVN_ERR(svn_io_check_path(local_abspath, &kind_on_disk, scratch_pool));
144c18b5d7peter      if (kind_on_disk == svn_node_dir)
145c18b5d7peter        {
146c18b5d7peter          if (b->ctx->notify_func2)
147c18b5d7peter            {
148c18b5d7peter              notify = svn_wc_create_notify(local_abspath,
149c18b5d7peter                                            svn_wc_notify_cleanup_external,
150c18b5d7peter                                            scratch_pool);
151c18b5d7peter              b->ctx->notify_func2(b->ctx->notify_baton2, notify,
152c18b5d7peter                                   scratch_pool);
153c18b5d7peter            }
154c18b5d7peter
155c18b5d7peter          err = do_cleanup(local_abspath,
156c18b5d7peter                           b->break_locks,
157c18b5d7peter                           b->fix_timestamps,
158c18b5d7peter                           b->clear_dav_cache,
159c18b5d7peter                           b->vacuum_pristines,
160c18b5d7peter                           b->remove_unversioned_items,
161c18b5d7peter                           b->remove_ignored_items,
162c18b5d7peter                           TRUE /* include_externals */,
163c18b5d7peter                           b->ctx, scratch_pool);
164c18b5d7peter          if (err && err->apr_err == SVN_ERR_WC_NOT_WORKING_COPY)
165c18b5d7peter            {
166c18b5d7peter              svn_error_clear(err);
167c18b5d7peter              return SVN_NO_ERROR;
168c18b5d7peter            }
169c18b5d7peter          else
170c18b5d7peter            SVN_ERR(err);
171c18b5d7peter        }
172c18b5d7peter
173c18b5d7peter      return SVN_NO_ERROR;
174c18b5d7peter    }
175c18b5d7peter
176c18b5d7peter  if (status->node_status == svn_wc_status_ignored)
177c18b5d7peter    {
178c18b5d7peter      if (!b->remove_ignored_items)
179c18b5d7peter        return SVN_NO_ERROR;
180c18b5d7peter    }
181c18b5d7peter  else if (status->node_status == svn_wc_status_unversioned)
182c18b5d7peter    {
183c18b5d7peter      if (!b->remove_unversioned_items)
184c18b5d7peter        return SVN_NO_ERROR;
185c18b5d7peter    }
186c18b5d7peter  else
187c18b5d7peter    return SVN_NO_ERROR;
188c18b5d7peter
189c18b5d7peter  SVN_ERR(svn_io_check_path(local_abspath, &kind_on_disk, scratch_pool));
190c18b5d7peter  switch (kind_on_disk)
191c18b5d7peter    {
192c18b5d7peter      case svn_node_file:
193c18b5d7peter      case svn_node_symlink:
194c18b5d7peter        SVN_ERR(svn_io_remove_file2(local_abspath, FALSE, scratch_pool));
195c18b5d7peter        break;
196c18b5d7peter      case svn_node_dir:
197c18b5d7peter        SVN_ERR(svn_io_remove_dir2(local_abspath, FALSE,
198c18b5d7peter                                   b->ctx->cancel_func, b->ctx->cancel_baton,
199c18b5d7peter                                   scratch_pool));
200c18b5d7peter        break;
201c18b5d7peter      case svn_node_none:
202c18b5d7peter      default:
203c18b5d7peter        return SVN_NO_ERROR;
204c18b5d7peter    }
205c18b5d7peter
206c18b5d7peter  if (b->ctx->notify_func2)
207c18b5d7peter    {
208c18b5d7peter      notify = svn_wc_create_notify(local_abspath, svn_wc_notify_delete,
209c18b5d7peter                                    scratch_pool);
210c18b5d7peter      notify->kind = kind_on_disk;
211c18b5d7peter      b->ctx->notify_func2(b->ctx->notify_baton2, notify, scratch_pool);
212c18b5d7peter    }
213c18b5d7peter
214c18b5d7peter  return SVN_NO_ERROR;
215c18b5d7peter}
216c18b5d7peter
217d25dac7petersvn_error_t *
218c18b5d7petersvn_client_cleanup2(const char *dir_abspath,
219c18b5d7peter                    svn_boolean_t break_locks,
220c18b5d7peter                    svn_boolean_t fix_recorded_timestamps,
221c18b5d7peter                    svn_boolean_t clear_dav_cache,
222c18b5d7peter                    svn_boolean_t vacuum_pristines,
223c18b5d7peter                    svn_boolean_t include_externals,
224c18b5d7peter                    svn_client_ctx_t *ctx,
225c18b5d7peter                    apr_pool_t *scratch_pool)
226d25dac7peter{
227c18b5d7peter  SVN_ERR_ASSERT(svn_dirent_is_absolute(dir_abspath));
228d25dac7peter
229c18b5d7peter  SVN_ERR(do_cleanup(dir_abspath,
230c18b5d7peter                     break_locks,
231c18b5d7peter                     fix_recorded_timestamps,
232c18b5d7peter                     clear_dav_cache,
233c18b5d7peter                     vacuum_pristines,
234c18b5d7peter                     FALSE /* remove_unversioned_items */,
235c18b5d7peter                     FALSE /* remove_ignored_items */,
236c18b5d7peter                     include_externals,
237c18b5d7peter                     ctx, scratch_pool));
238c18b5d7peter
239c18b5d7peter  return SVN_NO_ERROR;
240c18b5d7peter}
241c18b5d7peter
242c18b5d7petersvn_error_t *
243c18b5d7petersvn_client_vacuum(const char *dir_abspath,
244c18b5d7peter                  svn_boolean_t remove_unversioned_items,
245c18b5d7peter                  svn_boolean_t remove_ignored_items,
246c18b5d7peter                  svn_boolean_t fix_recorded_timestamps,
247c18b5d7peter                  svn_boolean_t vacuum_pristines,
248c18b5d7peter                  svn_boolean_t include_externals,
249c18b5d7peter                  svn_client_ctx_t *ctx,
250c18b5d7peter                  apr_pool_t *scratch_pool)
251c18b5d7peter{
252c18b5d7peter  SVN_ERR_ASSERT(svn_dirent_is_absolute(dir_abspath));
253d25dac7peter
254c18b5d7peter  SVN_ERR(do_cleanup(dir_abspath,
255c18b5d7peter                     FALSE /* break_locks */,
256c18b5d7peter                     fix_recorded_timestamps,
257c18b5d7peter                     FALSE /* clear_dav_cache */,
258c18b5d7peter                     vacuum_pristines,
259c18b5d7peter                     remove_unversioned_items,
260c18b5d7peter                     remove_ignored_items,
261c18b5d7peter                     include_externals,
262c18b5d7peter                     ctx, scratch_pool));
263d25dac7peter
264c18b5d7peter  return SVN_NO_ERROR;
265d25dac7peter}
266