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 "got_lib_diff.h"
33 #include "got_lib_path.h"
35 const struct got_error *
36 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
37 const char *label1, const char *label2, FILE *outfile)
38 {
39 struct got_diff_state ds;
40 struct got_diff_args args;
41 const struct got_error *err = NULL;
42 FILE *f1 = NULL, *f2 = NULL;
43 char hex1[SHA1_DIGEST_STRING_LENGTH];
44 char hex2[SHA1_DIGEST_STRING_LENGTH];
45 char *idstr1 = NULL, *idstr2 = NULL;
46 size_t len, hdrlen;
47 size_t size1, size2;
48 int res, flags = 0;
50 if (blob1) {
51 f1 = got_opentemp();
52 if (f1 == NULL)
53 return got_error(GOT_ERR_FILE_OPEN);
54 } else
55 flags |= D_EMPTY1;
57 if (blob2) {
58 f2 = got_opentemp();
59 if (f2 == NULL) {
60 fclose(f1);
61 return got_error(GOT_ERR_FILE_OPEN);
62 }
63 } else
64 flags |= D_EMPTY2;
66 size1 = 0;
67 if (blob1) {
68 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
69 hdrlen = got_object_blob_get_hdrlen(blob1);
70 do {
71 err = got_object_blob_read_block(&len, blob1);
72 if (err)
73 goto done;
74 if (len == 0)
75 break;
76 size1 += len;
77 /* Skip blob object header first time around. */
78 fwrite(got_object_blob_get_read_buf(blob1) + hdrlen,
79 len - hdrlen, 1, f1);
80 hdrlen = 0;
81 } while (len != 0);
82 } else
83 idstr1 = "/dev/null";
85 size2 = 0;
86 if (blob2) {
87 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
88 hdrlen = got_object_blob_get_hdrlen(blob2);
89 do {
90 err = got_object_blob_read_block(&len, blob2);
91 if (err)
92 goto done;
93 if (len == 0)
94 break;
95 size2 += len;
96 /* Skip blob object header first time around. */
97 fwrite(got_object_blob_get_read_buf(blob2) + hdrlen,
98 len - hdrlen, 1, f2);
99 hdrlen = 0;
100 } while (len != 0);
101 } else
102 idstr2 = "/dev/null";
104 if (f1) {
105 fflush(f1);
106 rewind(f1);
108 if (f2) {
109 fflush(f2);
110 rewind(f2);
113 memset(&ds, 0, sizeof(ds));
114 /* XXX should stat buffers be passed in args instead of ds? */
115 ds.stb1.st_mode = S_IFREG;
116 if (blob1)
117 ds.stb1.st_size = size1;
118 ds.stb1.st_mtime = 0; /* XXX */
120 ds.stb2.st_mode = S_IFREG;
121 if (blob2)
122 ds.stb2.st_size = size2;
123 ds.stb2.st_mtime = 0; /* XXX */
125 memset(&args, 0, sizeof(args));
126 args.diff_format = D_UNIFIED;
127 args.label[0] = label1 ? label1 : idstr1;
128 args.label[1] = label2 ? label2 : idstr2;
129 args.diff_context = 3;
130 flags |= D_PROTOTYPE;
132 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
133 done:
134 if (f1)
135 fclose(f1);
136 if (f2)
137 fclose(f2);
138 return err;
141 struct got_tree_entry *
142 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
144 struct got_tree_entry *te2;
146 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
147 if (strcmp(te1->name, te2->name) == 0)
148 return te2;
150 return NULL;
153 static const struct got_error *
154 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
155 FILE *outfile)
157 const struct got_error *err;
158 struct got_blob_object *blob = NULL;
159 struct got_object *obj = NULL;
161 err = got_object_open(&obj, repo, id);
162 if (err)
163 return err;
165 err = got_object_blob_open(&blob, repo, obj, 8192);
166 if (err)
167 goto done;
168 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
169 done:
170 got_object_close(obj);
171 if (blob)
172 got_object_blob_close(blob);
173 return err;
176 static const struct got_error *
177 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
178 struct got_repository *repo, FILE *outfile)
180 const struct got_error *err;
181 struct got_object *obj1 = NULL;
182 struct got_object *obj2 = NULL;
183 struct got_blob_object *blob1 = NULL;
184 struct got_blob_object *blob2 = NULL;
186 err = got_object_open(&obj1, repo, id1);
187 if (err)
188 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
190 err = got_error(GOT_ERR_OBJ_TYPE);
191 goto done;
194 err = got_object_open(&obj2, repo, id2);
195 if (err) {
196 err= got_error(GOT_ERR_BAD_OBJ_HDR);
197 goto done;
199 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
200 err = got_error(GOT_ERR_BAD_OBJ_DATA);
201 goto done;
204 err = got_object_blob_open(&blob1, repo, obj1, 8192);
205 if (err)
206 goto done;
208 err = got_object_blob_open(&blob2, repo, obj2, 8192);
209 if (err)
210 goto done;
212 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
214 done:
215 if (obj1)
216 got_object_close(obj1);
217 if (obj2)
218 got_object_close(obj2);
219 if (blob1)
220 got_object_blob_close(blob1);
221 if (blob2)
222 got_object_blob_close(blob2);
223 return err;
226 static const struct got_error *
227 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
228 FILE *outfile)
230 const struct got_error *err;
231 struct got_blob_object *blob = NULL;
232 struct got_object *obj = NULL;
234 err = got_object_open(&obj, repo, id);
235 if (err)
236 return err;
238 err = got_object_blob_open(&blob, repo, obj, 8192);
239 if (err)
240 goto done;
241 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
242 done:
243 got_object_close(obj);
244 if (blob)
245 got_object_blob_close(blob);
246 return err;
249 static const struct got_error *
250 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
251 FILE *outfile)
253 const struct got_error *err = NULL;
254 struct got_object *treeobj = NULL;
255 struct got_tree_object *tree = NULL;
257 err = got_object_open(&treeobj, repo, id);
258 if (err)
259 goto done;
261 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
262 err = got_error(GOT_ERR_OBJ_TYPE);
263 goto done;
266 err = got_object_tree_open(&tree, repo, treeobj);
267 if (err)
268 goto done;
270 err = got_diff_tree(NULL, tree, repo, outfile);
272 done:
273 if (tree)
274 got_object_tree_close(tree);
275 if (treeobj)
276 got_object_close(treeobj);
277 return err;
280 static const struct got_error *
281 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
282 struct got_repository *repo, FILE *outfile)
284 const struct got_error *err = NULL;
285 struct got_object *treeobj1 = NULL;
286 struct got_object *treeobj2 = NULL;
287 struct got_tree_object *tree1 = NULL;
288 struct got_tree_object *tree2 = NULL;
290 err = got_object_open(&treeobj1, repo, id1);
291 if (err)
292 goto done;
294 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
295 err = got_error(GOT_ERR_OBJ_TYPE);
296 goto done;
299 err = got_object_open(&treeobj2, repo, id2);
300 if (err)
301 goto done;
303 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
304 err = got_error(GOT_ERR_OBJ_TYPE);
305 goto done;
308 err = got_object_tree_open(&tree1, repo, treeobj1);
309 if (err)
310 goto done;
312 err = got_object_tree_open(&tree2, repo, treeobj2);
313 if (err)
314 goto done;
316 err = got_diff_tree(tree1, tree2, repo, outfile);
318 done:
319 if (tree1)
320 got_object_tree_close(tree1);
321 if (tree2)
322 got_object_tree_close(tree2);
323 if (treeobj1)
324 got_object_close(treeobj1);
325 if (treeobj2)
326 got_object_close(treeobj2);
327 return err;
330 static const struct got_error *
331 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
333 const struct got_error *err = NULL;
334 struct got_object *treeobj = NULL;
335 struct got_tree_object *tree = NULL;
337 err = got_object_open(&treeobj, repo, id);
338 if (err)
339 goto done;
341 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
342 err = got_error(GOT_ERR_OBJ_TYPE);
343 goto done;
346 err = got_object_tree_open(&tree, repo, treeobj);
347 if (err)
348 goto done;
350 err = got_diff_tree(tree, NULL, repo, outfile);
352 done:
353 if (tree)
354 got_object_tree_close(tree);
355 if (treeobj)
356 got_object_close(treeobj);
357 return err;
360 static const struct got_error *
361 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
362 FILE *outfile)
364 /* XXX TODO */
365 return NULL;
368 static const struct got_error *
369 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
370 struct got_repository *repo, FILE *outfile)
372 struct got_tree_entry *te2 = NULL;
374 if (tree2)
375 te2 = match_entry_by_name(te1, tree2);
376 if (te2 == NULL) {
377 if (S_ISDIR(te1->mode))
378 return diff_deleted_tree(te1->id, repo, outfile);
379 return diff_deleted_blob(te1->id, repo, outfile);
382 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
383 if (got_object_id_cmp(te1->id, te2->id) != 0)
384 return diff_modified_tree(te1->id, te2->id, repo,
385 outfile);
386 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
387 if (got_object_id_cmp(te1->id, te2->id) != 0)
388 return diff_modified_blob(te1->id, te2->id, repo,
389 outfile);
392 return diff_kind_mismatch(te1->id, te2->id, outfile);
395 static const struct got_error *
396 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
397 struct got_repository *repo, FILE *outfile)
399 if (tree1) {
400 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
401 if (te1 != NULL) /* handled by diff_entry_old_new() */
402 return NULL;
405 if (S_ISDIR(te2->mode))
406 return diff_added_tree(te2->id, repo, outfile);
407 return diff_added_blob(te2->id, repo, outfile);
410 const struct got_error *
411 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
412 struct got_repository *repo, FILE *outfile)
414 const struct got_error *err = NULL;
415 struct got_tree_entry *te1 = NULL;
416 struct got_tree_entry *te2 = NULL;
418 if (tree1)
419 te1 = SIMPLEQ_FIRST(&tree1->entries);
420 if (tree2)
421 te2 = SIMPLEQ_FIRST(&tree2->entries);
423 do {
424 if (te1) {
425 err = diff_entry_old_new(te1, tree2, repo, outfile);
426 if (err)
427 break;
430 if (te2) {
431 err = diff_entry_new_old(te2, tree1, repo, outfile);
432 if (err)
433 break;
436 if (te1)
437 te1 = SIMPLEQ_NEXT(te1, entry);
438 if (te2)
439 te2 = SIMPLEQ_NEXT(te2, entry);
440 } while (te1 || te2);
442 return err;