-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.c
476 lines (459 loc) · 9.91 KB
/
diff.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
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "conf.h"
#include "dr.h"
#include "dir.h"
#include "db.h"
#include "dc3.h"
#include "object.h"
#include "meta.h"
#include "checkout.h"
#include "patch.h"
#include "diff.h"
#define COST (10)
#define MIN(a, b) ((a) > (b) ? (b) : (a))
static int
match(const uint8_t *a, int an, const uint8_t *b, int bn, int *n)
{
int i, x = MIN(an, bn);
for (i = 0; i < x; i++) {
if (a[i] != b[i])
break;
}
*n = i;
if (i < x) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
} else {
return 0;
}
}
if (x == bn)
return 0;
return -1;
}
static int
search(dr_t old, int *SA, const uint8_t *ns, int nsize, int *pos)
{
int n = 0, sim = 0;
int osize = old->size;
int m = 0, l = 0, r = osize;
const uint8_t *p = old->buf;
*pos = -1;
while (l < r) {
int c, s;
m = (l + r) / 2;
n = SA[m];
c = match(&p[n], osize - n, ns, nsize, &s);
if (c == 0) { //contain
*pos = n;
sim = s;
assert(s == nsize);
break;
} else {
if (sim < s) {
*pos = n;
sim = s;
}
if (c < 0)
l = m + 1;
else
r = m;
}
}
return sim;
}
static dr_t
diff_content(dr_t old, dr_t new)
{
int *SA;
drb_t drb;
const uint8_t *np;
int ni = 0, oi = 0, sim;
int nsize = new->size;
np = new->buf;
SA = dc3(old->buf, old->size);
drb_init(&drb, 1024);
patch_total(&drb, new->size);
while (ni < nsize) {
struct COPY copy;
sim = search(old, SA, &np[ni], nsize - ni, &oi);
assert(sim <= nsize - ni);
if (sim <= COST) { //has no match, so find first match segment
struct INSERT insert;
int xi, xn = 0, xs = 0;
for (xi = ni+1; xi < nsize; xi++) {
xs = search(old, SA, &np[xi], nsize - xi, &xn);
assert(xs <= (nsize - xi));
if (xs > COST)
break;
}
insert.p = &new->buf[ni];
insert.size = xi - ni;
patch_insert(&drb, &insert);
//printf("INSERTT INTO %d SIZE %d\n", ni, xi - ni);
ni = xi;
oi = xn;
sim = xs;
}
if (sim > COST) {
copy.pos = oi;
copy.size = sim;
patch_copy(&drb, ©);
//printf("COPY FROM %d TO %d SIZE %d\n", oi, ni, sim);
ni += sim;
}
}
free(SA);
//printf("drb size:%d\n", drb.size);
return drb_dump(&drb);
}
static void
verify(dr_t a, dr_t b, dr_t p)
{
char *s, *e, buf[1024];
dr_t apath, bpath;
dr_t fingera, fingerb;
struct patch_args args;
snprintf(buf, sizeof(buf), "_temp/%s/", str(a));
apath = dr_newstr(buf);
snprintf(buf, sizeof(buf), "_temp/%s/", str(b));
bpath = dr_newstr(buf);
//checkout
checkout(a, apath);
checkout(b, bpath);
args.hash = a;
args.patch = p;
args.output = apath;
patch(&args);
//verify finger
snprintf(buf, sizeof(buf), "%s/"FINGERPRINT_NAME, str(apath));
fingera = dir_readfile(buf, NULL);
snprintf(buf, sizeof(buf), "%s/"FINGERPRINT_NAME, str(bpath));
fingerb = dir_readfile(buf, NULL);
assert(dr_cmp(fingera, fingerb) == 0);
//verify file
s = e = str(fingera);
e += fingera->size;
while (s < e) {
dr_t data, dhash;
char *hash, *path;
hash = s; s = strchr(s, '\t'); *s = 0;
path = ++s; s = strchr(s, '\n'); *s++ = 0;
data = dir_readfile(path, apath);
dhash = db_hash(data);
if (strcmp(str(dhash), hash) != 0) {
fprintf(stderr, "%s != %s", hash, str(dhash));
exit(EINVAL);
}
dr_unref(data);
dr_unref(dhash);
}
dr_unref(fingera);
dr_unref(fingerb);
dr_unref(apath);
dr_unref(bpath);
system("rm -rf _temp");
}
static void
diff_tree(dr_t a, dr_t b, dr_t root)
{
int i;
drb_t file;
dr_t data, afinger;
dr_t patchhash;
char buf[2048];
struct ref *r;
struct release ra, rb;
struct tree tb, ta_name, ta_hash;
int add, dff, cpy, del;
a = db_aliashash(a);
b = db_aliashash(b);
db_readrel(&ra, a);
db_readrel(&rb, b);
db_readtree(&ta_name, ra.tree);
db_readtree(&tb, rb.tree);
afinger = dr_ref(ra.finger);
printf("tree a:%s\n", ra.tree->buf);
printf("tree b:%s\n", rb.tree->buf);
release_destroy(&ra);
release_destroy(&rb);
tree_clone(&ta_hash, &ta_name);
tree_sort(&ta_name, ref_namecmp);
tree_sort(&ta_hash, ref_hashcmp);
drb_init(&file, 1024);
printf("diff start\n");
printf("detect change\n");
add = 0; dff = 0; cpy = 0; del = 0;
for (i = 0; i < tb.refn; i++) {
struct ref *a, *b;
dr_t patch = NULL;
b = &tb.refs[i];
if ((a = tree_search(&ta_hash, b, ref_hashcmp))) {
if (dr_cmp(b->name, a->name) != 0) {
struct CPY C;
C.namea = a->name;
C.name = b->name;
ctrl_cpy(&file, &C);
++cpy;
} else {
//printf("%s is same, skip it.\n", b->name->buf);
}
continue;
}
if (b->data == NULL)
b->data = db_read(b->hash);
a = tree_search(&ta_name, b, ref_namecmp);
if (a == NULL) {
struct NEW N;
N.name = (b->name);
N.data = (b->data);
ctrl_new(&file, &N);
add++;
} else {
if (a->data == NULL)
a->data = db_read(a->hash);
patch = diff_content(a->data, b->data);
if (b->data->size <= patch->size) {//no fix patch
struct NEW N;
N.name = (b->name);
N.data = (b->data);
ctrl_new(&file, &N);
add++;
} else {
struct DFF D;
D.name = b->name;
D.patch = patch;
ctrl_dff(&file, &D);
++dff;
}
dr_unref(patch);
}
}
printf("detect recpye\n");
#if ENABLE_DEL
tree_sort(&tb, ref_namecmp);
for (i = 0; i < ta_name.refn; i++) {
r = &ta_name.refs[i];
if (tree_search(&tb, r, ref_namecmp) == NULL) { //clear
struct DEL d;
d.name = r->name;
ctrl_del(&file, &d);
++del;
}
}
#endif
data = drb_dump(&file);
patchhash = db_hash(data);
snprintf(buf, sizeof(buf), "%s/patch.dat", str(afinger));
dir_writefile(buf, data, root);
snprintf(buf, sizeof(buf),
"{\"add\":%d, \"dff\":%d, \"cpy\":%d, "
"\"del\":%d, \"hash\":\"%s\", \"size\":%d }",
add, dff, cpy, del, patchhash->buf, data->size);
dr_unref(data);
data = dr_newstr(buf);
snprintf(buf, sizeof(buf), "%s/patch.json", str(afinger));
dir_writefile(buf, data, root);
printf("%s\n\n", str(data));
snprintf(buf, sizeof(buf), "%s/%s/patch.dat", str(root), str(afinger));
dr_unref(data);
dr_unref(patchhash);
dr_unref(afinger);
tree_destroy(&ta_name);
tree_destroy(&ta_hash);
tree_destroy(&tb);
data = dr_newstr(buf);
verify(a, b, data);
dr_unref(a);
dr_unref(b);
dr_unref(data);
return ;
}
#if 1
void
diff(struct diff_args *args)
{
dr_t head;
struct release rel;
if (args->r == 0)
return diff_tree(args->a, args->b, args->o);
head = db_readhead(&rel, NULL);
if (head->size == 0) {
printf("Empty db\n");
} else {
int i;
for (i = 0; i < args->r || args->r < 0; i++) {
dr_t h;
if (rel.prev->size <= 0)
break;
h = dr_ref(rel.prev);
diff_tree(h, head, args->o);
release_destroy(&rel);
db_readrel(&rel, h);
dr_unref(h);
}
}
dr_unref(head);
release_destroy(&rel);
return ;
}
#else
static int
cmpstr(dr_t a, dr_t b)
{
char c1, c2, c;
const char *sa, *ea, *sb, *eb;
sa = (char *)a->buf;
sb = (char *)b->buf;
ea = sa + a->size - 13;
eb = sb + b->size - 13;
while (sa < ea && sb < eb) {
c1 = *sa++;
c2 = *sb++;
c = c1 - c2;
if (c != 0)
return c;
}
return (ea - sa) - (eb - sb);
}
static int
shortnamecmp(const void *aa, const void *bb)
{
struct ref *a = (struct ref *)aa;
struct ref *b = (struct ref *)bb;
return cmpstr(a->name, b->name);
}
void
diff(struct diff_args *args)
{
int i;
drb_t file;
char buff[2048];
dr_t data, patchhash;
int add, dff, cpy, del;
struct release ra, rb;
struct tree ta, tb;
db_aliashash(&args->a);
db_aliashash(&args->b);
db_readrel(&ra, args->a);
db_readrel(&rb, args->b);
db_readtree(&ta, ra.tree);
db_readtree(&tb, rb.tree);
printf("tree a:%s\n", ra.tree->buf);
printf("tree b:%s\n", rb.tree->buf);
release_destroy(&ra);
release_destroy(&rb);
tree_sort(&ta, shortnamecmp);
drb_init(&file, 1024);
printf("diff start\n");
printf("detect change\n");
add = 0; dff = 0; cpy = 0; del = 0;
for (i = 0; i < tb.refn; i++) {
struct ref *b;
struct ref *a;
dr_t patch = NULL;
b = &tb.refs[i];
if (b->data == NULL)
b->data = db_read(b->hash);
a = tree_search(&ta, b, shortnamecmp);
if (a == NULL) {
struct NEW N;
N.name = (b->name);
N.data = (b->data);
ctrl_new(&file, &N);
add++;
continue;
}
if (dr_cmp(a->hash, b->hash) == 0) {
if (dr_cmp(b->name, a->name) != 0) {
struct MOV M;
M.namea = a->name;
M.name = b->name;
ctrl_cpy(&file, &M);
++cpy;
} else {
//printf("%s is same, skip it.\n", b->name->buf);
}
continue;
}
if (a->data == NULL)
a->data = db_read(a->hash);
patch = diff_content(a->data, b->data);
if (b->data->size <= patch->size) {//no fix patch
struct NEW N;
N.name = (b->name);
N.data = (b->data);
ctrl_new(&file, &N);
add++;
} else {
struct DFX D;
D.namea = a->name;
D.name = b->name;
D.patch = patch;
ctrl_dfx(&file, &D);
++dff;
}
dr_unref(patch);
}
printf("\ndetect recpye\n");
tree_sort(&tb, ref_namecmp);
#if ENABLE_DEL
for (i = 0; i < ta.refn; i++) {
struct ref *r = &ta.refs[i];
if (tree_search(&tb, r, ref_namecmp) == NULL) { //clear
struct DEL d;
d.name = r->name;
ctrl_del(&file, &d);
++del;
}
}
#endif
printf("\n");
data = drb_dump(&file);
patchhash = db_hash(data);
dir_writefile("patch.dat", data, args->p);
dr_unref(data);
snprintf(buff, sizeof(buff),
"{\"add\":%d, \"dff\":%d, \"cpy\":%d, \"del\":%d, \"hash\":\"%s\"}",
add, dff, cpy, del, patchhash->buf);
data = dr_newstr(buff);
dir_writefile("patch.json", data, args->p);
dr_unref(data);
dr_unref(patchhash);
tree_destroy(&ta);
tree_destroy(&tb);
return ;
}
#endif
#ifdef MAIN
int main(int argc, const char *argv[])
{
dr_t src, dst, patch;
if (argc != 3) {
fprintf(stderr, "USAGE: %s source destination\n", argv[0]);
exit(0);
}
src = dir_readfile(argv[1], NULL);
if (src == NULL) {
fprintf(stderr, "ERROR: file %s nonexist\n", argv[1]);
exit(errno);
}
dst = dir_readfile(argv[2], NULL);
if (dst == NULL) {
fprintf(stderr, "ERROR: file %s nonexist\n", argv[2]);
exit(errno);
}
patch = diff_content(src, dst);
dir_writefile("x.patch", patch, NULL);
dr_unref(src);
dr_unref(dst);
dr_unref(patch);
return 0;
}
#endif