Blob


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