git maintenance: avoid console window in scheduled tasks on Windows
[git.git] / t / helper / test-delta.c
blobe7d134ec251e01405a6f854bbd496d797980172f
1 /*
2 * test-delta.c: test code to exercise diff-delta.c and patch-delta.c
4 * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
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 */
11 #include "test-tool.h"
12 #include "git-compat-util.h"
13 #include "delta.h"
14 #include "wrapper.h"
16 static const char usage_str[] =
17 "test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
19 int cmd__delta(int argc, const char **argv)
21 int fd;
22 struct stat st;
23 void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL;
24 unsigned long from_size, data_size, out_size;
25 int ret = 1;
27 if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
28 fprintf(stderr, "usage: %s\n", usage_str);
29 return 1;
32 fd = open(argv[2], O_RDONLY);
33 if (fd < 0 || fstat(fd, &st)) {
34 perror(argv[2]);
35 return 1;
37 from_size = st.st_size;
38 from_buf = xmalloc(from_size);
39 if (read_in_full(fd, from_buf, from_size) < 0) {
40 perror(argv[2]);
41 close(fd);
42 goto cleanup;
44 close(fd);
46 fd = open(argv[3], O_RDONLY);
47 if (fd < 0 || fstat(fd, &st)) {
48 perror(argv[3]);
49 goto cleanup;
51 data_size = st.st_size;
52 data_buf = xmalloc(data_size);
53 if (read_in_full(fd, data_buf, data_size) < 0) {
54 perror(argv[3]);
55 close(fd);
56 goto cleanup;
58 close(fd);
60 if (argv[1][1] == 'd')
61 out_buf = diff_delta(from_buf, from_size,
62 data_buf, data_size,
63 &out_size, 0);
64 else
65 out_buf = patch_delta(from_buf, from_size,
66 data_buf, data_size,
67 &out_size);
68 if (!out_buf) {
69 fprintf(stderr, "delta operation failed (returned NULL)\n");
70 goto cleanup;
73 fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
74 if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
75 perror(argv[4]);
76 goto cleanup;
79 ret = 0;
80 cleanup:
81 free(from_buf);
82 free(data_buf);
83 free(out_buf);
85 return ret;