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 <limits.h>
24 #include <sha1.h>
25 #include <zlib.h>
27 #include "got_repository.h"
28 #include "got_object.h"
29 #include "got_error.h"
30 #include "got_diff.h"
32 #include "diff.h"
34 static FILE *
35 opentemp(void)
36 {
37 char name[PATH_MAX];
38 int fd;
39 FILE *f;
41 if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
42 return NULL;
44 fd = mkstemp(name);
45 if (fd < 0)
46 return NULL;
48 unlink(name);
49 f = fdopen(fd, "w+");
50 if (f == NULL) {
51 close(fd);
52 return NULL;
53 }
55 return f;
56 }
58 const struct got_error *
59 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
60 const char *label1, const char *label2, FILE *outfile)
61 {
62 struct got_diff_state ds;
63 struct got_diff_args args;
64 const struct got_error *err = NULL;
65 FILE *f1 = NULL, *f2 = NULL;
66 char hex1[SHA1_DIGEST_STRING_LENGTH];
67 char hex2[SHA1_DIGEST_STRING_LENGTH];
68 char *idstr1 = NULL, *idstr2 = NULL;
69 size_t len, hdrlen;
70 int res, flags = 0;
72 if (blob1) {
73 f1 = opentemp();
74 if (f1 == NULL)
75 return got_error(GOT_ERR_FILE_OPEN);
76 } else
77 flags |= D_EMPTY1;
79 if (blob2) {
80 f2 = opentemp();
81 if (f2 == NULL) {
82 fclose(f1);
83 return got_error(GOT_ERR_FILE_OPEN);
84 }
85 } else
86 flags |= D_EMPTY2;
88 if (blob1) {
89 idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
90 hdrlen = blob1->hdrlen;
91 do {
92 err = got_object_blob_read_block(blob1, &len);
93 if (err)
94 goto done;
95 /* Skip blob object header first time around. */
96 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
97 hdrlen = 0;
98 } while (len != 0);
99 } else
100 idstr1 = "/dev/null";
102 if (blob2) {
103 idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
104 hdrlen = blob2->hdrlen;
105 do {
106 err = got_object_blob_read_block(blob2, &len);
107 if (err)
108 goto done;
109 /* Skip blob object header first time around. */
110 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
111 hdrlen = 0;
112 } while (len != 0);
113 } else
114 idstr2 = "/dev/null";
116 if (f1)
117 fflush(f1);
118 if (f2)
119 fflush(f2);
121 memset(&ds, 0, sizeof(ds));
122 /* XXX should stat buffers be passed in args instead of ds? */
123 ds.stb1.st_mode = S_IFREG;
124 if (blob1)
125 ds.stb1.st_size = blob1->zb.z.total_out;
126 ds.stb1.st_mtime = 0; /* XXX */
128 ds.stb2.st_mode = S_IFREG;
129 if (blob2)
130 ds.stb2.st_size = blob2->zb.z.total_out;
131 ds.stb2.st_mtime = 0; /* XXX */
133 memset(&args, 0, sizeof(args));
134 args.diff_format = D_UNIFIED;
135 args.label[0] = label1 ? label1 : idstr1;
136 args.label[1] = label2 ? label2 : idstr2;
138 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
139 done:
140 if (f1)
141 fclose(f1);
142 if (f2)
143 fclose(f2);
144 return err;
147 struct got_tree_entry *
148 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
150 struct got_tree_entry *te2;
152 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
153 if (strcmp(te1->name, te2->name) == 0)
154 return te2;
156 return NULL;
159 static int
160 same_id(struct got_object_id *id1, struct got_object_id *id2)
162 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
165 static const struct got_error *
166 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
168 const struct got_error *err;
169 struct got_blob_object *blob;
170 struct got_object *obj;
172 err = got_object_open(&obj, repo, id);
173 if (err)
174 return err;
175 err = got_object_blob_open(&blob, repo, obj, 8192);
176 if (err != NULL)
177 return err;
179 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
182 static const struct got_error *
183 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
184 struct got_repository *repo)
186 const struct got_error *err;
187 struct got_object *obj1 = NULL;
188 struct got_object *obj2 = NULL;
189 struct got_blob_object *blob1 = NULL;
190 struct got_blob_object *blob2 = NULL;
192 err = got_object_open(&obj1, repo, id1);
193 if (err)
194 return got_error(GOT_ERR_BAD_OBJ_HDR);
195 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
196 err = got_error(GOT_ERR_OBJ_TYPE);
197 goto done;
200 err = got_object_open(&obj2, repo, id2);
201 if (err) {
202 err= got_error(GOT_ERR_BAD_OBJ_HDR);
203 goto done;
205 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
206 err = got_error(GOT_ERR_BAD_OBJ_DATA);
207 goto done;
210 err = got_object_blob_open(&blob1, repo, obj1, 8192);
211 if (err != NULL) {
212 err = got_error(GOT_ERR_FILE_OPEN);
213 goto done;
216 err = got_object_blob_open(&blob2, repo, obj2, 8192);
217 if (err != NULL) {
218 err = got_error(GOT_ERR_FILE_OPEN);
219 goto done;
222 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
224 done:
225 if (obj1)
226 got_object_close(obj1);
227 if (obj2)
228 got_object_close(obj2);
229 if (blob1)
230 got_object_blob_close(blob1);
231 if (blob2)
232 got_object_blob_close(blob2);
233 return err;
236 static const struct got_error *
237 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
239 const struct got_error *err;
240 struct got_blob_object *blob;
241 struct got_object *obj;
243 err = got_object_open(&obj, repo, id);
244 if (err)
245 return err;
246 err = got_object_blob_open(&blob, repo, obj, 8192);
247 if (err != NULL)
248 return err;
250 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
253 static const struct got_error *
254 diff_added_tree(struct got_object_id *id, struct got_repository *repo)
256 const struct got_error *err = NULL;
257 struct got_object *treeobj = NULL;
258 struct got_tree_object *tree = NULL;
260 err = got_object_open(&treeobj, repo, id);
261 if (err)
262 goto done;
264 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
265 err = got_error(GOT_ERR_OBJ_TYPE);
266 goto done;
269 err = got_object_tree_open(&tree, repo, treeobj);
270 if (err)
271 goto done;
273 err = got_diff_tree(NULL, tree, repo);
275 done:
276 if (tree)
277 got_object_tree_close(tree);
278 if (treeobj)
279 got_object_close(treeobj);
280 return err;
283 static const struct got_error *
284 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
285 struct got_repository *repo)
287 const struct got_error *err = NULL;
288 struct got_object *treeobj1 = NULL;
289 struct got_object *treeobj2 = NULL;
290 struct got_tree_object *tree1 = NULL;
291 struct got_tree_object *tree2 = NULL;
293 err = got_object_open(&treeobj1, repo, id1);
294 if (err)
295 goto done;
297 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
298 err = got_error(GOT_ERR_OBJ_TYPE);
299 goto done;
302 err = got_object_open(&treeobj2, repo, id2);
303 if (err)
304 goto done;
306 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
307 err = got_error(GOT_ERR_OBJ_TYPE);
308 goto done;
311 err = got_object_tree_open(&tree1, repo, treeobj1);
312 if (err)
313 goto done;
315 err = got_object_tree_open(&tree2, repo, treeobj2);
316 if (err)
317 goto done;
319 err = got_diff_tree(tree1, tree2, repo);
321 done:
322 if (tree1)
323 got_object_tree_close(tree1);
324 if (tree2)
325 got_object_tree_close(tree2);
326 if (treeobj1)
327 got_object_close(treeobj1);
328 if (treeobj2)
329 got_object_close(treeobj2);
330 return err;
333 static const struct got_error *
334 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
336 const struct got_error *err = NULL;
337 struct got_object *treeobj = NULL;
338 struct got_tree_object *tree = NULL;
340 err = got_object_open(&treeobj, repo, id);
341 if (err)
342 goto done;
344 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
345 err = got_error(GOT_ERR_OBJ_TYPE);
346 goto done;
349 err = got_object_tree_open(&tree, repo, treeobj);
350 if (err)
351 goto done;
353 err = got_diff_tree(tree, NULL, repo);
355 done:
356 if (tree)
357 got_object_tree_close(tree);
358 if (treeobj)
359 got_object_close(treeobj);
360 return err;
363 static const struct got_error *
364 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
366 /* XXX TODO */
367 return NULL;
370 static const struct got_error *
371 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
372 struct got_repository *repo)
374 const struct got_error *err;
375 struct got_tree_entry *te2;
376 char hex[SHA1_DIGEST_STRING_LENGTH];
378 te2 = match_entry_by_name(te1, tree2);
379 if (te2 == NULL) {
380 if (S_ISDIR(te1->mode))
381 return diff_deleted_tree(&te1->id, repo);
382 return diff_deleted_blob(&te1->id, repo);
385 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
386 if (!same_id(&te1->id, &te2->id))
387 return diff_modified_tree(&te1->id, &te2->id, repo);
388 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
389 if (!same_id(&te1->id, &te2->id))
390 return diff_modified_blob(&te1->id, &te2->id, repo);
394 return diff_kind_mismatch(&te1->id, &te2->id);
397 static const struct got_error *
398 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
399 struct got_repository *repo)
401 const struct got_error *err;
402 struct got_tree_entry *te1;
404 te1 = match_entry_by_name(te2, tree1);
405 if (te1 != NULL) /* handled by diff_entry_old_new() */
406 return NULL;
408 if (S_ISDIR(te2->mode))
409 return diff_added_tree(&te2->id, repo);
410 return diff_added_blob(&te2->id, repo);
413 const struct got_error *
414 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
415 struct got_repository *repo)
417 const struct got_error *err = NULL;
418 struct got_tree_entry *te1 = NULL;
419 struct got_tree_entry *te2 = NULL;
421 if (tree1)
422 te1 = SIMPLEQ_FIRST(&tree1->entries);
423 if (tree2)
424 te2 = SIMPLEQ_FIRST(&tree2->entries);
426 do {
427 if (te1) {
428 err = diff_entry_old_new(te1, tree2, repo);
429 if (err)
430 break;
433 if (te2) {
434 err = diff_entry_new_old(te2, tree1, repo);
435 if (err)
436 break;
439 if (te1)
440 te1 = SIMPLEQ_NEXT(te1, entry);
441 if (te2)
442 te2 = SIMPLEQ_NEXT(te2, entry);
443 } while (te1 || te2);
445 return err;