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"
31 #include "diff.h"
33 static FILE *
34 opentemp(void)
35 {
36 char name[PATH_MAX];
37 int fd;
39 if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
40 return NULL;
42 fd = mkstemp(name);
43 if (fd < 0)
44 return NULL;
46 unlink(name);
47 return (fdopen(fd, "w+"));
48 }
50 const struct got_error *
51 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
52 const char *label1, const char *label2, FILE *outfile)
53 {
54 struct got_diff_state ds;
55 struct got_diff_args args;
56 const struct got_error *err = NULL;
57 FILE *f1 = NULL, *f2 = NULL;
58 char hex1[SHA1_DIGEST_STRING_LENGTH];
59 char hex2[SHA1_DIGEST_STRING_LENGTH];
60 size_t len, hdrlen;
61 int res, flags = 0;
63 if (blob1) {
64 f1 = opentemp();
65 if (f1 == NULL)
66 return got_error(GOT_ERR_FILE_OPEN);
67 } else
68 flags |= D_EMPTY1;
70 if (blob2) {
71 f2 = opentemp();
72 if (f2 == NULL) {
73 fclose(f1);
74 return got_error(GOT_ERR_FILE_OPEN);
75 }
76 } else
77 flags |= D_EMPTY2;
79 if (blob1 == NULL) {
80 f1 = NULL;
81 } else {
82 hdrlen = blob1->hdrlen;
83 do {
84 err = got_object_blob_read_block(blob1, &len);
85 if (err)
86 goto done;
87 /* Skip blob object header first time around. */
88 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
89 hdrlen = 0;
90 } while (len != 0);
91 }
93 hdrlen = blob2->hdrlen;
94 do {
95 err = got_object_blob_read_block(blob2, &len);
96 if (err)
97 goto done;
98 /* Skip blob object header first time around. */
99 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
100 hdrlen = 0;
101 } while (len != 0);
103 fflush(f1);
104 fflush(f2);
105 /* rewind(f1); */
106 /* rewind(f2);*/
108 memset(&ds, 0, sizeof(ds));
109 /* XXX should stat buffers be passed in args instead of ds? */
110 ds.stb1.st_mode = S_IFREG;
111 ds.stb1.st_size = blob1->zb.z.total_out;
112 ds.stb1.st_mtime = 0; /* XXX */
114 ds.stb2.st_mode = S_IFREG;
115 ds.stb2.st_size = blob2->zb.z.total_out;
116 ds.stb2.st_mtime = 0; /* XXX */
118 memset(&args, 0, sizeof(args));
119 args.diff_format = D_UNIFIED;
120 args.label[0] = label1 ?
121 label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
122 args.label[1] = label2 ?
123 label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
125 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
126 done:
127 fclose(f1);
128 fclose(f2);
129 return err;
132 static const struct got_error *
133 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
134 struct got_tree_object *tree2)
136 *te = NULL;
137 return NULL;
140 static int
141 same_id(struct got_object_id *id1, struct got_object_id *id2)
143 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
146 static const struct got_error *
147 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
149 const struct got_error *err;
150 struct got_blob_object *blob;
151 struct got_object *obj;
153 err = got_object_open(&obj, repo, id);
154 if (err)
155 return err;
156 err = got_object_blob_open(&blob, repo, obj, 512);
157 if (err != NULL)
158 return err;
160 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
163 static const struct got_error *
164 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
165 struct got_repository *repo)
167 const struct got_error *err;
168 struct got_object *obj1 = NULL;
169 struct got_object *obj2 = NULL;
170 struct got_blob_object *blob1 = NULL;
171 struct got_blob_object *blob2 = NULL;
173 err = got_object_open(&obj1, repo, id1);
174 if (err)
175 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
177 err = got_error(GOT_ERR_OBJ_TYPE);
178 goto done;
181 err = got_object_open(&obj2, repo, id2);
182 if (err) {
183 err= got_error(GOT_ERR_BAD_OBJ_HDR);
184 goto done;
186 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
187 err = got_error(GOT_ERR_BAD_OBJ_DATA);
188 goto done;
191 err = got_object_blob_open(&blob1, repo, obj1, 512);
192 if (err != NULL) {
193 err = got_error(GOT_ERR_FILE_OPEN);
194 goto done;
197 err = got_object_blob_open(&blob2, repo, obj2, 512);
198 if (err != NULL) {
199 err = got_error(GOT_ERR_FILE_OPEN);
200 goto done;
203 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
205 done:
206 got_object_close(obj1);
207 got_object_close(obj2);
208 got_object_blob_close(blob1);
209 got_object_blob_close(blob2);
210 return err;
213 static const struct got_error *
214 diff_deleted_blob(struct got_object_id *id)
216 return NULL;
219 static const struct got_error *
220 diff_added_tree(struct got_object_id *id)
222 return NULL;
225 static const struct got_error *
226 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
228 return NULL;
231 static const struct got_error *
232 diff_deleted_tree(struct got_object_id *id)
234 return NULL;
237 static const struct got_error *
238 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
240 return NULL;
243 static const struct got_error *
244 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
245 struct got_repository *repo)
247 const struct got_error *err;
248 struct got_tree_entry *te2;
250 err = match_entry_by_name(&te2, te1, tree2);
251 if (err)
252 return err;
253 if (te2 == NULL) {
254 if (S_ISDIR(te1->mode))
255 return diff_deleted_tree(&te1->id);
256 return diff_deleted_blob(&te1->id);
259 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
260 if (!same_id(&te1->id, &te2->id))
261 return diff_modified_tree(&te1->id, &te2->id);
262 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
263 if (!same_id(&te1->id, &te2->id))
264 return diff_modified_blob(&te1->id, &te2->id, repo);
267 return diff_kind_mismatch(&te1->id, &te2->id);
270 static const struct got_error *
271 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
272 struct got_repository *repo)
274 const struct got_error *err;
275 struct got_tree_entry *te1;
277 err = match_entry_by_name(&te1, te2, tree1);
278 if (err)
279 return err;
280 if (te1 != NULL) /* handled by diff_entry_old_new() */
281 return NULL;
283 if (S_ISDIR(te2->mode))
284 return diff_added_tree(&te2->id);
285 return diff_added_blob(&te2->id, repo);
288 const struct got_error *
289 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
290 struct got_repository *repo)
292 const struct got_error *err = NULL;
293 struct got_tree_entry *te1;
294 struct got_tree_entry *te2;
296 if (tree1->nentries == 0 && tree2->nentries == 0)
297 return NULL;
299 te1 = SIMPLEQ_FIRST(&tree1->entries);
300 te2 = SIMPLEQ_FIRST(&tree2->entries);
302 do {
303 if (te1) {
304 err = diff_entry_old_new(te1, tree2, repo);
305 if (err)
306 break;
309 if (te2) {
310 err = diff_entry_new_old(te2, tree1, repo);
311 if (err)
312 break;
315 if (te1)
316 te1 = SIMPLEQ_NEXT(te1, entry);
317 if (te2)
318 te2 = SIMPLEQ_NEXT(te2, entry);
319 } while (te1 || te2);
321 return err;