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;
38 FILE *f;
40 if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
41 return NULL;
43 fd = mkstemp(name);
44 if (fd < 0)
45 return NULL;
47 unlink(name);
48 f = fdopen(fd, "w+");
49 if (f == NULL) {
50 close(fd);
51 return NULL;
52 }
54 return f;
55 }
57 const struct got_error *
58 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
59 const char *label1, const char *label2, FILE *outfile)
60 {
61 struct got_diff_state ds;
62 struct got_diff_args args;
63 const struct got_error *err = NULL;
64 FILE *f1 = NULL, *f2 = NULL;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
67 size_t len, hdrlen;
68 int res, flags = 0;
70 if (blob1) {
71 f1 = opentemp();
72 if (f1 == NULL)
73 return got_error(GOT_ERR_FILE_OPEN);
74 } else
75 flags |= D_EMPTY1;
77 if (blob2) {
78 f2 = opentemp();
79 if (f2 == NULL) {
80 fclose(f1);
81 return got_error(GOT_ERR_FILE_OPEN);
82 }
83 } else
84 flags |= D_EMPTY2;
86 if (blob1 == NULL) {
87 f1 = NULL;
88 } else {
89 hdrlen = blob1->hdrlen;
90 do {
91 err = got_object_blob_read_block(blob1, &len);
92 if (err)
93 goto done;
94 /* Skip blob object header first time around. */
95 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
96 hdrlen = 0;
97 } while (len != 0);
98 }
100 hdrlen = blob2->hdrlen;
101 do {
102 err = got_object_blob_read_block(blob2, &len);
103 if (err)
104 goto done;
105 /* Skip blob object header first time around. */
106 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
107 hdrlen = 0;
108 } while (len != 0);
110 fflush(f1);
111 fflush(f2);
112 /* rewind(f1); */
113 /* rewind(f2);*/
115 memset(&ds, 0, sizeof(ds));
116 /* XXX should stat buffers be passed in args instead of ds? */
117 ds.stb1.st_mode = S_IFREG;
118 ds.stb1.st_size = blob1->zb.z.total_out;
119 ds.stb1.st_mtime = 0; /* XXX */
121 ds.stb2.st_mode = S_IFREG;
122 ds.stb2.st_size = blob2->zb.z.total_out;
123 ds.stb2.st_mtime = 0; /* XXX */
125 memset(&args, 0, sizeof(args));
126 args.diff_format = D_UNIFIED;
127 args.label[0] = label1 ?
128 label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
129 args.label[1] = label2 ?
130 label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
132 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
133 done:
134 fclose(f1);
135 fclose(f2);
136 return err;
139 static const struct got_error *
140 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
141 struct got_tree_object *tree2)
143 *te = NULL;
144 return NULL;
147 static int
148 same_id(struct got_object_id *id1, struct got_object_id *id2)
150 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
153 static const struct got_error *
154 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
156 const struct got_error *err;
157 struct got_blob_object *blob;
158 struct got_object *obj;
160 err = got_object_open(&obj, repo, id);
161 if (err)
162 return err;
163 err = got_object_blob_open(&blob, repo, obj, 512);
164 if (err != NULL)
165 return err;
167 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
170 static const struct got_error *
171 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
172 struct got_repository *repo)
174 const struct got_error *err;
175 struct got_object *obj1 = NULL;
176 struct got_object *obj2 = NULL;
177 struct got_blob_object *blob1 = NULL;
178 struct got_blob_object *blob2 = NULL;
180 err = got_object_open(&obj1, repo, id1);
181 if (err)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
184 err = got_error(GOT_ERR_OBJ_TYPE);
185 goto done;
188 err = got_object_open(&obj2, repo, id2);
189 if (err) {
190 err= got_error(GOT_ERR_BAD_OBJ_HDR);
191 goto done;
193 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
194 err = got_error(GOT_ERR_BAD_OBJ_DATA);
195 goto done;
198 err = got_object_blob_open(&blob1, repo, obj1, 512);
199 if (err != NULL) {
200 err = got_error(GOT_ERR_FILE_OPEN);
201 goto done;
204 err = got_object_blob_open(&blob2, repo, obj2, 512);
205 if (err != NULL) {
206 err = got_error(GOT_ERR_FILE_OPEN);
207 goto done;
210 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
212 done:
213 got_object_close(obj1);
214 got_object_close(obj2);
215 got_object_blob_close(blob1);
216 got_object_blob_close(blob2);
217 return err;
220 static const struct got_error *
221 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
223 const struct got_error *err;
224 struct got_blob_object *blob;
225 struct got_object *obj;
227 err = got_object_open(&obj, repo, id);
228 if (err)
229 return err;
230 err = got_object_blob_open(&blob, repo, obj, 512);
231 if (err != NULL)
232 return err;
234 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
237 static const struct got_error *
238 diff_added_tree(struct got_object_id *id)
240 return NULL;
243 static const struct got_error *
244 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
246 return NULL;
249 static const struct got_error *
250 diff_deleted_tree(struct got_object_id *id)
252 return NULL;
255 static const struct got_error *
256 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
258 return NULL;
261 static const struct got_error *
262 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
263 struct got_repository *repo)
265 const struct got_error *err;
266 struct got_tree_entry *te2;
268 err = match_entry_by_name(&te2, te1, tree2);
269 if (err)
270 return err;
271 if (te2 == NULL) {
272 if (S_ISDIR(te1->mode))
273 return diff_deleted_tree(&te1->id);
274 return diff_deleted_blob(&te1->id, repo);
277 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
278 if (!same_id(&te1->id, &te2->id))
279 return diff_modified_tree(&te1->id, &te2->id);
280 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
281 if (!same_id(&te1->id, &te2->id))
282 return diff_modified_blob(&te1->id, &te2->id, repo);
285 return diff_kind_mismatch(&te1->id, &te2->id);
288 static const struct got_error *
289 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
290 struct got_repository *repo)
292 const struct got_error *err;
293 struct got_tree_entry *te1;
295 err = match_entry_by_name(&te1, te2, tree1);
296 if (err)
297 return err;
298 if (te1 != NULL) /* handled by diff_entry_old_new() */
299 return NULL;
301 if (S_ISDIR(te2->mode))
302 return diff_added_tree(&te2->id);
303 return diff_added_blob(&te2->id, repo);
306 const struct got_error *
307 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
308 struct got_repository *repo)
310 const struct got_error *err = NULL;
311 struct got_tree_entry *te1;
312 struct got_tree_entry *te2;
314 if (tree1->nentries == 0 && tree2->nentries == 0)
315 return NULL;
317 te1 = SIMPLEQ_FIRST(&tree1->entries);
318 te2 = SIMPLEQ_FIRST(&tree2->entries);
320 do {
321 if (te1) {
322 err = diff_entry_old_new(te1, tree2, repo);
323 if (err)
324 break;
327 if (te2) {
328 err = diff_entry_new_old(te2, tree1, repo);
329 if (err)
330 break;
333 if (te1)
334 te1 = SIMPLEQ_NEXT(te1, entry);
335 if (te2)
336 te2 = SIMPLEQ_NEXT(te2, entry);
337 } while (te1 || te2);
339 return err;