2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
5 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
6 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
8 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 e9ce266e 2022-03-07 op * Apply patches.
18 e9ce266e 2022-03-07 op * Things that we may want to support:
19 e9ce266e 2022-03-07 op * + support indented patches?
20 e9ce266e 2022-03-07 op * + support other kinds of patches?
23 e9ce266e 2022-03-07 op #include <sys/types.h>
24 e9ce266e 2022-03-07 op #include <sys/queue.h>
25 e9ce266e 2022-03-07 op #include <sys/socket.h>
26 5b67f96e 2022-03-13 op #include <sys/stat.h>
27 e9ce266e 2022-03-07 op #include <sys/uio.h>
29 a92a2042 2022-07-02 op #include <ctype.h>
30 dbda770b 2022-03-13 op #include <errno.h>
31 e9ce266e 2022-03-07 op #include <limits.h>
32 e9ce266e 2022-03-07 op #include <sha1.h>
33 e9ce266e 2022-03-07 op #include <stdint.h>
34 e9ce266e 2022-03-07 op #include <stdio.h>
35 e9ce266e 2022-03-07 op #include <stdlib.h>
36 e9ce266e 2022-03-07 op #include <string.h>
37 e9ce266e 2022-03-07 op #include <unistd.h>
38 e9ce266e 2022-03-07 op #include <imsg.h>
40 e9ce266e 2022-03-07 op #include "got_error.h"
41 e9ce266e 2022-03-07 op #include "got_object.h"
42 e9ce266e 2022-03-07 op #include "got_path.h"
43 e9ce266e 2022-03-07 op #include "got_reference.h"
44 e9ce266e 2022-03-07 op #include "got_cancel.h"
45 e9ce266e 2022-03-07 op #include "got_worktree.h"
46 55e9459f 2022-06-19 op #include "got_repository.h"
47 e9ce266e 2022-03-07 op #include "got_opentemp.h"
48 e9ce266e 2022-03-07 op #include "got_patch.h"
49 4b752015 2022-06-30 stsp #include "got_diff.h"
51 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
52 55e9459f 2022-06-19 op #include "got_lib_diff.h"
53 e9ce266e 2022-03-07 op #include "got_lib_object.h"
54 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
55 55e9459f 2022-06-19 op #include "got_lib_sha1.h"
57 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 e9ce266e 2022-03-07 op struct got_patch_hunk {
60 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
61 60aa1fa0 2022-03-17 op const struct got_error *err;
62 a92a2042 2022-07-02 op int ws_mangled;
67 35095610 2022-06-14 op int old_lines;
69 35095610 2022-06-14 op int new_lines;
75 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
76 e9ce266e 2022-03-07 op struct got_patch {
80 55e9459f 2022-06-19 op char blob[41];
81 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
84 b22138f5 2022-03-16 op struct patch_args {
85 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
86 b22138f5 2022-03-16 op void *progress_arg;
87 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
90 e9ce266e 2022-03-07 op static const struct got_error *
91 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
93 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
95 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
96 e9ce266e 2022-03-07 op NULL, 0) == -1) {
97 e9ce266e 2022-03-07 op err = got_error_from_errno(
98 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
103 08f44789 2022-07-07 op return got_privsep_flush_imsg(ibuf);
107 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
109 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
112 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
113 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
114 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
116 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
117 e9ce266e 2022-03-07 op free(h->lines[i]);
118 e9ce266e 2022-03-07 op free(h->lines);
122 e9ce266e 2022-03-07 op free(p->new);
123 e9ce266e 2022-03-07 op free(p->old);
125 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
126 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
129 e9ce266e 2022-03-07 op static const struct got_error *
130 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
133 e9ce266e 2022-03-07 op size_t newcap;
135 e9ce266e 2022-03-07 op if (h->len == h->cap) {
136 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
138 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
139 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
140 e9ce266e 2022-03-07 op if (t == NULL)
141 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
142 e9ce266e 2022-03-07 op h->lines = t;
143 e9ce266e 2022-03-07 op h->cap = newcap;
146 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
147 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
149 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
153 e9ce266e 2022-03-07 op static const struct got_error *
154 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
156 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
157 e9ce266e 2022-03-07 op struct imsg imsg;
158 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
159 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
160 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
161 e9ce266e 2022-03-07 op size_t datalen;
162 b2832778 2022-04-23 op int lastmode = -1;
164 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
165 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
167 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
174 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
178 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
180 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
183 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
185 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
187 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
189 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
193 497a5915 2022-06-27 op if (*patch.cid != '\0')
194 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
196 497a5915 2022-06-27 op if (*patch.blob != '\0')
197 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
199 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
200 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
201 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
206 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
210 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
211 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
216 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
217 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
222 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
223 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
227 e9ce266e 2022-03-07 op imsg_free(&imsg);
232 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
234 0e07a2a1 2022-06-13 op patch_free(p);
238 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
240 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
241 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
242 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
244 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
245 be33dff7 2022-05-13 op if (h != NULL &&
246 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
250 b2832778 2022-04-23 op lastmode = -1;
251 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
252 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
255 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
256 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
260 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
261 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
264 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
265 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
266 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
267 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
268 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
270 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
271 e9ce266e 2022-03-07 op if (h == NULL) {
272 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
275 e9ce266e 2022-03-07 op t = imsg.data;
276 b3c57ab2 2022-03-22 op /* at least one char */
277 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
278 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
281 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
282 b3c57ab2 2022-03-22 op *t != '\\') {
283 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
287 b2832778 2022-04-23 op if (*t != '\\')
288 b3c57ab2 2022-03-22 op err = pushline(h, t);
289 b2832778 2022-04-23 op else if (lastmode == '-')
290 b2832778 2022-04-23 op h->old_nonl = 1;
291 b2832778 2022-04-23 op else if (lastmode == '+')
292 b2832778 2022-04-23 op h->new_nonl = 1;
294 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
299 b2832778 2022-04-23 op lastmode = *t;
302 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
306 e9ce266e 2022-03-07 op imsg_free(&imsg);
311 0e07a2a1 2022-06-13 op patch_free(p);
313 e9ce266e 2022-03-07 op imsg_free(&imsg);
318 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
319 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
321 e9ce266e 2022-03-07 op static const struct got_error *
322 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
324 e9ce266e 2022-03-07 op char buf[BUFSIZ];
325 e9ce266e 2022-03-07 op size_t len, r, w;
327 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
328 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
330 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
331 e9ce266e 2022-03-07 op len = sizeof(buf);
333 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
334 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
335 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
336 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
337 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
339 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
340 e9ce266e 2022-03-07 op copypos += len;
341 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
342 e9ce266e 2022-03-07 op if (pos == -1)
344 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
350 e9ce266e 2022-03-07 op static const struct got_error *
351 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
353 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
354 e9ce266e 2022-03-07 op char *line = NULL;
355 e9ce266e 2022-03-07 op char mode = *h->lines[0];
356 e9ce266e 2022-03-07 op size_t linesize = 0;
357 e9ce266e 2022-03-07 op ssize_t linelen;
358 e9ce266e 2022-03-07 op off_t match = -1;
359 35095610 2022-06-14 op int match_lineno = -1;
362 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
363 e9ce266e 2022-03-07 op if (linelen == -1) {
364 e9ce266e 2022-03-07 op if (ferror(orig))
365 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
366 e9ce266e 2022-03-07 op else if (match == -1)
367 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
370 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
371 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
374 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
375 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
376 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
377 e9ce266e 2022-03-07 op match = ftello(orig);
378 e9ce266e 2022-03-07 op if (match == -1) {
379 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
382 e9ce266e 2022-03-07 op match -= linelen;
383 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
386 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
390 e9ce266e 2022-03-07 op if (err == NULL) {
391 33df9995 2022-03-11 op *pos = match;
392 e9ce266e 2022-03-07 op *lineno = match_lineno;
393 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
394 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
402 a92a2042 2022-07-02 op linecmp(const char *a, const char *b, int *mangled)
406 a92a2042 2022-07-02 op *mangled = 0;
407 a92a2042 2022-07-02 op c = strcmp(a, b);
411 a92a2042 2022-07-02 op *mangled = 1;
413 2c986b8f 2022-07-04 op while (*a == '\t' || *a == ' ' || *a == '\f')
415 2c986b8f 2022-07-04 op while (*b == '\t' || *b == ' ' || *b == '\f')
417 2c986b8f 2022-07-04 op if (*a == '\0' || *a != *b)
422 a92a2042 2022-07-02 op return *a - *b;
425 e9ce266e 2022-03-07 op static const struct got_error *
426 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
428 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
429 e9ce266e 2022-03-07 op char *line = NULL;
430 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
431 e9ce266e 2022-03-07 op ssize_t linelen;
434 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
435 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
440 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
441 e9ce266e 2022-03-07 op if (linelen == -1) {
442 e9ce266e 2022-03-07 op if (ferror(orig))
443 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
445 e9ce266e 2022-03-07 op err = got_error(
446 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
449 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
450 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
451 a92a2042 2022-07-02 op if (linecmp(h->lines[i] + 1, line, &mangled)) {
452 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
456 a92a2042 2022-07-02 op h->ws_mangled = 1;
466 e9ce266e 2022-03-07 op static const struct got_error *
467 a92a2042 2022-07-02 op apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
470 a92a2042 2022-07-02 op const struct got_error *err = NULL;
471 a92a2042 2022-07-02 op const char *t;
472 a92a2042 2022-07-02 op size_t linesize = 0, i, new = 0;
473 a92a2042 2022-07-02 op char *line = NULL;
475 a92a2042 2022-07-02 op ssize_t linelen;
477 a92a2042 2022-07-02 op if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
478 a92a2042 2022-07-02 op return got_error_from_errno("fseeko");
480 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
481 a92a2042 2022-07-02 op switch (mode = *h->lines[i]) {
485 a92a2042 2022-07-02 op if (orig != NULL) {
486 a92a2042 2022-07-02 op linelen = getline(&line, &linesize, orig);
487 a92a2042 2022-07-02 op if (linelen == -1) {
488 a92a2042 2022-07-02 op err = got_error_from_errno("getline");
491 a92a2042 2022-07-02 op if (line[linelen - 1] == '\n')
492 a92a2042 2022-07-02 op line[linelen - 1] = '\0';
495 a92a2042 2022-07-02 op t = h->lines[i] + 1;
496 a92a2042 2022-07-02 op if (mode == '-')
498 a92a2042 2022-07-02 op if (fprintf(tmp, "%s\n", t) < 0) {
499 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
505 a92a2042 2022-07-02 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
506 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
509 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
510 a92a2042 2022-07-02 op if (fprintf(tmp, "\n") < 0) {
511 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
524 6e96b326 2022-03-12 op static const struct got_error *
525 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
527 6e96b326 2022-03-12 op const struct got_error *err = NULL;
528 6e96b326 2022-03-12 op struct got_patch_hunk *h;
529 2be5e1a2 2022-03-16 op struct stat sb;
530 35095610 2022-06-14 op int lineno = 0;
531 6e96b326 2022-03-12 op off_t copypos, pos;
532 6e96b326 2022-03-12 op char *line = NULL;
533 6e96b326 2022-03-12 op size_t linesize = 0;
534 6e96b326 2022-03-12 op ssize_t linelen;
536 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
537 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
538 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
539 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
540 a92a2042 2022-07-02 op return apply_hunk(orig, tmp, h, &lineno, 0);
543 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
544 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
547 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
549 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
550 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
551 60aa1fa0 2022-03-17 op h->err = err;
552 e9ce266e 2022-03-07 op if (err != NULL)
554 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
555 e9ce266e 2022-03-07 op if (err != NULL)
557 e9ce266e 2022-03-07 op copypos = pos;
559 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
560 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
562 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
563 e9ce266e 2022-03-07 op * after the previous partial match.
565 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
566 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
567 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
568 827bcd6c 2022-06-19 op if (linelen == -1)
569 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
571 e9ce266e 2022-03-07 op goto tryagain;
573 e9ce266e 2022-03-07 op if (err != NULL)
576 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
577 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
579 a92a2042 2022-07-02 op err = apply_hunk(orig, tmp, h, &lineno, pos);
580 e9ce266e 2022-03-07 op if (err != NULL)
583 e9ce266e 2022-03-07 op copypos = ftello(orig);
584 827bcd6c 2022-06-19 op if (copypos == -1)
585 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
588 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
589 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
590 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
591 60aa1fa0 2022-03-17 op err = h->err;
592 05737d49 2022-06-19 op } else if (!feof(orig))
593 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
598 dbda770b 2022-03-13 op static const struct got_error *
599 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
600 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
602 60aa1fa0 2022-03-17 op const struct got_error *err;
603 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
605 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
606 a92a2042 2022-07-02 op orig_error, 0, 0, 0, 0, 0, 0, NULL);
610 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
611 a92a2042 2022-07-02 op if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
614 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
615 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
616 a92a2042 2022-07-02 op h->offset, h->ws_mangled, h->err);
624 60aa1fa0 2022-03-17 op static const struct got_error *
625 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
626 b22138f5 2022-03-16 op const char *path)
628 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
631 b22138f5 2022-03-16 op static const struct got_error *
632 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
634 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
637 b22138f5 2022-03-16 op static const struct got_error *
638 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
639 55e9459f 2022-06-19 op struct got_repository *repo)
641 6e96b326 2022-03-12 op const struct got_error *err = NULL;
642 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
643 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
644 eb81bc23 2022-06-28 tracey int fd = -1;
647 55e9459f 2022-06-19 op *path = NULL;
649 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
650 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
651 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
655 db0dfdd7 2022-06-27 op idptr = matched_id;
657 db0dfdd7 2022-06-27 op if (!got_parse_sha1_digest(id.sha1, blobid))
658 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
662 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
663 eb81bc23 2022-06-28 tracey if (fd == -1) {
664 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
665 eb81bc23 2022-06-28 tracey goto done;
668 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
672 55e9459f 2022-06-19 op err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
676 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
681 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
682 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
684 55e9459f 2022-06-19 op got_object_blob_close(blob);
685 db0dfdd7 2022-06-27 op if (matched_id != NULL)
686 db0dfdd7 2022-06-27 op free(matched_id);
688 55e9459f 2022-06-19 op if (*fp != NULL)
690 55e9459f 2022-06-19 op if (*path != NULL)
691 55e9459f 2022-06-19 op unlink(*path);
694 55e9459f 2022-06-19 op *path = NULL;
699 55e9459f 2022-06-19 op static const struct got_error *
700 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
701 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
702 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
703 55e9459f 2022-06-19 op struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
705 55e9459f 2022-06-19 op const struct got_error *err = NULL;
706 5dffb1a1 2022-07-02 op struct stat sb;
707 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
708 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
709 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
710 1b1b91ab 2022-06-15 op char *tmppath = NULL, *template = NULL, *parent = NULL;
711 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
712 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
714 55e9459f 2022-06-19 op const char *outpath;
715 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
717 55e9459f 2022-06-19 op *overlapcnt = 0;
719 55e9459f 2022-06-19 op /* don't run the diff3 merge on creations/deletions */
720 55e9459f 2022-06-19 op if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
721 55e9459f 2022-06-19 op err = open_blob(&apath, &afile, p->blob, repo);
723 dbc68eed 2022-06-21 op * ignore failures to open this blob, we might have
724 dbc68eed 2022-06-21 op * parsed gibberish.
726 db0dfdd7 2022-06-27 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
727 db0dfdd7 2022-06-27 op err->code != GOT_ERR_NO_OBJ)
729 55e9459f 2022-06-19 op else if (err == NULL)
730 55e9459f 2022-06-19 op do_merge = 1;
734 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
735 ed3bff83 2022-04-23 op old) == -1) {
736 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
740 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
741 ed3bff83 2022-04-23 op new) == -1) {
742 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
746 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
748 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
749 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
750 6e96b326 2022-03-12 op err = got_error_from_errno(template);
754 5dffb1a1 2022-07-02 op if (p->old != NULL) {
755 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
756 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
759 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
760 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
763 5dffb1a1 2022-07-02 op mode = sb.st_mode;
766 55e9459f 2022-06-19 op err = got_opentemp_named(&tmppath, &tmpfile, template);
769 55e9459f 2022-06-19 op outpath = tmppath;
770 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
771 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
775 55e9459f 2022-06-19 op if (do_merge) {
776 497a5915 2022-06-27 op const char *type, *id;
778 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
779 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
780 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
781 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
785 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
786 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
787 55e9459f 2022-06-19 op oldlabel = NULL;
791 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
792 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
793 55e9459f 2022-06-19 op newlabel = NULL;
797 497a5915 2022-06-27 op if (*p->cid != '\0') {
798 497a5915 2022-06-27 op type = "commit";
801 497a5915 2022-06-27 op type = "blob";
802 497a5915 2022-06-27 op id = p->blob;
805 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
806 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
807 d8b5af43 2022-06-19 op anclabel = NULL;
811 55e9459f 2022-06-19 op err = got_opentemp_named(&mergepath, &mergefile, template);
814 55e9459f 2022-06-19 op outpath = mergepath;
815 55e9459f 2022-06-19 op outfd = fileno(mergefile);
817 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
818 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
819 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
827 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
828 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
829 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
833 55e9459f 2022-06-19 op if (fchmod(outfd, mode) == -1) {
834 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
838 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
839 95d68340 2022-03-16 op if (errno != ENOENT) {
840 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
845 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
846 95d68340 2022-03-16 op if (err != NULL)
848 95d68340 2022-03-16 op err = got_path_mkdir(parent);
849 95d68340 2022-03-16 op if (err != NULL)
851 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
852 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
858 6e96b326 2022-03-12 op if (file_renamed) {
859 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
860 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
861 6e96b326 2022-03-12 op if (err == NULL)
862 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
863 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
866 78f5ac24 2022-03-19 op unlink(newpath);
867 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
868 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
869 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
871 78f5ac24 2022-03-19 op unlink(newpath);
872 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
873 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
874 9802c41c 2022-06-21 op else if (do_merge)
875 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
877 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
880 95d68340 2022-03-16 op free(parent);
881 6f5cb1bd 2022-03-08 op free(template);
883 e9ce266e 2022-03-07 op if (tmppath != NULL)
884 e9ce266e 2022-03-07 op unlink(tmppath);
885 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
886 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
887 e9ce266e 2022-03-07 op free(tmppath);
889 ed3bff83 2022-04-23 op free(oldpath);
890 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
891 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
893 55e9459f 2022-06-19 op if (apath != NULL)
894 55e9459f 2022-06-19 op unlink(apath);
895 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
896 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
899 55e9459f 2022-06-19 op if (mergepath != NULL)
900 55e9459f 2022-06-19 op unlink(mergepath);
901 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
902 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
903 55e9459f 2022-06-19 op free(mergepath);
905 ed3bff83 2022-04-23 op free(newpath);
906 55e9459f 2022-06-19 op free(oldlabel);
907 55e9459f 2022-06-19 op free(newlabel);
908 d8b5af43 2022-06-19 op free(anclabel);
913 bad961bf 2022-04-23 op reverse_patch(struct got_patch *p)
915 bad961bf 2022-04-23 op struct got_patch_hunk *h;
919 bad961bf 2022-04-23 op STAILQ_FOREACH(h, &p->head, entries) {
920 bad961bf 2022-04-23 op tmp = h->old_from;
921 bad961bf 2022-04-23 op h->old_from = h->new_from;
922 bad961bf 2022-04-23 op h->new_from = tmp;
924 bad961bf 2022-04-23 op tmp = h->old_lines;
925 bad961bf 2022-04-23 op h->old_lines = h->new_lines;
926 bad961bf 2022-04-23 op h->new_lines = tmp;
928 bad961bf 2022-04-23 op tmp = h->old_nonl;
929 bad961bf 2022-04-23 op h->old_nonl = h->new_nonl;
930 bad961bf 2022-04-23 op h->new_nonl = tmp;
932 bad961bf 2022-04-23 op for (i = 0; i < h->len; ++i) {
933 bad961bf 2022-04-23 op if (*h->lines[i] == '+')
934 bad961bf 2022-04-23 op *h->lines[i] = '-';
935 bad961bf 2022-04-23 op else if (*h->lines[i] == '-')
936 bad961bf 2022-04-23 op *h->lines[i] = '+';
941 e9ce266e 2022-03-07 op const struct got_error *
942 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
943 bad961bf 2022-04-23 op int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
944 bad961bf 2022-04-23 op void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
946 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
947 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
948 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
949 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
950 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
951 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
952 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
955 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
956 e9ce266e 2022-03-07 op if (ibuf == NULL) {
957 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
961 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
962 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
966 e9ce266e 2022-03-07 op pid = fork();
967 e9ce266e 2022-03-07 op if (pid == -1) {
968 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
970 e9ce266e 2022-03-07 op } else if (pid == 0) {
971 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
973 e9ce266e 2022-03-07 op /* not reached */
976 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
977 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
980 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
981 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
983 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
988 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
993 e9ce266e 2022-03-07 op while (!done && err == NULL) {
994 e9ce266e 2022-03-07 op struct got_patch p;
995 60aa1fa0 2022-03-17 op struct patch_args pa;
997 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
998 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
999 60aa1fa0 2022-03-17 op pa.head = &p.head;
1001 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
1002 e9ce266e 2022-03-07 op if (err || done)
1005 bad961bf 2022-04-23 op if (reverse)
1006 bad961bf 2022-04-23 op reverse_patch(&p);
1008 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1009 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
1010 78f5ac24 2022-03-19 op if (err == NULL)
1011 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
1012 55e9459f 2022-06-19 op fileindex, oldpath, newpath, &p, nop, &pa,
1013 55e9459f 2022-06-19 op cancel_cb, cancel_arg);
1014 60aa1fa0 2022-03-17 op if (err != NULL) {
1016 60aa1fa0 2022-03-17 op /* recoverable errors */
1017 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
1018 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1019 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1020 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
1021 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
1022 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1023 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
1025 55e9459f 2022-06-19 op if (overlapcnt != 0)
1028 60aa1fa0 2022-03-17 op free(oldpath);
1029 60aa1fa0 2022-03-17 op free(newpath);
1030 e9ce266e 2022-03-07 op patch_free(&p);
1037 4bcdc895 2022-05-17 op if (fileindex != NULL)
1038 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
1039 4bcdc895 2022-05-17 op fileindex_path);
1040 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
1041 f2dd7807 2022-05-17 op err = complete_err;
1042 4bcdc895 2022-05-17 op free(fileindex_path);
1043 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
1044 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1045 e9ce266e 2022-03-07 op if (ibuf != NULL)
1046 e9ce266e 2022-03-07 op imsg_clear(ibuf);
1047 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1048 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1049 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1050 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1051 60aa1fa0 2022-03-17 op if (err == NULL && failed)
1052 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);