forked from oridb/git9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathols.c
170 lines (154 loc) · 2.77 KB
/
ols.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include "git.h"
enum {
Sinit,
Siter,
};
static int
crackidx(char *path, int *np)
{
int fd;
char buf[4];
if((fd = open(path, OREAD)) == -1)
return -1;
if(seek(fd, 8 + 255*4, 0) == -1)
return -1;
if(readn(fd, buf, sizeof(buf)) != sizeof(buf))
return -1;
*np = GETBE32(buf);
return fd;
}
int
isloosedir(char *s)
{
return strlen(s) == 2 && isxdigit(s[0]) && isxdigit(s[1]);
}
int
endswith(char *n, char *s)
{
int nn, ns;
nn = strlen(n);
ns = strlen(s);
return nn > ns && strcmp(n + nn - ns, s) == 0;
}
int
olsreadpacked(Objlist *ols, Hash *h)
{
char *p;
int i, j;
i = ols->packidx;
j = ols->entidx;
if(ols->state == Siter)
goto step;
for(i = 0; i < ols->npack; i++){
if(!endswith(ols->pack[i].name, ".idx"))
continue;
if((p = smprint(".git/objects/pack/%s", ols->pack[i].name)) == nil)
sysfatal("smprint: %r");
ols->fd = crackidx(p, &ols->nent);
free(p);
if(ols->fd == -1)
continue;
j = 0;
while(j < ols->nent){
if(readn(ols->fd, h->h, sizeof(h->h)) != sizeof(h->h))
continue;
ols->state = Siter;
ols->packidx = i;
ols->entidx = j;
return 0;
step:
j++;
}
close(ols->fd);
}
ols->state = Sinit;
return -1;
}
int
olsreadloose(Objlist *ols, Hash *h)
{
char buf[64], *p;
int i, j, n;
i = ols->topidx;
j = ols->looseidx;
if(ols->state == Siter)
goto step;
for(i = 0; i < ols->ntop; i++){
if(!isloosedir(ols->top[i].name))
continue;
if((p = smprint(".git/objects/%s", ols->top[i].name)) == nil)
sysfatal("smprint: %r");
ols->fd = open(p, OREAD);
free(p);
if(ols->fd == -1)
continue;
while((ols->nloose = dirread(ols->fd, &ols->loose)) > 0){
j = 0;
while(j < ols->nloose){
n = snprint(buf, sizeof(buf), "%s%s", ols->top[i].name, ols->loose[j].name);
if(n >= sizeof(buf))
goto step;
if(hparse(h, buf) == -1)
goto step;
ols->state = Siter;
ols->topidx = i;
ols->looseidx = j;
return 0;
step:
j++;
}
free(ols->loose);
ols->loose = nil;
}
close(ols->fd);
ols->fd = -1;
}
ols->state = Sinit;
return -1;
}
Objlist*
mkols(void)
{
Objlist *ols;
ols = emalloc(sizeof(Objlist));
if((ols->ntop = slurpdir(".git/objects", &ols->top)) == -1)
sysfatal("read top level: %r");
if((ols->npack = slurpdir(".git/objects/pack", &ols->pack)) == -1)
ols->pack = nil;
ols->fd = -1;
return ols;
}
void
olsfree(Objlist *ols)
{
if(ols == nil)
return;
if(ols->fd != -1)
close(ols->fd);
free(ols->top);
free(ols->loose);
free(ols->pack);
free(ols);
}
int
olsnext(Objlist *ols, Hash *h)
{
if(ols->stage == 0){
if(olsreadloose(ols, h) != -1){
ols->idx++;
return 0;
}
ols->stage++;
}
if(ols->stage == 1){
if(olsreadpacked(ols, h) != -1){
ols->idx++;
return 0;
}
ols->stage++;
}
return -1;
}