2 * wc_db_wcroot.c : supporting datastructures for the administrative database
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
24 #define SVN_WC__I_AM_WC_DB
28 #include "svn_dirent_uri.h"
31 #include "svn_pools.h"
32 #include "svn_version.h"
35 #include "adm_files.h"
36 #include "wc_db_private.h"
37 #include "wc-queries.h"
39 #include "svn_private_config.h"
41 /* ### Same values as wc_db.c */
42 #define SDB_FILE "wc.db"
43 #define UNKNOWN_WC_ID ((apr_int64_t) -1)
44 #define FORMAT_FROM_SDB (-1)
46 /* #define VERIFY_ON_CLOSE */
48 /* Get the format version from a wc-1 directory. If it is not a working copy
49 directory, then it sets VERSION to zero and returns no error. */
51 get_old_version(int *version,
53 apr_pool_t *scratch_pool)
56 const char *format_file_path;
59 /* Try reading the format number from the entries file. */
60 format_file_path = svn_wc__adm_child(abspath, SVN_WC__ADM_ENTRIES,
63 /* Since trying to open a non-existent file is quite expensive, try a
64 quick stat call first. In wc-ng w/cs, this will be an early exit. */
65 SVN_ERR(svn_io_check_path(format_file_path, &kind, scratch_pool));
66 if (kind == svn_node_none)
72 err = svn_io_read_version_file(version, format_file_path, scratch_pool);
75 if (err->apr_err != SVN_ERR_BAD_VERSION_FILE_FORMAT
76 && !APR_STATUS_IS_ENOENT(err->apr_err)
77 && !APR_STATUS_IS_ENOTDIR(err->apr_err))
78 return svn_error_createf(SVN_ERR_WC_MISSING, err, _("'%s' does not exist"),
79 svn_dirent_local_style(abspath, scratch_pool));
82 /* This must be a really old working copy! Fall back to reading the
85 Note that the format file might not exist in newer working copies
86 (format 7 and higher), but in that case, the entries file should
87 have contained the format number. */
88 format_file_path = svn_wc__adm_child(abspath, SVN_WC__ADM_FORMAT,
90 err = svn_io_read_version_file(version, format_file_path, scratch_pool);
94 /* Whatever error may have occurred... we can just ignore. This is not
95 a working copy directory. Signal the caller. */
103 /* A helper function to parse_local_abspath() which returns the on-disk KIND
104 of LOCAL_ABSPATH, using DB and SCRATCH_POOL as needed.
106 This function may do strange things, but at long as it comes up with the
107 Right Answer, we should be happy. */
109 get_path_kind(svn_node_kind_t *kind,
111 const char *local_abspath,
112 apr_pool_t *scratch_pool)
114 svn_boolean_t special;
115 svn_node_kind_t node_kind;
117 /* This implements a *really* simple LRU cache, where "simple" is defined
118 as "only one element". In other words, we remember the most recently
119 queried path, and nothing else. This gives >80% cache hits. */
121 if (db->parse_cache.abspath
122 && strcmp(db->parse_cache.abspath->data, local_abspath) == 0)
125 *kind = db->parse_cache.kind;
129 if (!db->parse_cache.abspath)
131 db->parse_cache.abspath = svn_stringbuf_create(local_abspath,
136 svn_stringbuf_set(db->parse_cache.abspath, local_abspath);
139 SVN_ERR(svn_io_check_special_path(local_abspath, &node_kind,
140 &special, scratch_pool));
142 db->parse_cache.kind = (special ? svn_node_symlink : node_kind);
143 *kind = db->parse_cache.kind;
150 svn_wc__db_verify_no_work(svn_sqlite__db_t *sdb)
152 svn_sqlite__stmt_t *stmt;
153 svn_boolean_t have_row;
155 SVN_ERR(svn_sqlite__get_statement(&stmt, sdb, STMT_LOOK_FOR_WORK));
156 SVN_ERR(svn_sqlite__step(&have_row, stmt));
157 SVN_ERR(svn_sqlite__reset(stmt));
160 return svn_error_create(SVN_ERR_WC_CLEANUP_REQUIRED, NULL,
161 NULL /* nothing to add. */);
166 #if defined(VERIFY_ON_CLOSE) && defined(SVN_DEBUG)
167 /* Implements svn_wc__db_verify_cb_t */
169 verify_db_cb(void *baton,
170 const char *wc_abspath,
171 const char *local_relpath,
175 apr_pool_t *scratch_pool)
178 SVN_DBG(("DB-VRFY: %s: %s (%d): SV%04d %s",
179 wc_abspath, local_relpath, op_depth, id, msg));
181 SVN_DBG(("DB-VRFY: %s: %s: SV%04d %s",
182 wc_abspath, local_relpath, id, msg));
190 close_wcroot(void *data)
192 svn_wc__db_wcroot_t *wcroot = data;
195 SVN_ERR_ASSERT_NO_RETURN(wcroot->sdb != NULL);
197 #if defined(VERIFY_ON_CLOSE) && defined(SVN_DEBUG)
198 if (getenv("SVN_CMDLINE_VERIFY_SQL_AT_CLOSE"))
200 apr_pool_t *scratch_pool = svn_pool_create(NULL);
202 svn_error_clear(svn_wc__db_verify_db_full_internal(
203 wcroot, verify_db_cb, NULL, scratch_pool));
205 svn_pool_destroy(scratch_pool);
209 err = svn_sqlite__close(wcroot->sdb);
213 apr_status_t result = err->apr_err;
214 svn_error_clear(err);
223 svn_wc__db_open(svn_wc__db_t **db,
224 svn_config_t *config,
225 svn_boolean_t open_without_upgrade,
226 svn_boolean_t enforce_empty_wq,
227 apr_pool_t *result_pool,
228 apr_pool_t *scratch_pool)
230 *db = apr_pcalloc(result_pool, sizeof(**db));
231 (*db)->config = config;
232 (*db)->verify_format = !open_without_upgrade;
233 (*db)->enforce_empty_wq = enforce_empty_wq;
234 (*db)->dir_data = apr_hash_make(result_pool);
236 (*db)->state_pool = result_pool;
238 /* Don't need to initialize (*db)->parse_cache, due to the calloc above */
242 svn_boolean_t sqlite_exclusive = FALSE;
245 err = svn_config_get_bool(config, &sqlite_exclusive,
246 SVN_CONFIG_SECTION_WORKING_COPY,
247 SVN_CONFIG_OPTION_SQLITE_EXCLUSIVE,
251 svn_error_clear(err);
254 (*db)->exclusive = sqlite_exclusive;
256 err = svn_config_get_int64(config, &timeout,
257 SVN_CONFIG_SECTION_WORKING_COPY,
258 SVN_CONFIG_OPTION_SQLITE_BUSY_TIMEOUT,
260 if (err || timeout < 0 || timeout > APR_INT32_MAX)
261 svn_error_clear(err);
263 (*db)->timeout = (apr_int32_t)timeout;
271 svn_wc__db_close(svn_wc__db_t *db)
273 apr_pool_t *scratch_pool = db->state_pool;
274 apr_hash_t *roots = apr_hash_make(scratch_pool);
275 apr_hash_index_t *hi;
277 /* Collect all the unique WCROOT structures, and empty out DIR_DATA. */
278 for (hi = apr_hash_first(scratch_pool, db->dir_data);
280 hi = apr_hash_next(hi))
282 svn_wc__db_wcroot_t *wcroot = apr_hash_this_val(hi);
283 const char *local_abspath = apr_hash_this_key(hi);
286 svn_hash_sets(roots, wcroot->abspath, wcroot);
288 svn_hash_sets(db->dir_data, local_abspath, NULL);
291 /* Run the cleanup for each WCROOT. */
292 return svn_error_trace(svn_wc__db_close_many_wcroots(roots, db->state_pool,
298 svn_wc__db_pdh_create_wcroot(svn_wc__db_wcroot_t **wcroot,
299 const char *wcroot_abspath,
300 svn_sqlite__db_t *sdb,
303 svn_boolean_t verify_format,
304 apr_pool_t *result_pool,
305 apr_pool_t *scratch_pool)
307 if (sdb && format == FORMAT_FROM_SDB)
308 SVN_ERR(svn_sqlite__read_schema_version(&format, sdb, scratch_pool));
310 /* If we construct a wcroot, then we better have a format. */
311 SVN_ERR_ASSERT(format >= 1);
313 /* If this working copy is PRE-1.0, then simply bail out. */
316 return svn_error_createf(
317 SVN_ERR_WC_UNSUPPORTED_FORMAT, NULL,
318 _("Working copy format of '%s' is too old (%d); "
319 "please check out your working copy again"),
320 svn_dirent_local_style(wcroot_abspath, scratch_pool), format);
323 /* If this working copy is from a future version, then bail out. */
324 if (format > SVN_WC__VERSION)
326 return svn_error_createf(
327 SVN_ERR_WC_UNSUPPORTED_FORMAT, NULL,
328 _("This client is too old to work with the working copy at\n"
329 "'%s' (format %d).\n"
330 "You need to get a newer Subversion client. For more details, see\n"
331 " http://subversion.apache.org/faq.html#working-copy-format-change\n"
333 svn_dirent_local_style(wcroot_abspath, scratch_pool),
337 /* Verify that no work items exists. If they do, then our integrity is
338 suspect and, thus, we cannot upgrade this database. */
339 if (format >= SVN_WC__HAS_WORK_QUEUE &&
340 format < SVN_WC__VERSION && verify_format)
342 svn_error_t *err = svn_wc__db_verify_no_work(sdb);
345 /* Special message for attempts to upgrade a 1.7-dev wc with
346 outstanding workqueue items. */
347 if (err->apr_err == SVN_ERR_WC_CLEANUP_REQUIRED
348 && format < SVN_WC__VERSION && verify_format)
349 err = svn_error_quick_wrap(err, _("Cleanup with an older 1.7 "
350 "client before upgrading with "
352 return svn_error_trace(err);
356 /* Auto-upgrade the SDB if possible. */
357 if (format < SVN_WC__VERSION && verify_format)
359 return svn_error_createf(SVN_ERR_WC_UPGRADE_REQUIRED, NULL,
360 _("The working copy at '%s'\nis too old "
361 "(format %d) to work with client version "
362 "'%s' (expects format %d). You need to "
363 "upgrade the working copy first.\n"),
364 svn_dirent_local_style(wcroot_abspath,
366 format, SVN_VERSION, SVN_WC__VERSION);
369 *wcroot = apr_palloc(result_pool, sizeof(**wcroot));
371 (*wcroot)->abspath = wcroot_abspath;
372 (*wcroot)->sdb = sdb;
373 (*wcroot)->wc_id = wc_id;
374 (*wcroot)->format = format;
375 /* 8 concurrent locks is probably more than a typical wc_ng based svn client
377 (*wcroot)->owned_locks = apr_array_make(result_pool, 8,
378 sizeof(svn_wc__db_wclock_t));
379 (*wcroot)->access_cache = apr_hash_make(result_pool);
381 /* SDB will be NULL for pre-NG working copies. We only need to run a
382 cleanup when the SDB is present. */
384 apr_pool_cleanup_register(result_pool, *wcroot, close_wcroot,
385 apr_pool_cleanup_null);
391 svn_wc__db_close_many_wcroots(apr_hash_t *roots,
392 apr_pool_t *state_pool,
393 apr_pool_t *scratch_pool)
395 apr_hash_index_t *hi;
397 for (hi = apr_hash_first(scratch_pool, roots); hi; hi = apr_hash_next(hi))
399 svn_wc__db_wcroot_t *wcroot = apr_hash_this_val(hi);
402 result = apr_pool_cleanup_run(state_pool, wcroot, close_wcroot);
403 if (result != APR_SUCCESS)
404 return svn_error_wrap_apr(result, NULL);
411 /* POOL may be NULL if the lifetime of LOCAL_ABSPATH is sufficient. */
413 compute_relpath(const svn_wc__db_wcroot_t *wcroot,
414 const char *local_abspath,
415 apr_pool_t *result_pool)
417 const char *relpath = svn_dirent_is_child(wcroot->abspath, local_abspath,
425 /* Return in *LINK_TARGET_ABSPATH the absolute path the symlink at
426 * LOCAL_ABSPATH is pointing to. Perform all allocations in POOL. */
428 read_link_target(const char **link_target_abspath,
429 const char *local_abspath,
432 svn_string_t *link_target;
433 const char *canon_link_target;
435 SVN_ERR(svn_io_read_link(&link_target, local_abspath, pool));
436 if (link_target->len == 0)
437 return svn_error_createf(SVN_ERR_WC_NOT_SYMLINK, NULL,
438 _("The symlink at '%s' points nowhere"),
439 svn_dirent_local_style(local_abspath, pool));
441 canon_link_target = svn_dirent_canonicalize(link_target->data, pool);
443 /* Treat relative symlinks as relative to LOCAL_ABSPATH's parent. */
444 if (!svn_dirent_is_absolute(canon_link_target))
445 canon_link_target = svn_dirent_join(svn_dirent_dirname(local_abspath,
447 canon_link_target, pool);
449 /* Collapse any .. in the symlink part of the path. */
450 if (svn_path_is_backpath_present(canon_link_target))
451 SVN_ERR(svn_dirent_get_absolute(link_target_abspath, canon_link_target,
454 *link_target_abspath = canon_link_target;
459 /* Verify if the sqlite_stat1 table exists and if not tries to add
460 this table (but ignores errors on adding the schema) */
462 verify_stats_table(svn_sqlite__db_t *sdb,
464 apr_pool_t *scratch_pool)
466 svn_sqlite__stmt_t *stmt;
467 svn_boolean_t have_row;
469 if (format != SVN_WC__ENSURE_STAT1_TABLE)
472 SVN_ERR(svn_sqlite__get_statement(&stmt, sdb,
473 STMT_HAVE_STAT1_TABLE));
474 SVN_ERR(svn_sqlite__step(&have_row, stmt));
475 SVN_ERR(svn_sqlite__reset(stmt));
480 svn_wc__db_install_schema_statistics(sdb, scratch_pool));
486 /* Sqlite transaction helper for opening the db in
487 svn_wc__db_wcroot_parse_local_abspath() to avoid multiple
488 db operations that each obtain and release a lock */
490 fetch_sdb_info(apr_int64_t *wc_id,
492 svn_sqlite__db_t *sdb,
493 apr_pool_t *scratch_pool)
498 SVN_SQLITE__WITH_LOCK4(
499 svn_wc__db_util_fetch_wc_id(wc_id, sdb, scratch_pool),
500 svn_sqlite__read_schema_version(format, sdb, scratch_pool),
501 verify_stats_table(sdb, *format, scratch_pool),
510 svn_wc__db_wcroot_parse_local_abspath(svn_wc__db_wcroot_t **wcroot,
511 const char **local_relpath,
513 const char *local_abspath,
514 apr_pool_t *result_pool,
515 apr_pool_t *scratch_pool)
517 const char *local_dir_abspath;
518 const char *original_abspath = local_abspath;
519 svn_node_kind_t kind;
520 const char *build_relpath;
521 svn_wc__db_wcroot_t *probe_wcroot;
522 svn_wc__db_wcroot_t *found_wcroot = NULL;
523 const char *scan_abspath;
524 svn_sqlite__db_t *sdb = NULL;
525 svn_boolean_t moved_upwards = FALSE;
526 svn_boolean_t always_check = FALSE;
528 const char *adm_relpath;
529 /* Non-NULL if WCROOT is found through a symlink: */
530 const char *symlink_wcroot_abspath = NULL;
532 /* ### we need more logic for finding the database (if it is located
533 ### outside of the wcroot) and then managing all of that within DB.
534 ### for now: play quick & dirty. */
536 probe_wcroot = svn_hash_gets(db->dir_data, local_abspath);
537 if (probe_wcroot != NULL)
539 *wcroot = probe_wcroot;
541 /* We got lucky. Just return the thing BEFORE performing any I/O. */
542 /* ### validate SMODE against how we opened wcroot->sdb? and against
543 ### DB->mode? (will we record per-dir mode?) */
545 /* ### for most callers, we could pass NULL for result_pool. */
546 *local_relpath = compute_relpath(probe_wcroot, local_abspath,
552 /* ### at some point in the future, we may need to find a way to get
553 ### rid of this stat() call. it is going to happen for EVERY call
554 ### into wc_db which references a file. calls for directories could
555 ### get an early-exit in the hash lookup just above. */
556 SVN_ERR(get_path_kind(&kind, db, local_abspath, scratch_pool));
557 if (kind != svn_node_dir)
559 /* If the node specified by the path is NOT present, then it cannot
560 possibly be a directory containing ".svn/wc.db".
562 If it is a file, then it cannot contain ".svn/wc.db".
564 For both of these cases, strip the basename off of the path and
565 move up one level. Keep record of what we strip, though, since
566 we'll need it later to construct local_relpath. */
567 svn_dirent_split(&local_dir_abspath, &build_relpath, local_abspath,
570 /* Is this directory in our hash? */
571 probe_wcroot = svn_hash_gets(db->dir_data, local_dir_abspath);
572 if (probe_wcroot != NULL)
574 const char *dir_relpath;
576 *wcroot = probe_wcroot;
578 /* Stashed directory's local_relpath + basename. */
579 dir_relpath = compute_relpath(probe_wcroot, local_dir_abspath,
581 *local_relpath = svn_relpath_join(dir_relpath,
587 /* If the requested path is not on the disk, then we don't know how
588 many ancestors need to be scanned until we start hitting content
589 on the disk. Set ALWAYS_CHECK to keep looking for .svn/entries
590 rather than bailing out after the first check. */
591 if (kind == svn_node_none)
594 /* Start the scanning at LOCAL_DIR_ABSPATH. */
595 local_abspath = local_dir_abspath;
599 /* Start the local_relpath empty. If *this* directory contains the
600 wc.db, then relpath will be the empty string. */
603 /* Remember the dir containing LOCAL_ABSPATH (they're the same). */
604 local_dir_abspath = local_abspath;
607 /* LOCAL_ABSPATH refers to a directory at this point. At this point,
608 we've determined that an associated WCROOT is NOT in the DB's hash
609 table for this directory. Let's find an existing one in the ancestors,
610 or create one when we find the actual wcroot. */
612 /* Assume that LOCAL_ABSPATH is a directory, and look for the SQLite
613 database in the right place. If we find it... great! If not, then
614 peel off some components, and try again. */
616 adm_relpath = svn_wc_get_adm_dir(scratch_pool);
620 svn_node_kind_t adm_subdir_kind;
622 const char *adm_subdir = svn_dirent_join(local_abspath, adm_relpath,
625 SVN_ERR(svn_io_check_path(adm_subdir, &adm_subdir_kind, scratch_pool));
627 if (adm_subdir_kind == svn_node_dir)
629 /* We always open the database in read/write mode. If the database
630 isn't writable in the filesystem, SQLite will internally open
631 it as read-only, and we'll get an error if we try to do a write
634 We could decide what to do on a per-operation basis, but since
635 we're caching database handles, it make sense to be as permissive
636 as the filesystem allows. */
637 err = svn_wc__db_util_open_db(&sdb, local_abspath, SDB_FILE,
638 svn_sqlite__mode_readwrite,
639 db->exclusive, db->timeout, NULL,
640 db->state_pool, scratch_pool);
644 /* Install self-verification trigger statements. */
645 err = svn_sqlite__exec_statements(sdb,
646 STMT_VERIFICATION_TRIGGERS);
647 if (err && err->apr_err == SVN_ERR_SQLITE_ERROR)
649 /* Verification triggers can fail to install on old 1.7-dev
650 * formats which didn't have a NODES table yet. Ignore sqlite
651 * errors so such working copies can be upgraded. */
652 svn_error_clear(err);
659 if (err->apr_err != SVN_ERR_SQLITE_ERROR
660 && !APR_STATUS_IS_ENOENT(err->apr_err))
661 return svn_error_trace(err);
662 svn_error_clear(err);
664 /* If we have not moved upwards, then check for a wc-1 working copy.
665 Since wc-1 has a .svn in every directory, and we didn't find one
666 in the original directory, then we aren't looking at a wc-1.
668 If the original path is not present, then we have to check on every
669 iteration. The content may be the immediate parent, or possibly
670 five ancetors higher. We don't test for directory presence (just
671 for the presence of subdirs/files), so we don't know when we can
672 stop checking ... so just check always. */
673 if (!moved_upwards || always_check)
675 SVN_ERR(get_old_version(&wc_format, local_abspath,
682 /* We couldn't open the SDB within the specified directory, so
683 move up one more directory. */
684 if (svn_dirent_is_root(local_abspath, strlen(local_abspath)))
686 /* Hit the root without finding a wcroot. */
688 /* The wcroot could be a symlink to a directory.
689 * (Issue #2557, #3987). If so, try again, this time scanning
690 * for a db within the directory the symlink points to,
691 * rather than within the symlink's parent directory. */
692 if (kind == svn_node_symlink)
694 svn_node_kind_t resolved_kind;
696 local_abspath = original_abspath;
698 SVN_ERR(svn_io_check_resolved_path(local_abspath,
701 if (resolved_kind == svn_node_dir)
703 /* Is this directory recorded in our hash? */
704 found_wcroot = svn_hash_gets(db->dir_data, local_abspath);
708 symlink_wcroot_abspath = local_abspath;
709 SVN_ERR(read_link_target(&local_abspath, local_abspath,
713 moved_upwards = FALSE;
714 local_dir_abspath = local_abspath;
721 return svn_error_createf(SVN_ERR_WC_NOT_WORKING_COPY, NULL,
722 _("'%s' is not a working copy"),
723 svn_dirent_local_style(original_abspath,
727 local_abspath = svn_dirent_dirname(local_abspath, scratch_pool);
729 moved_upwards = TRUE;
730 symlink_wcroot_abspath = NULL;
732 /* Is the parent directory recorded in our hash? */
733 found_wcroot = svn_hash_gets(db->dir_data, local_abspath);
734 if (found_wcroot != NULL)
738 if (found_wcroot != NULL)
740 /* We found a hash table entry for an ancestor, so we stopped scanning
741 since all subdirectories use the same WCROOT. */
742 *wcroot = found_wcroot;
744 else if (wc_format == 0)
746 /* We finally found the database. Construct a wcroot_t for it. */
752 err = fetch_sdb_info(&wc_id, &format, sdb, scratch_pool);
755 if (err->apr_err == SVN_ERR_WC_CORRUPT)
756 return svn_error_quick_wrapf(
757 err, _("Missing a row in WCROOT for '%s'."),
758 svn_dirent_local_style(original_abspath, scratch_pool));
759 return svn_error_trace(err);
762 /* WCROOT.local_abspath may be NULL when the database is stored
763 inside the wcroot, but we know the abspath is this directory
764 (ie. where we found it). */
766 err = svn_wc__db_pdh_create_wcroot(wcroot,
767 apr_pstrdup(db->state_pool,
768 symlink_wcroot_abspath
769 ? symlink_wcroot_abspath
773 db->state_pool, scratch_pool);
774 if (err && (err->apr_err == SVN_ERR_WC_UNSUPPORTED_FORMAT ||
775 err->apr_err == SVN_ERR_WC_UPGRADE_REQUIRED) &&
776 kind == svn_node_symlink)
778 /* We found an unsupported WC after traversing upwards from a
779 * symlink. Fall through to code below to check if the symlink
780 * points at a supported WC. */
781 svn_error_clear(err);
786 /* Close handle if we are not going to use it to support
787 upgrading with exclusive wc locking. */
788 return svn_error_compose_create(err, svn_sqlite__close(sdb));
793 /* We found something that looks like a wc-1 working copy directory.
794 However, if the format version is 12 and the .svn/entries file
795 is only 3 bytes long, then it's a breadcrumb in a wc-ng working
796 copy that's missing an .svn/wc.db, or its .svn/wc.db is corrupt. */
797 if (wc_format == SVN_WC__WC_NG_VERSION /* 12 */)
801 /* Check attributes of .svn/entries */
802 const char *admin_abspath = svn_wc__adm_child(
803 local_abspath, SVN_WC__ADM_ENTRIES, scratch_pool);
804 svn_error_t *err = svn_io_stat(&info, admin_abspath, APR_FINFO_SIZE,
807 /* If the former does not succeed, something is seriously wrong. */
809 return svn_error_createf(
810 SVN_ERR_WC_CORRUPT, err,
811 _("The working copy at '%s' is corrupt."),
812 svn_dirent_local_style(local_abspath, scratch_pool));
813 svn_error_clear(err);
817 /* Check existence of .svn/wc.db */
818 admin_abspath = svn_wc__adm_child(local_abspath, SDB_FILE,
820 err = svn_io_stat(&info, admin_abspath, APR_FINFO_SIZE,
822 if (err && APR_STATUS_IS_ENOENT(err->apr_err))
824 svn_error_clear(err);
825 return svn_error_createf(
826 SVN_ERR_WC_CORRUPT, NULL,
827 _("The working copy database at '%s' is missing."),
828 svn_dirent_local_style(local_abspath, scratch_pool));
831 /* We should never have reached this point in the code
832 if .svn/wc.db exists; therefore it's best to assume
834 return svn_error_createf(
835 SVN_ERR_WC_CORRUPT, err,
836 _("The working copy database at '%s' is corrupt."),
837 svn_dirent_local_style(local_abspath, scratch_pool));
841 SVN_ERR(svn_wc__db_pdh_create_wcroot(wcroot,
842 apr_pstrdup(db->state_pool,
843 symlink_wcroot_abspath
844 ? symlink_wcroot_abspath
846 NULL, UNKNOWN_WC_ID, wc_format,
848 db->state_pool, scratch_pool));
853 const char *dir_relpath;
855 if (symlink_wcroot_abspath)
857 /* The WCROOT was found through a symlink pointing at the root of
858 * the WC. Cache the WCROOT under the symlink's path. */
859 local_dir_abspath = symlink_wcroot_abspath;
862 /* The subdirectory's relpath is easily computed relative to the
863 wcroot that we just found. */
864 dir_relpath = compute_relpath(*wcroot, local_dir_abspath, NULL);
866 /* And the result local_relpath may include a filename. */
867 *local_relpath = svn_relpath_join(dir_relpath, build_relpath, result_pool);
870 if (kind == svn_node_symlink)
872 svn_boolean_t retry_if_dir = FALSE;
873 svn_wc__db_status_t status;
874 svn_boolean_t conflicted;
877 /* Check if the symlink is versioned or obstructs a versioned node
878 * in this DB -- in that case, use this wcroot. Else, if the symlink
879 * points to a directory, try to find a wcroot in that directory
884 err = svn_wc__db_read_info_internal(&status, NULL, NULL, NULL, NULL,
885 NULL, NULL, NULL, NULL, NULL,
886 NULL, NULL, NULL, NULL, NULL,
887 NULL, NULL, NULL, &conflicted,
888 NULL, NULL, NULL, NULL, NULL,
889 NULL, *wcroot, *local_relpath,
890 scratch_pool, scratch_pool);
893 if (err->apr_err != SVN_ERR_WC_PATH_NOT_FOUND
894 && !SVN_WC__ERR_IS_NOT_CURRENT_WC(err))
895 return svn_error_trace(err);
897 svn_error_clear(err);
898 retry_if_dir = TRUE; /* The symlink is unversioned. */
902 /* The symlink is versioned, or obstructs a versioned node.
903 * Ignore non-conflicted not-present/excluded nodes.
904 * This allows the symlink to redirect the wcroot query to a
905 * directory, regardless of 'invisible' nodes in this WC. */
906 retry_if_dir = ((status == svn_wc__db_status_not_present ||
907 status == svn_wc__db_status_excluded ||
908 status == svn_wc__db_status_server_excluded)
917 svn_node_kind_t resolved_kind;
919 SVN_ERR(svn_io_check_resolved_path(original_abspath,
922 if (resolved_kind == svn_node_dir)
924 symlink_wcroot_abspath = original_abspath;
925 SVN_ERR(read_link_target(&local_abspath, original_abspath,
927 /* This handle was opened in this function but is not going
928 to be used further so close it. */
930 SVN_ERR(svn_sqlite__close(sdb));
931 goto try_symlink_as_dir;
936 /* We've found the appropriate WCROOT for the requested path. Stash
937 it into that path's directory. */
938 svn_hash_sets(db->dir_data,
939 apr_pstrdup(db->state_pool, local_dir_abspath),
942 /* Did we traverse up to parent directories? */
945 /* We did NOT move to a parent of the original requested directory.
946 We've constructed and filled in a WCROOT for the request, so we
951 /* The WCROOT that we just found/built was for the LOCAL_ABSPATH originally
952 passed into this function. We stepped *at least* one directory above that.
953 We should now associate the WROOT for each parent directory that does
954 not (yet) have one. */
956 scan_abspath = local_dir_abspath;
960 const char *parent_dir = svn_dirent_dirname(scan_abspath, scratch_pool);
961 svn_wc__db_wcroot_t *parent_wcroot;
963 parent_wcroot = svn_hash_gets(db->dir_data, parent_dir);
964 if (parent_wcroot == NULL)
966 svn_hash_sets(db->dir_data, apr_pstrdup(db->state_pool, parent_dir),
970 /* Move up a directory, stopping when we reach the directory where
971 we found/built the WCROOT. */
972 scan_abspath = parent_dir;
974 while (strcmp(scan_abspath, local_abspath) != 0);
981 svn_wc__db_drop_root(svn_wc__db_t *db,
982 const char *local_abspath,
983 apr_pool_t *scratch_pool)
985 svn_wc__db_wcroot_t *root_wcroot = svn_hash_gets(db->dir_data, local_abspath);
986 apr_hash_index_t *hi;
992 if (strcmp(root_wcroot->abspath, local_abspath) != 0)
993 return svn_error_createf(SVN_ERR_WC_NOT_WORKING_COPY, NULL,
994 _("'%s' is not a working copy root"),
995 svn_dirent_local_style(local_abspath,
998 for (hi = apr_hash_first(scratch_pool, db->dir_data);
1000 hi = apr_hash_next(hi))
1002 svn_wc__db_wcroot_t *wcroot = apr_hash_this_val(hi);
1004 if (wcroot == root_wcroot)
1005 svn_hash_sets(db->dir_data, apr_hash_this_key(hi), NULL);
1008 result = apr_pool_cleanup_run(db->state_pool, root_wcroot, close_wcroot);
1009 if (result != APR_SUCCESS)
1010 return svn_error_wrap_apr(result, NULL);
1012 return SVN_NO_ERROR;