]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/subversion/subversion/libsvn_wc/wc_db_wcroot.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / subversion / subversion / libsvn_wc / wc_db_wcroot.c
1 /*
2  * wc_db_wcroot.c :  supporting datastructures for the administrative database
3  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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
20  *    under the License.
21  * ====================================================================
22  */
23
24 #define SVN_WC__I_AM_WC_DB
25
26 #include <assert.h>
27
28 #include "svn_dirent_uri.h"
29 #include "svn_hash.h"
30 #include "svn_path.h"
31 #include "svn_version.h"
32
33 #include "wc.h"
34 #include "adm_files.h"
35 #include "wc_db_private.h"
36 #include "wc-queries.h"
37
38 #include "svn_private_config.h"
39
40 /* ### Same values as wc_db.c */
41 #define SDB_FILE  "wc.db"
42 #define UNKNOWN_WC_ID ((apr_int64_t) -1)
43 #define FORMAT_FROM_SDB (-1)
44
45
46 \f
47 /* Get the format version from a wc-1 directory. If it is not a working copy
48    directory, then it sets VERSION to zero and returns no error.  */
49 static svn_error_t *
50 get_old_version(int *version,
51                 const char *abspath,
52                 apr_pool_t *scratch_pool)
53 {
54   svn_error_t *err;
55   const char *format_file_path;
56   svn_node_kind_t kind;
57
58   /* Try reading the format number from the entries file.  */
59   format_file_path = svn_wc__adm_child(abspath, SVN_WC__ADM_ENTRIES,
60                                        scratch_pool);
61
62   /* Since trying to open a non-existent file is quite expensive, try a
63      quick stat call first. In wc-ng w/cs, this will be an early exit. */
64   SVN_ERR(svn_io_check_path(format_file_path, &kind, scratch_pool));
65   if (kind == svn_node_none)
66     {
67       *version = 0;
68       return SVN_NO_ERROR;
69     }
70
71   err = svn_io_read_version_file(version, format_file_path, scratch_pool);
72   if (err == NULL)
73     return SVN_NO_ERROR;
74   if (err->apr_err != SVN_ERR_BAD_VERSION_FILE_FORMAT
75       && !APR_STATUS_IS_ENOENT(err->apr_err)
76       && !APR_STATUS_IS_ENOTDIR(err->apr_err))
77     return svn_error_createf(SVN_ERR_WC_MISSING, err, _("'%s' does not exist"),
78                              svn_dirent_local_style(abspath, scratch_pool));
79   svn_error_clear(err);
80
81   /* This must be a really old working copy!  Fall back to reading the
82      format file.
83
84      Note that the format file might not exist in newer working copies
85      (format 7 and higher), but in that case, the entries file should
86      have contained the format number. */
87   format_file_path = svn_wc__adm_child(abspath, SVN_WC__ADM_FORMAT,
88                                        scratch_pool);
89   err = svn_io_read_version_file(version, format_file_path, scratch_pool);
90   if (err == NULL)
91     return SVN_NO_ERROR;
92
93   /* Whatever error may have occurred... we can just ignore. This is not
94      a working copy directory. Signal the caller.  */
95   svn_error_clear(err);
96
97   *version = 0;
98   return SVN_NO_ERROR;
99 }
100
101
102 /* A helper function to parse_local_abspath() which returns the on-disk KIND
103    of LOCAL_ABSPATH, using DB and SCRATCH_POOL as needed.
104
105    This function may do strange things, but at long as it comes up with the
106    Right Answer, we should be happy. */
107 static svn_error_t *
108 get_path_kind(svn_node_kind_t *kind,
109               svn_wc__db_t *db,
110               const char *local_abspath,
111               apr_pool_t *scratch_pool)
112 {
113   svn_boolean_t special;
114   svn_node_kind_t node_kind;
115
116   /* This implements a *really* simple LRU cache, where "simple" is defined
117      as "only one element".  In other words, we remember the most recently
118      queried path, and nothing else.  This gives >80% cache hits. */
119
120   if (db->parse_cache.abspath
121         && strcmp(db->parse_cache.abspath->data, local_abspath) == 0)
122     {
123       /* Cache hit! */
124       *kind = db->parse_cache.kind;
125       return SVN_NO_ERROR;
126     }
127
128   if (!db->parse_cache.abspath)
129     {
130       db->parse_cache.abspath = svn_stringbuf_create(local_abspath,
131                                                      db->state_pool);
132     }
133   else
134     {
135       svn_stringbuf_set(db->parse_cache.abspath, local_abspath);
136     }
137
138   SVN_ERR(svn_io_check_special_path(local_abspath, &node_kind,
139                                     &special, scratch_pool));
140
141   db->parse_cache.kind = (special ? svn_node_symlink : node_kind);
142   *kind = db->parse_cache.kind;
143
144   return SVN_NO_ERROR;
145 }
146
147
148 /* Return an error if the work queue in SDB is non-empty. */
149 static svn_error_t *
150 verify_no_work(svn_sqlite__db_t *sdb)
151 {
152   svn_sqlite__stmt_t *stmt;
153   svn_boolean_t have_row;
154
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));
158
159   if (have_row)
160     return svn_error_create(SVN_ERR_WC_CLEANUP_REQUIRED, NULL,
161                             NULL /* nothing to add.  */);
162
163   return SVN_NO_ERROR;
164 }
165
166
167 /* */
168 static apr_status_t
169 close_wcroot(void *data)
170 {
171   svn_wc__db_wcroot_t *wcroot = data;
172   svn_error_t *err;
173
174   SVN_ERR_ASSERT_NO_RETURN(wcroot->sdb != NULL);
175
176   err = svn_sqlite__close(wcroot->sdb);
177   wcroot->sdb = NULL;
178   if (err)
179     {
180       apr_status_t result = err->apr_err;
181       svn_error_clear(err);
182       return result;
183     }
184
185   return APR_SUCCESS;
186 }
187
188
189 svn_error_t *
190 svn_wc__db_open(svn_wc__db_t **db,
191                 svn_config_t *config,
192                 svn_boolean_t open_without_upgrade,
193                 svn_boolean_t enforce_empty_wq,
194                 apr_pool_t *result_pool,
195                 apr_pool_t *scratch_pool)
196 {
197   *db = apr_pcalloc(result_pool, sizeof(**db));
198   (*db)->config = config;
199   (*db)->verify_format = !open_without_upgrade;
200   (*db)->enforce_empty_wq = enforce_empty_wq;
201   (*db)->dir_data = apr_hash_make(result_pool);
202
203   (*db)->state_pool = result_pool;
204
205   /* Don't need to initialize (*db)->parse_cache, due to the calloc above */
206   if (config)
207     {
208       svn_error_t *err;
209       svn_boolean_t sqlite_exclusive = FALSE;
210
211       err = svn_config_get_bool(config, &sqlite_exclusive,
212                                 SVN_CONFIG_SECTION_WORKING_COPY,
213                                 SVN_CONFIG_OPTION_SQLITE_EXCLUSIVE,
214                                 FALSE);
215       if (err)
216         {
217           svn_error_clear(err);
218         }
219       else
220         (*db)->exclusive = sqlite_exclusive;
221     }
222
223   return SVN_NO_ERROR;
224 }
225
226
227 svn_error_t *
228 svn_wc__db_close(svn_wc__db_t *db)
229 {
230   apr_pool_t *scratch_pool = db->state_pool;
231   apr_hash_t *roots = apr_hash_make(scratch_pool);
232   apr_hash_index_t *hi;
233
234   /* Collect all the unique WCROOT structures, and empty out DIR_DATA.  */
235   for (hi = apr_hash_first(scratch_pool, db->dir_data);
236        hi;
237        hi = apr_hash_next(hi))
238     {
239       svn_wc__db_wcroot_t *wcroot = svn__apr_hash_index_val(hi);
240       const char *local_abspath = svn__apr_hash_index_key(hi);
241
242       if (wcroot->sdb)
243         svn_hash_sets(roots, wcroot->abspath, wcroot);
244
245       svn_hash_sets(db->dir_data, local_abspath, NULL);
246     }
247
248   /* Run the cleanup for each WCROOT.  */
249   return svn_error_trace(svn_wc__db_close_many_wcroots(roots, db->state_pool,
250                                                        scratch_pool));
251 }
252
253
254 svn_error_t *
255 svn_wc__db_pdh_create_wcroot(svn_wc__db_wcroot_t **wcroot,
256                              const char *wcroot_abspath,
257                              svn_sqlite__db_t *sdb,
258                              apr_int64_t wc_id,
259                              int format,
260                              svn_boolean_t verify_format,
261                              svn_boolean_t enforce_empty_wq,
262                              apr_pool_t *result_pool,
263                              apr_pool_t *scratch_pool)
264 {
265   if (sdb != NULL)
266     SVN_ERR(svn_sqlite__read_schema_version(&format, sdb, scratch_pool));
267
268   /* If we construct a wcroot, then we better have a format.  */
269   SVN_ERR_ASSERT(format >= 1);
270
271   /* If this working copy is PRE-1.0, then simply bail out.  */
272   if (format < 4)
273     {
274       return svn_error_createf(
275         SVN_ERR_WC_UNSUPPORTED_FORMAT, NULL,
276         _("Working copy format of '%s' is too old (%d); "
277           "please check out your working copy again"),
278         svn_dirent_local_style(wcroot_abspath, scratch_pool), format);
279     }
280
281   /* If this working copy is from a future version, then bail out.  */
282   if (format > SVN_WC__VERSION)
283     {
284       return svn_error_createf(
285         SVN_ERR_WC_UNSUPPORTED_FORMAT, NULL,
286         _("This client is too old to work with the working copy at\n"
287           "'%s' (format %d).\n"
288           "You need to get a newer Subversion client. For more details, see\n"
289           "  http://subversion.apache.org/faq.html#working-copy-format-change\n"
290           ),
291         svn_dirent_local_style(wcroot_abspath, scratch_pool),
292         format);
293     }
294
295   /* Verify that no work items exists. If they do, then our integrity is
296      suspect and, thus, we cannot use this database.  */
297   if (format >= SVN_WC__HAS_WORK_QUEUE
298       && (enforce_empty_wq || (format < SVN_WC__VERSION && verify_format)))
299     {
300       svn_error_t *err = verify_no_work(sdb);
301       if (err)
302         {
303           /* Special message for attempts to upgrade a 1.7-dev wc with
304              outstanding workqueue items. */
305           if (err->apr_err == SVN_ERR_WC_CLEANUP_REQUIRED
306               && format < SVN_WC__VERSION && verify_format)
307             err = svn_error_quick_wrap(err, _("Cleanup with an older 1.7 "
308                                               "client before upgrading with "
309                                               "this client"));
310           return svn_error_trace(err);
311         }
312     }
313
314   /* Auto-upgrade the SDB if possible.  */
315   if (format < SVN_WC__VERSION && verify_format)
316     {
317       return svn_error_createf(SVN_ERR_WC_UPGRADE_REQUIRED, NULL,
318                                _("The working copy at '%s'\nis too old "
319                                  "(format %d) to work with client version "
320                                  "'%s' (expects format %d). You need to "
321                                  "upgrade the working copy first.\n"),
322                                svn_dirent_local_style(wcroot_abspath,
323                                                       scratch_pool),
324                                format, SVN_VERSION, SVN_WC__VERSION);
325     }
326
327   *wcroot = apr_palloc(result_pool, sizeof(**wcroot));
328
329   (*wcroot)->abspath = wcroot_abspath;
330   (*wcroot)->sdb = sdb;
331   (*wcroot)->wc_id = wc_id;
332   (*wcroot)->format = format;
333   /* 8 concurrent locks is probably more than a typical wc_ng based svn client
334      uses. */
335   (*wcroot)->owned_locks = apr_array_make(result_pool, 8,
336                                           sizeof(svn_wc__db_wclock_t));
337   (*wcroot)->access_cache = apr_hash_make(result_pool);
338
339   /* SDB will be NULL for pre-NG working copies. We only need to run a
340      cleanup when the SDB is present.  */
341   if (sdb != NULL)
342     apr_pool_cleanup_register(result_pool, *wcroot, close_wcroot,
343                               apr_pool_cleanup_null);
344   return SVN_NO_ERROR;
345 }
346
347
348 svn_error_t *
349 svn_wc__db_close_many_wcroots(apr_hash_t *roots,
350                               apr_pool_t *state_pool,
351                               apr_pool_t *scratch_pool)
352 {
353   apr_hash_index_t *hi;
354
355   for (hi = apr_hash_first(scratch_pool, roots); hi; hi = apr_hash_next(hi))
356     {
357       svn_wc__db_wcroot_t *wcroot = svn__apr_hash_index_val(hi);
358       apr_status_t result;
359
360       result = apr_pool_cleanup_run(state_pool, wcroot, close_wcroot);
361       if (result != APR_SUCCESS)
362         return svn_error_wrap_apr(result, NULL);
363     }
364
365   return SVN_NO_ERROR;
366 }
367
368
369 /* POOL may be NULL if the lifetime of LOCAL_ABSPATH is sufficient.  */
370 static const char *
371 compute_relpath(const svn_wc__db_wcroot_t *wcroot,
372                 const char *local_abspath,
373                 apr_pool_t *result_pool)
374 {
375   const char *relpath = svn_dirent_is_child(wcroot->abspath, local_abspath,
376                                             result_pool);
377   if (relpath == NULL)
378     return "";
379   return relpath;
380 }
381
382
383 /* Return in *LINK_TARGET_ABSPATH the absolute path the symlink at
384  * LOCAL_ABSPATH is pointing to. Perform all allocations in POOL. */
385 static svn_error_t *
386 read_link_target(const char **link_target_abspath,
387                  const char *local_abspath,
388                  apr_pool_t *pool)
389 {
390   svn_string_t *link_target;
391   const char *canon_link_target;
392
393   SVN_ERR(svn_io_read_link(&link_target, local_abspath, pool));
394   if (link_target->len == 0)
395     return svn_error_createf(SVN_ERR_WC_NOT_SYMLINK, NULL,
396                              _("The symlink at '%s' points nowhere"),
397                              svn_dirent_local_style(local_abspath, pool));
398
399   canon_link_target = svn_dirent_canonicalize(link_target->data, pool);
400
401   /* Treat relative symlinks as relative to LOCAL_ABSPATH's parent. */
402   if (!svn_dirent_is_absolute(canon_link_target))
403     canon_link_target = svn_dirent_join(svn_dirent_dirname(local_abspath,
404                                                            pool),
405                                         canon_link_target, pool);
406
407   /* Collapse any .. in the symlink part of the path. */
408   if (svn_path_is_backpath_present(canon_link_target))
409     SVN_ERR(svn_dirent_get_absolute(link_target_abspath, canon_link_target,
410                                     pool));
411   else
412     *link_target_abspath = canon_link_target;
413
414   return SVN_NO_ERROR;
415 }
416
417 svn_error_t *
418 svn_wc__db_wcroot_parse_local_abspath(svn_wc__db_wcroot_t **wcroot,
419                                       const char **local_relpath,
420                                       svn_wc__db_t *db,
421                                       const char *local_abspath,
422                                       apr_pool_t *result_pool,
423                                       apr_pool_t *scratch_pool)
424 {
425   const char *local_dir_abspath;
426   const char *original_abspath = local_abspath;
427   svn_node_kind_t kind;
428   const char *build_relpath;
429   svn_wc__db_wcroot_t *probe_wcroot;
430   svn_wc__db_wcroot_t *found_wcroot = NULL;
431   const char *scan_abspath;
432   svn_sqlite__db_t *sdb = NULL;
433   svn_boolean_t moved_upwards = FALSE;
434   svn_boolean_t always_check = FALSE;
435   int wc_format = 0;
436   const char *adm_relpath;
437   /* Non-NULL if WCROOT is found through a symlink: */
438   const char *symlink_wcroot_abspath = NULL;
439
440   /* ### we need more logic for finding the database (if it is located
441      ### outside of the wcroot) and then managing all of that within DB.
442      ### for now: play quick & dirty. */
443
444   probe_wcroot = svn_hash_gets(db->dir_data, local_abspath);
445   if (probe_wcroot != NULL)
446     {
447       *wcroot = probe_wcroot;
448
449       /* We got lucky. Just return the thing BEFORE performing any I/O.  */
450       /* ### validate SMODE against how we opened wcroot->sdb? and against
451          ### DB->mode? (will we record per-dir mode?)  */
452
453       /* ### for most callers, we could pass NULL for result_pool.  */
454       *local_relpath = compute_relpath(probe_wcroot, local_abspath,
455                                        result_pool);
456
457       return SVN_NO_ERROR;
458     }
459
460   /* ### at some point in the future, we may need to find a way to get
461      ### rid of this stat() call. it is going to happen for EVERY call
462      ### into wc_db which references a file. calls for directories could
463      ### get an early-exit in the hash lookup just above.  */
464   SVN_ERR(get_path_kind(&kind, db, local_abspath, scratch_pool));
465   if (kind != svn_node_dir)
466     {
467       /* If the node specified by the path is NOT present, then it cannot
468          possibly be a directory containing ".svn/wc.db".
469
470          If it is a file, then it cannot contain ".svn/wc.db".
471
472          For both of these cases, strip the basename off of the path and
473          move up one level. Keep record of what we strip, though, since
474          we'll need it later to construct local_relpath.  */
475       svn_dirent_split(&local_dir_abspath, &build_relpath, local_abspath,
476                        scratch_pool);
477
478       /* Is this directory in our hash?  */
479       probe_wcroot = svn_hash_gets(db->dir_data, local_dir_abspath);
480       if (probe_wcroot != NULL)
481         {
482           const char *dir_relpath;
483
484           *wcroot = probe_wcroot;
485
486           /* Stashed directory's local_relpath + basename. */
487           dir_relpath = compute_relpath(probe_wcroot, local_dir_abspath,
488                                         NULL);
489           *local_relpath = svn_relpath_join(dir_relpath,
490                                             build_relpath,
491                                             result_pool);
492           return SVN_NO_ERROR;
493         }
494
495       /* If the requested path is not on the disk, then we don't know how
496          many ancestors need to be scanned until we start hitting content
497          on the disk. Set ALWAYS_CHECK to keep looking for .svn/entries
498          rather than bailing out after the first check.  */
499       if (kind == svn_node_none)
500         always_check = TRUE;
501
502       /* Start the scanning at LOCAL_DIR_ABSPATH.  */
503       local_abspath = local_dir_abspath;
504     }
505   else
506     {
507       /* Start the local_relpath empty. If *this* directory contains the
508          wc.db, then relpath will be the empty string.  */
509       build_relpath = "";
510
511       /* Remember the dir containing LOCAL_ABSPATH (they're the same).  */
512       local_dir_abspath = local_abspath;
513     }
514
515   /* LOCAL_ABSPATH refers to a directory at this point. At this point,
516      we've determined that an associated WCROOT is NOT in the DB's hash
517      table for this directory. Let's find an existing one in the ancestors,
518      or create one when we find the actual wcroot.  */
519
520   /* Assume that LOCAL_ABSPATH is a directory, and look for the SQLite
521      database in the right place. If we find it... great! If not, then
522      peel off some components, and try again. */
523
524   adm_relpath = svn_wc_get_adm_dir(scratch_pool);
525   while (TRUE)
526     {
527       svn_error_t *err;
528       svn_node_kind_t adm_subdir_kind;
529
530       const char *adm_subdir = svn_dirent_join(local_abspath, adm_relpath,
531                                                scratch_pool);
532
533       SVN_ERR(svn_io_check_path(adm_subdir, &adm_subdir_kind, scratch_pool));
534
535       if (adm_subdir_kind == svn_node_dir)
536         {
537           /* We always open the database in read/write mode.  If the database
538              isn't writable in the filesystem, SQLite will internally open
539              it as read-only, and we'll get an error if we try to do a write
540              operation.
541
542              We could decide what to do on a per-operation basis, but since
543              we're caching database handles, it make sense to be as permissive
544              as the filesystem allows. */
545           err = svn_wc__db_util_open_db(&sdb, local_abspath, SDB_FILE,
546                                         svn_sqlite__mode_readwrite,
547                                         db->exclusive, NULL,
548                                         db->state_pool, scratch_pool);
549           if (err == NULL)
550             {
551 #ifdef SVN_DEBUG
552               /* Install self-verification trigger statements. */
553               err = svn_sqlite__exec_statements(sdb,
554                                                 STMT_VERIFICATION_TRIGGERS);
555               if (err && err->apr_err == SVN_ERR_SQLITE_ERROR)
556                 {
557                   /* Verification triggers can fail to install on old 1.7-dev
558                    * formats which didn't have a NODES table yet. Ignore sqlite
559                    * errors so such working copies can be upgraded. */
560                   svn_error_clear(err);
561                 }
562               else
563                 SVN_ERR(err);
564 #endif
565               break;
566             }
567           if (err->apr_err != SVN_ERR_SQLITE_ERROR
568               && !APR_STATUS_IS_ENOENT(err->apr_err))
569             return svn_error_trace(err);
570           svn_error_clear(err);
571
572           /* If we have not moved upwards, then check for a wc-1 working copy.
573              Since wc-1 has a .svn in every directory, and we didn't find one
574              in the original directory, then we aren't looking at a wc-1.
575
576              If the original path is not present, then we have to check on every
577              iteration. The content may be the immediate parent, or possibly
578              five ancetors higher. We don't test for directory presence (just
579              for the presence of subdirs/files), so we don't know when we can
580              stop checking ... so just check always.  */
581           if (!moved_upwards || always_check)
582             {
583               SVN_ERR(get_old_version(&wc_format, local_abspath,
584                                       scratch_pool));
585               if (wc_format != 0)
586                 break;
587             }
588         }
589
590       /* We couldn't open the SDB within the specified directory, so
591          move up one more directory. */
592       if (svn_dirent_is_root(local_abspath, strlen(local_abspath)))
593         {
594           /* Hit the root without finding a wcroot. */
595
596           /* The wcroot could be a symlink to a directory.
597            * (Issue #2557, #3987). If so, try again, this time scanning
598            * for a db within the directory the symlink points to,
599            * rather than within the symlink's parent directory. */
600           if (kind == svn_node_symlink)
601             {
602               svn_node_kind_t resolved_kind;
603
604               local_abspath = original_abspath;
605
606               SVN_ERR(svn_io_check_resolved_path(local_abspath,
607                                                  &resolved_kind,
608                                                  scratch_pool));
609               if (resolved_kind == svn_node_dir)
610                 {
611                   /* Is this directory recorded in our hash?  */
612                   found_wcroot = svn_hash_gets(db->dir_data, local_abspath);
613                   if (found_wcroot)
614                     break;
615
616                   symlink_wcroot_abspath = local_abspath;
617                   SVN_ERR(read_link_target(&local_abspath, local_abspath,
618                                            scratch_pool));
619 try_symlink_as_dir:
620                   kind = svn_node_dir;
621                   moved_upwards = FALSE;
622                   local_dir_abspath = local_abspath;
623                   build_relpath = "";
624
625                   continue;
626                 }
627             }
628
629           return svn_error_createf(SVN_ERR_WC_NOT_WORKING_COPY, NULL,
630                                    _("'%s' is not a working copy"),
631                                    svn_dirent_local_style(original_abspath,
632                                                           scratch_pool));
633         }
634
635       local_abspath = svn_dirent_dirname(local_abspath, scratch_pool);
636
637       moved_upwards = TRUE;
638       symlink_wcroot_abspath = NULL;
639
640       /* Is the parent directory recorded in our hash?  */
641       found_wcroot = svn_hash_gets(db->dir_data, local_abspath);
642       if (found_wcroot != NULL)
643         break;
644     }
645
646   if (found_wcroot != NULL)
647     {
648       /* We found a hash table entry for an ancestor, so we stopped scanning
649          since all subdirectories use the same WCROOT.  */
650       *wcroot = found_wcroot;
651     }
652   else if (wc_format == 0)
653     {
654       /* We finally found the database. Construct a wcroot_t for it.  */
655
656       apr_int64_t wc_id;
657       svn_error_t *err;
658
659       err = svn_wc__db_util_fetch_wc_id(&wc_id, sdb, scratch_pool);
660       if (err)
661         {
662           if (err->apr_err == SVN_ERR_WC_CORRUPT)
663             return svn_error_quick_wrap(
664               err, apr_psprintf(scratch_pool,
665                                 _("Missing a row in WCROOT for '%s'."),
666                                 svn_dirent_local_style(original_abspath,
667                                                        scratch_pool)));
668           return svn_error_trace(err);
669         }
670
671       /* WCROOT.local_abspath may be NULL when the database is stored
672          inside the wcroot, but we know the abspath is this directory
673          (ie. where we found it).  */
674
675       err = svn_wc__db_pdh_create_wcroot(wcroot,
676                             apr_pstrdup(db->state_pool,
677                                         symlink_wcroot_abspath
678                                           ? symlink_wcroot_abspath
679                                           : local_abspath),
680                             sdb, wc_id, FORMAT_FROM_SDB,
681                             db->verify_format, db->enforce_empty_wq,
682                             db->state_pool, scratch_pool);
683       if (err && (err->apr_err == SVN_ERR_WC_UNSUPPORTED_FORMAT ||
684                   err->apr_err == SVN_ERR_WC_UPGRADE_REQUIRED) &&
685           kind == svn_node_symlink)
686         {
687           /* We found an unsupported WC after traversing upwards from a
688            * symlink. Fall through to code below to check if the symlink
689            * points at a supported WC. */
690           svn_error_clear(err);
691           *wcroot = NULL;
692         }
693       else
694         SVN_ERR(err);
695     }
696   else
697     {
698       /* We found something that looks like a wc-1 working copy directory.
699          However, if the format version is 12 and the .svn/entries file
700          is only 3 bytes long, then it's a breadcrumb in a wc-ng working
701          copy that's missing an .svn/wc.db, or its .svn/wc.db is corrupt. */
702       if (wc_format == SVN_WC__WC_NG_VERSION /* 12 */)
703         {
704           apr_finfo_t info;
705
706           /* Check attributes of .svn/entries */
707           const char *admin_abspath = svn_wc__adm_child(
708               local_abspath, SVN_WC__ADM_ENTRIES, scratch_pool);
709           svn_error_t *err = svn_io_stat(&info, admin_abspath, APR_FINFO_SIZE,
710                                          scratch_pool);
711
712           /* If the former does not succeed, something is seriously wrong. */
713           if (err)
714             return svn_error_createf(
715                 SVN_ERR_WC_CORRUPT, err,
716                 _("The working copy at '%s' is corrupt."),
717                 svn_dirent_local_style(local_abspath, scratch_pool));
718           svn_error_clear(err);
719
720           if (3 == info.size)
721             {
722               /* Check existence of .svn/wc.db */
723               admin_abspath = svn_wc__adm_child(local_abspath, SDB_FILE,
724                                                 scratch_pool);
725               err = svn_io_stat(&info, admin_abspath, APR_FINFO_SIZE,
726                                 scratch_pool);
727               if (err && APR_STATUS_IS_ENOENT(err->apr_err))
728                 {
729                   svn_error_clear(err);
730                   return svn_error_createf(
731                       SVN_ERR_WC_CORRUPT, NULL,
732                       _("The working copy database at '%s' is missing."),
733                       svn_dirent_local_style(local_abspath, scratch_pool));
734                 }
735               else
736                 /* We should never have reached this point in the code
737                    if .svn/wc.db exists; therefore it's best to assume
738                    it's corrupt. */
739                 return svn_error_createf(
740                     SVN_ERR_WC_CORRUPT, err,
741                     _("The working copy database at '%s' is corrupt."),
742                     svn_dirent_local_style(local_abspath, scratch_pool));
743             }
744         }
745
746       SVN_ERR(svn_wc__db_pdh_create_wcroot(wcroot,
747                             apr_pstrdup(db->state_pool,
748                                         symlink_wcroot_abspath
749                                           ? symlink_wcroot_abspath
750                                           : local_abspath),
751                             NULL, UNKNOWN_WC_ID, wc_format,
752                             db->verify_format, db->enforce_empty_wq,
753                             db->state_pool, scratch_pool));
754     }
755
756   if (*wcroot)
757     {
758       const char *dir_relpath;
759
760       if (symlink_wcroot_abspath)
761         {
762           /* The WCROOT was found through a symlink pointing at the root of
763            * the WC. Cache the WCROOT under the symlink's path. */
764           local_dir_abspath = symlink_wcroot_abspath;
765         }
766
767       /* The subdirectory's relpath is easily computed relative to the
768          wcroot that we just found.  */
769       dir_relpath = compute_relpath(*wcroot, local_dir_abspath, NULL);
770
771       /* And the result local_relpath may include a filename.  */
772       *local_relpath = svn_relpath_join(dir_relpath, build_relpath, result_pool);
773     }
774
775   if (kind == svn_node_symlink)
776     {
777       svn_boolean_t retry_if_dir = FALSE;
778       svn_wc__db_status_t status;
779       svn_boolean_t conflicted;
780       svn_error_t *err;
781
782       /* Check if the symlink is versioned or obstructs a versioned node
783        * in this DB -- in that case, use this wcroot. Else, if the symlink
784        * points to a directory, try to find a wcroot in that directory
785        * instead. */
786
787       if (*wcroot)
788         {
789           err = svn_wc__db_read_info_internal(&status, NULL, NULL, NULL, NULL,
790                                               NULL, NULL, NULL, NULL, NULL,
791                                               NULL, NULL, NULL, NULL, NULL,
792                                               NULL, NULL, NULL, &conflicted,
793                                               NULL, NULL, NULL, NULL, NULL,
794                                               NULL, *wcroot, *local_relpath,
795                                               scratch_pool, scratch_pool);
796           if (err)
797             {
798               if (err->apr_err != SVN_ERR_WC_PATH_NOT_FOUND
799                   && !SVN_WC__ERR_IS_NOT_CURRENT_WC(err))
800                 return svn_error_trace(err);
801
802               svn_error_clear(err);
803               retry_if_dir = TRUE; /* The symlink is unversioned. */
804             }
805           else
806             {
807               /* The symlink is versioned, or obstructs a versioned node.
808                * Ignore non-conflicted not-present/excluded nodes.
809                * This allows the symlink to redirect the wcroot query to a
810                * directory, regardless of 'invisible' nodes in this WC. */
811               retry_if_dir = ((status == svn_wc__db_status_not_present ||
812                                status == svn_wc__db_status_excluded ||
813                                status == svn_wc__db_status_server_excluded)
814                               && !conflicted);
815             }
816         }
817       else
818         retry_if_dir = TRUE;
819
820       if (retry_if_dir)
821         {
822           svn_node_kind_t resolved_kind;
823
824           SVN_ERR(svn_io_check_resolved_path(original_abspath,
825                                              &resolved_kind,
826                                              scratch_pool));
827           if (resolved_kind == svn_node_dir)
828             {
829               symlink_wcroot_abspath = original_abspath;
830               SVN_ERR(read_link_target(&local_abspath, original_abspath,
831                                        scratch_pool));
832               /* This handle was opened in this function but is not going
833                  to be used further so close it. */
834               if (sdb)
835                 SVN_ERR(svn_sqlite__close(sdb));
836               goto try_symlink_as_dir;
837             }
838         }
839     }
840
841   /* We've found the appropriate WCROOT for the requested path. Stash
842      it into that path's directory.  */
843   svn_hash_sets(db->dir_data,
844                 apr_pstrdup(db->state_pool, local_dir_abspath),
845                 *wcroot);
846
847   /* Did we traverse up to parent directories?  */
848   if (!moved_upwards)
849     {
850       /* We did NOT move to a parent of the original requested directory.
851          We've constructed and filled in a WCROOT for the request, so we
852          are done.  */
853       return SVN_NO_ERROR;
854     }
855
856   /* The WCROOT that we just found/built was for the LOCAL_ABSPATH originally
857      passed into this function. We stepped *at least* one directory above that.
858      We should now associate the WROOT for each parent directory that does
859      not (yet) have one.  */
860
861   scan_abspath = local_dir_abspath;
862
863   do
864     {
865       const char *parent_dir = svn_dirent_dirname(scan_abspath, scratch_pool);
866       svn_wc__db_wcroot_t *parent_wcroot;
867
868       parent_wcroot = svn_hash_gets(db->dir_data, parent_dir);
869       if (parent_wcroot == NULL)
870         {
871           svn_hash_sets(db->dir_data, apr_pstrdup(db->state_pool, parent_dir),
872                         *wcroot);
873         }
874
875       /* Move up a directory, stopping when we reach the directory where
876          we found/built the WCROOT.  */
877       scan_abspath = parent_dir;
878     }
879   while (strcmp(scan_abspath, local_abspath) != 0);
880
881   return SVN_NO_ERROR;
882 }
883
884
885 svn_error_t *
886 svn_wc__db_drop_root(svn_wc__db_t *db,
887                      const char *local_abspath,
888                      apr_pool_t *scratch_pool)
889 {
890   svn_wc__db_wcroot_t *root_wcroot = svn_hash_gets(db->dir_data, local_abspath);
891   apr_hash_index_t *hi;
892   apr_status_t result;
893
894   if (!root_wcroot)
895     return SVN_NO_ERROR;
896
897   if (strcmp(root_wcroot->abspath, local_abspath) != 0)
898     return svn_error_createf(SVN_ERR_WC_NOT_WORKING_COPY, NULL,
899                              _("'%s' is not a working copy root"),
900                              svn_dirent_local_style(local_abspath,
901                                                     scratch_pool));
902
903   for (hi = apr_hash_first(scratch_pool, db->dir_data);
904        hi;
905        hi = apr_hash_next(hi))
906     {
907       svn_wc__db_wcroot_t *wcroot = svn__apr_hash_index_val(hi);
908
909       if (wcroot == root_wcroot)
910         svn_hash_sets(db->dir_data, svn__apr_hash_index_key(hi), NULL);
911     }
912
913   result = apr_pool_cleanup_run(db->state_pool, root_wcroot, close_wcroot);
914   if (result != APR_SUCCESS)
915     return svn_error_wrap_apr(result, NULL);
916
917   return SVN_NO_ERROR;
918 }