]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/subversion/subversion/libsvn_wc/wc_db_util.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / subversion / subversion / libsvn_wc / wc_db_util.c
1 /*
2  * wc_db_util.c :  Various util functions for wc_db(_pdh)
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 /* About this file:
25    This file is meant to be a stash of fairly low-level functions used by both
26    wc_db.c and wc_db_pdh.c.  In breaking stuff out of the monolithic wc_db.c,
27    I have discovered that some utility functions are used by bits in both
28    files.  Rather than shoehorn those functions into one file or the other, or
29    create circular dependencies between the files, I felt a third file, with
30    a well-defined scope, would be sensible.  History will judge its effect.
31
32    The goal of it file is simple: just execute SQLite statements.  That is,
33    functions in this file should have no knowledge of pdh's or db's, and
34    should just operate on the raw sdb object.  If a function requires more
35    information than that, it shouldn't be in here.  -hkw
36  */
37
38 #define SVN_WC__I_AM_WC_DB
39
40 #include "svn_dirent_uri.h"
41 #include "private/svn_sqlite.h"
42
43 #include "wc.h"
44 #include "adm_files.h"
45 #include "wc_db_private.h"
46 #include "wc-queries.h"
47
48 #include "svn_private_config.h"
49
50 WC_QUERIES_SQL_DECLARE_STATEMENTS(statements);
51
52
53 \f
54 /* */
55 svn_error_t *
56 svn_wc__db_util_fetch_wc_id(apr_int64_t *wc_id,
57                             svn_sqlite__db_t *sdb,
58                             apr_pool_t *scratch_pool)
59 {
60   svn_sqlite__stmt_t *stmt;
61   svn_boolean_t have_row;
62
63   /* ### cheat. we know there is just one WORKING_COPY row, and it has a
64      ### NULL value for local_abspath. */
65   SVN_ERR(svn_sqlite__get_statement(&stmt, sdb, STMT_SELECT_WCROOT_NULL));
66   SVN_ERR(svn_sqlite__step(&have_row, stmt));
67   if (!have_row)
68     return svn_error_createf(SVN_ERR_WC_CORRUPT, svn_sqlite__reset(stmt),
69                              _("Missing a row in WCROOT."));
70
71   SVN_ERR_ASSERT(!svn_sqlite__column_is_null(stmt, 0));
72   *wc_id = svn_sqlite__column_int64(stmt, 0);
73
74   return svn_error_trace(svn_sqlite__reset(stmt));
75 }
76
77
78
79
80 /* An SQLite application defined function that allows SQL queries to
81    use "relpath_depth(local_relpath)".  */
82 static svn_error_t *
83 relpath_depth_sqlite(svn_sqlite__context_t *sctx,
84                      int argc,
85                      svn_sqlite__value_t *values[],
86                      apr_pool_t *scratch_pool)
87 {
88   const char *path = NULL;
89   apr_int64_t depth;
90
91   if (argc == 1 && svn_sqlite__value_type(values[0]) == SVN_SQLITE__TEXT)
92     path = svn_sqlite__value_text(values[0]);
93   if (!path)
94     {
95       svn_sqlite__result_null(sctx);
96       return SVN_NO_ERROR;
97     }
98
99   depth = *path ? 1 : 0;
100   while (*path)
101     {
102       if (*path == '/')
103         ++depth;
104       ++path;
105     }
106   svn_sqlite__result_int64(sctx, depth);
107
108   return SVN_NO_ERROR;
109 }
110
111
112 svn_error_t *
113 svn_wc__db_util_open_db(svn_sqlite__db_t **sdb,
114                         const char *dir_abspath,
115                         const char *sdb_fname,
116                         svn_sqlite__mode_t smode,
117                         svn_boolean_t exclusive,
118                         const char *const *my_statements,
119                         apr_pool_t *result_pool,
120                         apr_pool_t *scratch_pool)
121 {
122   const char *sdb_abspath = svn_wc__adm_child(dir_abspath, sdb_fname,
123                                               scratch_pool);
124
125   if (smode != svn_sqlite__mode_rwcreate)
126     {
127       svn_node_kind_t kind;
128
129       /* A file stat is much cheaper then a failed database open handled
130          by SQLite. */
131       SVN_ERR(svn_io_check_path(sdb_abspath, &kind, scratch_pool));
132
133       if (kind != svn_node_file)
134         return svn_error_createf(APR_ENOENT, NULL,
135                                  _("Working copy database '%s' not found"),
136                                  svn_dirent_local_style(sdb_abspath,
137                                                         scratch_pool));
138     }
139 #ifndef WIN32
140   else
141     {
142       apr_file_t *f;
143
144       /* A standard SQLite build creates a DB with mode 644 ^ !umask
145          which means the file doesn't have group/world write access
146          even when umask allows it. By ensuring the file exists before
147          SQLite gets involved we give it the permissions allowed by
148          umask. */
149       SVN_ERR(svn_io_file_open(&f, sdb_abspath,
150                                (APR_READ | APR_WRITE | APR_CREATE),
151                                APR_OS_DEFAULT, scratch_pool));
152       SVN_ERR(svn_io_file_close(f, scratch_pool));
153     }
154 #endif
155
156   SVN_ERR(svn_sqlite__open(sdb, sdb_abspath, smode,
157                            my_statements ? my_statements : statements,
158                            0, NULL, result_pool, scratch_pool));
159
160   if (exclusive)
161     SVN_ERR(svn_sqlite__exec_statements(*sdb, STMT_PRAGMA_LOCKING_MODE));
162
163   SVN_ERR(svn_sqlite__create_scalar_function(*sdb, "relpath_depth", 1,
164                                              relpath_depth_sqlite, NULL));
165
166   return SVN_NO_ERROR;
167 }
168
169 \f
170 /* Some helpful transaction helpers.
171
172    Instead of directly using SQLite transactions, these wrappers
173    relieve the consumer from the need to wrap the wcroot and
174    local_relpath, which are almost always used within the transaction.
175
176    This also means if we later want to implement some wc_db-specific txn
177    handling, we have a convenient place to do it.
178    */
179
180 /* A callback which supplies WCROOTs and LOCAL_RELPATHs. */
181 typedef svn_error_t *(*db_txn_callback_t)(void *baton,
182                                           svn_wc__db_wcroot_t *wcroot,
183                                           const char *local_relpath,
184                                           apr_pool_t *scratch_pool);
185
186 /* Baton for use with run_txn() and with_db_txn(). */
187 struct txn_baton_t
188 {
189   svn_wc__db_wcroot_t *wcroot;
190   const char *local_relpath;
191
192   db_txn_callback_t cb_func;
193   void *cb_baton;
194 };
195
196
197 /* Unwrap the sqlite transaction into a wc_db txn.
198    Implements svn_sqlite__transaction_callback_t. */
199 static svn_error_t *
200 run_txn(void *baton, svn_sqlite__db_t *db, apr_pool_t *scratch_pool)
201 {
202   struct txn_baton_t *tb = baton;
203
204   return svn_error_trace(
205     tb->cb_func(tb->cb_baton, tb->wcroot, tb->local_relpath, scratch_pool));
206 }
207
208
209 /* Run CB_FUNC in a SQLite transaction with CB_BATON, using WCROOT and
210    LOCAL_RELPATH.  If callbacks require additional information, they may
211    provide it using CB_BATON. */
212 svn_error_t *
213 svn_wc__db_with_txn(svn_wc__db_wcroot_t *wcroot,
214                     const char *local_relpath,
215                     svn_wc__db_txn_callback_t cb_func,
216                     void *cb_baton,
217                     apr_pool_t *scratch_pool)
218 {
219   struct txn_baton_t tb;
220
221   tb.wcroot = wcroot;
222   tb.local_relpath = local_relpath;
223   tb.cb_func = cb_func;
224   tb.cb_baton = cb_baton;
225
226   return svn_error_trace(
227     svn_sqlite__with_lock(wcroot->sdb, run_txn, &tb, scratch_pool));
228 }