2 * adm_files.c: helper routines for handling files & dirs in the
3 * working copy administrative area (creating,
4 * deleting, opening, and closing). This is the only
5 * code that actually knows where administrative
8 * ====================================================================
9 * Licensed to the Apache Software Foundation (ASF) under one
10 * or more contributor license agreements. See the NOTICE file
11 * distributed with this work for additional information
12 * regarding copyright ownership. The ASF licenses this file
13 * to you under the Apache License, Version 2.0 (the
14 * "License"); you may not use this file except in compliance
15 * with the License. You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing,
20 * software distributed under the License is distributed on an
21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22 * KIND, either express or implied. See the License for the
23 * specific language governing permissions and limitations
25 * ====================================================================
31 #include <apr_pools.h>
32 #include <apr_file_io.h>
33 #include <apr_strings.h>
35 #include "svn_types.h"
36 #include "svn_error.h"
38 #include "svn_dirent_uri.h"
43 #include "adm_files.h"
47 #include "svn_private_config.h"
48 #include "private/svn_wc_private.h"
51 /*** File names in the adm area. ***/
53 /* The default name of the WC admin directory. This name is always
54 checked by svn_wc_is_adm_dir. */
55 static const char default_adm_dir_name[] = ".svn";
57 /* The name that is actually used for the WC admin directory. The
58 commonest case where this won't be the default is in Windows
59 ASP.NET development environments, which used to choke on ".svn". */
60 static const char *adm_dir_name = default_adm_dir_name;
64 svn_wc_is_adm_dir(const char *name, apr_pool_t *pool)
66 return (0 == strcmp(name, adm_dir_name)
67 || 0 == strcmp(name, default_adm_dir_name));
72 svn_wc_get_adm_dir(apr_pool_t *pool)
79 svn_wc_set_adm_dir(const char *name, apr_pool_t *pool)
81 /* This is the canonical list of administrative directory names.
84 An identical list is used in
85 libsvn_subr/opt.c:svn_opt__args_to_target_array(),
86 but that function can't use this list, because that use would
87 create a circular dependency between libsvn_wc and libsvn_subr.
88 Make sure changes to the lists are always synchronized! */
89 static const char *valid_dir_names[] = {
95 const char **dir_name;
96 for (dir_name = valid_dir_names; *dir_name; ++dir_name)
97 if (0 == strcmp(name, *dir_name))
99 /* Use the pointer to the statically allocated string
100 constant, to avoid potential pool lifetime issues. */
101 adm_dir_name = *dir_name;
104 return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
105 _("'%s' is not a valid administrative "
107 svn_dirent_local_style(name, pool));
112 svn_wc__adm_child(const char *path,
114 apr_pool_t *result_pool)
116 return svn_dirent_join_many(result_pool,
125 svn_wc__adm_area_exists(const char *adm_abspath,
128 const char *path = svn_wc__adm_child(adm_abspath, NULL, pool);
129 svn_node_kind_t kind;
132 err = svn_io_check_path(path, &kind, pool);
135 svn_error_clear(err);
136 /* Return early, since kind is undefined in this case. */
140 return kind != svn_node_none;
145 /*** Making and using files in the adm area. ***/
150 make_adm_subdir(const char *path,
154 const char *fullpath;
156 fullpath = svn_wc__adm_child(path, subdir, pool);
158 return svn_io_dir_make(fullpath, APR_OS_DEFAULT, pool);
163 /*** Syncing files in the adm area. ***/
167 svn_wc__text_base_path_to_read(const char **result_abspath,
169 const char *local_abspath,
170 apr_pool_t *result_pool,
171 apr_pool_t *scratch_pool)
173 svn_wc__db_status_t status;
174 svn_node_kind_t kind;
175 const svn_checksum_t *checksum;
177 SVN_ERR(svn_wc__db_read_pristine_info(&status, &kind, NULL, NULL, NULL, NULL,
178 &checksum, NULL, NULL, NULL,
180 scratch_pool, scratch_pool));
183 if (kind != svn_node_file)
184 return svn_error_createf(SVN_ERR_NODE_UNEXPECTED_KIND, NULL,
185 _("Can only get the pristine contents of files; "
186 "'%s' is not a file"),
187 svn_dirent_local_style(local_abspath,
190 if (status == svn_wc__db_status_not_present)
191 /* We know that the delete of this node has been committed.
192 This should be the same as if called on an unknown path. */
193 return svn_error_createf(SVN_ERR_WC_PATH_NOT_FOUND, NULL,
194 _("Cannot get the pristine contents of '%s' "
195 "because its delete is already committed"),
196 svn_dirent_local_style(local_abspath,
198 else if (status == svn_wc__db_status_server_excluded
199 || status == svn_wc__db_status_excluded
200 || status == svn_wc__db_status_incomplete)
201 return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
202 _("Cannot get the pristine contents of '%s' "
203 "because it has an unexpected status"),
204 svn_dirent_local_style(local_abspath,
207 if (checksum == NULL)
208 return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
209 _("Node '%s' has no pristine text"),
210 svn_dirent_local_style(local_abspath,
212 SVN_ERR(svn_wc__db_pristine_get_path(result_abspath, db, local_abspath,
214 result_pool, scratch_pool));
219 svn_wc__get_pristine_contents(svn_stream_t **contents,
220 svn_filesize_t *size,
222 const char *local_abspath,
223 apr_pool_t *result_pool,
224 apr_pool_t *scratch_pool)
226 svn_wc__db_status_t status;
227 svn_node_kind_t kind;
228 const svn_checksum_t *sha1_checksum;
231 *size = SVN_INVALID_FILESIZE;
233 SVN_ERR(svn_wc__db_read_pristine_info(&status, &kind, NULL, NULL, NULL, NULL,
234 &sha1_checksum, NULL, NULL, NULL,
236 scratch_pool, scratch_pool));
239 if (kind != svn_node_file)
240 return svn_error_createf(SVN_ERR_NODE_UNEXPECTED_KIND, NULL,
241 _("Can only get the pristine contents of files; "
242 "'%s' is not a file"),
243 svn_dirent_local_style(local_abspath,
246 if (status == svn_wc__db_status_added && !sha1_checksum)
248 /* Simply added. The pristine base does not exist. */
252 else if (status == svn_wc__db_status_not_present)
253 /* We know that the delete of this node has been committed.
254 This should be the same as if called on an unknown path. */
255 return svn_error_createf(SVN_ERR_WC_PATH_NOT_FOUND, NULL,
256 _("Cannot get the pristine contents of '%s' "
257 "because its delete is already committed"),
258 svn_dirent_local_style(local_abspath,
260 else if (status == svn_wc__db_status_server_excluded
261 || status == svn_wc__db_status_excluded
262 || status == svn_wc__db_status_incomplete)
263 return svn_error_createf(SVN_ERR_WC_PATH_UNEXPECTED_STATUS, NULL,
264 _("Cannot get the pristine contents of '%s' "
265 "because it has an unexpected status"),
266 svn_dirent_local_style(local_abspath,
269 SVN_ERR(svn_wc__db_pristine_read(contents, size, db, local_abspath,
271 result_pool, scratch_pool));
279 /*** Opening and closing files in the adm area. ***/
282 svn_wc__open_adm_stream(svn_stream_t **stream,
283 const char *dir_abspath,
285 apr_pool_t *result_pool,
286 apr_pool_t *scratch_pool)
288 const char *local_abspath;
290 SVN_ERR_ASSERT(svn_dirent_is_absolute(dir_abspath));
292 local_abspath = svn_wc__adm_child(dir_abspath, fname, scratch_pool);
293 return svn_error_trace(svn_stream_open_readonly(stream, local_abspath,
294 result_pool, scratch_pool));
299 svn_wc__open_writable_base(svn_stream_t **stream,
300 const char **temp_base_abspath,
301 svn_checksum_t **md5_checksum,
302 svn_checksum_t **sha1_checksum,
304 const char *wri_abspath,
305 apr_pool_t *result_pool,
306 apr_pool_t *scratch_pool)
308 const char *temp_dir_abspath;
309 SVN_ERR_ASSERT(svn_dirent_is_absolute(wri_abspath));
311 SVN_ERR(svn_wc__db_pristine_get_tempdir(&temp_dir_abspath, db, wri_abspath,
312 scratch_pool, scratch_pool));
313 SVN_ERR(svn_stream_open_unique(stream,
316 svn_io_file_del_none,
317 result_pool, scratch_pool));
319 *stream = svn_stream_checksummed2(*stream, NULL, md5_checksum,
320 svn_checksum_md5, FALSE, result_pool);
322 *stream = svn_stream_checksummed2(*stream, NULL, sha1_checksum,
323 svn_checksum_sha1, FALSE, result_pool);
330 /*** Checking for and creating administrative subdirs. ***/
335 init_adm_tmp_area(const char *path, apr_pool_t *pool)
337 /* SVN_WC__ADM_TMP */
338 SVN_ERR(make_adm_subdir(path, SVN_WC__ADM_TMP, pool));
344 /* Set up a new adm area for PATH, with REPOS_* as the repos info, and
345 INITIAL_REV as the starting revision. The entries file starts out
346 marked as 'incomplete. The adm area starts out locked; remember to
347 unlock it when done. */
349 init_adm(svn_wc__db_t *db,
350 const char *local_abspath,
351 const char *repos_relpath,
352 const char *repos_root_url,
353 const char *repos_uuid,
354 svn_revnum_t initial_rev,
358 /* First, make an empty administrative area. */
359 SVN_ERR(svn_io_dir_make_hidden(svn_wc__adm_child(local_abspath, NULL, pool),
360 APR_OS_DEFAULT, pool));
362 /** Make subdirectories. ***/
364 /* SVN_WC__ADM_PRISTINE */
365 SVN_ERR(make_adm_subdir(local_abspath, SVN_WC__ADM_PRISTINE, pool));
367 /* ### want to add another directory? do a format bump to ensure that
368 ### all existing working copies get the new directories. or maybe
369 ### create-on-demand (more expensive) */
371 /** Init the tmp area. ***/
372 SVN_ERR(init_adm_tmp_area(local_abspath, pool));
374 /* Create the SDB. */
375 SVN_ERR(svn_wc__db_init(db, local_abspath,
376 repos_relpath, repos_root_url, repos_uuid,
380 /* Stamp ENTRIES and FORMAT files for old clients. */
381 SVN_ERR(svn_io_file_create(svn_wc__adm_child(local_abspath,
384 SVN_WC__NON_ENTRIES_STRING,
386 SVN_ERR(svn_io_file_create(svn_wc__adm_child(local_abspath,
389 SVN_WC__NON_ENTRIES_STRING,
396 svn_wc__internal_ensure_adm(svn_wc__db_t *db,
397 const char *local_abspath,
399 const char *repos_root_url,
400 const char *repos_uuid,
401 svn_revnum_t revision,
403 apr_pool_t *scratch_pool)
406 const char *original_repos_relpath;
407 const char *original_root_url;
408 svn_boolean_t is_op_root;
409 const char *repos_relpath = svn_uri_skip_ancestor(repos_root_url, url,
411 svn_wc__db_status_t status;
412 const char *db_repos_relpath, *db_repos_root_url, *db_repos_uuid;
413 svn_revnum_t db_revision;
415 SVN_ERR_ASSERT(svn_dirent_is_absolute(local_abspath));
416 SVN_ERR_ASSERT(url != NULL);
417 SVN_ERR_ASSERT(repos_root_url != NULL);
418 SVN_ERR_ASSERT(repos_uuid != NULL);
419 SVN_ERR_ASSERT(repos_relpath != NULL);
421 SVN_ERR(svn_wc__internal_check_wc(&format, db, local_abspath, TRUE,
424 /* Early out: we know we're not dealing with an existing wc, so
427 return svn_error_trace(init_adm(db, local_abspath,
428 repos_relpath, repos_root_url, repos_uuid,
429 revision, depth, scratch_pool));
431 SVN_ERR(svn_wc__db_read_info(&status, NULL,
432 &db_revision, &db_repos_relpath,
433 &db_repos_root_url, &db_repos_uuid,
434 NULL, NULL, NULL, NULL, NULL, NULL,
435 &original_repos_relpath, &original_root_url,
436 NULL, NULL, NULL, NULL, NULL, NULL,
437 NULL, &is_op_root, NULL, NULL,
439 db, local_abspath, scratch_pool, scratch_pool));
441 /* When the directory exists and is scheduled for deletion or is not-present
442 * do not check the revision or the URL. The revision can be any
443 * arbitrary revision and the URL may differ if the add is
444 * being driven from a merge which will have a different URL. */
445 if (status != svn_wc__db_status_deleted
446 && status != svn_wc__db_status_not_present)
448 /* ### Should we match copyfrom_revision? */
449 if (db_revision != revision)
451 svn_error_createf(SVN_ERR_WC_OBSTRUCTED_UPDATE, NULL,
452 _("Revision %ld doesn't match existing "
453 "revision %ld in '%s'"),
454 revision, db_revision, local_abspath);
456 if (!db_repos_root_url)
458 if (status == svn_wc__db_status_added)
459 SVN_ERR(svn_wc__db_scan_addition(NULL, NULL,
463 NULL, NULL, NULL, NULL,
465 scratch_pool, scratch_pool));
467 SVN_ERR(svn_wc__db_scan_base_repos(&db_repos_relpath,
471 scratch_pool, scratch_pool));
474 /* The caller gives us a URL which should match the entry. However,
475 some callers compensate for an old problem in entry->url and pass
476 the copyfrom_url instead. See ^/notes/api-errata/1.7/wc002.txt. As
477 a result, we allow the passed URL to match copyfrom_url if it
478 does not match the entry's primary URL. */
479 if (strcmp(db_repos_uuid, repos_uuid)
480 || strcmp(db_repos_root_url, repos_root_url)
481 || !svn_relpath_skip_ancestor(db_repos_relpath, repos_relpath))
483 if (!is_op_root /* copy_from was set on op-roots only */
484 || original_root_url == NULL
485 || strcmp(original_root_url, repos_root_url)
486 || strcmp(original_repos_relpath, repos_relpath))
488 svn_error_createf(SVN_ERR_WC_OBSTRUCTED_UPDATE, NULL,
489 _("URL '%s' (uuid: '%s') doesn't match existing "
490 "URL '%s' (uuid: '%s') in '%s'"),
493 svn_path_url_add_component2(db_repos_root_url,
505 svn_wc_ensure_adm4(svn_wc_context_t *wc_ctx,
506 const char *local_abspath,
508 const char *repos_root_url,
509 const char *repos_uuid,
510 svn_revnum_t revision,
512 apr_pool_t *scratch_pool)
514 return svn_error_trace(
515 svn_wc__internal_ensure_adm(wc_ctx->db, local_abspath, url, repos_root_url,
516 repos_uuid, revision, depth, scratch_pool));
520 svn_wc__adm_destroy(svn_wc__db_t *db,
521 const char *dir_abspath,
522 svn_cancel_func_t cancel_func,
524 apr_pool_t *scratch_pool)
526 svn_boolean_t is_wcroot;
528 SVN_ERR_ASSERT(svn_dirent_is_absolute(dir_abspath));
530 SVN_ERR(svn_wc__write_check(db, dir_abspath, scratch_pool));
532 SVN_ERR(svn_wc__db_is_wcroot(&is_wcroot, db, dir_abspath, scratch_pool));
534 /* Well, the coast is clear for blowing away the administrative
535 directory, which also removes remaining locks */
537 /* Now close the DB, and we can delete the working copy */
540 SVN_ERR(svn_wc__db_drop_root(db, dir_abspath, scratch_pool));
541 SVN_ERR(svn_io_remove_dir2(svn_wc__adm_child(dir_abspath, NULL,
544 cancel_func, cancel_baton,
553 svn_wc__adm_cleanup_tmp_area(svn_wc__db_t *db,
554 const char *adm_abspath,
555 apr_pool_t *scratch_pool)
557 const char *tmp_path;
559 SVN_ERR_ASSERT(svn_dirent_is_absolute(adm_abspath));
561 SVN_ERR(svn_wc__write_check(db, adm_abspath, scratch_pool));
563 /* Get the path to the tmp area, and blow it away. */
564 tmp_path = svn_wc__adm_child(adm_abspath, SVN_WC__ADM_TMP, scratch_pool);
566 SVN_ERR(svn_io_remove_dir2(tmp_path, TRUE, NULL, NULL, scratch_pool));
568 /* Now, rebuild the tmp area. */
569 return svn_error_trace(init_adm_tmp_area(adm_abspath, scratch_pool));
574 svn_wc__get_tmpdir(const char **tmpdir_abspath,
575 svn_wc_context_t *wc_ctx,
576 const char *wri_abspath,
577 apr_pool_t *result_pool,
578 apr_pool_t *scratch_pool)
580 SVN_ERR(svn_wc__db_temp_wcroot_tempdir(tmpdir_abspath,
581 wc_ctx->db, wri_abspath,
582 result_pool, scratch_pool));