| Nicolas Pitre | a310d43 | 2005-05-19 14:27:14 | [diff] [blame] | 1 | /* |
| 2 | * test-delta.c: test code to exercise diff-delta.c and patch-delta.c |
| 3 | * |
| 4 | * (C) 2005 Nicolas Pitre <nico@cam.org> |
| 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 <stdio.h> |
| 12 | #include <unistd.h> |
| 13 | #include <string.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include "delta.h" |
| 19 | |
| Petr Baudis | 4d1f119 | 2005-07-29 09:01:26 | [diff] [blame] | 20 | static const char usage[] = |
| Nicolas Pitre | a310d43 | 2005-05-19 14:27:14 | [diff] [blame] | 21 | "test-delta (-d|-p) <from_file> <data_file> <out_file>"; |
| 22 | |
| 23 | int main(int argc, char *argv[]) |
| 24 | { |
| 25 | int fd; |
| 26 | struct stat st; |
| 27 | void *from_buf, *data_buf, *out_buf; |
| 28 | unsigned long from_size, data_size, out_size; |
| 29 | |
| 30 | if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) { |
| 31 | fprintf(stderr, "Usage: %s\n", usage); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | fd = open(argv[2], O_RDONLY); |
| 36 | if (fd < 0 || fstat(fd, &st)) { |
| 37 | perror(argv[2]); |
| 38 | return 1; |
| 39 | } |
| 40 | from_size = st.st_size; |
| 41 | from_buf = mmap(NULL, from_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 42 | if (from_buf == MAP_FAILED) { |
| 43 | perror(argv[2]); |
| Pavel Roskin | e35f982 | 2005-07-29 14:49:14 | [diff] [blame] | 44 | close(fd); |
| Nicolas Pitre | a310d43 | 2005-05-19 14:27:14 | [diff] [blame] | 45 | return 1; |
| 46 | } |
| 47 | close(fd); |
| 48 | |
| 49 | fd = open(argv[3], O_RDONLY); |
| 50 | if (fd < 0 || fstat(fd, &st)) { |
| 51 | perror(argv[3]); |
| 52 | return 1; |
| 53 | } |
| 54 | data_size = st.st_size; |
| 55 | data_buf = mmap(NULL, data_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 56 | if (data_buf == MAP_FAILED) { |
| 57 | perror(argv[3]); |
| Pavel Roskin | e35f982 | 2005-07-29 14:49:14 | [diff] [blame] | 58 | close(fd); |
| Nicolas Pitre | a310d43 | 2005-05-19 14:27:14 | [diff] [blame] | 59 | return 1; |
| 60 | } |
| 61 | close(fd); |
| 62 | |
| 63 | if (argv[1][1] == 'd') |
| 64 | out_buf = diff_delta(from_buf, from_size, |
| Linus Torvalds | 75c42d8 | 2005-06-26 02:30:20 | [diff] [blame] | 65 | data_buf, data_size, |
| Junio C Hamano | 3c84974 | 2005-06-29 07:32:11 | [diff] [blame] | 66 | &out_size, 0); |
| Nicolas Pitre | a310d43 | 2005-05-19 14:27:14 | [diff] [blame] | 67 | else |
| 68 | out_buf = patch_delta(from_buf, from_size, |
| Linus Torvalds | 75c42d8 | 2005-06-26 02:30:20 | [diff] [blame] | 69 | data_buf, data_size, |
| 70 | &out_size); |
| Nicolas Pitre | a310d43 | 2005-05-19 14:27:14 | [diff] [blame] | 71 | if (!out_buf) { |
| 72 | fprintf(stderr, "delta operation failed (returned NULL)\n"); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 77 | if (fd < 0 || write(fd, out_buf, out_size) != out_size) { |
| 78 | perror(argv[4]); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |