]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/subversion/subversion/libsvn_fs_base/bdb/dbt.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / subversion / subversion / libsvn_fs_base / bdb / dbt.c
1 /* dbt.c --- DBT-frobbing functions
2  *
3  * ====================================================================
4  *    Licensed to the Apache Software Foundation (ASF) under one
5  *    or more contributor license agreements.  See the NOTICE file
6  *    distributed with this work for additional information
7  *    regarding copyright ownership.  The ASF licenses this file
8  *    to you under the Apache License, Version 2.0 (the
9  *    "License"); you may not use this file except in compliance
10  *    with the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *    Unless required by applicable law or agreed to in writing,
15  *    software distributed under the License is distributed on an
16  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  *    KIND, either express or implied.  See the License for the
18  *    specific language governing permissions and limitations
19  *    under the License.
20  * ====================================================================
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <apr_pools.h>
26 #include <apr_md5.h>
27 #include <apr_sha1.h>
28
29 #define SVN_WANT_BDB
30 #include "svn_private_config.h"
31
32 #include "../id.h"
33 #include "dbt.h"
34
35
36 DBT *
37 svn_fs_base__clear_dbt(DBT *dbt)
38 {
39   memset(dbt, 0, sizeof(*dbt));
40
41   return dbt;
42 }
43
44
45 DBT *svn_fs_base__nodata_dbt(DBT *dbt)
46 {
47   svn_fs_base__clear_dbt(dbt);
48
49   /* A `nodata' dbt is one which retrieves zero bytes from offset zero,
50      and stores them in a zero-byte buffer in user-allocated memory.  */
51   dbt->flags |= (DB_DBT_USERMEM | DB_DBT_PARTIAL);
52   dbt->doff = dbt->dlen = 0;
53
54   return dbt;
55 }
56
57
58 DBT *
59 svn_fs_base__set_dbt(DBT *dbt, const void *data, apr_size_t size)
60 {
61   svn_fs_base__clear_dbt(dbt);
62
63   dbt->data = (void *) data;
64   dbt->size = (u_int32_t) size;
65
66   return dbt;
67 }
68
69
70 DBT *
71 svn_fs_base__result_dbt(DBT *dbt)
72 {
73   svn_fs_base__clear_dbt(dbt);
74   dbt->flags |= DB_DBT_MALLOC;
75
76   return dbt;
77 }
78
79
80 /* An APR pool cleanup function that simply applies `free' to its
81    argument.  */
82 static apr_status_t
83 apr_free_cleanup(void *arg)
84 {
85   free(arg);
86
87   return 0;
88 }
89
90
91 DBT *
92 svn_fs_base__track_dbt(DBT *dbt, apr_pool_t *pool)
93 {
94   if (dbt->data)
95     apr_pool_cleanup_register(pool, dbt->data, apr_free_cleanup,
96                               apr_pool_cleanup_null);
97
98   return dbt;
99 }
100
101
102 DBT *
103 svn_fs_base__recno_dbt(DBT *dbt, db_recno_t *recno)
104 {
105   svn_fs_base__set_dbt(dbt, recno, sizeof(*recno));
106   dbt->ulen = dbt->size;
107   dbt->flags |= DB_DBT_USERMEM;
108
109   return dbt;
110 }
111
112
113 int
114 svn_fs_base__compare_dbt(const DBT *a, const DBT *b)
115 {
116   int common_size = a->size > b->size ? b->size : a->size;
117   int cmp = memcmp(a->data, b->data, common_size);
118
119   if (cmp)
120     return cmp;
121   else
122     return a->size - b->size;
123 }
124
125
126 \f
127 /* Building DBT's from interesting things.  */
128
129
130 /* Set DBT to the unparsed form of ID; allocate memory from POOL.
131    Return DBT.  */
132 DBT *
133 svn_fs_base__id_to_dbt(DBT *dbt,
134                        const svn_fs_id_t *id,
135                        apr_pool_t *pool)
136 {
137   svn_string_t *unparsed_id = svn_fs_base__id_unparse(id, pool);
138   svn_fs_base__set_dbt(dbt, unparsed_id->data, unparsed_id->len);
139   return dbt;
140 }
141
142
143 /* Set DBT to the unparsed form of SKEL; allocate memory from POOL.  */
144 DBT *
145 svn_fs_base__skel_to_dbt(DBT *dbt,
146                          svn_skel_t *skel,
147                          apr_pool_t *pool)
148 {
149   svn_stringbuf_t *unparsed_skel = svn_skel__unparse(skel, pool);
150   svn_fs_base__set_dbt(dbt, unparsed_skel->data, unparsed_skel->len);
151   return dbt;
152 }
153
154
155 /* Set DBT to the text of the null-terminated string STR.  DBT will
156    refer to STR's storage.  Return DBT.  */
157 DBT *
158 svn_fs_base__str_to_dbt(DBT *dbt, const char *str)
159 {
160   svn_fs_base__set_dbt(dbt, str, strlen(str));
161   return dbt;
162 }
163
164 DBT *
165 svn_fs_base__checksum_to_dbt(DBT *dbt, svn_checksum_t *checksum)
166 {
167   svn_fs_base__set_dbt(dbt, checksum->digest, svn_checksum_size(checksum));
168
169   return dbt;
170 }