Make 'svn cleanup' perform some verification of the WC metadata and raise an
error if any problem is found.  The main purpose of this is to get some DB
verification code started and for developers to be able to run it and extend
it; the decision to run it in 'svn cleanup' is just an initial UI choice.

* subversion/libsvn_wc/wc_db_private.h
  (svn_wc__db_verify_nodes): New function.

* subversion/libsvn_wc/wc_db_verify.c
  New file, implementing svn_wc__db_verify_nodes().

* subversion/libsvn_wc/wc_db.h,
  subversion/libsvn_wc/wc_db.c
  (svn_wc__db_verify): New function.

* subversion/libsvn_wc/cleanup.c
  (cleanup_internal): Call svn_wc__db_verify().

* subversion/libsvn_wc/wc-queries.sql
  (STMT_SELECT_ALL_NODES): New query.
--This line, and those below, will be ignored--

Index: subversion/libsvn_wc/cleanup.c
===================================================================
--- subversion/libsvn_wc/cleanup.c	(revision 1101096)
+++ subversion/libsvn_wc/cleanup.c	(working copy)
@@ -159,6 +159,8 @@ cleanup_internal(svn_wc__db_t *db,
   SVN_ERR(svn_wc__db_get_wcroot(&cleanup_abspath, db, dir_abspath,
                                 scratch_pool, scratch_pool));
 
+  SVN_ERR(svn_wc__db_verify(db, dir_abspath, scratch_pool));
+
   /* Perform these operations if we lock the entire working copy.
      Note that we really need to check a wcroot value and not
      svn_wc__check_wcroot() as that function, will just return true
Index: subversion/libsvn_wc/wc_db.c
===================================================================
--- subversion/libsvn_wc/wc_db.c	(revision 1101100)
+++ subversion/libsvn_wc/wc_db.c	(working copy)
@@ -11778,3 +11778,19 @@ svn_wc__db_base_get_lock_tokens_recursiv
     }
   return svn_sqlite__reset(stmt);
 }
+
+
+svn_error_t *
+svn_wc__db_verify(svn_wc__db_t *db,
+                  const char *wri_abspath,
+                  apr_pool_t *scratch_pool)
+{
+  svn_wc__db_wcroot_t *wcroot;
+  const char *local_relpath;
+
+  SVN_ERR(svn_wc__db_wcroot_parse_local_abspath(&wcroot, &local_relpath,
+                                                db, wri_abspath,
+                                                scratch_pool, scratch_pool));
+  SVN_ERR(svn_wc__db_verify_nodes(wcroot->sdb, wcroot->wc_id, scratch_pool));
+  return SVN_NO_ERROR;
+}
Index: subversion/libsvn_wc/wc_db.h
===================================================================
--- subversion/libsvn_wc/wc_db.h	(revision 1101096)
+++ subversion/libsvn_wc/wc_db.h	(working copy)
@@ -2995,6 +2995,14 @@ svn_wc__db_has_local_mods(svn_boolean_t
                           apr_pool_t *scratch_pool);
 
 
+/* Verify the consistency of metadata concerning the WC that contains
+ * WRI_ABSPATH, in DB.  Return an error if any problem is found. */
+svn_error_t *
+svn_wc__db_verify(svn_wc__db_t *db,
+                  const char *wri_abspath,
+                  apr_pool_t *scratch_pool);
+
+
 /* @} */
 
 
Index: subversion/libsvn_wc/wc_db_private.h
===================================================================
--- subversion/libsvn_wc/wc_db_private.h	(revision 1101096)
+++ subversion/libsvn_wc/wc_db_private.h	(working copy)
@@ -185,4 +185,11 @@ svn_wc__db_with_txn(svn_wc__db_wcroot_t
                     apr_pool_t *scratch_pool);
 
 
+/* Verify consistency of the metadata concerning WC_ID in SDB. */
+svn_error_t *
+svn_wc__db_verify_nodes(svn_sqlite__db_t *sdb,
+                        apr_int64_t wc_id,
+                        apr_pool_t *scratch_pool);
+
+
 #endif /* WC_DB_PDH_H */
Index: subversion/libsvn_wc/wc_db_verify.c
===================================================================
--- subversion/libsvn_wc/wc_db_verify.c	(revision 0)
+++ subversion/libsvn_wc/wc_db_verify.c	(working copy)
@@ -0,0 +1,123 @@
+/*
+ * wc_db_verify.c :  Verifying the consistency of the WC DB
+ *
+ * ====================================================================
+ *    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.
+ * ====================================================================
+ */
+
+
+#define SVN_WC__I_AM_WC_DB
+
+#include "svn_pools.h"
+#include "svn_dirent_uri.h"
+#include "wc.h"
+#include "adm_files.h"
+#include "wc_db_private.h"
+#include "wc-queries.h"
+
+#include "svn_private_config.h"
+
+
+/* Calculates the depth of the relpath below "".
+ * ### Duplicated from wc_db.c */
+APR_INLINE static apr_int64_t relpath_depth(const char *relpath)
+{
+  int n = 1;
+  if (*relpath == '\0')
+    return 0;
+
+  do
+  {
+    if (*relpath == '/')
+      n++;
+  }
+  while (*(++relpath));
+
+  return n;
+}
+
+
+/* Cause the caller to return an error if EXPRESSION is not true.  The error
+ * description will show EXPRESSION and the caller's LOCAL_RELPATH. */
+#define VERIFY(expression) \
+  do { \
+    if (! (expression)) \
+      return svn_error_createf(SVN_ERR_WC_CORRUPT, NULL, \
+        _("database inconsistency at local_relpath='%s' verifying " \
+          "expression '%s'"), local_relpath, #expression); \
+  } while (0)
+
+
+
+/* TODO: Add the following verifications:
+ *
+ *   * on every ACTUAL row (except root): a NODES row exists at its parent path
+ */
+svn_error_t *
+svn_wc__db_verify_nodes(svn_sqlite__db_t *sdb,
+                        apr_int64_t wc_id,
+                        apr_pool_t *scratch_pool)
+{
+  svn_sqlite__stmt_t *stmt;
+  apr_pool_t *iterpool = svn_pool_create(scratch_pool);
+
+  SVN_ERR(svn_sqlite__get_statement(&stmt, sdb, STMT_SELECT_ALL_NODES));
+  SVN_ERR(svn_sqlite__bindf(stmt, "i", wc_id));
+  while (TRUE)
+    {
+      svn_boolean_t have_row;
+      const char *local_relpath, *parent_relpath;
+      apr_int64_t op_depth;
+
+      SVN_ERR(svn_sqlite__step(&have_row, stmt));
+      if (!have_row)
+        break;
+      op_depth = svn_sqlite__column_int(stmt, 0);
+      local_relpath = svn_sqlite__column_text(stmt, 1, iterpool);
+      parent_relpath = svn_sqlite__column_text(stmt, 2, iterpool);
+
+      /* parent_relpath is the parent path of local_relpath */
+      VERIFY((parent_relpath == NULL)
+             ? (local_relpath[0] == '\0')
+             : (strcmp(svn_relpath_dirname(local_relpath, iterpool),
+                       parent_relpath) == 0));
+
+      /* op_depth <= relpath_depth(local_relpath) */
+      VERIFY(op_depth <= relpath_depth(local_relpath));
+
+      /* parent_relpath refers to a row that exists */
+      /* TODO: there is a suitable parent row - e.g. has op_depth <= child's
+       * and a suitable presence */
+      if (parent_relpath)
+        {
+          svn_sqlite__stmt_t *stmt2;
+          svn_boolean_t have_a_parent_row;
+
+          SVN_ERR(svn_sqlite__get_statement(&stmt2, sdb, STMT_SELECT_NODE_INFO));
+          SVN_ERR(svn_sqlite__bindf(stmt2, "is", wc_id, parent_relpath));
+          SVN_ERR(svn_sqlite__step(&have_a_parent_row, stmt2));
+          VERIFY(have_a_parent_row);
+          SVN_ERR(svn_sqlite__reset(stmt2));
+        }
+    }
+  svn_pool_destroy(iterpool);
+
+  return svn_error_return(svn_sqlite__reset(stmt));
+}
+
Index: subversion/libsvn_wc/wc-queries.sql
===================================================================
--- subversion/libsvn_wc/wc-queries.sql	(revision 1101096)
+++ subversion/libsvn_wc/wc-queries.sql	(working copy)
@@ -1125,6 +1125,16 @@ WHERE wc_id = ?1
   AND presence IN ('normal', 'incomplete')
   AND file_external IS NULL
 
+/* ------------------------------------------------------------------------- */
+
+/* Queries for verification. */
+
+-- STMT_SELECT_ALL_NODES
+SELECT op_depth, local_relpath, parent_relpath FROM nodes
+WHERE wc_id == ?1
+
+/* ------------------------------------------------------------------------- */
+
 /* Grab all the statements related to the schema.  */
 
 -- include: wc-metadata

