-
Notifications
You must be signed in to change notification settings - Fork 8
/
revdir.c
146 lines (126 loc) · 3.43 KB
/
revdir.c
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
/*
* Copyright © 2006 Keith Packard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "cvs.h"
static int
compare_names (const void *a, const void *b)
{
const rev_file *af = a, *bf = b;
return strcmp (af->name, bf->name);
}
#define REV_DIR_HASH 288361
typedef struct _rev_dir_hash {
struct _rev_dir_hash *next;
unsigned long hash;
rev_dir dir;
} rev_dir_hash;
static rev_dir_hash *buckets[REV_DIR_HASH];
static
unsigned long hash_files (rev_file **files, int nfiles)
{
unsigned long h = 0;
int i;
for (i = 0; i < nfiles; i++)
h = ((h << 1) | (h >> (sizeof (h) * 8 - 1))) ^ (unsigned long) files[i];
return h;
}
static int total_dirs = 0;
/*
* Take a collection of file revisions and pack them together
*/
static rev_dir *
rev_pack_dir (rev_file **files, int nfiles)
{
unsigned long hash = hash_files (files, nfiles);
rev_dir_hash **bucket = &buckets[hash % REV_DIR_HASH];
rev_dir_hash *h;
for (h = *bucket; h; h = h->next) {
if (h->hash == hash && h->dir.nfiles == nfiles &&
!memcmp (files, h->dir.files, nfiles * sizeof (rev_file *)))
{
return &h->dir;
}
}
h = malloc (sizeof (rev_dir_hash) + nfiles * sizeof (rev_file *));
h->next = *bucket;
*bucket = h;
h->hash = hash;
h->dir.nfiles = nfiles;
memcpy (h->dir.files, files, nfiles * sizeof (rev_file *));
total_dirs++;
return &h->dir;
}
static int sds = 0;
static rev_dir **rds = NULL;
void
rev_free_dirs (void)
{
unsigned long hash;
for (hash = 0; hash < REV_DIR_HASH; hash++) {
rev_dir_hash **bucket = &buckets[hash];
rev_dir_hash *h;
while ((h = *bucket)) {
*bucket = h->next;
free (h);
}
}
if (rds) {
free (rds);
rds = NULL;
sds = 0;
}
}
rev_dir **
rev_pack_files (rev_file **files, int nfiles, int *ndr)
{
char *dir = 0;
char *slash;
int dirlen = 0;
int i;
int start = 0;
int nds = 0;
rev_dir *rd;
if (!rds)
rds = malloc ((sds = 16) * sizeof (rev_dir *));
/* order by name */
qsort (files, nfiles, sizeof (rev_file *), compare_names);
/* pull out directories */
for (i = 0; i < nfiles; i++) {
if (!dir || strncmp (files[i]->name, dir, dirlen) != 0)
{
if (i > start) {
rd = rev_pack_dir (files + start, i - start);
if (nds == sds)
rds = realloc (rds, (sds *= 2) * sizeof (rev_dir *));
rds[nds++] = rd;
}
start = i;
dir = files[i]->name;
slash = strrchr (dir, '/');
if (slash)
dirlen = slash - dir;
else
dirlen = 0;
}
}
rd = rev_pack_dir (files + start, nfiles - start);
if (nds == sds)
rds = realloc (rds, (sds *= 2) * sizeof (rev_dir *));
rds[nds++] = rd;
*ndr = nds;
return rds;
}