2 * Copyright 1986, Larry Wall
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
25 * Permission to use, copy, modify, and distribute this software for any
26 * purpose with or without fee is hereby granted, provided that the above
27 * copyright notice and this permission notice appear in all copies.
29 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
30 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
32 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
33 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
34 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
35 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 #include "got_compat.h"
39 #include <sys/types.h>
40 #include <sys/queue.h>
53 #include "got_error.h"
54 #include "got_object.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_hash.h"
58 #include "got_lib_object.h"
59 #include "got_lib_privsep.h"
63 static const struct got_error
*
64 send_patch(const char *oldname
, const char *newname
, const char *commitid
,
65 const char *blob
, const int xbit
, int git
)
67 struct got_imsg_patch p
;
69 memset(&p
, 0, sizeof(p
));
72 strlcpy(p
.old
, oldname
, sizeof(p
.old
));
75 strlcpy(p
.new, newname
, sizeof(p
.new));
78 strlcpy(p
.cid
, commitid
, sizeof(p
.cid
));
81 strlcpy(p
.blob
, blob
, sizeof(p
.blob
));
85 if (imsg_compose(&ibuf
, GOT_IMSG_PATCH
, 0, 0, -1, &p
, sizeof(p
)) == -1)
86 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
90 static const struct got_error
*
93 if (imsg_compose(&ibuf
, GOT_IMSG_PATCH_DONE
, 0, 0, -1,
95 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
96 return got_privsep_flush_imsg(&ibuf
);
99 /* based on fetchname from usr.bin/patch/util.c */
100 static const struct got_error
*
101 filename(const char *at
, char **name
)
109 while (isspace((unsigned char)*at
))
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at
, _PATH_DEVNULL
, sizeof(_PATH_DEVNULL
) - 1))
118 return got_error_from_errno("strdup");
119 if ((t
= strchr(tmp
, '\t')) != NULL
)
121 if ((t
= strchr(tmp
, '\n')) != NULL
)
127 return got_error_from_errno("strdup");
132 binary_deleted(const char *line
)
134 const char *prefix
= "Binary files ";
135 const char *suffix
= " and /dev/null differ\n";
138 if (strncmp(line
, prefix
, strlen(prefix
)) != 0)
140 line
+= strlen(prefix
);
143 if (len
<= strlen(suffix
))
145 d
= len
- strlen(suffix
);
146 return (strcmp(line
+ d
, suffix
) == 0);
149 static const struct got_error
*
150 binaryfilename(const char *at
, char **name
)
152 const char *suffix
= " and /dev/null differ\n";
158 if (len
<= strlen(suffix
))
161 d
= len
- strlen(suffix
);
162 if (strcmp(at
+ d
, suffix
) != 0)
165 *name
= strndup(at
, d
);
167 return got_error_from_errno("strndup");
172 filexbit(const char *line
)
176 m
= strchr(line
, '(');
177 if (m
&& !strncmp(m
+ 1, "mode ", 5))
178 return strncmp(m
+ 6, "755", 3) == 0;
183 static const struct got_error
*
184 blobid(const char *line
, char **blob
, int git
)
190 len
= strspn(line
, "0123456789abcdefABCDEF");
191 if ((*blob
= strndup(line
, len
)) == NULL
)
192 return got_error_from_errno("strndup");
197 if (len
!= got_hash_digest_string_length(GOT_HASH_SHA1
) - 1 &&
198 len
!= got_hash_digest_string_length(GOT_HASH_SHA256
) - 1) {
199 /* silently ignore invalid blob ids */
206 static const struct got_error
*
207 patch_start(int *git
, char **cid
, FILE *fp
)
209 const struct got_error
*err
= NULL
;
216 while ((linelen
= getline(&line
, &linesize
, fp
)) != -1) {
217 if (!strncmp(line
, "diff --git ", 11)) {
222 } else if (!strncmp(line
, "diff ", 5)) {
226 } else if (!strncmp(line
, "commit - ", 9)) {
228 err
= blobid(line
+ 9, cid
, *git
);
231 } else if (!strncmp(line
, "--- ", 4) ||
232 !strncmp(line
, "+++ ", 4) ||
233 !strncmp(line
, "blob - ", 7) ||
234 binary_deleted(line
)) {
235 /* rewind to previous line */
236 if (fseeko(fp
, -linelen
, SEEK_CUR
) == -1)
237 err
= got_error_from_errno("fseeko");
243 if (ferror(fp
) && err
== NULL
)
244 err
= got_error_from_errno("getline");
245 if (feof(fp
) && err
== NULL
)
246 err
= got_error(GOT_ERR_NO_PATCH
);
250 static const struct got_error
*
251 find_diff(int *done
, int *next
, FILE *fp
, int git
, const char *commitid
)
253 const struct got_error
*err
= NULL
;
254 char *old
= NULL
, *new = NULL
;
259 int create
, delete_binary
= 0, rename
= 0, xbit
= 0;
263 while ((linelen
= getline(&line
, &linesize
, fp
)) != -1) {
265 * Ignore the Index name like GNU and larry' patch,
266 * we don't have to follow POSIX.
269 if (!strncmp(line
, "--- ", 4)) {
271 err
= filename(line
+4, &old
);
272 } else if (rename
&& !strncmp(line
, "rename from ", 12)) {
274 err
= filename(line
+12, &old
);
275 } else if (!strncmp(line
, "+++ ", 4)) {
277 err
= filename(line
+4, &new);
278 } else if (!strncmp(line
, "blob + ", 7) ||
279 !strncmp(line
, "file + ", 7)) {
280 xbit
= filexbit(line
);
281 } else if (!git
&& !strncmp(line
, "blob - ", 7)) {
283 err
= blobid(line
+ 7, &blob
, git
);
284 } else if (!strncmp(line
, "Binary files ", 13)) {
287 err
= binaryfilename(line
+ 13, &old
);
288 } else if (rename
&& !strncmp(line
, "rename to ", 10)) {
290 err
= filename(line
+ 10, &new);
291 } else if (git
&& !strncmp(line
, "similarity index 100%", 21))
293 else if (git
&& !strncmp(line
, "new file mode 100", 17))
294 xbit
= strncmp(line
+ 17, "755", 3) == 0;
295 else if (git
&& !strncmp(line
, "index ", 6)) {
297 err
= blobid(line
+ 6, &blob
, git
);
298 } else if (!strncmp(line
, "diff ", 5)) {
299 /* rewind to previous line */
300 if (fseeko(fp
, -linelen
, SEEK_CUR
) == -1)
301 err
= got_error_from_errno("fseeko");
310 * Git-style diffs with "similarity index 100%" don't
311 * have any hunks and ends with the "rename to foobar"
314 if (rename
&& old
!= NULL
&& new != NULL
) {
316 err
= send_patch(old
, new, commitid
,
322 * Diffs that remove binary files have no hunks.
324 if (delete_binary
&& old
!= NULL
) {
326 err
= send_patch(old
, new, commitid
,
331 if (!strncmp(line
, "@@ -", 4)) {
332 create
= !strncmp(line
+4, "0,0", 3);
333 if ((old
== NULL
&& new == NULL
) ||
334 (!create
&& old
== NULL
))
335 err
= got_error_fmt(GOT_ERR_PATCH_MALFORMED
,
338 err
= send_patch(old
, new, commitid
,
344 /* rewind to previous line */
345 if (fseeko(fp
, -linelen
, SEEK_CUR
) == -1)
346 err
= got_error_from_errno("fseeko");
355 if (ferror(fp
) && err
== NULL
)
356 err
= got_error_from_errno("getline");
357 if (feof(fp
) && err
== NULL
)
358 err
= got_error(GOT_ERR_NO_PATCH
);
362 static const struct got_error
*
363 strtolnum(char **str
, int *n
)
368 for (p
= *str
; isdigit((unsigned char)*p
); ++p
)
374 *n
= strtonum(*str
, 0, INT_MAX
, &errstr
);
376 return got_error_fmt(GOT_ERR_PATCH_MALFORMED
,
377 "%s: %s", *str
, errstr
);
384 static const struct got_error
*
385 parse_hdr(char *s
, int *done
, struct got_imsg_patch_hunk
*hdr
)
387 const struct got_error
*err
= NULL
;
390 if (strncmp(s
, "@@ -", 4)) {
398 err
= strtolnum(&s
, &hdr
->oldfrom
);
403 err
= strtolnum(&s
, &hdr
->oldlines
);
412 if (*s
!= '+' || !*++s
)
413 return got_error_fmt(GOT_ERR_PATCH_MALFORMED
, "%s", s0
);
414 err
= strtolnum(&s
, &hdr
->newfrom
);
419 err
= strtolnum(&s
, &hdr
->newlines
);
429 return got_error_fmt(GOT_ERR_PATCH_MALFORMED
, "%s", s0
);
431 if (hdr
->oldfrom
>= INT_MAX
- hdr
->oldlines
||
432 hdr
->newfrom
>= INT_MAX
- hdr
->newlines
||
433 /* not so sure about this one */
434 hdr
->oldlines
>= INT_MAX
- hdr
->newlines
- 1 ||
435 (hdr
->oldlines
== 0 && hdr
->newlines
== 0))
436 return got_error_fmt(GOT_ERR_PATCH_MALFORMED
, "%s", s0
);
438 if (hdr
->oldlines
== 0) {
439 /* larry says to "do append rather than insert"; I don't
440 * quite get it, but i trust him.
445 if (imsg_compose(&ibuf
, GOT_IMSG_PATCH_HUNK
, 0, 0, -1,
446 hdr
, sizeof(*hdr
)) == -1)
447 return got_error_from_errno(
448 "imsg_compose GOT_IMSG_PATCH_HUNK");
452 static const struct got_error
*
453 send_line(const char *line
, size_t len
)
455 const struct got_error
*err
= NULL
;
459 memset(&iov
, 0, sizeof(iov
));
461 if (*line
!= '+' && *line
!= '-' && *line
!= ' ' && *line
!= '\\') {
462 iov
[iovcnt
].iov_base
= (void *)" ";
463 iov
[iovcnt
].iov_len
= 1;
467 iov
[iovcnt
].iov_base
= (void *)line
;
468 iov
[iovcnt
].iov_len
= len
;
471 if (imsg_composev(&ibuf
, GOT_IMSG_PATCH_LINE
, 0, 0, -1,
473 err
= got_error_from_errno(
474 "imsg_compose GOT_IMSG_PATCH_LINE");
479 static const struct got_error
*
480 peek_special_line(FILE *fp
)
482 const struct got_error
*err
;
486 if (ch
!= EOF
&& ch
!= '\\') {
492 err
= send_line("\\", 2);
497 while (ch
!= EOF
&& ch
!= '\n')
500 if (ch
!= EOF
|| feof(fp
))
502 return got_error(GOT_ERR_IO
);
505 static const struct got_error
*
506 parse_hunk(FILE *fp
, int *done
)
508 const struct got_error
*err
= NULL
;
509 struct got_imsg_patch_hunk hdr
;
510 char *line
= NULL
, ch
;
513 int leftold
, leftnew
;
515 linelen
= getline(&line
, &linesize
, fp
);
520 if (line
[linelen
- 1] == '\n')
521 line
[linelen
- 1] = '\0';
523 err
= parse_hdr(line
, done
, &hdr
);
527 if (fseeko(fp
, -linelen
, SEEK_CUR
) == -1)
528 err
= got_error_from_errno("fseeko");
532 leftold
= hdr
.oldlines
;
533 leftnew
= hdr
.newlines
;
535 while (leftold
> 0 || leftnew
> 0) {
536 linelen
= getline(&line
, &linesize
, fp
);
539 err
= got_error_from_errno("getline");
543 /* trailing newlines may be chopped */
544 if (leftold
< 3 && leftnew
< 3) {
549 err
= got_error(GOT_ERR_PATCH_TRUNCATED
);
552 if (line
[linelen
- 1] == '\n')
553 line
[linelen
- 1] = '\0';
555 /* usr.bin/patch allows '=' as context char */
560 if (ch
== '\t' || ch
== '\0')
561 ch
= ' '; /* the space got eaten */
575 err
= got_error_fmt(GOT_ERR_PATCH_MALFORMED
,
580 if (leftold
< 0 || leftnew
< 0) {
581 err
= got_error_fmt(GOT_ERR_PATCH_MALFORMED
,
586 err
= send_line(line
, linelen
);
590 if ((ch
== '-' && leftold
== 0) ||
591 (ch
== '+' && leftnew
== 0)) {
592 err
= peek_special_line(fp
);
603 static const struct got_error
*
604 read_patch(struct imsgbuf
*ibuf
, FILE *fp
)
606 const struct got_error
*err
= NULL
;
607 int git
, patch_found
= 0;
610 while ((err
= patch_start(&git
, &cid
, fp
)) == NULL
) {
613 err
= find_diff(&done
, &next
, fp
, git
, cid
);
622 err
= parse_hunk(fp
, &done
);
627 err
= send_patch_done();
635 /* ignore trailing gibberish */
636 if (err
!= NULL
&& err
->code
== GOT_ERR_NO_PATCH
&& patch_found
)
643 main(int argc
, char **argv
)
645 const struct got_error
*err
= NULL
;
655 if (imsgbuf_init(&ibuf
, GOT_IMSG_FD_CHILD
) == -1) {
656 warn("imsgbuf_init");
659 imsgbuf_allow_fdpass(&ibuf
);
661 /* revoke access to most system calls */
662 if (pledge("stdio recvfd", NULL
) == -1) {
663 err
= got_error_from_errno("pledge");
664 got_privsep_send_error(&ibuf
, err
);
668 /* revoke fs access */
669 if (landlock_no_fs() == -1) {
670 err
= got_error_from_errno("landlock_no_fs");
671 got_privsep_send_error(&ibuf
, err
);
674 if (cap_enter() == -1) {
675 err
= got_error_from_errno("cap_enter");
676 got_privsep_send_error(&ibuf
, err
);
681 err
= got_privsep_recv_imsg(&imsg
, &ibuf
, 0);
684 if (imsg
.hdr
.type
!= GOT_IMSG_PATCH_FILE
) {
685 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
688 fd
= imsg_get_fd(&imsg
);
690 err
= got_error(GOT_ERR_PRIVSEP_NO_FD
);
694 fp
= fdopen(fd
, "r");
696 err
= got_error_from_errno("fdopen");
701 err
= read_patch(&ibuf
, fp
);
704 if (imsg_compose(&ibuf
, GOT_IMSG_PATCH_EOF
, 0, 0, -1,
706 err
= got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
709 err
= got_privsep_flush_imsg(&ibuf
);
713 if (err
->code
!= GOT_ERR_PRIVSEP_PIPE
) {
714 fprintf(stderr
, "%s: %s\n", getprogname(), err
->msg
);
717 got_privsep_send_error(&ibuf
, err
);
719 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
720 err
= got_error_from_errno("close");
721 if (fp
!= NULL
&& fclose(fp
) == EOF
&& err
== NULL
)
722 err
= got_error_from_errno("fclose");
723 if (close(GOT_IMSG_FD_CHILD
) == -1 && err
== NULL
)
724 err
= got_error_from_errno("close");