1/* id.c : operations on node-revision IDs
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 <string.h>
24#include <stdlib.h>
25
26#include "id.h"
27#include "../libsvn_fs/fs-loader.h"
28
29
30
31typedef struct id_private_t {
32  const char *node_id;
33  const char *copy_id;
34  const char *txn_id;
35} id_private_t;
36
37
38/* Accessing ID Pieces.  */
39
40const char *
41svn_fs_base__id_node_id(const svn_fs_id_t *id)
42{
43  id_private_t *pvt = id->fsap_data;
44
45  return pvt->node_id;
46}
47
48
49const char *
50svn_fs_base__id_copy_id(const svn_fs_id_t *id)
51{
52  id_private_t *pvt = id->fsap_data;
53
54  return pvt->copy_id;
55}
56
57
58const char *
59svn_fs_base__id_txn_id(const svn_fs_id_t *id)
60{
61  id_private_t *pvt = id->fsap_data;
62
63  return pvt->txn_id;
64}
65
66
67svn_string_t *
68svn_fs_base__id_unparse(const svn_fs_id_t *id,
69                        apr_pool_t *pool)
70{
71  id_private_t *pvt = id->fsap_data;
72
73  return svn_string_createf(pool, "%s.%s.%s",
74                            pvt->node_id, pvt->copy_id, pvt->txn_id);
75}
76
77
78/*** Comparing node IDs ***/
79
80svn_boolean_t
81svn_fs_base__id_eq(const svn_fs_id_t *a,
82                   const svn_fs_id_t *b)
83{
84  id_private_t *pvta = a->fsap_data, *pvtb = b->fsap_data;
85
86  if (a == b)
87    return TRUE;
88  if (strcmp(pvta->node_id, pvtb->node_id) != 0)
89     return FALSE;
90  if (strcmp(pvta->copy_id, pvtb->copy_id) != 0)
91    return FALSE;
92  if (strcmp(pvta->txn_id, pvtb->txn_id) != 0)
93    return FALSE;
94  return TRUE;
95}
96
97
98svn_boolean_t
99svn_fs_base__id_check_related(const svn_fs_id_t *a,
100                              const svn_fs_id_t *b)
101{
102  id_private_t *pvta = a->fsap_data, *pvtb = b->fsap_data;
103
104  if (a == b)
105    return TRUE;
106
107  return (strcmp(pvta->node_id, pvtb->node_id) == 0);
108}
109
110
111int
112svn_fs_base__id_compare(const svn_fs_id_t *a,
113                        const svn_fs_id_t *b)
114{
115  if (svn_fs_base__id_eq(a, b))
116    return 0;
117  return (svn_fs_base__id_check_related(a, b) ? 1 : -1);
118}
119
120
121
122/* Creating ID's.  */
123
124static id_vtable_t id_vtable = {
125  svn_fs_base__id_unparse,
126  svn_fs_base__id_compare
127};
128
129
130svn_fs_id_t *
131svn_fs_base__id_create(const char *node_id,
132                       const char *copy_id,
133                       const char *txn_id,
134                       apr_pool_t *pool)
135{
136  svn_fs_id_t *id = apr_palloc(pool, sizeof(*id));
137  id_private_t *pvt = apr_palloc(pool, sizeof(*pvt));
138
139  pvt->node_id = apr_pstrdup(pool, node_id);
140  pvt->copy_id = apr_pstrdup(pool, copy_id);
141  pvt->txn_id = apr_pstrdup(pool, txn_id);
142  id->vtable = &id_vtable;
143  id->fsap_data = pvt;
144  return id;
145}
146
147
148svn_fs_id_t *
149svn_fs_base__id_copy(const svn_fs_id_t *id, apr_pool_t *pool)
150{
151  svn_fs_id_t *new_id = apr_palloc(pool, sizeof(*new_id));
152  id_private_t *new_pvt = apr_palloc(pool, sizeof(*new_pvt));
153  id_private_t *pvt = id->fsap_data;
154
155  new_pvt->node_id = apr_pstrdup(pool, pvt->node_id);
156  new_pvt->copy_id = apr_pstrdup(pool, pvt->copy_id);
157  new_pvt->txn_id = apr_pstrdup(pool, pvt->txn_id);
158  new_id->vtable = &id_vtable;
159  new_id->fsap_data = new_pvt;
160  return new_id;
161}
162
163
164svn_fs_id_t *
165svn_fs_base__id_parse(const char *data,
166                      apr_size_t len,
167                      apr_pool_t *pool)
168{
169  svn_fs_id_t *id;
170  id_private_t *pvt;
171  char *data_copy, *str;
172
173  /* Dup the ID data into POOL.  Our returned ID will have references
174     into this memory. */
175  data_copy = apr_pstrmemdup(pool, data, len);
176
177  /* Alloc a new svn_fs_id_t structure. */
178  id = apr_palloc(pool, sizeof(*id));
179  pvt = apr_palloc(pool, sizeof(*pvt));
180  id->vtable = &id_vtable;
181  id->fsap_data = pvt;
182
183  /* Now, we basically just need to "split" this data on `.'
184     characters.  We will use svn_cstring_tokenize, which will put
185     terminators where each of the '.'s used to be.  Then our new
186     id field will reference string locations inside our duplicate
187     string.*/
188
189  /* Node Id */
190  str = svn_cstring_tokenize(".", &data_copy);
191  if (str == NULL)
192    return NULL;
193  pvt->node_id = str;
194
195  /* Copy Id */
196  str = svn_cstring_tokenize(".", &data_copy);
197  if (str == NULL)
198    return NULL;
199  pvt->copy_id = str;
200
201  /* Txn Id */
202  str = svn_cstring_tokenize(".", &data_copy);
203  if (str == NULL)
204    return NULL;
205  pvt->txn_id = str;
206
207  return id;
208}
209