1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* $OpenBSD: entries.c,v 1.4 2004/07/14 19:03:00 jfb Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "log.h"
#include "cvs.h"
#define CVS_ENTRIES_NFIELDS 5
#define CVS_ENTRIES_DELIM '/'
static struct cvs_ent* cvs_ent_clone (const struct cvs_ent *);
/*
* cvs_ent_open()
*
* Open the CVS Entries file for the directory <dir>.
* Returns a pointer to the CVSENTRIES file structure on success, or NULL
* on failure.
*/
CVSENTRIES*
cvs_ent_open(const char *dir, int flags)
{
size_t len;
char entpath[MAXPATHLEN], ebuf[128], mode[4];
FILE *fp;
struct cvs_ent *ent;
CVSENTRIES *ep;
memset(mode, 0, sizeof(mode));
switch (flags & O_ACCMODE) {
case O_RDWR:
mode[1] = '+';
/* fallthrough */
case O_RDONLY:
mode[0] = 'r';
break;
case O_WRONLY:
mode[0] = 'w';
break;
}
snprintf(entpath, sizeof(entpath), "%s/" CVS_PATH_ENTRIES, dir);
fp = fopen(entpath, mode);
if (fp == NULL) {
cvs_log(LP_ERRNO, "cannot open CVS/Entries for reading",
entpath);
return (NULL);
}
ep = (CVSENTRIES *)malloc(sizeof(CVSENTRIES));
if (ep == NULL) {
cvs_log(LP_ERRNO, "failed to allocate Entries data");
(void)fclose(fp);
return (NULL);
}
ep->cef_path = strdup(dir);
if (ep->cef_path == NULL) {
cvs_log(LP_ERRNO, "failed to copy Entries path");
free(ep);
(void)fclose(fp);
return (NULL);
}
ep->cef_cur = NULL;
TAILQ_INIT(&(ep->cef_ent));
while (fgets(ebuf, sizeof(ebuf), fp) != NULL) {
len = strlen(ebuf);
if ((len > 0) && (ebuf[len - 1] == '\n'))
ebuf[--len] = '\0';
ent = cvs_ent_parse(ebuf);
if (ent == NULL)
continue;
TAILQ_INSERT_TAIL(&(ep->cef_ent), ent, ce_list);
}
/* only keep a pointer to the open file if we're in writing mode */
if ((flags & O_WRONLY) || (flags & O_RDWR))
ep->cef_file = fp;
else
(void)fclose(fp);
return (ep);
}
/*
* cvs_ent_close()
*
* Close the Entries file <ep>.
*/
void
cvs_ent_close(CVSENTRIES *ep)
{
free(ep);
}
/*
* cvs_ent_add()
*
* Add the entry <ent> to the Entries file <ef>.
*/
int
cvs_ent_add(CVSENTRIES *ef, struct cvs_ent *ent)
{
void *tmp;
if (ef->cef_file == NULL) {
cvs_log(LP_ERR, "Entries file is opened in read-only mode");
return (-1);
}
if (cvs_ent_get(ef, ent->ce_name) != NULL)
return (-1);
if (fseek(ef->cef_file, (long)0, SEEK_END) == -1) {
cvs_log(LP_ERRNO, "failed to seek to end of CVS/Entries file");
return (-1);
}
fprintf(ef->cef_file, "%s\n", ent->ce_line);
TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
return (0);
}
/*
* cvs_ent_addln()
*
* Add a line to the Entries file.
*/
int
cvs_ent_addln(CVSENTRIES *ef, const char *line)
{
void *tmp;
struct cvs_ent *ent;
if (ef->cef_file == NULL) {
cvs_log(LP_ERR, "Entries file is opened in read-only mode");
return (-1);
}
ent = cvs_ent_parse(line);
if (ent == NULL)
return (-1);
if (cvs_ent_get(ef, ent->ce_name) != NULL)
return (-1);
TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
return (0);
}
/*
* cvs_ent_get()
*
* Get the CVS entry from the Entries file <ef> whose 'name' portion matches
* <file>.
* Returns a pointer to the cvs entry structure on success, or NULL on failure.
*/
struct cvs_ent*
cvs_ent_get(CVSENTRIES *ef, const char *file)
{
u_int i;
struct cvs_ent *ep;
TAILQ_FOREACH(ep, &(ef->cef_ent), ce_list)
if (strcmp(ep->ce_name, file) == 0)
return (ep);
return (NULL);
}
/*
* cvs_ent_next()
*
* This function is used to iterate over the entries in an Entries file. The
* first call will return the first entry of the file and each subsequent call
* will return the entry following the last one returned.
* Returns a pointer to the cvs entry structure on success, or NULL on failure.
*/
struct cvs_ent*
cvs_ent_next(CVSENTRIES *ef)
{
if (ef->cef_cur == NULL) {
ef->cef_cur = TAILQ_FIRST(&(ef->cef_ent));
return (ef->cef_cur);
}
return TAILQ_NEXT(ef->cef_cur, ce_list);
}
/*
* cvs_ent_parse()
*
* Parse a single line from a CVS/Entries file and return a cvs_entry structure
* containing all the parsed information.
*/
struct cvs_ent*
cvs_ent_parse(const char *entry)
{
int i;
char *fields[CVS_ENTRIES_NFIELDS], *sp, *dp;
struct cvs_ent *entp;
entp = (struct cvs_ent *)malloc(sizeof(*entp));
if (entp == NULL) {
cvs_log(LP_ERRNO, "failed to allocate CVS entry");
return (NULL);
}
entp->ce_rev = rcsnum_alloc();
if (entp->ce_rev == NULL) {
free(entp);
return (NULL);
}
entp->ce_line = strdup(entry);
if (entp->ce_line == NULL) {
free(entp);
return (NULL);
}
entp->ce_buf = strdup(entry);
if (entp->ce_buf == NULL) {
free(entp->ce_line);
free(entp);
return (NULL);
}
sp = entp->ce_buf;
if (*sp == CVS_ENTRIES_DELIM)
entp->ce_type = CVS_ENT_FILE;
else if (*sp == 'D') {
entp->ce_type = CVS_ENT_DIR;
sp++;
}
else {
/* unknown entry, ignore for future expansion */
entp->ce_type = CVS_ENT_NONE;
sp++;
}
sp++;
i = 0;
do {
dp = strchr(sp, CVS_ENTRIES_DELIM);
if (dp != NULL)
*(dp++) = '\0';
fields[i++] = sp;
sp = dp;
} while ((dp != NULL) && (i < CVS_ENTRIES_NFIELDS));
entp->ce_name = fields[0];
if (entp->ce_type == CVS_ENT_FILE) {
rcsnum_aton(fields[1], NULL, entp->ce_rev);
entp->ce_timestamp = fields[2];
entp->ce_opts = fields[3];
entp->ce_tag = fields[4];
}
return (entp);
}
|