forked from glandium/git-cinnabar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hg-bundle.c
76 lines (70 loc) · 1.59 KB
/
hg-bundle.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
#include "git-compat-util.h"
#include "hg-bundle.h"
#include <stdint.h>
static size_t copy_data(uint32_t len, FILE *in, FILE *out)
{
unsigned char buf[4096];
size_t ret = len;
while (len) {
uint32_t sz = len > sizeof(buf) ? sizeof(buf) : len;
fread(buf, 1, sz, in);
fwrite(buf, 1, sz, out);
len -= sz;
}
return ret;
}
static size_t copy_chunk(int adjust, FILE *in, FILE *out)
{
unsigned char buf[4];
const unsigned char *p = buf;
uint32_t len;
size_t ret = 0;
//TODO: Check for errors, etc.
fread(buf, 1, 4, in);
fwrite(buf, 1, 4, out);
len = get_be32(p);
if (len <= adjust)
//TODO: len != 0 is actually invalid
return 0;
ret = len -= adjust;
copy_data(len, in, out);
return ret;
}
static size_t copy_changegroup_chunk(FILE *in, FILE *out)
{
return copy_chunk(4, in, out);
}
static void copy_changegroup(FILE *in, FILE *out)
{
/* changesets */
while (copy_changegroup_chunk(in, out)) {}
/* manifests */
while (copy_changegroup_chunk(in, out)) {}
/* files */
while (copy_changegroup_chunk(in, out)) {
while (copy_changegroup_chunk(in, out)) {}
}
}
static size_t copy_bundle2_chunk(FILE *in, FILE *out)
{
return copy_chunk(0, in, out);
}
void copy_bundle(FILE *in, FILE *out)
{
unsigned char buf[4];
const unsigned char *p = buf;
//TODO: Check for errors, etc.
fread(buf, 1, 4, in);
fwrite(buf, 1, 4, out);
if (memcmp(buf, "HG20", 4)) {
copy_data(get_be32(p) - 4, in, out);
copy_changegroup(in, out);
return;
}
// bundle2 parameters
copy_bundle2_chunk(in, out);
// bundle2 parts
while (copy_bundle2_chunk(in, out)) {
while (copy_bundle2_chunk(in, out)) {}
}
}