2 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 069bbb86 2022-03-07 thomas * Permission to use, copy, modify, and distribute this software for any
5 069bbb86 2022-03-07 thomas * purpose with or without fee is hereby granted, provided that the above
6 069bbb86 2022-03-07 thomas * copyright notice and this permission notice appear in all copies.
8 069bbb86 2022-03-07 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 069bbb86 2022-03-07 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 069bbb86 2022-03-07 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 069bbb86 2022-03-07 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 069bbb86 2022-03-07 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 069bbb86 2022-03-07 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 069bbb86 2022-03-07 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 069bbb86 2022-03-07 thomas * Apply patches.
18 069bbb86 2022-03-07 thomas * Things that are still missing:
19 069bbb86 2022-03-07 thomas * + "No final newline" handling
21 069bbb86 2022-03-07 thomas * Things that we may want to support:
22 069bbb86 2022-03-07 thomas * + support indented patches?
23 069bbb86 2022-03-07 thomas * + support other kinds of patches?
26 069bbb86 2022-03-07 thomas #include <sys/types.h>
27 069bbb86 2022-03-07 thomas #include <sys/queue.h>
28 069bbb86 2022-03-07 thomas #include <sys/socket.h>
29 7dd42450 2022-03-13 thomas #include <sys/stat.h>
30 069bbb86 2022-03-07 thomas #include <sys/uio.h>
32 42d9d68e 2022-03-13 thomas #include <errno.h>
33 069bbb86 2022-03-07 thomas #include <limits.h>
34 069bbb86 2022-03-07 thomas #include <stdint.h>
35 069bbb86 2022-03-07 thomas #include <stdio.h>
36 069bbb86 2022-03-07 thomas #include <stdlib.h>
37 069bbb86 2022-03-07 thomas #include <string.h>
38 069bbb86 2022-03-07 thomas #include <unistd.h>
39 069bbb86 2022-03-07 thomas #include <imsg.h>
41 069bbb86 2022-03-07 thomas #include "got_error.h"
42 069bbb86 2022-03-07 thomas #include "got_object.h"
43 069bbb86 2022-03-07 thomas #include "got_path.h"
44 069bbb86 2022-03-07 thomas #include "got_reference.h"
45 069bbb86 2022-03-07 thomas #include "got_cancel.h"
46 069bbb86 2022-03-07 thomas #include "got_worktree.h"
47 069bbb86 2022-03-07 thomas #include "got_opentemp.h"
48 069bbb86 2022-03-07 thomas #include "got_patch.h"
50 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
51 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
52 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
54 069bbb86 2022-03-07 thomas #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 069bbb86 2022-03-07 thomas struct got_patch_hunk {
57 069bbb86 2022-03-07 thomas STAILQ_ENTRY(got_patch_hunk) entries;
58 069bbb86 2022-03-07 thomas long old_from;
59 069bbb86 2022-03-07 thomas long old_lines;
60 069bbb86 2022-03-07 thomas long new_from;
61 069bbb86 2022-03-07 thomas long new_lines;
62 069bbb86 2022-03-07 thomas size_t len;
63 069bbb86 2022-03-07 thomas size_t cap;
64 069bbb86 2022-03-07 thomas char **lines;
67 069bbb86 2022-03-07 thomas struct got_patch {
68 069bbb86 2022-03-07 thomas char *old;
69 069bbb86 2022-03-07 thomas char *new;
70 069bbb86 2022-03-07 thomas STAILQ_HEAD(, got_patch_hunk) head;
73 c71da4f7 2022-03-22 thomas struct patch_args {
74 c71da4f7 2022-03-22 thomas got_patch_progress_cb progress_cb;
75 c71da4f7 2022-03-22 thomas void *progress_arg;
78 069bbb86 2022-03-07 thomas static const struct got_error *
79 069bbb86 2022-03-07 thomas send_patch(struct imsgbuf *ibuf, int fd)
81 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
83 069bbb86 2022-03-07 thomas if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
84 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
85 069bbb86 2022-03-07 thomas err = got_error_from_errno(
86 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_FILE");
87 069bbb86 2022-03-07 thomas close(fd);
88 069bbb86 2022-03-07 thomas return err;
91 069bbb86 2022-03-07 thomas if (imsg_flush(ibuf) == -1) {
92 069bbb86 2022-03-07 thomas err = got_error_from_errno("imsg_flush");
93 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
96 069bbb86 2022-03-07 thomas return err;
99 069bbb86 2022-03-07 thomas static void
100 069bbb86 2022-03-07 thomas patch_free(struct got_patch *p)
102 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
103 069bbb86 2022-03-07 thomas size_t i;
105 069bbb86 2022-03-07 thomas while (!STAILQ_EMPTY(&p->head)) {
106 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
107 069bbb86 2022-03-07 thomas STAILQ_REMOVE_HEAD(&p->head, entries);
109 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i)
110 069bbb86 2022-03-07 thomas free(h->lines[i]);
111 069bbb86 2022-03-07 thomas free(h->lines);
115 069bbb86 2022-03-07 thomas free(p->new);
116 069bbb86 2022-03-07 thomas free(p->old);
119 069bbb86 2022-03-07 thomas static const struct got_error *
120 069bbb86 2022-03-07 thomas pushline(struct got_patch_hunk *h, const char *line)
123 069bbb86 2022-03-07 thomas size_t newcap;
125 069bbb86 2022-03-07 thomas if (h->len == h->cap) {
126 069bbb86 2022-03-07 thomas if ((newcap = h->cap * 1.5) == 0)
127 069bbb86 2022-03-07 thomas newcap = 16;
128 069bbb86 2022-03-07 thomas t = recallocarray(h->lines, h->cap, newcap,
129 069bbb86 2022-03-07 thomas sizeof(h->lines[0]));
130 069bbb86 2022-03-07 thomas if (t == NULL)
131 069bbb86 2022-03-07 thomas return got_error_from_errno("recallocarray");
132 069bbb86 2022-03-07 thomas h->lines = t;
133 069bbb86 2022-03-07 thomas h->cap = newcap;
136 069bbb86 2022-03-07 thomas if ((t = strdup(line)) == NULL)
137 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
139 069bbb86 2022-03-07 thomas h->lines[h->len++] = t;
140 069bbb86 2022-03-07 thomas return NULL;
143 069bbb86 2022-03-07 thomas static const struct got_error *
144 069bbb86 2022-03-07 thomas recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
146 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
147 069bbb86 2022-03-07 thomas struct imsg imsg;
148 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
149 069bbb86 2022-03-07 thomas struct got_imsg_patch patch;
150 069bbb86 2022-03-07 thomas struct got_patch_hunk *h = NULL;
151 069bbb86 2022-03-07 thomas size_t datalen;
153 069bbb86 2022-03-07 thomas memset(p, 0, sizeof(*p));
154 069bbb86 2022-03-07 thomas STAILQ_INIT(&p->head);
156 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
158 069bbb86 2022-03-07 thomas return err;
159 069bbb86 2022-03-07 thomas if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
160 069bbb86 2022-03-07 thomas *done = 1;
161 069bbb86 2022-03-07 thomas goto done;
163 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH) {
164 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
165 069bbb86 2022-03-07 thomas goto done;
167 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 069bbb86 2022-03-07 thomas if (datalen != sizeof(patch)) {
169 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
170 069bbb86 2022-03-07 thomas goto done;
172 069bbb86 2022-03-07 thomas memcpy(&patch, imsg.data, sizeof(patch));
173 069bbb86 2022-03-07 thomas if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
174 069bbb86 2022-03-07 thomas err = got_error_from_errno("strdup");
175 069bbb86 2022-03-07 thomas goto done;
177 069bbb86 2022-03-07 thomas if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
178 069bbb86 2022-03-07 thomas err = got_error_from_errno("strdup");
179 069bbb86 2022-03-07 thomas goto done;
181 64c9e565 2022-03-13 thomas if (p->old == NULL && p->new == NULL) {
182 64c9e565 2022-03-13 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
183 64c9e565 2022-03-13 thomas goto done;
186 069bbb86 2022-03-07 thomas imsg_free(&imsg);
188 069bbb86 2022-03-07 thomas for (;;) {
191 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
193 069bbb86 2022-03-07 thomas return err;
195 069bbb86 2022-03-07 thomas switch (imsg.hdr.type) {
196 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_DONE:
197 069bbb86 2022-03-07 thomas goto done;
198 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_HUNK:
199 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
200 069bbb86 2022-03-07 thomas if (datalen != sizeof(hdr)) {
201 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
202 069bbb86 2022-03-07 thomas goto done;
204 069bbb86 2022-03-07 thomas memcpy(&hdr, imsg.data, sizeof(hdr));
205 069bbb86 2022-03-07 thomas if ((h = calloc(1, sizeof(*h))) == NULL) {
206 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
207 069bbb86 2022-03-07 thomas goto done;
209 069bbb86 2022-03-07 thomas h->old_from = hdr.oldfrom;
210 069bbb86 2022-03-07 thomas h->old_lines = hdr.oldlines;
211 069bbb86 2022-03-07 thomas h->new_from = hdr.newfrom;
212 069bbb86 2022-03-07 thomas h->new_lines = hdr.newlines;
213 069bbb86 2022-03-07 thomas STAILQ_INSERT_TAIL(&p->head, h, entries);
215 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_LINE:
216 069bbb86 2022-03-07 thomas if (h == NULL) {
217 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
218 069bbb86 2022-03-07 thomas goto done;
220 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
221 069bbb86 2022-03-07 thomas t = imsg.data;
222 069bbb86 2022-03-07 thomas /* at least one char plus newline */
223 069bbb86 2022-03-07 thomas if (datalen < 2 || t[datalen-1] != '\0') {
224 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
225 069bbb86 2022-03-07 thomas goto done;
227 069bbb86 2022-03-07 thomas if (*t != ' ' && *t != '-' && *t != '+') {
228 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
229 069bbb86 2022-03-07 thomas goto done;
231 069bbb86 2022-03-07 thomas err = pushline(h, t);
233 069bbb86 2022-03-07 thomas goto done;
236 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
237 069bbb86 2022-03-07 thomas goto done;
240 069bbb86 2022-03-07 thomas imsg_free(&imsg);
244 069bbb86 2022-03-07 thomas imsg_free(&imsg);
245 069bbb86 2022-03-07 thomas return err;
249 069bbb86 2022-03-07 thomas * Copy data from orig starting at copypos until pos into tmp.
250 069bbb86 2022-03-07 thomas * If pos is -1, copy until EOF.
252 069bbb86 2022-03-07 thomas static const struct got_error *
253 069bbb86 2022-03-07 thomas copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
255 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
256 069bbb86 2022-03-07 thomas size_t len, r, w;
258 069bbb86 2022-03-07 thomas if (fseek(orig, copypos, SEEK_SET) == -1)
259 069bbb86 2022-03-07 thomas return got_error_from_errno("fseek");
261 069bbb86 2022-03-07 thomas while (pos == -1 || copypos < pos) {
262 069bbb86 2022-03-07 thomas len = sizeof(buf);
263 069bbb86 2022-03-07 thomas if (pos > 0)
264 069bbb86 2022-03-07 thomas len = MIN(len, (size_t)pos - copypos);
265 069bbb86 2022-03-07 thomas r = fread(buf, 1, len, orig);
266 069bbb86 2022-03-07 thomas if (r != len && ferror(orig))
267 069bbb86 2022-03-07 thomas return got_error_from_errno("fread");
268 069bbb86 2022-03-07 thomas w = fwrite(buf, 1, r, tmp);
269 069bbb86 2022-03-07 thomas if (w != r)
270 069bbb86 2022-03-07 thomas return got_error_from_errno("fwrite");
271 069bbb86 2022-03-07 thomas copypos += len;
272 069bbb86 2022-03-07 thomas if (r != len && feof(orig)) {
273 069bbb86 2022-03-07 thomas if (pos == -1)
274 069bbb86 2022-03-07 thomas return NULL;
275 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_DONT_APPLY);
278 069bbb86 2022-03-07 thomas return NULL;
281 069bbb86 2022-03-07 thomas static const struct got_error *
282 122e1611 2022-03-22 thomas locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
284 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
285 069bbb86 2022-03-07 thomas char *line = NULL;
286 069bbb86 2022-03-07 thomas char mode = *h->lines[0];
287 069bbb86 2022-03-07 thomas size_t linesize = 0;
288 069bbb86 2022-03-07 thomas ssize_t linelen;
289 069bbb86 2022-03-07 thomas off_t match = -1;
290 069bbb86 2022-03-07 thomas long match_lineno = -1;
292 069bbb86 2022-03-07 thomas for (;;) {
293 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
294 069bbb86 2022-03-07 thomas if (linelen == -1) {
295 069bbb86 2022-03-07 thomas if (ferror(orig))
296 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
297 069bbb86 2022-03-07 thomas else if (match == -1)
298 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_DONT_APPLY);
301 069bbb86 2022-03-07 thomas (*lineno)++;
303 069bbb86 2022-03-07 thomas if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
304 069bbb86 2022-03-07 thomas (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
305 069bbb86 2022-03-07 thomas (mode == '+' && *lineno == h->old_from)) {
306 069bbb86 2022-03-07 thomas match = ftello(orig);
307 069bbb86 2022-03-07 thomas if (match == -1) {
308 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
311 069bbb86 2022-03-07 thomas match -= linelen;
312 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
315 069bbb86 2022-03-07 thomas if (*lineno >= h->old_from && match != -1)
319 069bbb86 2022-03-07 thomas if (err == NULL) {
320 122e1611 2022-03-22 thomas *pos = match;
321 069bbb86 2022-03-07 thomas *lineno = match_lineno;
322 069bbb86 2022-03-07 thomas if (fseek(orig, match, SEEK_SET) == -1)
323 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
326 069bbb86 2022-03-07 thomas free(line);
327 069bbb86 2022-03-07 thomas return err;
330 069bbb86 2022-03-07 thomas static const struct got_error *
331 069bbb86 2022-03-07 thomas test_hunk(FILE *orig, struct got_patch_hunk *h)
333 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
334 069bbb86 2022-03-07 thomas char *line = NULL;
335 069bbb86 2022-03-07 thomas size_t linesize = 0, i = 0;
336 069bbb86 2022-03-07 thomas ssize_t linelen;
338 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
339 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
340 069bbb86 2022-03-07 thomas case '+':
341 069bbb86 2022-03-07 thomas continue;
342 069bbb86 2022-03-07 thomas case ' ':
343 069bbb86 2022-03-07 thomas case '-':
344 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
345 069bbb86 2022-03-07 thomas if (linelen == -1) {
346 069bbb86 2022-03-07 thomas if (ferror(orig))
347 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
349 069bbb86 2022-03-07 thomas err = got_error(
350 069bbb86 2022-03-07 thomas GOT_ERR_PATCH_DONT_APPLY);
351 069bbb86 2022-03-07 thomas goto done;
353 069bbb86 2022-03-07 thomas if (strcmp(h->lines[i]+1, line)) {
354 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_DONT_APPLY);
355 069bbb86 2022-03-07 thomas goto done;
362 069bbb86 2022-03-07 thomas free(line);
363 069bbb86 2022-03-07 thomas return err;
366 069bbb86 2022-03-07 thomas static const struct got_error *
367 069bbb86 2022-03-07 thomas apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
369 069bbb86 2022-03-07 thomas size_t i = 0;
371 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
372 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
373 069bbb86 2022-03-07 thomas case ' ':
374 069bbb86 2022-03-07 thomas if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
375 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
376 069bbb86 2022-03-07 thomas /* fallthrough */
377 069bbb86 2022-03-07 thomas case '-':
378 069bbb86 2022-03-07 thomas (*lineno)++;
380 069bbb86 2022-03-07 thomas case '+':
381 069bbb86 2022-03-07 thomas if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
382 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
386 069bbb86 2022-03-07 thomas return NULL;
389 46e6bdd4 2022-03-22 thomas static const struct got_error *
390 c71da4f7 2022-03-22 thomas patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop)
392 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
393 bb2ad8ff 2022-03-13 thomas struct got_patch_hunk *h;
394 bb2ad8ff 2022-03-13 thomas size_t i;
395 bb2ad8ff 2022-03-13 thomas long lineno = 0;
396 bb2ad8ff 2022-03-13 thomas FILE *orig;
397 bb2ad8ff 2022-03-13 thomas off_t copypos, pos;
398 bb2ad8ff 2022-03-13 thomas char *line = NULL;
399 bb2ad8ff 2022-03-13 thomas size_t linesize = 0;
400 bb2ad8ff 2022-03-13 thomas ssize_t linelen;
402 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
403 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
404 bb2ad8ff 2022-03-13 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
405 bb2ad8ff 2022-03-13 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
407 eaf99875 2022-03-22 thomas return NULL;
408 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
409 bb2ad8ff 2022-03-13 thomas if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
410 bb2ad8ff 2022-03-13 thomas return got_error_from_errno("fprintf");
412 bb2ad8ff 2022-03-13 thomas return err;
415 069bbb86 2022-03-07 thomas if ((orig = fopen(path, "r")) == NULL) {
416 069bbb86 2022-03-07 thomas err = got_error_from_errno2("fopen", path);
417 069bbb86 2022-03-07 thomas goto done;
420 069bbb86 2022-03-07 thomas copypos = 0;
421 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
422 bb2ad8ff 2022-03-13 thomas if (h->lines == NULL)
425 069bbb86 2022-03-07 thomas tryagain:
426 122e1611 2022-03-22 thomas err = locate_hunk(orig, h, &pos, &lineno);
427 069bbb86 2022-03-07 thomas if (err != NULL)
428 069bbb86 2022-03-07 thomas goto done;
429 c71da4f7 2022-03-22 thomas if (!nop)
430 eaf99875 2022-03-22 thomas err = copy(tmp, orig, copypos, pos);
431 069bbb86 2022-03-07 thomas if (err != NULL)
432 069bbb86 2022-03-07 thomas goto done;
433 069bbb86 2022-03-07 thomas copypos = pos;
435 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
436 069bbb86 2022-03-07 thomas if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
438 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
439 069bbb86 2022-03-07 thomas * after the previous partial match.
441 069bbb86 2022-03-07 thomas if (fseek(orig, pos, SEEK_SET) == -1) {
442 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
443 069bbb86 2022-03-07 thomas goto done;
445 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
446 069bbb86 2022-03-07 thomas if (linelen == -1) {
447 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
448 069bbb86 2022-03-07 thomas goto done;
450 069bbb86 2022-03-07 thomas lineno++;
451 069bbb86 2022-03-07 thomas goto tryagain;
453 069bbb86 2022-03-07 thomas if (err != NULL)
454 069bbb86 2022-03-07 thomas goto done;
456 c71da4f7 2022-03-22 thomas if (!nop)
457 bfc91212 2022-03-13 thomas err = apply_hunk(tmp, h, &lineno);
458 069bbb86 2022-03-07 thomas if (err != NULL)
459 069bbb86 2022-03-07 thomas goto done;
461 069bbb86 2022-03-07 thomas copypos = ftello(orig);
462 069bbb86 2022-03-07 thomas if (copypos == -1) {
463 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
464 069bbb86 2022-03-07 thomas goto done;
469 eaf99875 2022-03-22 thomas if (p->new == NULL) {
470 eaf99875 2022-03-22 thomas struct stat sb;
472 eaf99875 2022-03-22 thomas if (fstat(fileno(orig), &sb) == -1)
473 eaf99875 2022-03-22 thomas err = got_error_from_errno("fstat");
474 eaf99875 2022-03-22 thomas else if (sb.st_size != copypos)
475 eaf99875 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_DONT_APPLY);
476 c71da4f7 2022-03-22 thomas } else if (!nop && !feof(orig))
477 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
480 bb2ad8ff 2022-03-13 thomas if (orig != NULL)
481 bb2ad8ff 2022-03-13 thomas fclose(orig);
482 bb2ad8ff 2022-03-13 thomas return err;
485 bb2ad8ff 2022-03-13 thomas static const struct got_error *
486 10e55613 2022-03-22 thomas build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
487 10e55613 2022-03-22 thomas struct got_worktree *worktree)
489 10e55613 2022-03-22 thomas const struct got_error *err;
490 10e55613 2022-03-22 thomas struct got_pathlist_entry *pe;
492 10e55613 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
493 10e55613 2022-03-22 thomas if (err == NULL)
494 10e55613 2022-03-22 thomas err = got_pathlist_insert(&pe, head, *path, NULL);
495 10e55613 2022-03-22 thomas return err;
498 10e55613 2022-03-22 thomas static const struct got_error *
499 10e55613 2022-03-22 thomas can_rm(void *arg, unsigned char status, unsigned char staged_status,
500 10e55613 2022-03-22 thomas const char *path, struct got_object_id *blob_id,
501 10e55613 2022-03-22 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
502 10e55613 2022-03-22 thomas int dirfd, const char *de_name)
504 10e55613 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
505 10e55613 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
506 10e55613 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
507 10e55613 2022-03-22 thomas status != GOT_STATUS_ADD &&
508 10e55613 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
509 10e55613 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
510 10e55613 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
511 10e55613 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
512 10e55613 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
513 10e55613 2022-03-22 thomas return NULL;
516 10e55613 2022-03-22 thomas static const struct got_error *
517 10e55613 2022-03-22 thomas can_add(void *arg, unsigned char status, unsigned char staged_status,
518 10e55613 2022-03-22 thomas const char *path, struct got_object_id *blob_id,
519 10e55613 2022-03-22 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
520 10e55613 2022-03-22 thomas int dirfd, const char *de_name)
522 10e55613 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
523 10e55613 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
524 10e55613 2022-03-22 thomas return NULL;
527 10e55613 2022-03-22 thomas static const struct got_error *
528 10e55613 2022-03-22 thomas can_edit(void *arg, unsigned char status, unsigned char staged_status,
529 10e55613 2022-03-22 thomas const char *path, struct got_object_id *blob_id,
530 10e55613 2022-03-22 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
531 10e55613 2022-03-22 thomas int dirfd, const char *de_name)
533 10e55613 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
534 10e55613 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
535 10e55613 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
536 10e55613 2022-03-22 thomas status != GOT_STATUS_ADD &&
537 10e55613 2022-03-22 thomas status != GOT_STATUS_MODIFY)
538 10e55613 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
539 10e55613 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
540 10e55613 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
541 10e55613 2022-03-22 thomas return NULL;
544 10e55613 2022-03-22 thomas static const struct got_error *
545 10e55613 2022-03-22 thomas check_file_status(struct got_patch *p, int file_renamed,
546 10e55613 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo,
547 10e55613 2022-03-22 thomas struct got_pathlist_head *old, struct got_pathlist_head *new,
548 10e55613 2022-03-22 thomas got_cancel_cb cancel_cb, void *cancel_arg)
550 10e55613 2022-03-22 thomas static const struct got_error *err;
552 10e55613 2022-03-22 thomas if (p->old != NULL && p->new == NULL)
553 10e55613 2022-03-22 thomas return got_worktree_status(worktree, old, repo, 0,
554 10e55613 2022-03-22 thomas can_rm, NULL, cancel_cb, cancel_arg);
555 10e55613 2022-03-22 thomas else if (file_renamed) {
556 10e55613 2022-03-22 thomas err = got_worktree_status(worktree, old, repo, 0,
557 10e55613 2022-03-22 thomas can_rm, NULL, cancel_cb, cancel_arg);
559 10e55613 2022-03-22 thomas return err;
560 10e55613 2022-03-22 thomas return got_worktree_status(worktree, new, repo, 0,
561 10e55613 2022-03-22 thomas can_add, NULL, cancel_cb, cancel_arg);
562 10e55613 2022-03-22 thomas } else if (p->old == NULL)
563 10e55613 2022-03-22 thomas return got_worktree_status(worktree, new, repo, 0,
564 10e55613 2022-03-22 thomas can_add, NULL, cancel_cb, cancel_arg);
566 10e55613 2022-03-22 thomas return got_worktree_status(worktree, new, repo, 0,
567 10e55613 2022-03-22 thomas can_edit, NULL, cancel_cb, cancel_arg);
570 10e55613 2022-03-22 thomas static const struct got_error *
571 c71da4f7 2022-03-22 thomas patch_delete(void *arg, unsigned char status, unsigned char staged_status,
572 c71da4f7 2022-03-22 thomas const char *path)
574 c71da4f7 2022-03-22 thomas struct patch_args *pa = arg;
576 c71da4f7 2022-03-22 thomas return pa->progress_cb(pa->progress_arg, path, NULL, status);
579 c71da4f7 2022-03-22 thomas static const struct got_error *
580 c71da4f7 2022-03-22 thomas patch_add(void *arg, unsigned char status, const char *path)
582 c71da4f7 2022-03-22 thomas struct patch_args *pa = arg;
584 c71da4f7 2022-03-22 thomas return pa->progress_cb(pa->progress_arg, NULL, path, status);
587 c71da4f7 2022-03-22 thomas static const struct got_error *
588 bb2ad8ff 2022-03-13 thomas apply_patch(struct got_worktree *worktree, struct got_repository *repo,
589 c71da4f7 2022-03-22 thomas struct got_patch *p, int nop, struct patch_args *pa,
590 c71da4f7 2022-03-22 thomas got_cancel_cb cancel_cb, void *cancel_arg)
592 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
593 10e55613 2022-03-22 thomas struct got_pathlist_head oldpaths, newpaths;
594 bb2ad8ff 2022-03-13 thomas int file_renamed = 0;
595 bb2ad8ff 2022-03-13 thomas char *oldpath = NULL, *newpath = NULL;
596 bb2ad8ff 2022-03-13 thomas char *tmppath = NULL, *template = NULL;
597 bb2ad8ff 2022-03-13 thomas FILE *tmp = NULL;
599 10e55613 2022-03-22 thomas TAILQ_INIT(&oldpaths);
600 10e55613 2022-03-22 thomas TAILQ_INIT(&newpaths);
602 10e55613 2022-03-22 thomas err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
603 10e55613 2022-03-22 thomas &oldpaths, worktree);
605 bb2ad8ff 2022-03-13 thomas goto done;
607 10e55613 2022-03-22 thomas err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
608 10e55613 2022-03-22 thomas &newpaths, worktree);
610 bb2ad8ff 2022-03-13 thomas goto done;
612 10e55613 2022-03-22 thomas if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
613 10e55613 2022-03-22 thomas file_renamed = 1;
615 10e55613 2022-03-22 thomas err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
616 10e55613 2022-03-22 thomas &newpaths, cancel_cb, cancel_arg);
618 10e55613 2022-03-22 thomas goto done;
620 46e6bdd4 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
622 46e6bdd4 2022-03-22 thomas * special case: delete a file. don't try to match
623 46e6bdd4 2022-03-22 thomas * the lines but just schedule the removal.
625 10e55613 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
626 10e55613 2022-03-22 thomas 0, NULL, delete_cb, delete_arg, repo, 0, 0);
627 42d9d68e 2022-03-13 thomas goto done;
630 bb2ad8ff 2022-03-13 thomas if (asprintf(&template, "%s/got-patch",
631 bb2ad8ff 2022-03-13 thomas got_worktree_get_root_path(worktree)) == -1) {
632 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno(template);
633 069bbb86 2022-03-07 thomas goto done;
636 c71da4f7 2022-03-22 thomas if (!nop)
637 eaf99875 2022-03-22 thomas err = got_opentemp_named(&tmppath, &tmp, template);
639 bb2ad8ff 2022-03-13 thomas goto done;
640 c71da4f7 2022-03-22 thomas err = patch_file(p, oldpath, tmp, nop);
642 bb2ad8ff 2022-03-13 thomas goto done;
645 eaf99875 2022-03-22 thomas goto done;
647 eaf99875 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
648 eaf99875 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
649 c71da4f7 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
650 eaf99875 2022-03-22 thomas goto done;
653 bb2ad8ff 2022-03-13 thomas if (rename(tmppath, newpath) == -1) {
654 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno3("rename", tmppath, newpath);
655 bb2ad8ff 2022-03-13 thomas goto done;
658 bb2ad8ff 2022-03-13 thomas if (file_renamed) {
659 10e55613 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
660 c71da4f7 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
661 bb2ad8ff 2022-03-13 thomas if (err == NULL)
662 10e55613 2022-03-22 thomas err = got_worktree_schedule_add(worktree, &newpaths,
663 c71da4f7 2022-03-22 thomas patch_add, pa, repo, 1);
664 bb2ad8ff 2022-03-13 thomas } else if (p->old == NULL)
665 10e55613 2022-03-22 thomas err = got_worktree_schedule_add(worktree, &newpaths,
666 c71da4f7 2022-03-22 thomas patch_add, pa, repo, 1);
668 c71da4f7 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, oldpath, newpath,
669 c71da4f7 2022-03-22 thomas GOT_STATUS_MODIFY);
672 10e55613 2022-03-22 thomas if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
673 bb2ad8ff 2022-03-13 thomas unlink(newpath);
674 94af5a06 2022-03-08 thomas free(template);
675 069bbb86 2022-03-07 thomas if (tmppath != NULL)
676 069bbb86 2022-03-07 thomas unlink(tmppath);
677 069bbb86 2022-03-07 thomas free(tmppath);
678 10e55613 2022-03-22 thomas got_pathlist_free(&oldpaths);
679 10e55613 2022-03-22 thomas got_pathlist_free(&newpaths);
680 bb2ad8ff 2022-03-13 thomas free(oldpath);
681 bb2ad8ff 2022-03-13 thomas free(newpath);
682 069bbb86 2022-03-07 thomas return err;
685 069bbb86 2022-03-07 thomas const struct got_error *
686 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
687 c71da4f7 2022-03-22 thomas int nop, got_patch_progress_cb progress_cb, void *progress_arg,
688 c71da4f7 2022-03-22 thomas got_cancel_cb cancel_cb, void *cancel_arg)
690 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
691 c71da4f7 2022-03-22 thomas struct patch_args pa;
692 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
693 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
694 069bbb86 2022-03-07 thomas int done = 0;
695 069bbb86 2022-03-07 thomas pid_t pid;
697 c71da4f7 2022-03-22 thomas pa.progress_cb = progress_cb;
698 c71da4f7 2022-03-22 thomas pa.progress_arg = progress_arg;
700 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
701 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
702 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
703 069bbb86 2022-03-07 thomas goto done;
706 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
707 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
708 069bbb86 2022-03-07 thomas goto done;
711 069bbb86 2022-03-07 thomas pid = fork();
712 069bbb86 2022-03-07 thomas if (pid == -1) {
713 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
714 069bbb86 2022-03-07 thomas goto done;
715 069bbb86 2022-03-07 thomas } else if (pid == 0) {
716 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
718 069bbb86 2022-03-07 thomas /* not reached */
721 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
722 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
723 069bbb86 2022-03-07 thomas goto done;
725 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
726 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
728 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
731 069bbb86 2022-03-07 thomas goto done;
733 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
734 069bbb86 2022-03-07 thomas struct got_patch p;
736 069bbb86 2022-03-07 thomas err = recv_patch(ibuf, &done, &p);
737 069bbb86 2022-03-07 thomas if (err || done)
740 c71da4f7 2022-03-22 thomas err = apply_patch(worktree, repo, &p, nop, &pa,
741 c71da4f7 2022-03-22 thomas cancel_cb, cancel_arg);
742 069bbb86 2022-03-07 thomas patch_free(&p);
748 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
749 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
750 069bbb86 2022-03-07 thomas if (ibuf != NULL)
751 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
752 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
753 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
754 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
755 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
756 069bbb86 2022-03-07 thomas return err;