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_diff_priv.h"
33 #include "got_path_priv.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, len - hdrlen, 1, f1);
79 hdrlen = 0;
80 } while (len != 0);
81 } else
82 idstr1 = "/dev/null";
84 size2 = 0;
85 if (blob2) {
86 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 hdrlen = got_object_blob_get_hdrlen(blob2);
88 do {
89 err = got_object_blob_read_block(&len, blob2);
90 if (err)
91 goto done;
92 if (len == 0)
93 break;
94 size2 += len;
95 /* Skip blob object header first time around. */
96 fwrite(got_object_blob_get_read_buf(blob2) + hdrlen, len - hdrlen, 1, f2);
97 hdrlen = 0;
98 } while (len != 0);
99 } else
100 idstr2 = "/dev/null";
102 if (f1) {
103 fflush(f1);
104 rewind(f1);
106 if (f2) {
107 fflush(f2);
108 rewind(f2);
111 memset(&ds, 0, sizeof(ds));
112 /* XXX should stat buffers be passed in args instead of ds? */
113 ds.stb1.st_mode = S_IFREG;
114 if (blob1)
115 ds.stb1.st_size = size1;
116 ds.stb1.st_mtime = 0; /* XXX */
118 ds.stb2.st_mode = S_IFREG;
119 if (blob2)
120 ds.stb2.st_size = size2;
121 ds.stb2.st_mtime = 0; /* XXX */
123 memset(&args, 0, sizeof(args));
124 args.diff_format = D_UNIFIED;
125 args.label[0] = label1 ? label1 : idstr1;
126 args.label[1] = label2 ? label2 : idstr2;
128 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
129 done:
130 if (f1)
131 fclose(f1);
132 if (f2)
133 fclose(f2);
134 return err;
137 struct got_tree_entry *
138 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
140 struct got_tree_entry *te2;
142 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
143 if (strcmp(te1->name, te2->name) == 0)
144 return te2;
146 return NULL;
149 static const struct got_error *
150 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
151 FILE *outfile)
153 const struct got_error *err;
154 struct got_blob_object *blob;
155 struct got_object *obj;
157 err = got_object_open(&obj, repo, id);
158 if (err)
159 return err;
160 err = got_object_blob_open(&blob, repo, obj, 8192);
161 if (err != NULL)
162 return err;
164 return got_diff_blob(NULL, blob, NULL, NULL, outfile);
167 static const struct got_error *
168 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
169 struct got_repository *repo, FILE *outfile)
171 const struct got_error *err;
172 struct got_object *obj1 = NULL;
173 struct got_object *obj2 = NULL;
174 struct got_blob_object *blob1 = NULL;
175 struct got_blob_object *blob2 = NULL;
177 err = got_object_open(&obj1, repo, id1);
178 if (err)
179 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
181 err = got_error(GOT_ERR_OBJ_TYPE);
182 goto done;
185 err = got_object_open(&obj2, repo, id2);
186 if (err) {
187 err= got_error(GOT_ERR_BAD_OBJ_HDR);
188 goto done;
190 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
191 err = got_error(GOT_ERR_BAD_OBJ_DATA);
192 goto done;
195 err = got_object_blob_open(&blob1, repo, obj1, 8192);
196 if (err != NULL) {
197 err = got_error(GOT_ERR_FILE_OPEN);
198 goto done;
201 err = got_object_blob_open(&blob2, repo, obj2, 8192);
202 if (err != NULL) {
203 err = got_error(GOT_ERR_FILE_OPEN);
204 goto done;
207 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
209 done:
210 if (obj1)
211 got_object_close(obj1);
212 if (obj2)
213 got_object_close(obj2);
214 if (blob1)
215 got_object_blob_close(blob1);
216 if (blob2)
217 got_object_blob_close(blob2);
218 return err;
221 static const struct got_error *
222 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
223 FILE *outfile)
225 const struct got_error *err;
226 struct got_blob_object *blob;
227 struct got_object *obj;
229 err = got_object_open(&obj, repo, id);
230 if (err)
231 return err;
232 err = got_object_blob_open(&blob, repo, obj, 8192);
233 if (err != NULL)
234 return err;
236 return got_diff_blob(blob, NULL, NULL, NULL, outfile);
239 static const struct got_error *
240 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
241 FILE *outfile)
243 const struct got_error *err = NULL;
244 struct got_object *treeobj = NULL;
245 struct got_tree_object *tree = NULL;
247 err = got_object_open(&treeobj, repo, id);
248 if (err)
249 goto done;
251 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
252 err = got_error(GOT_ERR_OBJ_TYPE);
253 goto done;
256 err = got_object_tree_open(&tree, repo, treeobj);
257 if (err)
258 goto done;
260 err = got_diff_tree(NULL, tree, repo, outfile);
262 done:
263 if (tree)
264 got_object_tree_close(tree);
265 if (treeobj)
266 got_object_close(treeobj);
267 return err;
270 static const struct got_error *
271 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
272 struct got_repository *repo, FILE *outfile)
274 const struct got_error *err = NULL;
275 struct got_object *treeobj1 = NULL;
276 struct got_object *treeobj2 = NULL;
277 struct got_tree_object *tree1 = NULL;
278 struct got_tree_object *tree2 = NULL;
280 err = got_object_open(&treeobj1, repo, id1);
281 if (err)
282 goto done;
284 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
285 err = got_error(GOT_ERR_OBJ_TYPE);
286 goto done;
289 err = got_object_open(&treeobj2, repo, id2);
290 if (err)
291 goto done;
293 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
294 err = got_error(GOT_ERR_OBJ_TYPE);
295 goto done;
298 err = got_object_tree_open(&tree1, repo, treeobj1);
299 if (err)
300 goto done;
302 err = got_object_tree_open(&tree2, repo, treeobj2);
303 if (err)
304 goto done;
306 err = got_diff_tree(tree1, tree2, repo, outfile);
308 done:
309 if (tree1)
310 got_object_tree_close(tree1);
311 if (tree2)
312 got_object_tree_close(tree2);
313 if (treeobj1)
314 got_object_close(treeobj1);
315 if (treeobj2)
316 got_object_close(treeobj2);
317 return err;
320 static const struct got_error *
321 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
323 const struct got_error *err = NULL;
324 struct got_object *treeobj = NULL;
325 struct got_tree_object *tree = NULL;
327 err = got_object_open(&treeobj, repo, id);
328 if (err)
329 goto done;
331 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
332 err = got_error(GOT_ERR_OBJ_TYPE);
333 goto done;
336 err = got_object_tree_open(&tree, repo, treeobj);
337 if (err)
338 goto done;
340 err = got_diff_tree(tree, NULL, repo, outfile);
342 done:
343 if (tree)
344 got_object_tree_close(tree);
345 if (treeobj)
346 got_object_close(treeobj);
347 return err;
350 static const struct got_error *
351 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
352 FILE *outfile)
354 /* XXX TODO */
355 return NULL;
358 static const struct got_error *
359 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
360 struct got_repository *repo, FILE *outfile)
362 const struct got_error *err;
363 struct got_tree_entry *te2;
364 char hex[SHA1_DIGEST_STRING_LENGTH];
366 te2 = match_entry_by_name(te1, tree2);
367 if (te2 == NULL) {
368 if (S_ISDIR(te1->mode))
369 return diff_deleted_tree(te1->id, repo, outfile);
370 return diff_deleted_blob(te1->id, repo, outfile);
373 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
374 if (got_object_id_cmp(te1->id, te2->id) != 0)
375 return diff_modified_tree(te1->id, te2->id, repo,
376 outfile);
377 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
378 if (got_object_id_cmp(te1->id, te2->id) != 0)
379 return diff_modified_blob(te1->id, te2->id, repo,
380 outfile);
383 return diff_kind_mismatch(te1->id, te2->id, outfile);
386 static const struct got_error *
387 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
388 struct got_repository *repo, FILE *outfile)
390 const struct got_error *err;
391 struct got_tree_entry *te1;
393 te1 = match_entry_by_name(te2, tree1);
394 if (te1 != NULL) /* handled by diff_entry_old_new() */
395 return NULL;
397 if (S_ISDIR(te2->mode))
398 return diff_added_tree(te2->id, repo, outfile);
399 return diff_added_blob(te2->id, repo, outfile);
402 const struct got_error *
403 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
404 struct got_repository *repo, FILE *outfile)
406 const struct got_error *err = NULL;
407 struct got_tree_entry *te1 = NULL;
408 struct got_tree_entry *te2 = NULL;
410 if (tree1)
411 te1 = SIMPLEQ_FIRST(&tree1->entries);
412 if (tree2)
413 te2 = SIMPLEQ_FIRST(&tree2->entries);
415 do {
416 if (te1) {
417 err = diff_entry_old_new(te1, tree2, repo, outfile);
418 if (err)
419 break;
422 if (te2) {
423 err = diff_entry_new_old(te2, tree1, repo, outfile);
424 if (err)
425 break;
428 if (te1)
429 te1 = SIMPLEQ_NEXT(te1, entry);
430 if (te2)
431 te2 = SIMPLEQ_NEXT(te2, entry);
432 } while (te1 || te2);
434 return err;