Blame


1 7d283eee 2017-11-29 stsp /*
2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7d283eee 2017-11-29 stsp *
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
7 7d283eee 2017-11-29 stsp *
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7d283eee 2017-11-29 stsp */
16 7d283eee 2017-11-29 stsp
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 7d283eee 2017-11-29 stsp
19 7d283eee 2017-11-29 stsp #include <stdio.h>
20 7d283eee 2017-11-29 stsp #include <stdlib.h>
21 7d283eee 2017-11-29 stsp #include <string.h>
22 7d283eee 2017-11-29 stsp #include <sha1.h>
23 7d283eee 2017-11-29 stsp #include <zlib.h>
24 7d283eee 2017-11-29 stsp
25 7d283eee 2017-11-29 stsp #include "got_repository.h"
26 7d283eee 2017-11-29 stsp #include "got_object.h"
27 7d283eee 2017-11-29 stsp #include "got_error.h"
28 7d283eee 2017-11-29 stsp
29 7d283eee 2017-11-29 stsp #include "diff.h"
30 7d283eee 2017-11-29 stsp
31 7d283eee 2017-11-29 stsp static const struct got_error *
32 7d283eee 2017-11-29 stsp open_tempfile(FILE **sfp, char **sfn)
33 7d283eee 2017-11-29 stsp {
34 7d283eee 2017-11-29 stsp static const int sfnlen = 20;
35 7d283eee 2017-11-29 stsp int fd;
36 7d283eee 2017-11-29 stsp
37 ed9e98a8 2017-11-29 stsp *sfn = calloc(sfnlen, sizeof(char));
38 7d283eee 2017-11-29 stsp if (*sfn == NULL)
39 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_MEM);
40 7d283eee 2017-11-29 stsp strlcpy(*sfn, "/tmp/got.XXXXXXXXXX", sfnlen);
41 7d283eee 2017-11-29 stsp if ((fd = mkstemp(*sfn)) == -1 ||
42 7d283eee 2017-11-29 stsp ((*sfp) = fdopen(fd, "w+")) == NULL) {
43 7d283eee 2017-11-29 stsp if (fd != -1) {
44 7d283eee 2017-11-29 stsp unlink(*sfn);
45 7d283eee 2017-11-29 stsp close(fd);
46 7d283eee 2017-11-29 stsp }
47 7d283eee 2017-11-29 stsp free(*sfn);
48 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_FILE_OPEN);
49 7d283eee 2017-11-29 stsp }
50 7d283eee 2017-11-29 stsp return NULL;
51 7d283eee 2017-11-29 stsp }
52 7d283eee 2017-11-29 stsp
53 7d283eee 2017-11-29 stsp const struct got_error *
54 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
55 7d283eee 2017-11-29 stsp FILE *outfile)
56 7d283eee 2017-11-29 stsp {
57 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
58 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
59 7d283eee 2017-11-29 stsp FILE *f1, *f2;
60 7d283eee 2017-11-29 stsp char *n1, *n2;
61 7d283eee 2017-11-29 stsp size_t len, hdrlen;
62 7d283eee 2017-11-29 stsp int res;
63 7d283eee 2017-11-29 stsp
64 7d283eee 2017-11-29 stsp err = open_tempfile(&f1, &n1);
65 7d283eee 2017-11-29 stsp if (err != NULL)
66 7d283eee 2017-11-29 stsp return err;
67 7d283eee 2017-11-29 stsp
68 7d283eee 2017-11-29 stsp err = open_tempfile(&f2, &n2);
69 7d283eee 2017-11-29 stsp if (err != NULL) {
70 7d283eee 2017-11-29 stsp fclose(f1);
71 7d283eee 2017-11-29 stsp free(n1);
72 7d283eee 2017-11-29 stsp return err;
73 7d283eee 2017-11-29 stsp }
74 7d283eee 2017-11-29 stsp
75 7d283eee 2017-11-29 stsp
76 7d283eee 2017-11-29 stsp hdrlen = blob1->hdrlen;
77 7d283eee 2017-11-29 stsp do {
78 7d283eee 2017-11-29 stsp err = got_object_blob_read_block(blob1, &len);
79 7d283eee 2017-11-29 stsp if (err)
80 7d283eee 2017-11-29 stsp goto done;
81 7d283eee 2017-11-29 stsp /* Skip blob object header first time around. */
82 7d283eee 2017-11-29 stsp fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
83 7d283eee 2017-11-29 stsp hdrlen = 0;
84 7d283eee 2017-11-29 stsp } while (len != 0);
85 7d283eee 2017-11-29 stsp
86 7d283eee 2017-11-29 stsp hdrlen = blob2->hdrlen;
87 7d283eee 2017-11-29 stsp do {
88 7d283eee 2017-11-29 stsp err = got_object_blob_read_block(blob2, &len);
89 7d283eee 2017-11-29 stsp if (err)
90 7d283eee 2017-11-29 stsp goto done;
91 7d283eee 2017-11-29 stsp /* Skip blob object header first time around. */
92 7d283eee 2017-11-29 stsp fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
93 7d283eee 2017-11-29 stsp hdrlen = 0;
94 7d283eee 2017-11-29 stsp } while (len != 0);
95 7d283eee 2017-11-29 stsp
96 7d283eee 2017-11-29 stsp fflush(f1);
97 7d283eee 2017-11-29 stsp fflush(f2);
98 7d283eee 2017-11-29 stsp
99 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
100 ed9e98a8 2017-11-29 stsp err = got_diffreg(&res, n1, n2, 0, &ds);
101 7d283eee 2017-11-29 stsp done:
102 7d283eee 2017-11-29 stsp unlink(n1);
103 7d283eee 2017-11-29 stsp unlink(n2);
104 7d283eee 2017-11-29 stsp fclose(f1);
105 7d283eee 2017-11-29 stsp fclose(f2);
106 7d283eee 2017-11-29 stsp free(n1);
107 7d283eee 2017-11-29 stsp free(n2);
108 7d283eee 2017-11-29 stsp return err;
109 7d283eee 2017-11-29 stsp }