blob: e749a49c88e66e4b3ce388b2c0762d36d4090f99 [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
Nguyễn Thái Ngọc Duy9153dde2018-03-24 08:44:37 +010011#include "test-tool.h"
Junio C Hamano85023572006-12-19 14:34:12 -080012#include "git-compat-util.h"
Nicolas Pitrea310d432005-05-19 10:27:14 -040013#include "delta.h"
Martin Koegleradb7b5f2007-05-01 11:47:55 +020014#include "cache.h"
Nicolas Pitrea310d432005-05-19 10:27:14 -040015
Martin Koegleradb7b5f2007-05-01 11:47:55 +020016static const char usage_str[] =
Nguyễn Thái Ngọc Duy9153dde2018-03-24 08:44:37 +010017 "test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
Nicolas Pitrea310d432005-05-19 10:27:14 -040018
Nguyễn Thái Ngọc Duy9153dde2018-03-24 08:44:37 +010019int cmd__delta(int argc, const char **argv)
Nicolas Pitrea310d432005-05-19 10:27:14 -040020{
21 int fd;
22 struct stat st;
23 void *from_buf, *data_buf, *out_buf;
24 unsigned long from_size, data_size, out_size;
25
26 if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
David Aguilarb9784032013-02-24 14:48:39 -080027 fprintf(stderr, "usage: %s\n", usage_str);
Nicolas Pitrea310d432005-05-19 10:27:14 -040028 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;
Jeff Kingd65930c2018-08-30 03:07:52 -040037 from_buf = xmalloc(from_size);
38 if (read_in_full(fd, from_buf, from_size) < 0) {
Nicolas Pitrea310d432005-05-19 10:27:14 -040039 perror(argv[2]);
Pavel Roskine35f9822005-07-29 10:49:14 -040040 close(fd);
Nicolas Pitrea310d432005-05-19 10:27:14 -040041 return 1;
42 }
43 close(fd);
44
45 fd = open(argv[3], O_RDONLY);
46 if (fd < 0 || fstat(fd, &st)) {
47 perror(argv[3]);
48 return 1;
49 }
50 data_size = st.st_size;
Jeff Kingd65930c2018-08-30 03:07:52 -040051 data_buf = xmalloc(data_size);
52 if (read_in_full(fd, data_buf, data_size) < 0) {
Nicolas Pitrea310d432005-05-19 10:27:14 -040053 perror(argv[3]);
Pavel Roskine35f9822005-07-29 10:49:14 -040054 close(fd);
Nicolas Pitrea310d432005-05-19 10:27:14 -040055 return 1;
56 }
57 close(fd);
58
59 if (argv[1][1] == 'd')
60 out_buf = diff_delta(from_buf, from_size,
Linus Torvalds75c42d82005-06-25 19:30:20 -070061 data_buf, data_size,
Junio C Hamano3c849742005-06-29 00:32:11 -070062 &out_size, 0);
Nicolas Pitrea310d432005-05-19 10:27:14 -040063 else
64 out_buf = patch_delta(from_buf, from_size,
Linus Torvalds75c42d82005-06-25 19:30:20 -070065 data_buf, data_size,
66 &out_size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040067 if (!out_buf) {
68 fprintf(stderr, "delta operation failed (returned NULL)\n");
69 return 1;
70 }
71
72 fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
Jeff King06f46f22017-09-13 13:16:03 -040073 if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
Nicolas Pitrea310d432005-05-19 10:27:14 -040074 perror(argv[4]);
75 return 1;
76 }
77
78 return 0;
79}