|
11 | 11 | #include "test-tool.h"
|
12 | 12 | #include "git-compat-util.h"
|
13 | 13 | #include "delta.h"
|
| 14 | +#include "strbuf.h" |
14 | 15 |
|
15 | 16 | static const char usage_str[] =
|
16 | 17 | "test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
|
17 | 18 |
|
18 | 19 | int cmd__delta(int argc, const char **argv)
|
19 | 20 | {
|
20 | 21 | int fd;
|
21 |
| - struct stat st; |
22 |
| - void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL; |
23 |
| - unsigned long from_size, data_size, out_size; |
24 |
| - int ret = 1; |
| 22 | + struct strbuf from = STRBUF_INIT, data = STRBUF_INIT; |
| 23 | + char *out_buf; |
| 24 | + unsigned long out_size; |
25 | 25 |
|
26 |
| - if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) { |
27 |
| - fprintf(stderr, "usage: %s\n", usage_str); |
28 |
| - return 1; |
29 |
| - } |
| 26 | + if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) |
| 27 | + usage(usage_str); |
30 | 28 |
|
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; |
37 |
| - from_buf = xmalloc(from_size); |
38 |
| - if (read_in_full(fd, from_buf, from_size) < 0) { |
39 |
| - perror(argv[2]); |
40 |
| - close(fd); |
41 |
| - goto cleanup; |
42 |
| - } |
43 |
| - close(fd); |
44 |
| - |
45 |
| - fd = open(argv[3], O_RDONLY); |
46 |
| - if (fd < 0 || fstat(fd, &st)) { |
47 |
| - perror(argv[3]); |
48 |
| - goto cleanup; |
49 |
| - } |
50 |
| - data_size = st.st_size; |
51 |
| - data_buf = xmalloc(data_size); |
52 |
| - if (read_in_full(fd, data_buf, data_size) < 0) { |
53 |
| - perror(argv[3]); |
54 |
| - close(fd); |
55 |
| - goto cleanup; |
56 |
| - } |
57 |
| - close(fd); |
| 29 | + if (strbuf_read_file(&from, argv[2], 0) < 0) |
| 30 | + die_errno("unable to read '%s'", argv[2]); |
| 31 | + if (strbuf_read_file(&data, argv[3], 0) < 0) |
| 32 | + die_errno("unable to read '%s'", argv[3]); |
58 | 33 |
|
59 | 34 | if (argv[1][1] == 'd')
|
60 |
| - out_buf = diff_delta(from_buf, from_size, |
61 |
| - data_buf, data_size, |
| 35 | + out_buf = diff_delta(from.buf, from.len, |
| 36 | + data.buf, data.len, |
62 | 37 | &out_size, 0);
|
63 | 38 | else
|
64 |
| - out_buf = patch_delta(from_buf, from_size, |
65 |
| - data_buf, data_size, |
| 39 | + out_buf = patch_delta(from.buf, from.len, |
| 40 | + data.buf, data.len, |
66 | 41 | &out_size);
|
67 |
| - if (!out_buf) { |
68 |
| - fprintf(stderr, "delta operation failed (returned NULL)\n"); |
69 |
| - goto cleanup; |
70 |
| - } |
| 42 | + if (!out_buf) |
| 43 | + die("delta operation failed (returned NULL)"); |
71 | 44 |
|
72 |
| - fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666); |
73 |
| - if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) { |
74 |
| - perror(argv[4]); |
75 |
| - goto cleanup; |
76 |
| - } |
| 45 | + fd = xopen(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 46 | + if (write_in_full(fd, out_buf, out_size) < 0) |
| 47 | + die_errno("write(%s)", argv[4]); |
| 48 | + if (close(fd) < 0) |
| 49 | + die_errno("close(%s)", argv[4]); |
77 | 50 |
|
78 |
| - ret = 0; |
79 |
| -cleanup: |
80 |
| - free(from_buf); |
81 |
| - free(data_buf); |
| 51 | + strbuf_release(&from); |
| 52 | + strbuf_release(&data); |
82 | 53 | free(out_buf);
|
83 | 54 |
|
84 |
| - return ret; |
| 55 | + return 0; |
85 | 56 | }
|
0 commit comments