blob: 4595cd6433f9fd543791ee5a8a59a9112b50c046 [file] [log] [blame]
Nicolas Pitrea310d432005-05-19 10:27:14 -04001/*
2 * test-delta.c: test code to exercise diff-delta.c and patch-delta.c
3 *
Nicolas Pitre03aa8ff2009-09-14 02:41:16 -04004 * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitrea310d432005-05-19 10:27:14 -04005 *
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
Junio C Hamano85023572006-12-19 14:34:12 -080011#include "git-compat-util.h"
Nicolas Pitrea310d432005-05-19 10:27:14 -040012#include "delta.h"
Martin Koegleradb7b5f2007-05-01 11:47:55 +020013#include "cache.h"
Nicolas Pitrea310d432005-05-19 10:27:14 -040014
Martin Koegleradb7b5f2007-05-01 11:47:55 +020015static const char usage_str[] =
Nicolas Pitrea310d432005-05-19 10:27:14 -040016 "test-delta (-d|-p) <from_file> <data_file> <out_file>";
17
18int main(int argc, char *argv[])
19{
20 int fd;
21 struct stat st;
22 void *from_buf, *data_buf, *out_buf;
23 unsigned long from_size, data_size, out_size;
24
25 if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
David Aguilarb9784032013-02-24 14:48:39 -080026 fprintf(stderr, "usage: %s\n", usage_str);
Nicolas Pitrea310d432005-05-19 10:27:14 -040027 return 1;
28 }
29
30 fd = open(argv[2], O_RDONLY);
31 if (fd < 0 || fstat(fd, &st)) {
32 perror(argv[2]);
33 return 1;
34 }
35 from_size = st.st_size;
36 from_buf = mmap(NULL, from_size, PROT_READ, MAP_PRIVATE, fd, 0);
37 if (from_buf == MAP_FAILED) {
38 perror(argv[2]);
Pavel Roskine35f9822005-07-29 10:49:14 -040039 close(fd);
Nicolas Pitrea310d432005-05-19 10:27:14 -040040 return 1;
41 }
42 close(fd);
43
44 fd = open(argv[3], O_RDONLY);
45 if (fd < 0 || fstat(fd, &st)) {
46 perror(argv[3]);
47 return 1;
48 }
49 data_size = st.st_size;
50 data_buf = mmap(NULL, data_size, PROT_READ, MAP_PRIVATE, fd, 0);
51 if (data_buf == MAP_FAILED) {
52 perror(argv[3]);
Pavel Roskine35f9822005-07-29 10:49:14 -040053 close(fd);
Nicolas Pitrea310d432005-05-19 10:27:14 -040054 return 1;
55 }
56 close(fd);
57
58 if (argv[1][1] == 'd')
59 out_buf = diff_delta(from_buf, from_size,
Linus Torvalds75c42d82005-06-25 19:30:20 -070060 data_buf, data_size,
Junio C Hamano3c849742005-06-29 00:32:11 -070061 &out_size, 0);
Nicolas Pitrea310d432005-05-19 10:27:14 -040062 else
63 out_buf = patch_delta(from_buf, from_size,
Linus Torvalds75c42d82005-06-25 19:30:20 -070064 data_buf, data_size,
65 &out_size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040066 if (!out_buf) {
67 fprintf(stderr, "delta operation failed (returned NULL)\n");
68 return 1;
69 }
70
71 fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
Andy Whitcroft93822c22007-01-08 15:58:23 +000072 if (fd < 0 || write_in_full(fd, out_buf, out_size) != out_size) {
Nicolas Pitrea310d432005-05-19 10:27:14 -040073 perror(argv[4]);
74 return 1;
75 }
76
77 return 0;
78}