]>
Commit | Line | Data |
---|---|---|
a310d434 NP |
1 | /* |
2 | * test-delta.c: test code to exercise diff-delta.c and patch-delta.c | |
3 | * | |
03aa8ff3 | 4 | * (C) 2005 Nicolas Pitre <nico@fluxnic.net> |
a310d434 NP |
5 | * |
6 | * This code is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
9153dde5 | 11 | #include "test-tool.h" |
85023577 | 12 | #include "git-compat-util.h" |
a310d434 NP |
13 | #include "delta.h" |
14 | ||
adb7b5fc | 15 | static const char usage_str[] = |
9153dde5 | 16 | "test-tool delta (-d|-p) <from_file> <data_file> <out_file>"; |
a310d434 | 17 | |
9153dde5 | 18 | int cmd__delta(int argc, const char **argv) |
a310d434 NP |
19 | { |
20 | int fd; | |
21 | struct stat st; | |
f40a6934 | 22 | void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL; |
a310d434 | 23 | unsigned long from_size, data_size, out_size; |
f40a6934 | 24 | int ret = 1; |
a310d434 NP |
25 | |
26 | if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) { | |
b978403a | 27 | fprintf(stderr, "usage: %s\n", usage_str); |
a310d434 NP |
28 | return 1; |
29 | } | |
30 | ||
31 | fd = open(argv[2], O_RDONLY); | |
32 | if (fd < 0 || fstat(fd, &st)) { | |
33 | perror(argv[2]); | |
34 | return 1; | |
35 | } | |
36 | from_size = st.st_size; | |
d65930c5 JK |
37 | from_buf = xmalloc(from_size); |
38 | if (read_in_full(fd, from_buf, from_size) < 0) { | |
a310d434 | 39 | perror(argv[2]); |
e35f9824 | 40 | close(fd); |
f40a6934 | 41 | goto cleanup; |
a310d434 NP |
42 | } |
43 | close(fd); | |
44 | ||
45 | fd = open(argv[3], O_RDONLY); | |
46 | if (fd < 0 || fstat(fd, &st)) { | |
47 | perror(argv[3]); | |
f40a6934 | 48 | goto cleanup; |
a310d434 NP |
49 | } |
50 | data_size = st.st_size; | |
d65930c5 JK |
51 | data_buf = xmalloc(data_size); |
52 | if (read_in_full(fd, data_buf, data_size) < 0) { | |
a310d434 | 53 | perror(argv[3]); |
e35f9824 | 54 | close(fd); |
f40a6934 | 55 | goto cleanup; |
a310d434 NP |
56 | } |
57 | close(fd); | |
58 | ||
59 | if (argv[1][1] == 'd') | |
60 | out_buf = diff_delta(from_buf, from_size, | |
75c42d8c | 61 | data_buf, data_size, |
3c849742 | 62 | &out_size, 0); |
a310d434 NP |
63 | else |
64 | out_buf = patch_delta(from_buf, from_size, | |
75c42d8c LT |
65 | data_buf, data_size, |
66 | &out_size); | |
a310d434 NP |
67 | if (!out_buf) { |
68 | fprintf(stderr, "delta operation failed (returned NULL)\n"); | |
f40a6934 | 69 | goto cleanup; |
a310d434 NP |
70 | } |
71 | ||
72 | fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666); | |
06f46f23 | 73 | if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) { |
a310d434 | 74 | perror(argv[4]); |
f40a6934 | 75 | goto cleanup; |
a310d434 NP |
76 | } |
77 | ||
f40a6934 ÆAB |
78 | ret = 0; |
79 | cleanup: | |
80 | free(from_buf); | |
81 | free(data_buf); | |
82 | free(out_buf); | |
83 | ||
84 | return ret; | |
a310d434 | 85 | } |