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;
123 static const struct got_error *
124 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
125 struct got_tree_object *tree2)
127 *te = NULL;
128 return NULL;
131 static int
132 same_id(struct got_object_id *id1, struct got_object_id *id2)
134 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
137 static const struct got_error *
138 diff_added_blob(struct got_object_id *id)
140 return NULL;
143 static const struct got_error *
144 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2)
146 return NULL;
149 static const struct got_error *
150 diff_deleted_blob(struct got_object_id *id)
152 return NULL;
155 static const struct got_error *
156 diff_added_tree(struct got_object_id *id)
158 return NULL;
161 static const struct got_error *
162 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
164 return NULL;
167 static const struct got_error *
168 diff_deleted_tree(struct got_object_id *id)
170 return NULL;
173 static const struct got_error *
174 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
176 return NULL;
179 static const struct got_error *
180 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2)
182 const struct got_error *err;
183 struct got_tree_entry *te2;
185 err = match_entry_by_name(&te2, te1, tree2);
186 if (err)
187 return err;
188 if (te2 == NULL) {
189 if (S_ISDIR(te1->mode))
190 return diff_deleted_tree(&te1->id);
191 return diff_deleted_blob(&te1->id);
194 if (S_ISDIR(te1->mode) == S_ISDIR(te2->mode)) {
195 if (!same_id(&te1->id, &te2->id))
196 return diff_modified_tree(&te1->id, &te2->id);
197 } else if (S_ISREG(te1->mode) == S_ISREG(te2->mode)) {
198 if (!same_id(&te1->id, &te2->id))
199 return diff_modified_blob(&te1->id, &te2->id);
200 } else
201 return diff_kind_mismatch(&te1->id, &te2->id);
203 return NULL;
206 static const struct got_error *
207 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1)
209 const struct got_error *err;
210 struct got_tree_entry *te1;
212 err = match_entry_by_name(&te1, te2, tree1);
213 if (err)
214 return err;
215 if (te1 != NULL) /* handled by diff_entry_old_new() */
216 return NULL;
218 if (S_ISDIR(te2->mode))
219 return diff_added_tree(&te2->id);
220 return diff_added_blob(&te2->id);
223 const struct got_error *
224 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
225 struct got_repository *repo)
227 const struct got_error *err = NULL;
228 struct got_tree_entry *te1;
229 struct got_tree_entry *te2;
231 if (tree1->nentries == 0 && tree2->nentries == 0)
232 return NULL;
234 te1 = SIMPLEQ_FIRST(&tree1->entries);
235 te2 = SIMPLEQ_FIRST(&tree2->entries);
237 do {
238 if (te1) {
239 err = diff_entry_old_new(te1, tree2);
240 if (err)
241 break;
244 if (te2) {
245 err = diff_entry_new_old(te2, tree1);
246 if (err)
247 break;
250 if (te1)
251 te1 = SIMPLEQ_NEXT(te1, entry);
252 if (te2)
253 te2 = SIMPLEQ_NEXT(te2, entry);
254 } while (te1 || te2);
256 return err;