filter.c 20.8 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9 10 11
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "fileops.h"
#include "hash.h"
#include "filter.h"
12
#include "repository.h"
13
#include "global.h"
14
#include "git2/sys/filter.h"
15
#include "git2/config.h"
16
#include "blob.h"
17
#include "attr_file.h"
18
#include "array.h"
19

20 21 22 23 24
struct git_filter_source {
	git_repository *repo;
	const char     *path;
	git_oid         oid;  /* zero if unknown (which is likely) */
	uint16_t        filemode; /* zero if unknown */
25
	git_filter_mode_t mode;
26
	uint32_t        flags;
27 28
};

29
typedef struct {
30
	const char *filter_name;
31 32 33 34 35 36 37
	git_filter *filter;
	void *payload;
} git_filter_entry;

struct git_filter_list {
	git_array_t(git_filter_entry) filters;
	git_filter_source source;
38
	git_buf *temp_buf;
39 40 41 42
	char path[GIT_FLEX_ARRAY];
};

typedef struct {
43
	char *filter_name;
44
	git_filter *filter;
45
	int priority;
46
	int initialized;
47 48 49
	size_t nattrs, nmatches;
	char *attrdata;
	const char *attrs[GIT_FLEX_ARRAY];
50 51
} git_filter_def;

52 53 54 55 56 57 58
static int filter_def_priority_cmp(const void *a, const void *b)
{
	int pa = ((const git_filter_def *)a)->priority;
	int pb = ((const git_filter_def *)b)->priority;
	return (pa < pb) ? -1 : (pa > pb) ? 1 : 0;
}

59 60
struct filter_registry {
	git_vector filters;
61 62
};

63 64 65 66 67 68 69 70 71 72 73 74
static struct filter_registry *git__filter_registry = NULL;

static void filter_registry_shutdown(void)
{
	struct filter_registry *reg = NULL;
	size_t pos;
	git_filter_def *fdef;

	if ((reg = git__swap(git__filter_registry, NULL)) == NULL)
		return;

	git_vector_foreach(&reg->filters, pos, fdef) {
75
		if (fdef->filter && fdef->filter->shutdown) {
76 77 78 79
			fdef->filter->shutdown(fdef->filter);
			fdef->initialized = false;
		}

80
		git__free(fdef->filter_name);
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
		git__free(fdef->attrdata);
		git__free(fdef);
	}

	git_vector_free(&reg->filters);
	git__free(reg);
}

static int filter_registry_initialize(void)
{
	int error = 0;
	struct filter_registry *reg;

	if (git__filter_registry)
		return 0;

	reg = git__calloc(1, sizeof(struct filter_registry));
	GITERR_CHECK_ALLOC(reg);

	if ((error = git_vector_init(
			&reg->filters, 2, filter_def_priority_cmp)) < 0)
		goto cleanup;

	reg = git__compare_and_swap(&git__filter_registry, NULL, reg);
	if (reg != NULL)
		goto cleanup;

	git__on_shutdown(filter_registry_shutdown);

Russell Belfer committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	/* try to register both default filters */
	{
		git_filter *crlf = git_crlf_filter_new();
		git_filter *ident = git_ident_filter_new();

		if (crlf && git_filter_register(
				GIT_FILTER_CRLF, crlf, GIT_FILTER_CRLF_PRIORITY) < 0)
			crlf = NULL;
		if (ident && git_filter_register(
				GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
			ident = NULL;

		if (!crlf || !ident)
			return -1;
	}

	return 0;
127 128 129 130 131 132 133

cleanup:
	git_vector_free(&reg->filters);
	git__free(reg);
	return error;
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
static int filter_def_scan_attrs(
	git_buf *attrs, size_t *nattr, size_t *nmatch, const char *attr_str)
{
	const char *start, *scan = attr_str;
	int has_eq;

	*nattr = *nmatch = 0;

	if (!scan)
		return 0;

	while (*scan) {
		while (git__isspace(*scan)) scan++;

		for (start = scan, has_eq = 0; *scan && !git__isspace(*scan); ++scan) {
			if (*scan == '=')
				has_eq = 1;
		}

		if (scan > start) {
			(*nattr)++;
Russell Belfer committed
155
			if (has_eq || *start == '-' || *start == '+' || *start == '!')
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
				(*nmatch)++;

			if (has_eq)
				git_buf_putc(attrs, '=');
			git_buf_put(attrs, start, scan - start);
			git_buf_putc(attrs, '\0');
		}
	}

	return 0;
}

static void filter_def_set_attrs(git_filter_def *fdef)
{
	char *scan = fdef->attrdata;
	size_t i;

	for (i = 0; i < fdef->nattrs; ++i) {
		const char *name, *value;

		switch (*scan) {
		case '=':
			name = scan + 1;
			for (scan++; *scan != '='; scan++) /* find '=' */;
			*scan++ = '\0';
			value = scan;
			break;
		case '-':
			name = scan + 1; value = git_attr__false; break;
		case '+':
			name = scan + 1; value = git_attr__true;  break;
		case '!':
			name = scan + 1; value = git_attr__unset; break;
		default:
			name = scan;     value = NULL; break;
		}

		fdef->attrs[i] = name;
		fdef->attrs[i + fdef->nattrs] = value;

		scan += strlen(scan) + 1;
	}
}

200 201 202 203
static int filter_def_name_key_check(const void *key, const void *fdef)
{
	const char *name =
		fdef ? ((const git_filter_def *)fdef)->filter_name : NULL;
204 205 206 207 208 209 210
	return name ? git__strcmp(key, name) : -1;
}

static int filter_def_filter_key_check(const void *key, const void *fdef)
{
	const void *filter = fdef ? ((const git_filter_def *)fdef)->filter : NULL;
	return (key == filter) ? 0 : -1;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}

static int filter_registry_find(size_t *pos, const char *name)
{
	return git_vector_search2(
		pos, &git__filter_registry->filters, filter_def_name_key_check, name);
}

static git_filter_def *filter_registry_lookup(size_t *pos, const char *name)
{
	git_filter_def *fdef = NULL;

	if (!filter_registry_find(pos, name))
		fdef = git_vector_get(&git__filter_registry->filters, *pos);

	return fdef;
}

229 230 231 232
int git_filter_register(
	const char *name, git_filter *filter, int priority)
{
	git_filter_def *fdef;
233
	size_t nattr = 0, nmatch = 0, alloc_len;
234 235
	git_buf attrs = GIT_BUF_INIT;

236 237
	assert(name && filter);

238 239 240 241
	if (filter_registry_initialize() < 0)
		return -1;

	if (!filter_registry_find(NULL, name)) {
242 243
		giterr_set(
			GITERR_FILTER, "Attempt to reregister existing filter '%s'", name);
Russell Belfer committed
244
		return GIT_EEXISTS;
245 246 247 248 249
	}

	if (filter_def_scan_attrs(&attrs, &nattr, &nmatch, filter->attributes) < 0)
		return -1;

250 251 252
	GITERR_CHECK_ALLOC_MULTIPLY(&alloc_len, nattr, 2);
	GITERR_CHECK_ALLOC_MULTIPLY(&alloc_len, alloc_len, sizeof(char *));
	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, sizeof(git_filter_def));
253 254

	fdef = git__calloc(1, alloc_len);
255 256
	GITERR_CHECK_ALLOC(fdef);

257 258 259
	fdef->filter_name = git__strdup(name);
	GITERR_CHECK_ALLOC(fdef->filter_name);

260 261 262 263 264 265 266 267
	fdef->filter      = filter;
	fdef->priority    = priority;
	fdef->nattrs      = nattr;
	fdef->nmatches    = nmatch;
	fdef->attrdata    = git_buf_detach(&attrs);

	filter_def_set_attrs(fdef);

268
	if (git_vector_insert(&git__filter_registry->filters, fdef) < 0) {
269
		git__free(fdef->filter_name);
270 271 272 273 274
		git__free(fdef->attrdata);
		git__free(fdef);
		return -1;
	}

275
	git_vector_sort(&git__filter_registry->filters);
276 277 278
	return 0;
}

279 280 281 282 283
int git_filter_unregister(const char *name)
{
	size_t pos;
	git_filter_def *fdef;

284 285
	assert(name);

286
	/* cannot unregister default filters */
Russell Belfer committed
287
	if (!strcmp(GIT_FILTER_CRLF, name) || !strcmp(GIT_FILTER_IDENT, name)) {
288 289 290 291
		giterr_set(GITERR_FILTER, "Cannot unregister filter '%s'", name);
		return -1;
	}

292
	if ((fdef = filter_registry_lookup(&pos, name)) == NULL) {
293 294 295 296
		giterr_set(GITERR_FILTER, "Cannot find filter '%s' to unregister", name);
		return GIT_ENOTFOUND;
	}

297
	(void)git_vector_remove(&git__filter_registry->filters, pos);
298

299
	if (fdef->initialized && fdef->filter && fdef->filter->shutdown) {
300
		fdef->filter->shutdown(fdef->filter);
301 302
		fdef->initialized = false;
	}
303

304
	git__free(fdef->filter_name);
305 306 307 308 309 310
	git__free(fdef->attrdata);
	git__free(fdef);

	return 0;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static int filter_initialize(git_filter_def *fdef)
{
	int error = 0;

	if (!fdef->initialized &&
		fdef->filter &&
		fdef->filter->initialize &&
		(error = fdef->filter->initialize(fdef->filter)) < 0)
	{
		/* auto-unregister if initialize fails */
		git_filter_unregister(fdef->filter_name);
		return error;
	}

	fdef->initialized = true;
	return 0;
}

329 330 331
git_filter *git_filter_lookup(const char *name)
{
	size_t pos;
332 333 334 335
	git_filter_def *fdef;

	if (filter_registry_initialize() < 0)
		return NULL;
336

337
	if ((fdef = filter_registry_lookup(&pos, name)) == NULL)
338 339 340 341 342 343
		return NULL;

	if (!fdef->initialized && filter_initialize(fdef) < 0)
		return NULL;

	return fdef->filter;
344 345
}

Russell Belfer committed
346 347 348 349 350
void git_filter_free(git_filter *filter)
{
	git__free(filter);
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
git_repository *git_filter_source_repo(const git_filter_source *src)
{
	return src->repo;
}

const char *git_filter_source_path(const git_filter_source *src)
{
	return src->path;
}

uint16_t git_filter_source_filemode(const git_filter_source *src)
{
	return src->filemode;
}

const git_oid *git_filter_source_id(const git_filter_source *src)
{
	return git_oid_iszero(&src->oid) ? NULL : &src->oid;
}

371 372 373 374 375
git_filter_mode_t git_filter_source_mode(const git_filter_source *src)
{
	return src->mode;
}

376
uint32_t git_filter_source_flags(const git_filter_source *src)
377
{
378
	return src->flags;
379 380
}

381
static int filter_list_new(
382
	git_filter_list **out, const git_filter_source *src)
383
{
384
	git_filter_list *fl = NULL;
385
	size_t pathlen = src->path ? strlen(src->path) : 0, alloclen;
386

387 388
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_filter_list), pathlen);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
389

390
	fl = git__calloc(1, alloclen);
391 392 393 394 395 396
	GITERR_CHECK_ALLOC(fl);

	if (src->path)
		memcpy(fl->path, src->path, pathlen);
	fl->source.repo = src->repo;
	fl->source.path = fl->path;
397
	fl->source.mode = src->mode;
398
	fl->source.flags = src->flags;
399 400 401 402 403

	*out = fl;
	return 0;
}

404
static int filter_list_check_attributes(
405 406 407 408 409
	const char ***out,
	git_repository *repo,
	git_attr_session *attr_session,
	git_filter_def *fdef,
	const git_filter_source *src)
410 411 412 413 414 415
{
	int error;
	size_t i;
	const char **strs = git__calloc(fdef->nattrs, sizeof(const char *));
	GITERR_CHECK_ALLOC(strs);

416 417
	error = git_attr_get_many_with_session(
		strs, repo, attr_session, 0, src->path, fdef->nattrs, fdef->attrs);
418 419 420 421

	/* if no values were found but no matches are needed, it's okay! */
	if (error == GIT_ENOTFOUND && !fdef->nmatches) {
		giterr_clear();
Russell Belfer committed
422
		git__free((void *)strs);
423 424 425 426 427 428 429 430 431 432 433 434 435
		return 0;
	}

	for (i = 0; !error && i < fdef->nattrs; ++i) {
		const char *want = fdef->attrs[fdef->nattrs + i];
		git_attr_t want_type, found_type;

		if (!want)
			continue;

		want_type  = git_attr_value(want);
		found_type = git_attr_value(strs[i]);

436 437 438 439 440
		if (want_type != found_type)
			error = GIT_ENOTFOUND;
		else if (want_type == GIT_ATTR_VALUE_T &&
				strcmp(want, strs[i]) &&
				strcmp(want, "*"))
441 442 443 444
			error = GIT_ENOTFOUND;
	}

	if (error)
Russell Belfer committed
445
		git__free((void *)strs);
446 447 448 449 450 451
	else
		*out = strs;

	return error;
}

452
int git_filter_list_new(
453 454 455
	git_filter_list **out,
	git_repository *repo,
	git_filter_mode_t mode,
456
	uint32_t flags)
457 458 459 460 461
{
	git_filter_source src = { 0 };
	src.repo = repo;
	src.path = NULL;
	src.mode = mode;
462
	src.flags = flags;
463 464 465
	return filter_list_new(out, &src);
}

466
int git_filter_list__load_ext(
467 468
	git_filter_list **filters,
	git_repository *repo,
Russell Belfer committed
469
	git_blob *blob, /* can be NULL */
470
	const char *path,
471
	git_filter_mode_t mode,
472
	git_filter_options *filter_opts)
473 474 475 476 477
{
	int error = 0;
	git_filter_list *fl = NULL;
	git_filter_source src = { 0 };
	git_filter_entry *fe;
478 479
	size_t idx;
	git_filter_def *fdef;
480

481
	if (filter_registry_initialize() < 0)
482 483 484 485
		return -1;

	src.repo = repo;
	src.path = path;
486
	src.mode = mode;
487 488
	src.flags = filter_opts->flags;

Russell Belfer committed
489 490
	if (blob)
		git_oid_cpy(&src.oid, git_blob_id(blob));
491

492
	git_vector_foreach(&git__filter_registry->filters, idx, fdef) {
493
		const char **values = NULL;
494 495 496 497
		void *payload = NULL;

		if (!fdef || !fdef->filter)
			continue;
498

499
		if (fdef->nattrs > 0) {
500
			error = filter_list_check_attributes(
501
				&values, repo, filter_opts->attr_session, fdef, &src);
502

503 504 505 506 507 508 509
			if (error == GIT_ENOTFOUND) {
				error = 0;
				continue;
			} else if (error < 0)
				break;
		}

510 511 512
		if (!fdef->initialized && (error = filter_initialize(fdef)) < 0)
			break;

513
		if (fdef->filter->check)
514
			error = fdef->filter->check(
515
				fdef->filter, &payload, &src, values);
516

Russell Belfer committed
517
		git__free((void *)values);
518

Russell Belfer committed
519
		if (error == GIT_PASSTHROUGH)
520 521 522 523
			error = 0;
		else if (error < 0)
			break;
		else {
524 525 526 527 528 529
			if (!fl) {
				if ((error = filter_list_new(&fl, &src)) < 0)
					return error;

				fl->temp_buf = filter_opts->temp_buf;
			}
530 531 532

			fe = git_array_alloc(fl->filters);
			GITERR_CHECK_ALLOC(fe);
533 534 535

			fe->filter = fdef->filter;
			fe->filter_name = fdef->filter_name;
536 537 538 539 540 541 542 543 544 545 546 547 548 549
			fe->payload = payload;
		}
	}

	if (error && fl != NULL) {
		git_array_clear(fl->filters);
		git__free(fl);
		fl = NULL;
	}

	*filters = fl;
	return error;
}

550 551 552 553 554 555
int git_filter_list_load(
	git_filter_list **filters,
	git_repository *repo,
	git_blob *blob, /* can be NULL */
	const char *path,
	git_filter_mode_t mode,
556
	uint32_t flags)
557
{
558 559 560 561 562 563
	git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;

	filter_opts.flags = flags;

	return git_filter_list__load_ext(
		filters, repo, blob, path, mode, &filter_opts);
564 565
}

566 567 568 569 570 571 572 573 574 575 576
void git_filter_list_free(git_filter_list *fl)
{
	uint32_t i;

	if (!fl)
		return;

	for (i = 0; i < git_array_size(fl->filters); ++i) {
		git_filter_entry *fe = git_array_get(fl->filters, i);
		if (fe->filter->cleanup)
			fe->filter->cleanup(fe->filter, fe->payload);
577 578
	}

579 580
	git_array_clear(fl->filters);
	git__free(fl);
581 582
}

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
int git_filter_list_contains(
	git_filter_list *fl,
	const char *name)
{
	size_t i;

	assert(name);

	if (!fl)
		return 0;

	for (i = 0; i < fl->filters.size; i++) {
		if (strcmp(fl->filters.ptr[i].filter_name, name) == 0)
			return 1;
	}

	return 0;
}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
int git_filter_list_push(
	git_filter_list *fl, git_filter *filter, void *payload)
{
	int error = 0;
	size_t pos;
	git_filter_def *fdef;
	git_filter_entry *fe;

	assert(fl && filter);

	if (git_vector_search2(
			&pos, &git__filter_registry->filters,
			filter_def_filter_key_check, filter) < 0) {
		giterr_set(GITERR_FILTER, "Cannot use an unregistered filter");
		return -1;
	}

	fdef = git_vector_get(&git__filter_registry->filters, pos);

	if (!fdef->initialized && (error = filter_initialize(fdef)) < 0)
		return error;

	fe = git_array_alloc(fl->filters);
	GITERR_CHECK_ALLOC(fe);
	fe->filter  = filter;
	fe->payload = payload;

	return 0;
}

Russell Belfer committed
632 633 634 635 636
size_t git_filter_list_length(const git_filter_list *fl)
{
	return fl ? git_array_size(fl->filters) : 0;
}

637
struct buf_stream {
638
	git_writestream parent;
639 640 641 642 643
	git_buf *target;
	bool complete;
};

static int buf_stream_write(
644
	git_writestream *s, const char *buffer, size_t len)
645
{
646 647
	struct buf_stream *buf_stream = (struct buf_stream *)s;
	assert(buf_stream);
648

649
	assert(buf_stream->complete == 0);
650

651 652
	return git_buf_put(buf_stream->target, buffer, len);
}
653

654
static int buf_stream_close(git_writestream *s)
655 656 657
{
	struct buf_stream *buf_stream = (struct buf_stream *)s;
	assert(buf_stream);
658

659 660
	assert(buf_stream->complete == 0);
	buf_stream->complete = 1;
661

662 663
	return 0;
}
664

665
static void buf_stream_free(git_writestream *s)
666 667 668
{
	GIT_UNUSED(s);
}
669

670 671 672
static void buf_stream_init(struct buf_stream *writer, git_buf *target)
{
	memset(writer, 0, sizeof(struct buf_stream));
673

674 675 676
	writer->parent.write = buf_stream_write;
	writer->parent.close = buf_stream_close;
	writer->parent.free = buf_stream_free;
677
	writer->target = target;
678

679 680
	git_buf_clear(target);
}
681

682 683 684 685 686 687 688 689 690
int git_filter_list_apply_to_data(
	git_buf *tgt, git_filter_list *filters, git_buf *src)
{
	struct buf_stream writer;
	int error;

	git_buf_sanitize(tgt);
	git_buf_sanitize(src);

691 692 693 694
	if (!filters) {
		git_buf_attach_notowned(tgt, src->ptr, src->size);
		return 0;
	}
695 696 697 698

	buf_stream_init(&writer, tgt);

	if ((error = git_filter_list_stream_data(filters, src,
Leo Yang committed
699
		&writer.parent)) < 0)
700 701 702
			return error;

	assert(writer.complete);
703
	return error;
704
}
705 706

int git_filter_list_apply_to_file(
707
	git_buf *out,
708 709 710 711
	git_filter_list *filters,
	git_repository *repo,
	const char *path)
{
712
	struct buf_stream writer;
713 714
	int error;

715
	buf_stream_init(&writer, out);
716

717
	if ((error = git_filter_list_stream_file(
Leo Yang committed
718
		filters, repo, path, &writer.parent)) < 0)
719
			return error;
720

721
	assert(writer.complete);
722 723 724
	return error;
}

725 726 727 728 729 730 731 732 733
static int buf_from_blob(git_buf *out, git_blob *blob)
{
	git_off_t rawsize = git_blob_rawsize(blob);

	if (!git__is_sizet(rawsize)) {
		giterr_set(GITERR_OS, "Blob is too large to filter");
		return -1;
	}

734
	git_buf_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize);
735 736 737
	return 0;
}

738
int git_filter_list_apply_to_blob(
739
	git_buf *out,
740 741 742
	git_filter_list *filters,
	git_blob *blob)
{
743 744
	struct buf_stream writer;
	int error;
745

746
	buf_stream_init(&writer, out);
747

748
	if ((error = git_filter_list_stream_blob(
Leo Yang committed
749
		filters, blob, &writer.parent)) < 0)
750
			return error;
751

752 753
	assert(writer.complete);
	return error;
754 755 756
}

struct proxy_stream {
757
	git_writestream parent;
758 759 760 761
	git_filter *filter;
	const git_filter_source *source;
	void **payload;
	git_buf input;
762 763
	git_buf temp_buf;
	git_buf *output;
764
	git_writestream *target;
765 766 767
};

static int proxy_stream_write(
768
	git_writestream *s, const char *buffer, size_t len)
769 770 771 772 773 774 775
{
	struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
	assert(proxy_stream);

	return git_buf_put(&proxy_stream->input, buffer, len);
}

776
static int proxy_stream_close(git_writestream *s)
777 778 779 780 781 782 783 784 785 786
{
	struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
	git_buf *writebuf;
	int error;

	assert(proxy_stream);

	error = proxy_stream->filter->apply(
		proxy_stream->filter,
		proxy_stream->payload,
787
		proxy_stream->output,
788 789 790 791 792 793
		&proxy_stream->input,
		proxy_stream->source);

	if (error == GIT_PASSTHROUGH) {
		writebuf = &proxy_stream->input;
	} else if (error == 0) {
794 795
		git_buf_sanitize(proxy_stream->output);
		writebuf = proxy_stream->output;
796 797 798 799 800 801 802 803 804 805 806
	} else {
		return error;
	}

	if ((error = proxy_stream->target->write(
			proxy_stream->target, writebuf->ptr, writebuf->size)) == 0)
		error = proxy_stream->target->close(proxy_stream->target);

	return error;
}

807
static void proxy_stream_free(git_writestream *s)
808 809 810 811 812
{
	struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
	assert(proxy_stream);

	git_buf_free(&proxy_stream->input);
813
	git_buf_free(&proxy_stream->temp_buf);
814 815 816 817
	git__free(proxy_stream);
}

static int proxy_stream_init(
818
	git_writestream **out,
819
	git_filter *filter,
820
	git_buf *temp_buf,
821 822
	void **payload,
	const git_filter_source *source,
823
	git_writestream *target)
824 825 826 827
{
	struct proxy_stream *proxy_stream = git__calloc(1, sizeof(struct proxy_stream));
	GITERR_CHECK_ALLOC(proxy_stream);

828 829 830
	proxy_stream->parent.write = proxy_stream_write;
	proxy_stream->parent.close = proxy_stream_close;
	proxy_stream->parent.free = proxy_stream_free;
831 832 833 834
	proxy_stream->filter = filter;
	proxy_stream->payload = payload;
	proxy_stream->source = source;
	proxy_stream->target = target;
835
	proxy_stream->output = temp_buf ? temp_buf : &proxy_stream->temp_buf;
836

837 838 839
	if (temp_buf)
		git_buf_clear(temp_buf);

840
	*out = (git_writestream *)proxy_stream;
841 842 843 844
	return 0;
}

static int stream_list_init(
845
	git_writestream **out,
846 847
	git_vector *streams,
	git_filter_list *filters,
848
	git_writestream *target)
849
{
850
	git_writestream *last_stream = target;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	size_t i;
	int error = 0;

	*out = NULL;

	if (!filters) {
		*out = target;
		return 0;
	}

	/* Create filters last to first to get the chaining direction */
	for (i = 0; i < git_array_size(filters->filters); ++i) {
		size_t filter_idx = (filters->source.mode == GIT_FILTER_TO_WORKTREE) ?
			git_array_size(filters->filters) - 1 - i : i;
		git_filter_entry *fe = git_array_get(filters->filters, filter_idx);
866
		git_writestream *filter_stream;
867 868 869
		
		assert(fe->filter->stream || fe->filter->apply);

870 871 872
		/* If necessary, create a stream that proxies the traditional
		 * application.
		 */
873 874
		if (fe->filter->stream)
			error = fe->filter->stream(&filter_stream, fe->filter,
875
				&fe->payload, &filters->source, last_stream);
876 877 878 879 880
		else
			/* Create a stream that proxies the one-shot apply */
			error = proxy_stream_init(&filter_stream, fe->filter,
				filters->temp_buf, &fe->payload, &filters->source,
				last_stream);
881

882 883 884 885
		if (error < 0)
			return error;

		git_vector_insert(streams, filter_stream);
886
		last_stream = filter_stream;
887 888
	}

889 890 891 892 893 894
	*out = last_stream;
	return 0;
}

void stream_list_free(git_vector *streams)
{
895
	git_writestream *stream;
896 897 898 899
	size_t i;

	git_vector_foreach(streams, i, stream)
		stream->free(stream);
900
	git_vector_free(streams);
901 902 903 904 905 906
}

int git_filter_list_stream_file(
	git_filter_list *filters,
	git_repository *repo,
	const char *path,
907
	git_writestream *target)
908
{
909
	char buf[FILTERIO_BUFSIZE];
910
	git_buf abspath = GIT_BUF_INIT;
911 912
	const char *base = repo ? git_repository_workdir(repo) : NULL;
	git_vector filter_streams = GIT_VECTOR_INIT;
913
	git_writestream *stream_start;
914
	ssize_t readlen;
915
	int fd = -1, error;
916 917 918 919 920 921

	if ((error = stream_list_init(
			&stream_start, &filter_streams, filters, target)) < 0 ||
		(error = git_path_join_unrooted(&abspath, path, base, NULL)) < 0)
		goto done;

922
	if ((fd = git_futils_open_ro(abspath.ptr)) < 0) {
923 924 925 926
		error = fd;
		goto done;
	}

927
	while ((readlen = p_read(fd, buf, sizeof(buf))) > 0) {
928
		if ((error = stream_start->write(stream_start, buf, readlen)) < 0)
929 930 931 932 933 934 935 936 937 938
			goto done;
	}

	if (!readlen)
		error = stream_start->close(stream_start);
	else if (readlen < 0)
		error = readlen;


done:
939 940
	if (fd >= 0)
		p_close(fd);
941 942 943 944 945 946 947 948
	stream_list_free(&filter_streams);
	git_buf_free(&abspath);
	return error;
}

int git_filter_list_stream_data(
	git_filter_list *filters,
	git_buf *data,
949
	git_writestream *target)
950 951
{
	git_vector filter_streams = GIT_VECTOR_INIT;
952
	git_writestream *stream_start;
953
	int error = 0, close_error;
954

955 956
	git_buf_sanitize(data);

957 958
	if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0)
		goto out;
959

960 961 962 963
	error = stream_start->write(stream_start, data->ptr, data->size);

out:
	close_error = stream_start->close(stream_start);
964
	stream_list_free(&filter_streams);
965 966
	/* propagate the stream init or write error */
	return error < 0 ? error : close_error;
967 968 969 970 971
}

int git_filter_list_stream_blob(
	git_filter_list *filters,
	git_blob *blob,
972
	git_writestream *target)
973 974 975 976 977
{
	git_buf in = GIT_BUF_INIT;

	if (buf_from_blob(&in, blob) < 0)
		return -1;
978

Russell Belfer committed
979 980 981
	if (filters)
		git_oid_cpy(&filters->source.oid, git_blob_id(blob));

982
	return git_filter_list_stream_data(filters, &in, target);
983
}