Blame


1 574ed2c3 2017-11-29 stsp /*
2 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 574ed2c3 2017-11-29 stsp *
5 fe621944 2020-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
6 fe621944 2020-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
7 fe621944 2020-11-10 stsp * copyright notice and this permission notice appear in all copies.
8 574ed2c3 2017-11-29 stsp *
9 fe621944 2020-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fe621944 2020-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fe621944 2020-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fe621944 2020-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fe621944 2020-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fe621944 2020-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fe621944 2020-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 574ed2c3 2017-11-29 stsp */
17 574ed2c3 2017-11-29 stsp
18 fe621944 2020-11-10 stsp #include <sys/mman.h>
19 574ed2c3 2017-11-29 stsp #include <sys/stat.h>
20 574ed2c3 2017-11-29 stsp
21 574ed2c3 2017-11-29 stsp #include <errno.h>
22 9a267125 2022-05-31 thomas #include <sha1.h>
23 574ed2c3 2017-11-29 stsp #include <stdio.h>
24 574ed2c3 2017-11-29 stsp #include <stdlib.h>
25 574ed2c3 2017-11-29 stsp #include <string.h>
26 574ed2c3 2017-11-29 stsp
27 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
28 dd038bc6 2021-09-21 thomas.ad
29 7d283eee 2017-11-29 stsp #include "got_object.h"
30 fe621944 2020-11-10 stsp #include "got_opentemp.h"
31 fe621944 2020-11-10 stsp #include "got_error.h"
32 7d283eee 2017-11-29 stsp
33 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
34 574ed2c3 2017-11-29 stsp
35 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_patience;
36 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_myers_divide;
37 fe621944 2020-11-10 stsp const struct diff_algo_config patience;
38 fe621944 2020-11-10 stsp const struct diff_algo_config myers_divide;
39 574ed2c3 2017-11-29 stsp
40 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
41 fe621944 2020-11-10 stsp .impl = diff_algo_myers,
42 fe621944 2020-11-10 stsp .permitted_state_size = 1024 * 1024 * sizeof(int),
43 fe621944 2020-11-10 stsp .fallback_algo = &patience,
44 fe621944 2020-11-10 stsp };
45 574ed2c3 2017-11-29 stsp
46 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_myers_divide =
47 fe621944 2020-11-10 stsp (struct diff_algo_config){
48 fe621944 2020-11-10 stsp .impl = diff_algo_myers,
49 fe621944 2020-11-10 stsp .permitted_state_size = 1024 * 1024 * sizeof(int),
50 fe621944 2020-11-10 stsp .fallback_algo = &myers_divide,
51 fe621944 2020-11-10 stsp };
52 574ed2c3 2017-11-29 stsp
53 fe621944 2020-11-10 stsp const struct diff_algo_config patience = (struct diff_algo_config){
54 fe621944 2020-11-10 stsp .impl = diff_algo_patience,
55 fe621944 2020-11-10 stsp /* After subdivision, do Patience again: */
56 fe621944 2020-11-10 stsp .inner_algo = &patience,
57 fe621944 2020-11-10 stsp /* If subdivision failed, do Myers Divide et Impera: */
58 fe621944 2020-11-10 stsp .fallback_algo = &myers_then_myers_divide,
59 574ed2c3 2017-11-29 stsp };
60 574ed2c3 2017-11-29 stsp
61 fe621944 2020-11-10 stsp const struct diff_algo_config myers_divide = (struct diff_algo_config){
62 fe621944 2020-11-10 stsp .impl = diff_algo_myers_divide,
63 fe621944 2020-11-10 stsp /* When division succeeded, start from the top: */
64 fe621944 2020-11-10 stsp .inner_algo = &myers_then_myers_divide,
65 fe621944 2020-11-10 stsp /* (fallback_algo = NULL implies diff_algo_none). */
66 8020fd50 2017-11-29 stsp };
67 574ed2c3 2017-11-29 stsp
68 fe621944 2020-11-10 stsp /* If the state for a forward-Myers is small enough, use Myers, otherwise first
69 fe621944 2020-11-10 stsp * do a Myers-divide. */
70 fe621944 2020-11-10 stsp const struct diff_config diff_config_myers_then_myers_divide = {
71 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
72 fe621944 2020-11-10 stsp .algo = &myers_then_myers_divide,
73 fe621944 2020-11-10 stsp };
74 574ed2c3 2017-11-29 stsp
75 fe621944 2020-11-10 stsp /* If the state for a forward-Myers is small enough, use Myers, otherwise first
76 fe621944 2020-11-10 stsp * do a Patience. */
77 fe621944 2020-11-10 stsp const struct diff_config diff_config_myers_then_patience = {
78 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
79 fe621944 2020-11-10 stsp .algo = &myers_then_patience,
80 574ed2c3 2017-11-29 stsp };
81 574ed2c3 2017-11-29 stsp
82 fe621944 2020-11-10 stsp /* Directly force Patience as a first divider of the source file. */
83 fe621944 2020-11-10 stsp const struct diff_config diff_config_patience = {
84 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
85 fe621944 2020-11-10 stsp .algo = &patience,
86 574ed2c3 2017-11-29 stsp };
87 574ed2c3 2017-11-29 stsp
88 fe621944 2020-11-10 stsp /* Directly force Patience as a first divider of the source file. */
89 fe621944 2020-11-10 stsp const struct diff_config diff_config_no_algo = {
90 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
91 fe621944 2020-11-10 stsp };
92 fe621944 2020-11-10 stsp
93 fe621944 2020-11-10 stsp const struct got_error *
94 fe621944 2020-11-10 stsp got_diffreg_close(FILE *f1, char *p1, size_t size1,
95 fe621944 2020-11-10 stsp FILE *f2, char *p2, size_t size2)
96 cb74ff21 2017-11-30 stsp {
97 fe621944 2020-11-10 stsp const struct got_error *err = NULL;
98 cb74ff21 2017-11-30 stsp
99 fe621944 2020-11-10 stsp if (p1 && munmap(p1, size1) == -1 && err == NULL)
100 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
101 fe621944 2020-11-10 stsp if (p2 && munmap(p2, size2) == -1 && err == NULL)
102 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
103 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
104 fe621944 2020-11-10 stsp err = got_error_from_errno("fclose");
105 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
106 fe621944 2020-11-10 stsp err = got_error_from_errno("fclose");
107 fe621944 2020-11-10 stsp return err;
108 dc424a06 2019-08-07 stsp }
109 dc424a06 2019-08-07 stsp
110 cca5682e 2020-11-18 stsp const struct got_error *
111 cca5682e 2020-11-18 stsp got_diff_get_config(struct diff_config **cfg,
112 cca5682e 2020-11-18 stsp enum got_diff_algorithm algorithm,
113 cca5682e 2020-11-18 stsp diff_atomize_func_t atomize_func, void *atomize_func_data)
114 dc424a06 2019-08-07 stsp {
115 cca5682e 2020-11-18 stsp *cfg = calloc(1, sizeof(**cfg));
116 cca5682e 2020-11-18 stsp if (*cfg == NULL)
117 cca5682e 2020-11-18 stsp return got_error_from_errno("calloc");
118 cca5682e 2020-11-18 stsp
119 fe621944 2020-11-10 stsp switch (algorithm) {
120 fe621944 2020-11-10 stsp case GOT_DIFF_ALGORITHM_PATIENCE:
121 cca5682e 2020-11-18 stsp (*cfg)->algo = &patience;
122 cca5682e 2020-11-18 stsp break;
123 fe621944 2020-11-10 stsp case GOT_DIFF_ALGORITHM_MYERS:
124 cca5682e 2020-11-18 stsp (*cfg)->algo = &myers_then_myers_divide;
125 cca5682e 2020-11-18 stsp break;
126 cca5682e 2020-11-18 stsp default:
127 cca5682e 2020-11-18 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad diff algorithm");
128 fe621944 2020-11-10 stsp }
129 cca5682e 2020-11-18 stsp
130 cca5682e 2020-11-18 stsp if (atomize_func) {
131 cca5682e 2020-11-18 stsp (*cfg)->atomize_func = atomize_func;
132 cca5682e 2020-11-18 stsp (*cfg)->atomize_func_data = atomize_func_data;
133 cca5682e 2020-11-18 stsp } else
134 cca5682e 2020-11-18 stsp (*cfg)->atomize_func = diff_atomize_text_by_line;
135 cca5682e 2020-11-18 stsp
136 cca5682e 2020-11-18 stsp (*cfg)->max_recursion_depth = 0; /* use default recursion depth */
137 cca5682e 2020-11-18 stsp
138 cca5682e 2020-11-18 stsp return NULL;
139 cb74ff21 2017-11-30 stsp }
140 cb74ff21 2017-11-30 stsp
141 7d283eee 2017-11-29 stsp const struct got_error *
142 72254787 2020-11-18 stsp got_diff_prepare_file(FILE *f, char **p, size_t *size,
143 fe621944 2020-11-10 stsp struct diff_data *diff_data, const struct diff_config *cfg,
144 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff)
145 574ed2c3 2017-11-29 stsp {
146 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
147 fe621944 2020-11-10 stsp struct stat st;
148 fe621944 2020-11-10 stsp int diff_flags = 0, rc;
149 7d283eee 2017-11-29 stsp
150 fe621944 2020-11-10 stsp *size = 0;
151 fe621944 2020-11-10 stsp
152 fe621944 2020-11-10 stsp diff_flags |= DIFF_FLAG_SHOW_PROTOTYPES;
153 fe621944 2020-11-10 stsp if (ignore_whitespace)
154 fe621944 2020-11-10 stsp diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
155 64453f7e 2020-11-21 stsp if (force_text_diff)
156 64453f7e 2020-11-21 stsp diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
157 fe621944 2020-11-10 stsp
158 72254787 2020-11-18 stsp if (fstat(fileno(f), &st) == -1) {
159 72254787 2020-11-18 stsp err = got_error_from_errno("fstat");
160 72254787 2020-11-18 stsp goto done;
161 fe621944 2020-11-10 stsp }
162 72254787 2020-11-18 stsp #ifndef GOT_DIFF_NO_MMAP
163 72254787 2020-11-18 stsp *p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE,
164 72254787 2020-11-18 stsp fileno(f), 0);
165 72254787 2020-11-18 stsp if (*p == MAP_FAILED)
166 72254787 2020-11-18 stsp #endif
167 72254787 2020-11-18 stsp *p = NULL; /* fall back on file I/O */
168 fe621944 2020-11-10 stsp
169 72254787 2020-11-18 stsp rc = diff_atomize_file(diff_data, cfg, f, *p, st.st_size, diff_flags);
170 fe621944 2020-11-10 stsp if (rc) {
171 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_atomize_file");
172 fe621944 2020-11-10 stsp goto done;
173 fe621944 2020-11-10 stsp }
174 fe621944 2020-11-10 stsp done:
175 fe621944 2020-11-10 stsp if (err)
176 fe621944 2020-11-10 stsp diff_data_free(diff_data);
177 574ed2c3 2017-11-29 stsp else
178 fe621944 2020-11-10 stsp *size = st.st_size;
179 fe621944 2020-11-10 stsp return err;
180 fe621944 2020-11-10 stsp }
181 fe621944 2020-11-10 stsp
182 fe621944 2020-11-10 stsp const struct got_error *
183 fe621944 2020-11-10 stsp got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
184 64453f7e 2020-11-21 stsp enum got_diff_algorithm algorithm, int ignore_whitespace,
185 64453f7e 2020-11-21 stsp int force_text_diff)
186 fe621944 2020-11-10 stsp {
187 fe621944 2020-11-10 stsp const struct got_error *err = NULL;
188 cca5682e 2020-11-18 stsp struct diff_config *cfg = NULL;
189 fe621944 2020-11-10 stsp char *p1 = NULL, *p2 = NULL;
190 fe621944 2020-11-10 stsp int f1_created = 0, f2_created = 0;
191 fe621944 2020-11-10 stsp size_t size1, size2;
192 fe621944 2020-11-10 stsp struct diff_data d_left, d_right;
193 fe621944 2020-11-10 stsp struct diff_data *left, *right;
194 fe621944 2020-11-10 stsp struct diff_result *diff_result;
195 574ed2c3 2017-11-29 stsp
196 fe621944 2020-11-10 stsp if (diffreg_result) {
197 fe621944 2020-11-10 stsp *diffreg_result = calloc(1, sizeof(**diffreg_result));
198 fe621944 2020-11-10 stsp if (*diffreg_result == NULL)
199 fe621944 2020-11-10 stsp return got_error_from_errno("calloc");
200 fe621944 2020-11-10 stsp left = &(*diffreg_result)->left;
201 fe621944 2020-11-10 stsp right = &(*diffreg_result)->right;
202 fe621944 2020-11-10 stsp } else {
203 fe621944 2020-11-10 stsp memset(&d_left, 0, sizeof(d_left));
204 fe621944 2020-11-10 stsp memset(&d_right, 0, sizeof(d_right));
205 fe621944 2020-11-10 stsp left = &d_left;
206 fe621944 2020-11-10 stsp right = &d_right;
207 574ed2c3 2017-11-29 stsp }
208 72254787 2020-11-18 stsp
209 cca5682e 2020-11-18 stsp err = got_diff_get_config(&cfg, algorithm, NULL, NULL);
210 cca5682e 2020-11-18 stsp if (err)
211 fe621944 2020-11-10 stsp goto done;
212 574ed2c3 2017-11-29 stsp
213 72254787 2020-11-18 stsp if (f1 == NULL) {
214 72254787 2020-11-18 stsp f1_created = 1;
215 72254787 2020-11-18 stsp f1 = got_opentemp();
216 72254787 2020-11-18 stsp if (f1 == NULL) {
217 72254787 2020-11-18 stsp err = got_error_from_errno("got_opentemp");
218 72254787 2020-11-18 stsp goto done;
219 72254787 2020-11-18 stsp }
220 72254787 2020-11-18 stsp }
221 72254787 2020-11-18 stsp if (f2 == NULL) {
222 72254787 2020-11-18 stsp f2_created = 1;
223 72254787 2020-11-18 stsp f2 = got_opentemp();
224 72254787 2020-11-18 stsp if (f2 == NULL) {
225 72254787 2020-11-18 stsp err = got_error_from_errno("got_opentemp");
226 72254787 2020-11-18 stsp goto done;
227 72254787 2020-11-18 stsp }
228 72254787 2020-11-18 stsp }
229 72254787 2020-11-18 stsp
230 72254787 2020-11-18 stsp err = got_diff_prepare_file(f1, &p1, &size1, left, cfg,
231 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
232 fe621944 2020-11-10 stsp if (err)
233 fe621944 2020-11-10 stsp goto done;
234 574ed2c3 2017-11-29 stsp
235 72254787 2020-11-18 stsp err = got_diff_prepare_file(f2, &p2, &size2, right, cfg,
236 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
237 fe621944 2020-11-10 stsp if (err)
238 fe621944 2020-11-10 stsp goto done;
239 574ed2c3 2017-11-29 stsp
240 fe621944 2020-11-10 stsp diff_result = diff_main(cfg, left, right);
241 fe621944 2020-11-10 stsp if (diff_result == NULL) {
242 fe621944 2020-11-10 stsp err = got_error_set_errno(ENOMEM, "malloc");
243 fe621944 2020-11-10 stsp goto done;
244 322260e1 2018-01-26 mpi }
245 fe621944 2020-11-10 stsp if (diff_result->rc != DIFF_RC_OK) {
246 fe621944 2020-11-10 stsp err = got_error_set_errno(diff_result->rc, "diff");
247 fe621944 2020-11-10 stsp goto done;
248 7d283eee 2017-11-29 stsp }
249 574ed2c3 2017-11-29 stsp
250 fe621944 2020-11-10 stsp if (diffreg_result) {
251 fe621944 2020-11-10 stsp (*diffreg_result)->result = diff_result;
252 fe621944 2020-11-10 stsp if (f1_created)
253 fe621944 2020-11-10 stsp (*diffreg_result)->f1 = f1;
254 fe621944 2020-11-10 stsp (*diffreg_result)->map1 = p1;
255 fe621944 2020-11-10 stsp (*diffreg_result)->size1 = size1;
256 fe621944 2020-11-10 stsp if (f2_created)
257 fe621944 2020-11-10 stsp (*diffreg_result)->f2 = f2;
258 fe621944 2020-11-10 stsp (*diffreg_result)->map2 = p2;
259 fe621944 2020-11-10 stsp (*diffreg_result)->size2 = size2;
260 7d283eee 2017-11-29 stsp }
261 fe621944 2020-11-10 stsp done:
262 cca5682e 2020-11-18 stsp free(cfg);
263 fe621944 2020-11-10 stsp if (diffreg_result == NULL) {
264 fe621944 2020-11-10 stsp diff_data_free(left);
265 fe621944 2020-11-10 stsp diff_data_free(right);
266 7d283eee 2017-11-29 stsp }
267 fe621944 2020-11-10 stsp if (err) {
268 fe621944 2020-11-10 stsp got_diffreg_close(f1_created ? f1 : NULL, p1, size1,
269 fe621944 2020-11-10 stsp f2_created ? f2 : NULL, p2, size2);
270 fe621944 2020-11-10 stsp if (diffreg_result) {
271 fe621944 2020-11-10 stsp diff_data_free(left);
272 fe621944 2020-11-10 stsp diff_data_free(right);
273 fe621944 2020-11-10 stsp free(*diffreg_result);
274 fe621944 2020-11-10 stsp *diffreg_result = NULL;
275 fe621944 2020-11-10 stsp }
276 322260e1 2018-01-26 mpi }
277 fe621944 2020-11-10 stsp
278 fe621944 2020-11-10 stsp return err;
279 574ed2c3 2017-11-29 stsp }
280 574ed2c3 2017-11-29 stsp
281 fe621944 2020-11-10 stsp const struct got_error *
282 fe621944 2020-11-10 stsp got_diffreg_output(off_t **line_offsets, size_t *nlines,
283 1cb46f00 2020-11-21 stsp struct got_diffreg_result *diff_result, int f1_exists, int f2_exists,
284 fe621944 2020-11-10 stsp const char *path1, const char *path2,
285 fe621944 2020-11-10 stsp enum got_diff_output_format output_format, int context_lines, FILE *outfile)
286 574ed2c3 2017-11-29 stsp {
287 fe621944 2020-11-10 stsp struct diff_input_info info = {
288 fe621944 2020-11-10 stsp .left_path = path1,
289 fe621944 2020-11-10 stsp .right_path = path2,
290 1cb46f00 2020-11-21 stsp .flags = 0,
291 fe621944 2020-11-10 stsp };
292 fe621944 2020-11-10 stsp int rc;
293 fe621944 2020-11-10 stsp struct diff_output_info *output_info;
294 1cb46f00 2020-11-21 stsp
295 1cb46f00 2020-11-21 stsp if (!f1_exists)
296 1cb46f00 2020-11-21 stsp info.flags |= DIFF_INPUT_LEFT_NONEXISTENT;
297 1cb46f00 2020-11-21 stsp if (!f2_exists)
298 1cb46f00 2020-11-21 stsp info.flags |= DIFF_INPUT_RIGHT_NONEXISTENT;
299 574ed2c3 2017-11-29 stsp
300 fe621944 2020-11-10 stsp switch (output_format) {
301 fe621944 2020-11-10 stsp case GOT_DIFF_OUTPUT_UNIDIFF:
302 fe621944 2020-11-10 stsp rc = diff_output_unidiff(
303 fe621944 2020-11-10 stsp line_offsets ? &output_info : NULL, outfile, &info,
304 fe621944 2020-11-10 stsp diff_result->result, context_lines);
305 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
306 fe621944 2020-11-10 stsp return got_error_set_errno(rc, "diff_output_unidiff");
307 fe621944 2020-11-10 stsp break;
308 fe621944 2020-11-10 stsp case GOT_DIFF_OUTPUT_EDSCRIPT:
309 fe621944 2020-11-10 stsp rc = diff_output_edscript(line_offsets ? &output_info : NULL,
310 fe621944 2020-11-10 stsp outfile, &info, diff_result->result);
311 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
312 fe621944 2020-11-10 stsp return got_error_set_errno(rc, "diff_output_edscript");
313 fe621944 2020-11-10 stsp break;
314 fe621944 2020-11-10 stsp
315 574ed2c3 2017-11-29 stsp }
316 574ed2c3 2017-11-29 stsp
317 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets) {
318 fe621944 2020-11-10 stsp if (output_info->line_offsets.len > 0) {
319 fe621944 2020-11-10 stsp off_t prev_offset = 0, *p, *o;
320 fe621944 2020-11-10 stsp int i, len;
321 fe621944 2020-11-10 stsp if (*nlines > 0) {
322 fe621944 2020-11-10 stsp prev_offset = (*line_offsets)[*nlines - 1];
323 fe621944 2020-11-10 stsp /*
324 fe621944 2020-11-10 stsp * First line offset is always zero. Skip it
325 fe621944 2020-11-10 stsp * when appending to a pre-populated array.
326 fe621944 2020-11-10 stsp */
327 fe621944 2020-11-10 stsp o = &output_info->line_offsets.head[1];
328 fe621944 2020-11-10 stsp len = output_info->line_offsets.len - 1;
329 fe621944 2020-11-10 stsp } else {
330 fe621944 2020-11-10 stsp o = &output_info->line_offsets.head[0];
331 fe621944 2020-11-10 stsp len = output_info->line_offsets.len;
332 df51fc4e 2018-04-02 stsp }
333 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + len,
334 fe621944 2020-11-10 stsp sizeof(off_t));
335 fe621944 2020-11-10 stsp if (p == NULL)
336 fe621944 2020-11-10 stsp return got_error_from_errno("calloc");
337 fe621944 2020-11-10 stsp for (i = 0; i < len; i++)
338 fe621944 2020-11-10 stsp p[*nlines + i] = o[i] + prev_offset;
339 fe621944 2020-11-10 stsp *line_offsets = p;
340 fe621944 2020-11-10 stsp *nlines += len;
341 574ed2c3 2017-11-29 stsp }
342 fe621944 2020-11-10 stsp diff_output_info_free(output_info);
343 574ed2c3 2017-11-29 stsp }
344 322260e1 2018-01-26 mpi
345 fe621944 2020-11-10 stsp return NULL;
346 574ed2c3 2017-11-29 stsp }
347 574ed2c3 2017-11-29 stsp
348 fe621944 2020-11-10 stsp const struct got_error *
349 fe621944 2020-11-10 stsp got_diffreg_result_free(struct got_diffreg_result *diffreg_result)
350 574ed2c3 2017-11-29 stsp {
351 fe621944 2020-11-10 stsp const struct got_error *err;
352 574ed2c3 2017-11-29 stsp
353 fe621944 2020-11-10 stsp diff_result_free(diffreg_result->result);
354 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->left);
355 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->right);
356 fe621944 2020-11-10 stsp err = got_diffreg_close(diffreg_result->f1, diffreg_result->map1,
357 fe621944 2020-11-10 stsp diffreg_result->size1, diffreg_result->f2,
358 fe621944 2020-11-10 stsp diffreg_result->map2, diffreg_result->size2);
359 fe621944 2020-11-10 stsp free(diffreg_result);
360 fe621944 2020-11-10 stsp return err;
361 574ed2c3 2017-11-29 stsp }
362 574ed2c3 2017-11-29 stsp
363 fe621944 2020-11-10 stsp const struct got_error *
364 fe621944 2020-11-10 stsp got_diffreg_result_free_left(struct got_diffreg_result *diffreg_result)
365 574ed2c3 2017-11-29 stsp {
366 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->left);
367 fe621944 2020-11-10 stsp memset(&diffreg_result->left, 0, sizeof(diffreg_result->left));
368 fe621944 2020-11-10 stsp return got_diffreg_close(diffreg_result->f1, diffreg_result->map1,
369 fe621944 2020-11-10 stsp diffreg_result->size1, NULL, NULL, 0);
370 574ed2c3 2017-11-29 stsp }
371 574ed2c3 2017-11-29 stsp
372 fe621944 2020-11-10 stsp const struct got_error *
373 fe621944 2020-11-10 stsp got_diffreg_result_free_right(struct got_diffreg_result *diffreg_result)
374 574ed2c3 2017-11-29 stsp {
375 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->right);
376 fe621944 2020-11-10 stsp memset(&diffreg_result->right, 0, sizeof(diffreg_result->right));
377 fe621944 2020-11-10 stsp return got_diffreg_close(NULL, NULL, 0, diffreg_result->f2,
378 fe621944 2020-11-10 stsp diffreg_result->map2, diffreg_result->size2);
379 dc424a06 2019-08-07 stsp }