File: cache.c

package info (click to toggle)
cvsps 2.1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 352 kB
  • ctags: 402
  • sloc: ansic: 4,687; sh: 189; makefile: 77
file content (528 lines) | stat: -rw-r--r-- 12,436 bytes parent folder | download | duplicates (5)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
 * See COPYING file for license information 
 */

#include <stdio.h>
#include <search.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>

#include <cbtcommon/hash.h>
#include <cbtcommon/debug.h>

#include "cache.h"
#include "cvsps_types.h"
#include "cvsps.h"
#include "util.h"

#define CACHE_DESCR_BOUNDARY "-=-END CVSPS DESCR-=-\n"

/* change this when making the on-disk cache-format invalid */
static int cache_version = 1;

/* the tree walk API pretty much requries use of globals :-( */
static FILE * cache_fp;
static int ps_counter;

static void write_patch_set_to_cache(PatchSet *);
static void parse_cache_revision(PatchSetMember *, const char *);
static void dump_patch_set(FILE *, PatchSet *);

static FILE *cache_open(char const *mode)
{
    char *prefix;
    char fname[PATH_MAX];
    char root[PATH_MAX];
    char repository[PATH_MAX];
    FILE * fp;

    /* Get the prefix */
    prefix = get_cvsps_dir();
    if (!prefix)
	return NULL;
    
    /* Generate the full path */
    strcpy(root, root_path);
    strcpy(repository, repository_path);

    strrep(root, '/', '#');
    strrep(repository, '/', '#');

    snprintf(fname, PATH_MAX, "%s/%s#%s", prefix, root, repository);
    
    if (!(fp = fopen(fname, mode)) && *mode == 'r')
    {
	if ((fp = fopen("CVS/cvsps.cache", mode)))
	{
	    fprintf(stderr, "\n");
	    fprintf(stderr, "****WARNING**** Obsolete CVS/cvsps.cache file found.\n");
	    fprintf(stderr, "                New file will be re-written in ~/%s/\n", CVSPS_PREFIX);
	    fprintf(stderr, "                Old file will be ignored.\n");
	    fprintf(stderr, "                Please manually remove the old file.\n");
	    fprintf(stderr, "                Continuing in 5 seconds.\n");
	    sleep(5);
	    fclose(fp);
	    fp = NULL;
	}
    }

    return fp;
}

/* ************ Reading ************ */

enum
{
    CACHE_NEED_FILE,
    CACHE_NEED_BRANCHES,
    CACHE_NEED_SYMBOLS,
    CACHE_NEED_REV,
    CACHE_NEED_PS,
    CACHE_NEED_PS_DATE,
    CACHE_NEED_PS_AUTHOR,
    CACHE_NEED_PS_TAG,
    CACHE_NEED_PS_TAG_FLAGS,
    CACHE_NEED_PS_BRANCH,
    CACHE_NEED_PS_BRANCH_ADD,
    CACHE_NEED_PS_DESCR,
    CACHE_NEED_PS_EOD,
    CACHE_NEED_PS_MEMBERS,
    CACHE_NEED_PS_EOM
};

time_t read_cache()
{
    FILE * fp;
    char buff[BUFSIZ];
    int state = CACHE_NEED_FILE;
    CvsFile * f = NULL;
    PatchSet * ps = NULL;
    char datebuff[20] = "";
    char authbuff[AUTH_STR_MAX] = "";
    char tagbuff[LOG_STR_MAX] = "";
    int tag_flags = 0;
    char branchbuff[LOG_STR_MAX] = "";
    int branch_add = 0;
    char logbuff[LOG_STR_MAX] = "";
    time_t cache_date = -1;
    int read_version;

    if (!(fp = cache_open("r")))
	goto out;

    /* first line is cache version  format "cache version: %d\n" */
    if (!fgets(buff, BUFSIZ, fp) || strncmp(buff, "cache version:", 14))
    {
	debug(DEBUG_APPERROR, "bad cvsps.cache file");
	goto out_close;
    }

    if ((read_version = atoi(buff + 15)) != cache_version)
    {
	debug(DEBUG_APPERROR, "bad cvsps.cache version %d, expecting %d.  ignoring cache",
	      read_version, cache_version);
	goto out_close;
    }

    /* second line is date cache was created, format "cache date: %d\n" */
    if (!fgets(buff, BUFSIZ, fp) || strncmp(buff, "cache date:", 11))
    {
	debug(DEBUG_APPERROR, "bad cvsps.cache file");
	goto out_close;
    }

    cache_date = atoi(buff + 12);
    debug(DEBUG_STATUS, "read cache_date %d", (int)cache_date);

    while (fgets(buff, BUFSIZ, fp))
    {
	int len = strlen(buff);

	switch(state)
	{
	case CACHE_NEED_FILE:
	    if (strncmp(buff, "file:", 5) == 0)
	    {
		len -= 6;
		f = create_cvsfile();
		f->filename = xstrdup(buff + 6);
		f->filename[len-1] = 0; /* Remove the \n at the end of line */
		debug(DEBUG_STATUS, "read cache filename '%s'", f->filename);
		put_hash_object_ex(file_hash, f->filename, f, HT_NO_KEYCOPY, NULL, NULL);
		state = CACHE_NEED_BRANCHES;
	    }
	    else
	    {
		state = CACHE_NEED_PS;
	    }
	    break;
	case CACHE_NEED_BRANCHES:
	    if (buff[0] != '\n')
	    {
		char * tag;

		tag = strchr(buff, ':');
		if (tag)
		{
		    *tag = 0;
		    tag += 2;
		    buff[len - 1] = 0;
		    cvs_file_add_branch(f, buff, tag);
		}
	    }
	    else
	    {
		f->have_branches = 1;
		state = CACHE_NEED_SYMBOLS;
	    }
	    break;
	case CACHE_NEED_SYMBOLS:
	    if (buff[0] != '\n')
	    {
		char * rev;

		rev = strchr(buff, ':');
		if (rev)
		{
		    *rev = 0;
		    rev += 2;
		    buff[len - 1] = 0;
		    cvs_file_add_symbol(f, rev, buff);
		}
	    }
	    else
	    {
		state = CACHE_NEED_REV;
	    }
	    break;
	case CACHE_NEED_REV:
	    if (isdigit(buff[0]))
	    {
		char * p = strchr(buff, ' ');
		if (p)
		{
		    CvsFileRevision * rev;
		    *p++ = 0;
		    buff[len-1] = 0;
		    rev = cvs_file_add_revision(f, buff);
		    if (strcmp(rev->branch, p) != 0)
		    {
			debug(DEBUG_APPERROR, "branch mismatch for %s:%s %s != %s", 
			      rev->file->filename, rev->rev, rev->branch, p);
		    }
		}
	    }
	    else
	    {
		state = CACHE_NEED_FILE;
	    }
	    break;
	case CACHE_NEED_PS:
	    if (strncmp(buff, "patchset:", 9) == 0)
		state = CACHE_NEED_PS_DATE;
	    break;
	case CACHE_NEED_PS_DATE:
	    if (strncmp(buff, "date:", 5) == 0)
	    {
		/* remove prefix "date: " and LF from len */
		len -= 6;
		strzncpy(datebuff, buff + 6, MIN(len, sizeof(datebuff)));
		state = CACHE_NEED_PS_AUTHOR;
	    }
	    break;
	case CACHE_NEED_PS_AUTHOR:
	    if (strncmp(buff, "author:", 7) == 0)
	    {
		/* remove prefix "author: " and LF from len */
		len -= 8;
		strzncpy(authbuff, buff + 8, MIN(len, AUTH_STR_MAX));
		state = CACHE_NEED_PS_TAG;
	    }
	    break;
	case CACHE_NEED_PS_TAG:
	    if (strncmp(buff, "tag:", 4) == 0)
	    {
		/* remove prefix "tag: " and LF from len */
		len -= 5;
		strzncpy(tagbuff, buff + 5, MIN(len, LOG_STR_MAX));
		state = CACHE_NEED_PS_TAG_FLAGS;
	    }
	    break;
	case CACHE_NEED_PS_TAG_FLAGS:
	    if (strncmp(buff, "tag_flags:", 10) == 0)
	    {
		/* remove prefix "tag_flags: " and LF from len */
		len -= 11;
		tag_flags = atoi(buff + 11);
		state = CACHE_NEED_PS_BRANCH;
	    }
	    break;
	case CACHE_NEED_PS_BRANCH:
	    if (strncmp(buff, "branch:", 7) == 0)
	    {
		/* remove prefix "branch: " and LF from len */
		len -= 8;
		strzncpy(branchbuff, buff + 8, MIN(len, LOG_STR_MAX));
		state = CACHE_NEED_PS_BRANCH_ADD;
	    }
	    break;
	case CACHE_NEED_PS_BRANCH_ADD:
	    if (strncmp(buff, "branch_add:", 11) == 0)
	    {
		/* remove prefix "branch_add: " and LF from len */
		len -= 12;
		branch_add = atoi(buff + 12);
		state = CACHE_NEED_PS_DESCR;
	    }
	    break;
	case CACHE_NEED_PS_DESCR:
	    if (strncmp(buff, "descr:", 6) == 0)
		state = CACHE_NEED_PS_EOD;
	    break;
	case CACHE_NEED_PS_EOD:
	    if (strcmp(buff, CACHE_DESCR_BOUNDARY) == 0)
	    {
		debug(DEBUG_STATUS, "patch set %s %s %s %s", datebuff, authbuff, logbuff, branchbuff);
		ps = get_patch_set(datebuff, logbuff, authbuff, branchbuff, NULL);
		/* the tag and tag_flags will be assigned by the resolve_global_symbols code 
		 * ps->tag = (strlen(tagbuff)) ? get_string(tagbuff) : NULL;
		 * ps->tag_flags = tag_flags;
		 */
		ps->branch_add = branch_add;
		state = CACHE_NEED_PS_MEMBERS;
	    }
	    else
	    {
		/* Make sure we have enough in the buffer */
		if (strlen(logbuff)+strlen(buff)<LOG_STR_MAX)
		    strcat(logbuff, buff);
	    }
	    break;
	case CACHE_NEED_PS_MEMBERS:
	    if (strncmp(buff, "members:", 8) == 0)
		state = CACHE_NEED_PS_EOM;
	    break;
	case CACHE_NEED_PS_EOM:
	    if (buff[0] == '\n')
	    {
		datebuff[0] = 0;
		authbuff[0] = 0;
		tagbuff[0] = 0;
		tag_flags = 0;
		branchbuff[0] = 0;
		branch_add = 0;
		logbuff[0] = 0;
		state = CACHE_NEED_PS;
	    }
	    else
	    {
		PatchSetMember * psm = create_patch_set_member();
		parse_cache_revision(psm, buff);
		patch_set_add_member(ps, psm);
	    }
	    break;
	}
    }

 out_close:
    fclose(fp);
 out:
    return cache_date;
}

enum
{
    CR_FILENAME,
    CR_PRE_REV,
    CR_POST_REV,
    CR_DEAD,
    CR_BRANCH_POINT
};

static void parse_cache_revision(PatchSetMember * psm, const char * p_buff)
{
    /* The format used to generate is:
     * "file:%s; pre_rev:%s; post_rev:%s; dead:%d; branch_point:%d\n"
     */
    char filename[PATH_MAX];
    char pre[REV_STR_MAX];
    char post[REV_STR_MAX];
    int dead = 0;
    int bp = 0;
    char buff[BUFSIZ];
    int state = CR_FILENAME;
    const char *s;
    char * p = buff;

    strcpy(buff, p_buff);

    while ((s = strsep(&p, ";")))
    {
	char * c = strchr(s, ':');

	if (!c)
	{
	    debug(DEBUG_APPERROR, "invalid cache revision line '%s'|'%s'", p_buff, s);
	    exit(1);
	}

	*c++ = 0;

	switch(state)
	{
	case CR_FILENAME:
	    strcpy(filename, c);
	    break;
	case CR_PRE_REV:
	    strcpy(pre, c);
	    break;
	case CR_POST_REV:
	    strcpy(post, c);
	    break;
	case CR_DEAD:
	    dead = atoi(c);
	    break;
	case CR_BRANCH_POINT:
	    bp = atoi(c);
	    break;
	}
	state++;
    }

    psm->file = (CvsFile*)get_hash_object(file_hash, filename);

    if (!psm->file)
    {
	debug(DEBUG_APPERROR, "file '%s' not found in hash", filename);
	exit(1);
    }

    psm->pre_rev = file_get_revision(psm->file, pre);
    psm->post_rev = file_get_revision(psm->file, post);
    psm->post_rev->dead = dead;
    psm->post_rev->post_psm = psm;

    if (!bp)
    {
	if (psm->pre_rev)
	    psm->pre_rev->pre_psm = psm;
    }
    else
    {
	list_add(&psm->post_rev->link, &psm->pre_rev->branch_children);
    }
}

/************ Writing ************/

void write_cache(time_t cache_date)
{
    struct hash_entry * file_iter;

    ps_counter = 0;

    if ((cache_fp = cache_open("w")) == NULL)
    {
	debug(DEBUG_SYSERROR, "can't open cvsps.cache for write");
	return;
    }

    fprintf(cache_fp, "cache version: %d\n", cache_version);
    fprintf(cache_fp, "cache date: %d\n", (int)cache_date);

    reset_hash_iterator(file_hash);

    while ((file_iter = next_hash_entry(file_hash)))
    {
	CvsFile * file = (CvsFile*)file_iter->he_obj;
	struct hash_entry * rev_iter;

	fprintf(cache_fp, "file: %s\n", file->filename);

	reset_hash_iterator(file->branches);
	while ((rev_iter = next_hash_entry(file->branches)))
	{
	    char * rev = (char *)rev_iter->he_key;
	    char * tag = (char *)rev_iter->he_obj;
	    fprintf(cache_fp, "%s: %s\n", rev, tag);
	}

	fprintf(cache_fp, "\n");

	reset_hash_iterator(file->symbols);
	while ((rev_iter = next_hash_entry(file->symbols)))
	{
	    char * tag = (char *)rev_iter->he_key;
	    CvsFileRevision * rev = (CvsFileRevision*)rev_iter->he_obj;
	    
	    if (rev->present)
		fprintf(cache_fp, "%s: %s\n", tag, rev->rev);
	}

	fprintf(cache_fp, "\n");

	reset_hash_iterator(file->revisions);
	while ((rev_iter = next_hash_entry(file->revisions)))
	{
	    CvsFileRevision * rev = (CvsFileRevision*)rev_iter->he_obj;
	    if (rev->present)
		fprintf(cache_fp, "%s %s\n", rev->rev, rev->branch);
	}

	fprintf(cache_fp, "\n");
    }

    fprintf(cache_fp, "\n");
    walk_all_patch_sets(write_patch_set_to_cache);
    fclose(cache_fp);
    cache_fp = NULL;
}

static void write_patch_set_to_cache(PatchSet * ps)
{
    dump_patch_set(cache_fp, ps);
}

static void dump_patch_set(FILE * fp, PatchSet * ps)
{
    struct list_head * next = ps->members.next;

    ps_counter++;
    fprintf(fp, "patchset: %d\n", ps_counter);
    fprintf(fp, "date: %d\n", (int)ps->date);
    fprintf(fp, "author: %s\n", ps->author);
    fprintf(fp, "tag: %s\n", ps->tag ? ps->tag : "");
    fprintf(fp, "tag_flags: %d\n", ps->tag_flags);
    fprintf(fp, "branch: %s\n", ps->branch);
    fprintf(fp, "branch_add: %d\n", ps->branch_add);
    fprintf(fp, "descr:\n%s", ps->descr); /* descr is guaranteed to end with LF */
    fprintf(fp, CACHE_DESCR_BOUNDARY);
    fprintf(fp, "members:\n");

    while (next != &ps->members)
    {
	PatchSetMember * psm = list_entry(next, PatchSetMember, link);
	int bp = 1;
	
	/* this actually deduces if this revision is a branch point... */
	if (!psm->pre_rev || (psm->pre_rev->pre_psm && psm->pre_rev->pre_psm == psm))
	    bp = 0;

	fflush(fp);
    
	fprintf(fp, "file:%s; pre_rev:%s; post_rev:%s; dead:%d; branch_point:%d\n", 
		psm->file->filename, 
		psm->pre_rev ? psm->pre_rev->rev : "INITIAL", psm->post_rev->rev, 
		psm->post_rev->dead, bp);
	next = next->next;
    }

    fprintf(fp, "\n");
}

/* where's arithmetic?... */