| 1 | /* |
| 2 | * test-delta.c: test code to exercise diff-delta.c and patch-delta.c |
| 3 | * |
| 4 | * (C) 2005 Nicolas Pitre <nico@fluxnic.net> |
| 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 | |
| 11 | #include "test-tool.h" |
| 12 | #include "git-compat-util.h" |
| 13 | #include "delta.h" |
| 14 | #include "strbuf.h" |
| 15 | |
| 16 | static const char usage_str[] = |
| 17 | "test-tool delta (-d|-p) <from_file> <data_file> <out_file>"; |
| 18 | |
| 19 | int cmd__delta(int argc, const char **argv) |
| 20 | { |
| 21 | int fd; |
| 22 | struct strbuf from = STRBUF_INIT, data = STRBUF_INIT; |
| 23 | char *out_buf; |
| 24 | size_t out_size; |
| 25 | |
| 26 | if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) |
| 27 | usage(usage_str); |
| 28 | |
| 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]); |
| 33 | |
| 34 | if (argv[1][1] == 'd') { |
| 35 | unsigned long delta_size; |
| 36 | out_buf = diff_delta(from.buf, from.len, |
| 37 | data.buf, data.len, |
| 38 | &delta_size, 0); |
| 39 | out_size = delta_size; |
| 40 | } else |
| 41 | out_buf = patch_delta(from.buf, from.len, |
| 42 | data.buf, data.len, |
| 43 | &out_size); |
| 44 | if (!out_buf) |
| 45 | die("delta operation failed (returned NULL)"); |
| 46 | |
| 47 | fd = xopen(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 48 | if (write_in_full(fd, out_buf, out_size) < 0) |
| 49 | die_errno("write(%s)", argv[4]); |
| 50 | if (close(fd) < 0) |
| 51 | die_errno("close(%s)", argv[4]); |
| 52 | |
| 53 | strbuf_release(&from); |
| 54 | strbuf_release(&data); |
| 55 | free(out_buf); |
| 56 | |
| 57 | return 0; |
| 58 | } |