Hash :
9d6cabd5
Author :
Date :
2022-04-07T09:28:59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/*
* Copyright 1986, Larry Wall
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following condition is met:
* 1. Redistributions of source code must retain the above copyright notice,
* this condition and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <ctype.h>
#include <limits.h>
#include <paths.h>
#include <sha1.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <imsg.h>
#include "got_error.h"
#include "got_object.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
#include "got_lib_privsep.h"
struct imsgbuf ibuf;
static const struct got_error *
send_patch(const char *oldname, const char *newname, int git)
{
struct got_imsg_patch p;
memset(&p, 0, sizeof(p));
if (oldname != NULL)
strlcpy(p.old, oldname, sizeof(p.old));
if (newname != NULL)
strlcpy(p.new, newname, sizeof(p.new));
p.git = git;
if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
&p, sizeof(p)) == -1)
return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
return NULL;
}
static const struct got_error *
send_patch_done(void)
{
if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
NULL, 0) == -1)
return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
if (imsg_flush(&ibuf) == -1)
return got_error_from_errno("imsg_flush");
return NULL;
}
/* based on fetchname from usr.bin/patch/util.c */
static const struct got_error *
filename(const char *at, char **name)
{
char *tmp, *t;
*name = NULL;
if (*at == '\0')
return NULL;
while (isspace((unsigned char)*at))
at++;
/* files can be created or removed by diffing against /dev/null */
if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
return NULL;
tmp = strdup(at);
if (tmp == NULL)
return got_error_from_errno("strdup");
if ((t = strchr(tmp, '\t')) != NULL)
*t = '\0';
if ((t = strchr(tmp, '\n')) != NULL)
*t = '\0';
*name = strdup(tmp);
free(tmp);
if (*name == NULL)
return got_error_from_errno("strdup");
return NULL;
}
static const struct got_error *
find_patch(FILE *fp)
{
const struct got_error *err = NULL;
char *old = NULL, *new = NULL;
char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
int create, git = 0;
while ((linelen = getline(&line, &linesize, fp)) != -1) {
/*
* Ignore the Index name like GNU and larry' patch,
* we don't have to follow POSIX.
*/
if (!strncmp(line, "--- ", 4)) {
free(old);
err = filename(line+4, &old);
} else if (!strncmp(line, "+++ ", 4)) {
free(new);
err = filename(line+4, &new);
} else if (!strncmp(line, "diff --git a/", 13))
git = 1;
if (err)
break;
if (!strncmp(line, "@@ -", 4)) {
create = !strncmp(line+4, "0,0", 3);
if ((old == NULL && new == NULL) ||
(!create && old == NULL))
err = got_error(GOT_ERR_PATCH_MALFORMED);
else
err = send_patch(old, new, git);
if (err)
break;
/* rewind to previous line */
if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
err = got_error_from_errno("fseek");
break;
}
}
free(old);
free(new);
free(line);
if (ferror(fp) && err == NULL)
err = got_error_from_errno("getline");
if (feof(fp) && err == NULL)
err = got_error(GOT_ERR_NO_PATCH);
return err;
}
static const struct got_error *
strtolnum(char **str, long *n)
{
char *p, c;
const char *errstr;
for (p = *str; isdigit((unsigned char)*p); ++p)
/* nop */;
c = *p;
*p = '\0';
*n = strtonum(*str, 0, LONG_MAX, &errstr);
if (errstr != NULL)
return got_error(GOT_ERR_PATCH_MALFORMED);
*p = c;
*str = p;
return NULL;
}
static const struct got_error *
parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
{
static const struct got_error *err = NULL;
*ok = 1;
if (strncmp(s, "@@ -", 4)) {
*ok = 0;
return NULL;
}
s += 4;
if (!*s)
return NULL;
err = strtolnum(&s, &hdr->oldfrom);
if (err)
return err;
if (*s == ',') {
s++;
err = strtolnum(&s, &hdr->oldlines);
if (err)
return err;
} else
hdr->oldlines = 1;
if (*s == ' ')
s++;
if (*s != '+' || !*++s)
return got_error(GOT_ERR_PATCH_MALFORMED);
err = strtolnum(&s, &hdr->newfrom);
if (err)
return err;
if (*s == ',') {
s++;
err = strtolnum(&s, &hdr->newlines);
if (err)
return err;
} else
hdr->newlines = 1;
if (*s == ' ')
s++;
if (*s != '@')
return got_error(GOT_ERR_PATCH_MALFORMED);
if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
hdr->newfrom >= LONG_MAX - hdr->newlines ||
/* not so sure about this one */
hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
return got_error(GOT_ERR_PATCH_MALFORMED);
if (hdr->oldlines == 0) {
/* larry says to "do append rather than insert"; I don't
* quite get it, but i trust him.
*/
hdr->oldfrom++;
}
if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
hdr, sizeof(*hdr)) == -1)
return got_error_from_errno(
"imsg_compose GOT_IMSG_PATCH_HUNK");
return NULL;
}
static const struct got_error *
send_line(const char *line)
{
static const struct got_error *err = NULL;
char *p = NULL;
if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
if (asprintf(&p, " %s", line) == -1)
return got_error_from_errno("asprintf");
line = p;
}
if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
line, strlen(line) + 1) == -1)
err = got_error_from_errno(
"imsg_compose GOT_IMSG_PATCH_LINE");
free(p);
return err;
}
static const struct got_error *
peek_special_line(FILE *fp, int send)
{
const struct got_error *err;
int ch;
ch = fgetc(fp);
if (ch != EOF && ch != '\\') {
ungetc(ch, fp);
return NULL;
}
if (ch == '\\' && send) {
err = send_line("\\");
if (err)
return err;
}
while (ch != EOF && ch != '\n')
ch = fgetc(fp);
if (ch != EOF || feof(fp))
return NULL;
return got_error(GOT_ERR_IO);
}
static const struct got_error *
parse_hunk(FILE *fp, int *ok)
{
static const struct got_error *err = NULL;
struct got_imsg_patch_hunk hdr;
char *line = NULL, ch;
size_t linesize = 0;
ssize_t linelen;
long leftold, leftnew;
linelen = getline(&line, &linesize, fp);
if (linelen == -1) {
*ok = 0;
goto done;
}
err = parse_hdr(line, ok, &hdr);
if (err)
goto done;
if (!*ok) {
if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
err = got_error_from_errno("fseek");
goto done;
}
leftold = hdr.oldlines;
leftnew = hdr.newlines;
while (leftold > 0 || leftnew > 0) {
linelen = getline(&line, &linesize, fp);
if (linelen == -1) {
if (ferror(fp)) {
err = got_error_from_errno("getline");
goto done;
}
/* trailing newlines may be chopped */
if (leftold < 3 && leftnew < 3) {
*ok = 0;
break;
}
err = got_error(GOT_ERR_PATCH_TRUNCATED);
goto done;
}
if (line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
/* usr.bin/patch allows '=' as context char */
if (*line == '=')
*line = ' ';
ch = *line;
if (ch == '\t' || ch == '\0')
ch = ' '; /* the space got eaten */
switch (ch) {
case '-':
leftold--;
break;
case ' ':
leftold--;
leftnew--;
break;
case '+':
leftnew--;
break;
default:
err = got_error(GOT_ERR_PATCH_MALFORMED);
goto done;
}
if (leftold < 0 || leftnew < 0) {
err = got_error(GOT_ERR_PATCH_MALFORMED);
goto done;
}
err = send_line(line);
if (err)
goto done;
if ((ch == '-' && leftold == 0) ||
(ch == '+' && leftnew == 0)) {
err = peek_special_line(fp, ch == '+');
if (err)
goto done;
}
}
done:
free(line);
return err;
}
static const struct got_error *
read_patch(struct imsgbuf *ibuf, int fd)
{
const struct got_error *err = NULL;
FILE *fp;
int ok, patch_found = 0;
if ((fp = fdopen(fd, "r")) == NULL) {
err = got_error_from_errno("fdopen");
close(fd);
return err;
}
while (!feof(fp)) {
err = find_patch(fp);
if (err)
goto done;
patch_found = 1;
for (;;) {
err = parse_hunk(fp, &ok);
if (err)
goto done;
if (!ok) {
err = send_patch_done();
if (err)
goto done;
break;
}
}
}
done:
fclose(fp);
/* ignore trailing gibberish */
if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
err = NULL;
return err;
}
int
main(int argc, char **argv)
{
const struct got_error *err = NULL;
struct imsg imsg;
#if 0
static int attached;
while (!attached)
sleep(1);
#endif
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
/* revoke access to most system calls */
if (pledge("stdio recvfd", NULL) == -1) {
err = got_error_from_errno("pledge");
got_privsep_send_error(&ibuf, err);
return 1;
}
#endif
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err)
goto done;
if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
err = read_patch(&ibuf, imsg.fd);
if (err)
goto done;
if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
NULL, 0) == -1) {
err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
goto done;
}
err = got_privsep_flush_imsg(&ibuf);
done:
imsg_free(&imsg);
if (err != NULL) {
got_privsep_send_error(&ibuf, err);
err = NULL;
}
if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
err = got_error_from_errno("close");
if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
return err ? 1 : 0;
}