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_object.h"
28 #include "got_repository.h"
29 #include "got_error.h"
30 #include "got_diff.h"
31 #include "got_opentemp.h"
32 #include "got_path.h"
34 #include "got_lib_diff.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 static const struct got_error *
40 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
41 const char *label1, const char *label2, int diff_context, FILE *outfile,
42 struct got_diff_changes *changes)
43 {
44 struct got_diff_state ds;
45 struct got_diff_args args;
46 const struct got_error *err = NULL;
47 FILE *f1 = NULL, *f2 = NULL;
48 char hex1[SHA1_DIGEST_STRING_LENGTH];
49 char hex2[SHA1_DIGEST_STRING_LENGTH];
50 char *idstr1 = NULL, *idstr2 = NULL;
51 size_t size1, size2;
52 int res, flags = 0;
54 if (blob1) {
55 f1 = got_opentemp();
56 if (f1 == NULL)
57 return got_error_from_errno("got_opentemp");
58 } else
59 flags |= D_EMPTY1;
61 if (blob2) {
62 f2 = got_opentemp();
63 if (f2 == NULL) {
64 err = got_error_from_errno("got_opentemp");
65 fclose(f1);
66 return err;
67 }
68 } else
69 flags |= D_EMPTY2;
71 size1 = 0;
72 if (blob1) {
73 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
74 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
75 blob1);
76 if (err)
77 goto done;
78 } else
79 idstr1 = "/dev/null";
81 size2 = 0;
82 if (blob2) {
83 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
84 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
85 blob2);
86 if (err)
87 goto done;
88 } else
89 idstr2 = "/dev/null";
91 memset(&ds, 0, sizeof(ds));
92 /* XXX should stat buffers be passed in args instead of ds? */
93 ds.stb1.st_mode = S_IFREG;
94 if (blob1)
95 ds.stb1.st_size = size1;
96 ds.stb1.st_mtime = 0; /* XXX */
98 ds.stb2.st_mode = S_IFREG;
99 if (blob2)
100 ds.stb2.st_size = size2;
101 ds.stb2.st_mtime = 0; /* XXX */
103 memset(&args, 0, sizeof(args));
104 args.diff_format = D_UNIFIED;
105 args.label[0] = label1 ? label1 : idstr1;
106 args.label[1] = label2 ? label2 : idstr2;
107 args.diff_context = diff_context;
108 flags |= D_PROTOTYPE;
110 if (outfile) {
111 fprintf(outfile, "blob - %s\n", idstr1);
112 fprintf(outfile, "blob + %s\n", idstr2);
114 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
115 got_diff_state_free(&ds);
116 done:
117 if (f1 && fclose(f1) != 0 && err == NULL)
118 err = got_error_from_errno("fclose");
119 if (f2 && fclose(f2) != 0 && err == NULL)
120 err = got_error_from_errno("fclose");
121 return err;
124 const struct got_error *
125 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
126 struct got_blob_object *blob2, struct got_object_id *id1,
127 struct got_object_id *id2, const char *label1, const char *label2,
128 struct got_repository *repo)
130 struct got_diff_blob_output_unidiff_arg *a = arg;
132 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
133 a->outfile, NULL);
136 const struct got_error *
137 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
138 const char *label1, const char *label2, int diff_context, FILE *outfile)
140 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
141 NULL);
144 static const struct got_error *
145 alloc_changes(struct got_diff_changes **changes)
147 *changes = calloc(1, sizeof(**changes));
148 if (*changes == NULL)
149 return got_error_from_errno("calloc");
150 SIMPLEQ_INIT(&(*changes)->entries);
151 return NULL;
154 static const struct got_error *
155 diff_blob_file(struct got_diff_changes **changes,
156 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
157 const char *label2, int diff_context, FILE *outfile)
159 struct got_diff_state ds;
160 struct got_diff_args args;
161 const struct got_error *err = NULL;
162 FILE *f1 = NULL;
163 char hex1[SHA1_DIGEST_STRING_LENGTH];
164 char *idstr1 = NULL;
165 size_t size1;
166 int res, flags = 0;
168 if (changes)
169 *changes = NULL;
171 size1 = 0;
172 if (blob1) {
173 f1 = got_opentemp();
174 if (f1 == NULL)
175 return got_error_from_errno("got_opentemp");
176 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
177 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
178 blob1);
179 if (err)
180 goto done;
181 } else {
182 flags |= D_EMPTY1;
183 idstr1 = "/dev/null";
186 if (f2 == NULL)
187 flags |= D_EMPTY2;
189 memset(&ds, 0, sizeof(ds));
190 /* XXX should stat buffers be passed in args instead of ds? */
191 ds.stb1.st_mode = S_IFREG;
192 if (blob1)
193 ds.stb1.st_size = size1;
194 ds.stb1.st_mtime = 0; /* XXX */
196 ds.stb2.st_mode = S_IFREG;
197 ds.stb2.st_size = size2;
198 ds.stb2.st_mtime = 0; /* XXX */
200 memset(&args, 0, sizeof(args));
201 args.diff_format = D_UNIFIED;
202 args.label[0] = label2;
203 args.label[1] = label2;
204 args.diff_context = diff_context;
205 flags |= D_PROTOTYPE;
207 if (outfile) {
208 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
209 fprintf(outfile, "file + %s\n",
210 f2 == NULL ? "/dev/null" : label2);
212 if (changes) {
213 err = alloc_changes(changes);
214 if (err)
215 return err;
217 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
218 changes ? *changes : NULL);
219 got_diff_state_free(&ds);
220 done:
221 if (f1 && fclose(f1) != 0 && err == NULL)
222 err = got_error_from_errno("fclose");
223 return err;
226 const struct got_error *
227 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
228 FILE *f2, size_t size2, const char *label2, int diff_context,
229 FILE *outfile)
231 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
232 diff_context, outfile);
235 const struct got_error *
236 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
237 struct got_blob_object *blob1, FILE *f2, size_t size2)
239 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
240 0, NULL);
243 const struct got_error *
244 got_diff_blob_lines_changed(struct got_diff_changes **changes,
245 struct got_blob_object *blob1, struct got_blob_object *blob2)
247 const struct got_error *err = NULL;
249 err = alloc_changes(changes);
250 if (err)
251 return err;
253 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
254 if (err) {
255 got_diff_free_changes(*changes);
256 *changes = NULL;
258 return err;
261 void
262 got_diff_free_changes(struct got_diff_changes *changes)
264 struct got_diff_change *change;
265 while (!SIMPLEQ_EMPTY(&changes->entries)) {
266 change = SIMPLEQ_FIRST(&changes->entries);
267 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
268 free(change);
270 free(changes);
273 static const struct got_error *
274 diff_added_blob(struct got_object_id *id, const char *label,
275 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
277 const struct got_error *err;
278 struct got_blob_object *blob = NULL;
279 struct got_object *obj = NULL;
281 err = got_object_open(&obj, repo, id);
282 if (err)
283 return err;
285 err = got_object_blob_open(&blob, repo, obj, 8192);
286 if (err)
287 goto done;
288 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
289 done:
290 got_object_close(obj);
291 if (blob)
292 got_object_blob_close(blob);
293 return err;
296 static const struct got_error *
297 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
298 const char *label1, const char *label2, struct got_repository *repo,
299 got_diff_blob_cb cb, void *cb_arg)
301 const struct got_error *err;
302 struct got_object *obj1 = NULL;
303 struct got_object *obj2 = NULL;
304 struct got_blob_object *blob1 = NULL;
305 struct got_blob_object *blob2 = NULL;
307 err = got_object_open(&obj1, repo, id1);
308 if (err)
309 return err;
310 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
311 err = got_error(GOT_ERR_OBJ_TYPE);
312 goto done;
315 err = got_object_open(&obj2, repo, id2);
316 if (err)
317 goto done;
318 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
320 goto done;
323 err = got_object_blob_open(&blob1, repo, obj1, 8192);
324 if (err)
325 goto done;
327 err = got_object_blob_open(&blob2, repo, obj2, 8192);
328 if (err)
329 goto done;
331 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
332 done:
333 if (obj1)
334 got_object_close(obj1);
335 if (obj2)
336 got_object_close(obj2);
337 if (blob1)
338 got_object_blob_close(blob1);
339 if (blob2)
340 got_object_blob_close(blob2);
341 return err;
344 static const struct got_error *
345 diff_deleted_blob(struct got_object_id *id, const char *label,
346 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
348 const struct got_error *err;
349 struct got_blob_object *blob = NULL;
350 struct got_object *obj = NULL;
352 err = got_object_open(&obj, repo, id);
353 if (err)
354 return err;
356 err = got_object_blob_open(&blob, repo, obj, 8192);
357 if (err)
358 goto done;
359 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
360 done:
361 got_object_close(obj);
362 if (blob)
363 got_object_blob_close(blob);
364 return err;
367 static const struct got_error *
368 diff_added_tree(struct got_object_id *id, const char *label,
369 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
370 int diff_content)
372 const struct got_error *err = NULL;
373 struct got_object *treeobj = NULL;
374 struct got_tree_object *tree = NULL;
376 err = got_object_open(&treeobj, repo, id);
377 if (err)
378 goto done;
380 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
381 err = got_error(GOT_ERR_OBJ_TYPE);
382 goto done;
385 err = got_object_tree_open(&tree, repo, treeobj);
386 if (err)
387 goto done;
389 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
390 diff_content);
391 done:
392 if (tree)
393 got_object_tree_close(tree);
394 if (treeobj)
395 got_object_close(treeobj);
396 return err;
399 static const struct got_error *
400 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
401 const char *label1, const char *label2, struct got_repository *repo,
402 got_diff_blob_cb cb, void *cb_arg, int diff_content)
404 const struct got_error *err;
405 struct got_object *treeobj1 = NULL;
406 struct got_object *treeobj2 = NULL;
407 struct got_tree_object *tree1 = NULL;
408 struct got_tree_object *tree2 = NULL;
410 err = got_object_open(&treeobj1, repo, id1);
411 if (err)
412 goto done;
414 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
415 err = got_error(GOT_ERR_OBJ_TYPE);
416 goto done;
419 err = got_object_open(&treeobj2, repo, id2);
420 if (err)
421 goto done;
423 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
424 err = got_error(GOT_ERR_OBJ_TYPE);
425 goto done;
428 err = got_object_tree_open(&tree1, repo, treeobj1);
429 if (err)
430 goto done;
432 err = got_object_tree_open(&tree2, repo, treeobj2);
433 if (err)
434 goto done;
436 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
437 diff_content);
439 done:
440 if (tree1)
441 got_object_tree_close(tree1);
442 if (tree2)
443 got_object_tree_close(tree2);
444 if (treeobj1)
445 got_object_close(treeobj1);
446 if (treeobj2)
447 got_object_close(treeobj2);
448 return err;
451 static const struct got_error *
452 diff_deleted_tree(struct got_object_id *id, const char *label,
453 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
454 int diff_content)
456 const struct got_error *err;
457 struct got_object *treeobj = NULL;
458 struct got_tree_object *tree = NULL;
460 err = got_object_open(&treeobj, repo, id);
461 if (err)
462 goto done;
464 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
465 err = got_error(GOT_ERR_OBJ_TYPE);
466 goto done;
469 err = got_object_tree_open(&tree, repo, treeobj);
470 if (err)
471 goto done;
473 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
474 diff_content);
475 done:
476 if (tree)
477 got_object_tree_close(tree);
478 if (treeobj)
479 got_object_close(treeobj);
480 return err;
483 static const struct got_error *
484 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
485 const char *label1, const char *label2, struct got_repository *repo,
486 got_diff_blob_cb cb, void *cb_arg)
488 /* XXX TODO */
489 return NULL;
492 static const struct got_error *
493 diff_entry_old_new(const struct got_tree_entry *te1,
494 const struct got_tree_entry *te2, const char *label1, const char *label2,
495 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
496 int diff_content)
498 const struct got_error *err = NULL;
499 int id_match;
501 if (got_object_tree_entry_is_submodule(te1))
502 return NULL;
504 if (te2 == NULL) {
505 if (S_ISDIR(te1->mode))
506 err = diff_deleted_tree(te1->id, label1, repo,
507 cb, cb_arg, diff_content);
508 else {
509 if (diff_content)
510 err = diff_deleted_blob(te1->id, label1, repo,
511 cb, cb_arg);
512 else
513 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
514 label1, NULL, repo);
516 return err;
517 } else if (got_object_tree_entry_is_submodule(te2))
518 return NULL;
520 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
521 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
522 if (!id_match)
523 return diff_modified_tree(te1->id, te2->id,
524 label1, label2, repo, cb, cb_arg, diff_content);
525 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
526 if (!id_match) {
527 if (diff_content)
528 return diff_modified_blob(te1->id, te2->id,
529 label1, label2, repo, cb, cb_arg);
530 else
531 return cb(cb_arg, NULL, NULL, te1->id,
532 te2->id, label1, label2, repo);
536 if (id_match)
537 return NULL;
539 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
540 cb, cb_arg);
543 static const struct got_error *
544 diff_entry_new_old(const struct got_tree_entry *te2,
545 const struct got_tree_entry *te1, const char *label2,
546 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
547 int diff_content)
549 if (te1 != NULL) /* handled by diff_entry_old_new() */
550 return NULL;
552 if (got_object_tree_entry_is_submodule(te2))
553 return NULL;
555 if (S_ISDIR(te2->mode))
556 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
557 diff_content);
559 if (diff_content)
560 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
562 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
565 const struct got_error *
566 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
567 const char *label1, const char *label2, struct got_repository *repo,
568 got_diff_blob_cb cb, void *cb_arg, int diff_content)
570 const struct got_error *err = NULL;
571 struct got_tree_entry *te1 = NULL;
572 struct got_tree_entry *te2 = NULL;
573 char *l1 = NULL, *l2 = NULL;
575 if (tree1) {
576 const struct got_tree_entries *entries;
577 entries = got_object_tree_get_entries(tree1);
578 te1 = SIMPLEQ_FIRST(&entries->head);
579 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
580 te1->name) == -1)
581 return got_error_from_errno("asprintf");
583 if (tree2) {
584 const struct got_tree_entries *entries;
585 entries = got_object_tree_get_entries(tree2);
586 te2 = SIMPLEQ_FIRST(&entries->head);
587 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
588 te2->name) == -1)
589 return got_error_from_errno("asprintf");
592 do {
593 if (te1) {
594 const struct got_tree_entry *te = NULL;
595 if (tree2)
596 te = got_object_tree_find_entry(tree2,
597 te1->name);
598 if (te) {
599 free(l2);
600 l2 = NULL;
601 if (te && asprintf(&l2, "%s%s%s", label2,
602 label2[0] ? "/" : "", te->name) == -1)
603 return
604 got_error_from_errno("asprintf");
606 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
607 cb_arg, diff_content);
608 if (err)
609 break;
612 if (te2) {
613 const struct got_tree_entry *te = NULL;
614 if (tree1)
615 te = got_object_tree_find_entry(tree1,
616 te2->name);
617 free(l2);
618 if (te) {
619 if (asprintf(&l2, "%s%s%s", label2,
620 label2[0] ? "/" : "", te->name) == -1)
621 return
622 got_error_from_errno("asprintf");
623 } else {
624 if (asprintf(&l2, "%s%s%s", label2,
625 label2[0] ? "/" : "", te2->name) == -1)
626 return
627 got_error_from_errno("asprintf");
629 err = diff_entry_new_old(te2, te, l2, repo,
630 cb, cb_arg, diff_content);
631 if (err)
632 break;
635 free(l1);
636 l1 = NULL;
637 if (te1) {
638 te1 = SIMPLEQ_NEXT(te1, entry);
639 if (te1 &&
640 asprintf(&l1, "%s%s%s", label1,
641 label1[0] ? "/" : "", te1->name) == -1)
642 return got_error_from_errno("asprintf");
644 free(l2);
645 l2 = NULL;
646 if (te2) {
647 te2 = SIMPLEQ_NEXT(te2, entry);
648 if (te2 &&
649 asprintf(&l2, "%s%s%s", label2,
650 label2[0] ? "/" : "", te2->name) == -1)
651 return got_error_from_errno("asprintf");
653 } while (te1 || te2);
655 return err;
658 const struct got_error *
659 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
660 const char *label1, const char *label2, int diff_context,
661 struct got_repository *repo, FILE *outfile)
663 const struct got_error *err;
664 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
666 if (id1 == NULL && id2 == NULL)
667 return got_error(GOT_ERR_NO_OBJ);
669 if (id1) {
670 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
671 if (err)
672 goto done;
674 if (id2) {
675 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
676 if (err)
677 goto done;
679 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
680 outfile);
681 done:
682 if (blob1)
683 got_object_blob_close(blob1);
684 if (blob2)
685 got_object_blob_close(blob2);
686 return err;
689 const struct got_error *
690 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
691 char *label1, char *label2, int diff_context, struct got_repository *repo,
692 FILE *outfile)
694 const struct got_error *err;
695 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
696 struct got_diff_blob_output_unidiff_arg arg;
698 if (id1 == NULL && id2 == NULL)
699 return got_error(GOT_ERR_NO_OBJ);
701 if (id1) {
702 err = got_object_open_as_tree(&tree1, repo, id1);
703 if (err)
704 goto done;
706 if (id2) {
707 err = got_object_open_as_tree(&tree2, repo, id2);
708 if (err)
709 goto done;
711 arg.diff_context = diff_context;
712 arg.outfile = outfile;
713 err = got_diff_tree(tree1, tree2, label1, label2, repo,
714 got_diff_blob_output_unidiff, &arg, 1);
715 done:
716 if (tree1)
717 got_object_tree_close(tree1);
718 if (tree2)
719 got_object_tree_close(tree2);
720 return err;
723 const struct got_error *
724 got_diff_objects_as_commits(struct got_object_id *id1,
725 struct got_object_id *id2, int diff_context,
726 struct got_repository *repo, FILE *outfile)
728 const struct got_error *err;
729 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
731 if (id2 == NULL)
732 return got_error(GOT_ERR_NO_OBJ);
734 if (id1) {
735 err = got_object_open_as_commit(&commit1, repo, id1);
736 if (err)
737 goto done;
740 err = got_object_open_as_commit(&commit2, repo, id2);
741 if (err)
742 goto done;
744 err = got_diff_objects_as_trees(
745 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
746 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
747 outfile);
748 done:
749 if (commit1)
750 got_object_commit_close(commit1);
751 if (commit2)
752 got_object_commit_close(commit2);
753 return err;
756 const struct got_error *
757 got_diff_files(struct got_diff_changes **changes,
758 struct got_diff_state **ds,
759 struct got_diff_args **args,
760 int *flags,
761 FILE *f1, size_t size1, const char *label1,
762 FILE *f2, size_t size2, const char *label2,
763 int diff_context, FILE *outfile)
765 const struct got_error *err = NULL;
766 int res;
768 *flags = 0;
769 *ds = calloc(1, sizeof(**ds));
770 if (*ds == NULL)
771 return got_error_from_errno("calloc");
772 *args = calloc(1, sizeof(**args));
773 if (*args == NULL) {
774 err = got_error_from_errno("calloc");
775 goto done;
778 if (changes)
779 *changes = NULL;
781 if (f1 == NULL)
782 *flags |= D_EMPTY1;
784 if (f2 == NULL)
785 *flags |= D_EMPTY2;
787 /* XXX should stat buffers be passed in args instead of ds? */
788 (*ds)->stb1.st_mode = S_IFREG;
789 (*ds)->stb1.st_size = size1;
790 (*ds)->stb1.st_mtime = 0; /* XXX */
792 (*ds)->stb2.st_mode = S_IFREG;
793 (*ds)->stb2.st_size = size2;
794 (*ds)->stb2.st_mtime = 0; /* XXX */
796 (*args)->diff_format = D_UNIFIED;
797 (*args)->label[0] = label1;
798 (*args)->label[1] = label2;
799 (*args)->diff_context = diff_context;
800 *flags |= D_PROTOTYPE;
802 if (outfile) {
803 fprintf(outfile, "file - %s\n",
804 f1 == NULL ? "/dev/null" : label1);
805 fprintf(outfile, "file + %s\n",
806 f2 == NULL ? "/dev/null" : label2);
808 if (changes) {
809 err = alloc_changes(changes);
810 if (err)
811 goto done;
813 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
814 changes ? *changes : NULL);
815 done:
816 if (err) {
817 if (*ds) {
818 got_diff_state_free(*ds);
819 free(*ds);
820 *ds = NULL;
822 if (*args) {
823 free(*args);
824 *args = NULL;
826 if (changes) {
827 if (*changes)
828 got_diff_free_changes(*changes);
829 *changes = NULL;
832 return err;