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

8 9
#include "filter.h"

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

21 22 23 24 25
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 */
26
	git_filter_mode_t mode;
27
	uint32_t        flags;
28 29
};

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

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

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

53 54 55 56 57 58 59
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;
}

60 61
struct git_filter_registry {
	git_rwlock lock;
62
	git_vector filters;
63 64
};

65
static struct git_filter_registry filter_registry;
66

67
static void git_filter_global_shutdown(void);
Russell Belfer committed
68

69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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
91
			if (has_eq || *start == '-' || *start == '+' || *start == '!')
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
				(*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;
	}
}

136 137 138 139
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;
140 141 142 143 144 145 146
	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;
147 148
}

149 150
/* Note: callers must lock the registry before calling this function */
static int filter_registry_insert(
151 152 153
	const char *name, git_filter *filter, int priority)
{
	git_filter_def *fdef;
154
	size_t nattr = 0, nmatch = 0, alloc_len;
155 156 157 158 159
	git_buf attrs = GIT_BUF_INIT;

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

160 161 162
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, nattr, 2);
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, alloc_len, sizeof(char *));
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, sizeof(git_filter_def));
163 164

	fdef = git__calloc(1, alloc_len);
165
	GIT_ERROR_CHECK_ALLOC(fdef);
166

167
	fdef->filter_name = git__strdup(name);
168
	GIT_ERROR_CHECK_ALLOC(fdef->filter_name);
169

170 171 172 173 174 175 176 177
	fdef->filter      = filter;
	fdef->priority    = priority;
	fdef->nattrs      = nattr;
	fdef->nmatches    = nmatch;
	fdef->attrdata    = git_buf_detach(&attrs);

	filter_def_set_attrs(fdef);

178
	if (git_vector_insert(&filter_registry.filters, fdef) < 0) {
179
		git__free(fdef->filter_name);
180 181 182 183 184
		git__free(fdef->attrdata);
		git__free(fdef);
		return -1;
	}

185
	git_vector_sort(&filter_registry.filters);
186 187 188
	return 0;
}

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
int git_filter_global_init(void)
{
	git_filter *crlf = NULL, *ident = NULL;
	int error = 0;

	if (git_rwlock_init(&filter_registry.lock) < 0)
		return -1;

	if ((error = git_vector_init(&filter_registry.filters, 2,
		filter_def_priority_cmp)) < 0)
		goto done;

	if ((crlf = git_crlf_filter_new()) == NULL ||
		filter_registry_insert(
			GIT_FILTER_CRLF, crlf, GIT_FILTER_CRLF_PRIORITY) < 0 ||
		(ident = git_ident_filter_new()) == NULL ||
		filter_registry_insert(
			GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
		error = -1;

	git__on_shutdown(git_filter_global_shutdown);

done:
	if (error) {
		git_filter_free(crlf);
		git_filter_free(ident);
	}

	return error;
}

static void git_filter_global_shutdown(void)
{
	size_t pos;
	git_filter_def *fdef;

	if (git_rwlock_wrlock(&filter_registry.lock) < 0)
		return;

	git_vector_foreach(&filter_registry.filters, pos, fdef) {
		if (fdef->filter && fdef->filter->shutdown) {
			fdef->filter->shutdown(fdef->filter);
			fdef->initialized = false;
		}

		git__free(fdef->filter_name);
		git__free(fdef->attrdata);
		git__free(fdef);
	}

	git_vector_free(&filter_registry.filters);

	git_rwlock_wrunlock(&filter_registry.lock);
	git_rwlock_free(&filter_registry.lock);
}

/* Note: callers must lock the registry before calling this function */
static int filter_registry_find(size_t *pos, const char *name)
{
	return git_vector_search2(
		pos, &filter_registry.filters, filter_def_name_key_check, name);
}

/* Note: callers must lock the registry before calling this function */
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(&filter_registry.filters, *pos);

	return fdef;
}


int git_filter_register(
	const char *name, git_filter *filter, int priority)
{
	int error;

	assert(name && filter);

	if (git_rwlock_wrlock(&filter_registry.lock) < 0) {
272
		git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
273 274 275 276
		return -1;
	}

	if (!filter_registry_find(NULL, name)) {
277 278
		git_error_set(
			GIT_ERROR_FILTER, "attempt to reregister existing filter '%s'", name);
279 280 281 282 283 284 285 286 287 288 289
		error = GIT_EEXISTS;
		goto done;
	}

	error = filter_registry_insert(name, filter, priority);

done:
	git_rwlock_wrunlock(&filter_registry.lock);
	return error;
}

290 291 292 293
int git_filter_unregister(const char *name)
{
	size_t pos;
	git_filter_def *fdef;
294
	int error = 0;
295

296 297
	assert(name);

298
	/* cannot unregister default filters */
Russell Belfer committed
299
	if (!strcmp(GIT_FILTER_CRLF, name) || !strcmp(GIT_FILTER_IDENT, name)) {
300
		git_error_set(GIT_ERROR_FILTER, "cannot unregister filter '%s'", name);
301 302 303
		return -1;
	}

304
	if (git_rwlock_wrlock(&filter_registry.lock) < 0) {
305
		git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
306 307 308
		return -1;
	}

309
	if ((fdef = filter_registry_lookup(&pos, name)) == NULL) {
310
		git_error_set(GIT_ERROR_FILTER, "cannot find filter '%s' to unregister", name);
311 312
		error = GIT_ENOTFOUND;
		goto done;
313 314
	}

315
	git_vector_remove(&filter_registry.filters, pos);
316

317
	if (fdef->initialized && fdef->filter && fdef->filter->shutdown) {
318
		fdef->filter->shutdown(fdef->filter);
319 320
		fdef->initialized = false;
	}
321

322
	git__free(fdef->filter_name);
323 324 325
	git__free(fdef->attrdata);
	git__free(fdef);

326 327 328
done:
	git_rwlock_wrunlock(&filter_registry.lock);
	return error;
329 330
}

331 332 333 334
static int filter_initialize(git_filter_def *fdef)
{
	int error = 0;

335 336 337
	if (!fdef->initialized && fdef->filter && fdef->filter->initialize) {
		if ((error = fdef->filter->initialize(fdef->filter)) < 0)
			return error;
338 339 340 341 342 343
	}

	fdef->initialized = true;
	return 0;
}

344 345 346
git_filter *git_filter_lookup(const char *name)
{
	size_t pos;
347
	git_filter_def *fdef;
348
	git_filter *filter = NULL;
349

350
	if (git_rwlock_rdlock(&filter_registry.lock) < 0) {
351
		git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
352
		return NULL;
353
	}
354

355 356 357
	if ((fdef = filter_registry_lookup(&pos, name)) == NULL ||
		(!fdef->initialized && filter_initialize(fdef) < 0))
		goto done;
358

359
	filter = fdef->filter;
360

361 362 363
done:
	git_rwlock_rdunlock(&filter_registry.lock);
	return filter;
364 365
}

Russell Belfer committed
366 367 368 369 370
void git_filter_free(git_filter *filter)
{
	git__free(filter);
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
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)
{
388
	return git_oid_is_zero(&src->oid) ? NULL : &src->oid;
389 390
}

391 392 393 394 395
git_filter_mode_t git_filter_source_mode(const git_filter_source *src)
{
	return src->mode;
}

396
uint32_t git_filter_source_flags(const git_filter_source *src)
397
{
398
	return src->flags;
399 400
}

401
static int filter_list_new(
402
	git_filter_list **out, const git_filter_source *src)
403
{
404
	git_filter_list *fl = NULL;
405
	size_t pathlen = src->path ? strlen(src->path) : 0, alloclen;
406

407 408
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_filter_list), pathlen);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
409

410
	fl = git__calloc(1, alloclen);
411
	GIT_ERROR_CHECK_ALLOC(fl);
412 413 414 415 416

	if (src->path)
		memcpy(fl->path, src->path, pathlen);
	fl->source.repo = src->repo;
	fl->source.path = fl->path;
417
	fl->source.mode = src->mode;
418
	fl->source.flags = src->flags;
419 420 421 422 423

	*out = fl;
	return 0;
}

424
static int filter_list_check_attributes(
425 426 427 428 429
	const char ***out,
	git_repository *repo,
	git_attr_session *attr_session,
	git_filter_def *fdef,
	const git_filter_source *src)
430 431
{
	const char **strs = git__calloc(fdef->nattrs, sizeof(const char *));
432 433 434 435
	uint32_t flags = 0;
	size_t i;
	int error;

436
	GIT_ERROR_CHECK_ALLOC(strs);
437

438 439 440
	if ((src->flags & GIT_FILTER_NO_SYSTEM_ATTRIBUTES) != 0)
		flags |= GIT_ATTR_CHECK_NO_SYSTEM;

441 442 443
	if ((src->flags & GIT_FILTER_ATTRIBUTES_FROM_HEAD) != 0)
		flags |= GIT_ATTR_CHECK_INCLUDE_HEAD;

444
	error = git_attr_get_many_with_session(
445
		strs, repo, attr_session, flags, src->path, fdef->nattrs, fdef->attrs);
446 447 448

	/* if no values were found but no matches are needed, it's okay! */
	if (error == GIT_ENOTFOUND && !fdef->nmatches) {
449
		git_error_clear();
Russell Belfer committed
450
		git__free((void *)strs);
451 452 453 454 455
		return 0;
	}

	for (i = 0; !error && i < fdef->nattrs; ++i) {
		const char *want = fdef->attrs[fdef->nattrs + i];
456
		git_attr_value_t want_type, found_type;
457 458 459 460 461 462 463

		if (!want)
			continue;

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

464 465
		if (want_type != found_type)
			error = GIT_ENOTFOUND;
466
		else if (want_type == GIT_ATTR_VALUE_STRING &&
467 468
				strcmp(want, strs[i]) &&
				strcmp(want, "*"))
469 470 471 472
			error = GIT_ENOTFOUND;
	}

	if (error)
Russell Belfer committed
473
		git__free((void *)strs);
474 475 476 477 478 479
	else
		*out = strs;

	return error;
}

480
int git_filter_list_new(
481 482 483
	git_filter_list **out,
	git_repository *repo,
	git_filter_mode_t mode,
484
	uint32_t flags)
485 486 487 488 489
{
	git_filter_source src = { 0 };
	src.repo = repo;
	src.path = NULL;
	src.mode = mode;
490
	src.flags = flags;
491 492 493
	return filter_list_new(out, &src);
}

494
int git_filter_list__load_ext(
495 496
	git_filter_list **filters,
	git_repository *repo,
Russell Belfer committed
497
	git_blob *blob, /* can be NULL */
498
	const char *path,
499
	git_filter_mode_t mode,
500
	git_filter_options *filter_opts)
501 502 503 504 505
{
	int error = 0;
	git_filter_list *fl = NULL;
	git_filter_source src = { 0 };
	git_filter_entry *fe;
506 507
	size_t idx;
	git_filter_def *fdef;
508

509
	if (git_rwlock_rdlock(&filter_registry.lock) < 0) {
510
		git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
511
		return -1;
512
	}
513 514 515

	src.repo = repo;
	src.path = path;
516
	src.mode = mode;
517 518
	src.flags = filter_opts->flags;

Russell Belfer committed
519 520
	if (blob)
		git_oid_cpy(&src.oid, git_blob_id(blob));
521

522
	git_vector_foreach(&filter_registry.filters, idx, fdef) {
523
		const char **values = NULL;
524 525 526 527
		void *payload = NULL;

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

529
		if (fdef->nattrs > 0) {
530
			error = filter_list_check_attributes(
531
				&values, repo, filter_opts->attr_session, fdef, &src);
532

533 534 535 536 537 538 539
			if (error == GIT_ENOTFOUND) {
				error = 0;
				continue;
			} else if (error < 0)
				break;
		}

540 541 542
		if (!fdef->initialized && (error = filter_initialize(fdef)) < 0)
			break;

543
		if (fdef->filter->check)
544
			error = fdef->filter->check(
545
				fdef->filter, &payload, &src, values);
546

Russell Belfer committed
547
		git__free((void *)values);
548

Russell Belfer committed
549
		if (error == GIT_PASSTHROUGH)
550 551 552 553
			error = 0;
		else if (error < 0)
			break;
		else {
554 555
			if (!fl) {
				if ((error = filter_list_new(&fl, &src)) < 0)
556
					break;
557 558 559

				fl->temp_buf = filter_opts->temp_buf;
			}
560 561

			fe = git_array_alloc(fl->filters);
562
			GIT_ERROR_CHECK_ALLOC(fe);
563 564 565

			fe->filter = fdef->filter;
			fe->filter_name = fdef->filter_name;
566 567 568 569
			fe->payload = payload;
		}
	}

570 571
	git_rwlock_rdunlock(&filter_registry.lock);

572 573 574 575 576 577 578 579 580 581
	if (error && fl != NULL) {
		git_array_clear(fl->filters);
		git__free(fl);
		fl = NULL;
	}

	*filters = fl;
	return error;
}

582 583 584 585 586 587
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,
588
	uint32_t flags)
589
{
590 591 592 593 594 595
	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);
596 597
}

598 599 600 601 602 603 604 605 606 607 608
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);
609 610
	}

611 612
	git_array_clear(fl->filters);
	git__free(fl);
613 614
}

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
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;
}

634 635 636 637 638
int git_filter_list_push(
	git_filter_list *fl, git_filter *filter, void *payload)
{
	int error = 0;
	size_t pos;
639
	git_filter_def *fdef = NULL;
640 641 642 643
	git_filter_entry *fe;

	assert(fl && filter);

644
	if (git_rwlock_rdlock(&filter_registry.lock) < 0) {
645
		git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
646 647 648
		return -1;
	}

649
	if (git_vector_search2(
650 651 652 653 654 655 656
			&pos, &filter_registry.filters,
			filter_def_filter_key_check, filter) == 0)
		fdef = git_vector_get(&filter_registry.filters, pos);

	git_rwlock_rdunlock(&filter_registry.lock);

	if (fdef == NULL) {
657
		git_error_set(GIT_ERROR_FILTER, "cannot use an unregistered filter");
658 659 660 661 662 663 664
		return -1;
	}

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

	fe = git_array_alloc(fl->filters);
665
	GIT_ERROR_CHECK_ALLOC(fe);
666 667 668 669 670 671
	fe->filter  = filter;
	fe->payload = payload;

	return 0;
}

Russell Belfer committed
672 673 674 675 676
size_t git_filter_list_length(const git_filter_list *fl)
{
	return fl ? git_array_size(fl->filters) : 0;
}

677
struct buf_stream {
678
	git_writestream parent;
679 680 681 682 683
	git_buf *target;
	bool complete;
};

static int buf_stream_write(
684
	git_writestream *s, const char *buffer, size_t len)
685
{
686 687
	struct buf_stream *buf_stream = (struct buf_stream *)s;
	assert(buf_stream);
688

689
	assert(buf_stream->complete == 0);
690

691 692
	return git_buf_put(buf_stream->target, buffer, len);
}
693

694
static int buf_stream_close(git_writestream *s)
695 696 697
{
	struct buf_stream *buf_stream = (struct buf_stream *)s;
	assert(buf_stream);
698

699 700
	assert(buf_stream->complete == 0);
	buf_stream->complete = 1;
701

702 703
	return 0;
}
704

705
static void buf_stream_free(git_writestream *s)
706 707 708
{
	GIT_UNUSED(s);
}
709

710 711 712
static void buf_stream_init(struct buf_stream *writer, git_buf *target)
{
	memset(writer, 0, sizeof(struct buf_stream));
713

714 715 716
	writer->parent.write = buf_stream_write;
	writer->parent.close = buf_stream_close;
	writer->parent.free = buf_stream_free;
717
	writer->target = target;
718

719 720
	git_buf_clear(target);
}
721

722 723 724 725 726 727 728 729 730
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);

731 732 733 734
	if (!filters) {
		git_buf_attach_notowned(tgt, src->ptr, src->size);
		return 0;
	}
735 736 737 738

	buf_stream_init(&writer, tgt);

	if ((error = git_filter_list_stream_data(filters, src,
Leo Yang committed
739
		&writer.parent)) < 0)
740 741 742
			return error;

	assert(writer.complete);
743
	return error;
744
}
745 746

int git_filter_list_apply_to_file(
747
	git_buf *out,
748 749 750 751
	git_filter_list *filters,
	git_repository *repo,
	const char *path)
{
752
	struct buf_stream writer;
753 754
	int error;

755
	buf_stream_init(&writer, out);
756

757
	if ((error = git_filter_list_stream_file(
Leo Yang committed
758
		filters, repo, path, &writer.parent)) < 0)
759
			return error;
760

761
	assert(writer.complete);
762 763 764
	return error;
}

765 766
static int buf_from_blob(git_buf *out, git_blob *blob)
{
767
	git_object_size_t rawsize = git_blob_rawsize(blob);
768 769

	if (!git__is_sizet(rawsize)) {
770
		git_error_set(GIT_ERROR_OS, "blob is too large to filter");
771 772 773
		return -1;
	}

774
	git_buf_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize);
775 776 777
	return 0;
}

778
int git_filter_list_apply_to_blob(
779
	git_buf *out,
780 781 782
	git_filter_list *filters,
	git_blob *blob)
{
783 784
	struct buf_stream writer;
	int error;
785

786
	buf_stream_init(&writer, out);
787

788
	if ((error = git_filter_list_stream_blob(
Leo Yang committed
789
		filters, blob, &writer.parent)) < 0)
790
			return error;
791

792 793
	assert(writer.complete);
	return error;
794 795 796
}

struct proxy_stream {
797
	git_writestream parent;
798 799 800 801
	git_filter *filter;
	const git_filter_source *source;
	void **payload;
	git_buf input;
802 803
	git_buf temp_buf;
	git_buf *output;
804
	git_writestream *target;
805 806 807
};

static int proxy_stream_write(
808
	git_writestream *s, const char *buffer, size_t len)
809 810 811 812 813 814 815
{
	struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
	assert(proxy_stream);

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

816
static int proxy_stream_close(git_writestream *s)
817 818 819
{
	struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
	git_buf *writebuf;
820
	git_error_state error_state = {0};
821 822 823 824 825 826 827
	int error;

	assert(proxy_stream);

	error = proxy_stream->filter->apply(
		proxy_stream->filter,
		proxy_stream->payload,
828
		proxy_stream->output,
829 830 831 832 833 834
		&proxy_stream->input,
		proxy_stream->source);

	if (error == GIT_PASSTHROUGH) {
		writebuf = &proxy_stream->input;
	} else if (error == 0) {
835 836
		git_buf_sanitize(proxy_stream->output);
		writebuf = proxy_stream->output;
837
	} else {
838 839
		/* close stream before erroring out taking care
		 * to preserve the original error */
840
		git_error_state_capture(&error_state, error);
841
		proxy_stream->target->close(proxy_stream->target);
842
		git_error_state_restore(&error_state);
843 844 845 846 847 848 849 850 851 852
		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;
}

853
static void proxy_stream_free(git_writestream *s)
854 855 856 857
{
	struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
	assert(proxy_stream);

858 859
	git_buf_dispose(&proxy_stream->input);
	git_buf_dispose(&proxy_stream->temp_buf);
860 861 862 863
	git__free(proxy_stream);
}

static int proxy_stream_init(
864
	git_writestream **out,
865
	git_filter *filter,
866
	git_buf *temp_buf,
867 868
	void **payload,
	const git_filter_source *source,
869
	git_writestream *target)
870 871
{
	struct proxy_stream *proxy_stream = git__calloc(1, sizeof(struct proxy_stream));
872
	GIT_ERROR_CHECK_ALLOC(proxy_stream);
873

874 875 876
	proxy_stream->parent.write = proxy_stream_write;
	proxy_stream->parent.close = proxy_stream_close;
	proxy_stream->parent.free = proxy_stream_free;
877 878 879 880
	proxy_stream->filter = filter;
	proxy_stream->payload = payload;
	proxy_stream->source = source;
	proxy_stream->target = target;
881
	proxy_stream->output = temp_buf ? temp_buf : &proxy_stream->temp_buf;
882

883 884 885
	if (temp_buf)
		git_buf_clear(temp_buf);

886
	*out = (git_writestream *)proxy_stream;
887 888 889 890
	return 0;
}

static int stream_list_init(
891
	git_writestream **out,
892 893
	git_vector *streams,
	git_filter_list *filters,
894
	git_writestream *target)
895
{
896
	git_writestream *last_stream = target;
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
	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);
912
		git_writestream *filter_stream;
913

914 915
		assert(fe->filter->stream || fe->filter->apply);

916 917 918
		/* If necessary, create a stream that proxies the traditional
		 * application.
		 */
919 920
		if (fe->filter->stream)
			error = fe->filter->stream(&filter_stream, fe->filter,
921
				&fe->payload, &filters->source, last_stream);
922 923 924 925 926
		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);
927

928
		if (error < 0)
929
			goto out;
930 931

		git_vector_insert(streams, filter_stream);
932
		last_stream = filter_stream;
933 934
	}

935 936 937 938 939 940 941
out:
	if (error)
		last_stream->close(last_stream);
	else
		*out = last_stream;

	return error;
942 943
}

944
static void filter_streams_free(git_vector *streams)
945
{
946
	git_writestream *stream;
947 948 949 950
	size_t i;

	git_vector_foreach(streams, i, stream)
		stream->free(stream);
951
	git_vector_free(streams);
952 953 954 955 956 957
}

int git_filter_list_stream_file(
	git_filter_list *filters,
	git_repository *repo,
	const char *path,
958
	git_writestream *target)
959
{
960
	char buf[FILTERIO_BUFSIZE];
961
	git_buf abspath = GIT_BUF_INIT;
962 963
	const char *base = repo ? git_repository_workdir(repo) : NULL;
	git_vector filter_streams = GIT_VECTOR_INIT;
964
	git_writestream *stream_start;
965
	ssize_t readlen;
966
	int fd = -1, error, initialized = 0;
967 968 969 970 971

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

974
	if ((fd = git_futils_open_ro(abspath.ptr)) < 0) {
975 976 977 978
		error = fd;
		goto done;
	}

979
	while ((readlen = p_read(fd, buf, sizeof(buf))) > 0) {
980
		if ((error = stream_start->write(stream_start, buf, readlen)) < 0)
981 982 983
			goto done;
	}

984
	if (readlen < 0)
Edward Thomson committed
985
		error = -1;
986 987

done:
988 989 990
	if (initialized)
		error |= stream_start->close(stream_start);

991 992
	if (fd >= 0)
		p_close(fd);
993
	filter_streams_free(&filter_streams);
994
	git_buf_dispose(&abspath);
995 996 997 998 999 1000
	return error;
}

int git_filter_list_stream_data(
	git_filter_list *filters,
	git_buf *data,
1001
	git_writestream *target)
1002 1003
{
	git_vector filter_streams = GIT_VECTOR_INIT;
1004
	git_writestream *stream_start;
1005
	int error, initialized = 0;
1006

1007 1008
	git_buf_sanitize(data);

1009 1010
	if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0)
		goto out;
1011
	initialized = 1;
1012

1013 1014 1015
	if ((error = stream_start->write(
			stream_start, data->ptr, data->size)) < 0)
		goto out;
1016 1017

out:
1018 1019 1020
	if (initialized)
		error |= stream_start->close(stream_start);

1021
	filter_streams_free(&filter_streams);
1022
	return error;
1023 1024 1025 1026 1027
}

int git_filter_list_stream_blob(
	git_filter_list *filters,
	git_blob *blob,
1028
	git_writestream *target)
1029 1030 1031 1032 1033
{
	git_buf in = GIT_BUF_INIT;

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

Russell Belfer committed
1035 1036 1037
	if (filters)
		git_oid_cpy(&filters->source.oid, git_blob_id(blob));

1038
	return git_filter_list_stream_data(filters, &in, target);
1039
}
1040 1041 1042 1043 1044 1045

int git_filter_init(git_filter *filter, unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT);
	return 0;
}