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 static const struct got_error *
148 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
149 struct got_tree_object *tree2)
151 *te = NULL;
152 return NULL;
155 static int
156 same_id(struct got_object_id *id1, struct got_object_id *id2)
158 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
161 static const struct got_error *
162 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
164 const struct got_error *err;
165 struct got_blob_object *blob;
166 struct got_object *obj;
168 err = got_object_open(&obj, repo, id);
169 if (err)
170 return err;
171 err = got_object_blob_open(&blob, repo, obj, 8192);
172 if (err != NULL)
173 return err;
175 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
178 static const struct got_error *
179 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
180 struct got_repository *repo)
182 const struct got_error *err;
183 struct got_object *obj1 = NULL;
184 struct got_object *obj2 = NULL;
185 struct got_blob_object *blob1 = NULL;
186 struct got_blob_object *blob2 = NULL;
188 err = got_object_open(&obj1, repo, id1);
189 if (err)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
191 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
192 err = got_error(GOT_ERR_OBJ_TYPE);
193 goto done;
196 err = got_object_open(&obj2, repo, id2);
197 if (err) {
198 err= got_error(GOT_ERR_BAD_OBJ_HDR);
199 goto done;
201 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
202 err = got_error(GOT_ERR_BAD_OBJ_DATA);
203 goto done;
206 err = got_object_blob_open(&blob1, repo, obj1, 8192);
207 if (err != NULL) {
208 err = got_error(GOT_ERR_FILE_OPEN);
209 goto done;
212 err = got_object_blob_open(&blob2, repo, obj2, 8192);
213 if (err != NULL) {
214 err = got_error(GOT_ERR_FILE_OPEN);
215 goto done;
218 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
220 done:
221 got_object_close(obj1);
222 got_object_close(obj2);
223 got_object_blob_close(blob1);
224 got_object_blob_close(blob2);
225 return err;
228 static const struct got_error *
229 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
231 const struct got_error *err;
232 struct got_blob_object *blob;
233 struct got_object *obj;
235 err = got_object_open(&obj, repo, id);
236 if (err)
237 return err;
238 err = got_object_blob_open(&blob, repo, obj, 8192);
239 if (err != NULL)
240 return err;
242 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
245 static const struct got_error *
246 diff_added_tree(struct got_object_id *id, struct got_repository *repo)
248 const struct got_error *err = NULL;
249 struct got_object *treeobj = NULL;
250 struct got_tree_object *tree = NULL;
252 err = got_object_open(&treeobj, repo, id);
253 if (err)
254 goto done;
256 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
257 err = got_error(GOT_ERR_OBJ_TYPE);
258 goto done;
261 err = got_object_tree_open(&tree, repo, treeobj);
262 if (err)
263 goto done;
265 err = got_diff_tree(NULL, tree, repo);
267 done:
268 if (tree)
269 got_object_tree_close(tree);
270 if (treeobj)
271 got_object_close(treeobj);
272 return err;
275 static const struct got_error *
276 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
277 struct got_repository *repo)
279 const struct got_error *err = NULL;
280 struct got_object *treeobj1 = NULL;
281 struct got_object *treeobj2 = NULL;
282 struct got_tree_object *tree1 = NULL;
283 struct got_tree_object *tree2 = NULL;
285 err = got_object_open(&treeobj1, repo, id1);
286 if (err)
287 goto done;
289 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
290 err = got_error(GOT_ERR_OBJ_TYPE);
291 goto done;
294 err = got_object_open(&treeobj2, repo, id2);
295 if (err)
296 goto done;
298 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
299 err = got_error(GOT_ERR_OBJ_TYPE);
300 goto done;
303 err = got_object_tree_open(&tree1, repo, treeobj1);
304 if (err)
305 goto done;
307 err = got_object_tree_open(&tree2, repo, treeobj2);
308 if (err)
309 goto done;
311 err = got_diff_tree(tree1, tree2, repo);
313 done:
314 if (tree1)
315 got_object_tree_close(tree1);
316 if (tree2)
317 got_object_tree_close(tree2);
318 if (treeobj1)
319 got_object_close(treeobj1);
320 if (treeobj2)
321 got_object_close(treeobj2);
322 return err;
325 static const struct got_error *
326 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
328 const struct got_error *err = NULL;
329 struct got_object *treeobj = NULL;
330 struct got_tree_object *tree = NULL;
332 err = got_object_open(&treeobj, repo, id);
333 if (err)
334 goto done;
336 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
337 err = got_error(GOT_ERR_OBJ_TYPE);
338 goto done;
341 err = got_object_tree_open(&tree, repo, treeobj);
342 if (err)
343 goto done;
345 err = got_diff_tree(tree, NULL, repo);
347 done:
348 if (tree)
349 got_object_tree_close(tree);
350 if (treeobj)
351 got_object_close(treeobj);
352 return err;
355 static const struct got_error *
356 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
358 /* XXX TODO */
359 return NULL;
362 static const struct got_error *
363 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
364 struct got_repository *repo)
366 const struct got_error *err;
367 struct got_tree_entry *te2;
369 err = match_entry_by_name(&te2, te1, tree2);
370 if (err)
371 return err;
372 if (te2 == NULL) {
373 if (S_ISDIR(te1->mode))
374 return diff_deleted_tree(&te1->id, repo);
375 return diff_deleted_blob(&te1->id, repo);
378 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
379 if (!same_id(&te1->id, &te2->id))
380 return diff_modified_tree(&te1->id, &te2->id, repo);
381 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
382 if (!same_id(&te1->id, &te2->id))
383 return diff_modified_blob(&te1->id, &te2->id, repo);
386 return diff_kind_mismatch(&te1->id, &te2->id);
389 static const struct got_error *
390 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
391 struct got_repository *repo)
393 const struct got_error *err;
394 struct got_tree_entry *te1;
396 err = match_entry_by_name(&te1, te2, tree1);
397 if (err)
398 return err;
399 if (te1 != NULL) /* handled by diff_entry_old_new() */
400 return NULL;
402 if (S_ISDIR(te2->mode))
403 return diff_added_tree(&te2->id, repo);
404 return diff_added_blob(&te2->id, repo);
407 const struct got_error *
408 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
409 struct got_repository *repo)
411 const struct got_error *err = NULL;
412 struct got_tree_entry *te1 = NULL;
413 struct got_tree_entry *te2 = NULL;
415 if (tree1)
416 te1 = SIMPLEQ_FIRST(&tree1->entries);
417 if (tree2)
418 te2 = SIMPLEQ_FIRST(&tree2->entries);
420 do {
421 if (te1) {
422 err = diff_entry_old_new(te1, tree2, repo);
423 if (err)
424 break;
427 if (te2) {
428 err = diff_entry_new_old(te2, tree1, repo);
429 if (err)
430 break;
433 if (te1)
434 te1 = SIMPLEQ_NEXT(te1, entry);
435 if (te2)
436 te2 = SIMPLEQ_NEXT(te2, entry);
437 } while (te1 || te2);
439 return err;