]> git.proxmox.com Git - libgit2.git/blob - src/filter.c
09b57dc80210e1e628f1b6dcc8dd2924ce490917
[libgit2.git] / src / filter.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "filter.h"
9
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 struct git_filter_source {
22 git_repository *repo;
23 const char *path;
24 git_oid oid; /* zero if unknown (which is likely) */
25 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 git_filter *filter;
33 void *payload;
34 } git_filter_entry;
35
36 struct git_filter_list {
37 git_array_t(git_filter_entry) filters;
38 git_filter_source source;
39 git_buf *temp_buf;
40 char path[GIT_FLEX_ARRAY];
41 };
42
43 typedef struct {
44 char *filter_name;
45 git_filter *filter;
46 int priority;
47 int initialized;
48 size_t nattrs, nmatches;
49 char *attrdata;
50 const char *attrs[GIT_FLEX_ARRAY];
51 } git_filter_def;
52
53 static int filter_def_priority_cmp(const void *a, const void *b)
54 {
55 int pa = ((const git_filter_def *)a)->priority;
56 int pb = ((const git_filter_def *)b)->priority;
57 return (pa < pb) ? -1 : (pa > pb) ? 1 : 0;
58 }
59
60 struct git_filter_registry {
61 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);
68
69
70 static int filter_def_scan_attrs(
71 git_buf *attrs, size_t *nattr, size_t *nmatch, const char *attr_str)
72 {
73 const char *start, *scan = attr_str;
74 int has_eq;
75
76 *nattr = *nmatch = 0;
77
78 if (!scan)
79 return 0;
80
81 while (*scan) {
82 while (git__isspace(*scan)) scan++;
83
84 for (start = scan, has_eq = 0; *scan && !git__isspace(*scan); ++scan) {
85 if (*scan == '=')
86 has_eq = 1;
87 }
88
89 if (scan > start) {
90 (*nattr)++;
91 if (has_eq || *start == '-' || *start == '+' || *start == '!')
92 (*nmatch)++;
93
94 if (has_eq)
95 git_buf_putc(attrs, '=');
96 git_buf_put(attrs, start, scan - start);
97 git_buf_putc(attrs, '\0');
98 }
99 }
100
101 return 0;
102 }
103
104 static void filter_def_set_attrs(git_filter_def *fdef)
105 {
106 char *scan = fdef->attrdata;
107 size_t i;
108
109 for (i = 0; i < fdef->nattrs; ++i) {
110 const char *name, *value;
111
112 switch (*scan) {
113 case '=':
114 name = scan + 1;
115 for (scan++; *scan != '='; scan++) /* find '=' */;
116 *scan++ = '\0';
117 value = scan;
118 break;
119 case '-':
120 name = scan + 1; value = git_attr__false; break;
121 case '+':
122 name = scan + 1; value = git_attr__true; break;
123 case '!':
124 name = scan + 1; value = git_attr__unset; break;
125 default:
126 name = scan; value = NULL; break;
127 }
128
129 fdef->attrs[i] = name;
130 fdef->attrs[i + fdef->nattrs] = value;
131
132 scan += strlen(scan) + 1;
133 }
134 }
135
136 static int filter_def_name_key_check(const void *key, const void *fdef)
137 {
138 const char *name =
139 fdef ? ((const git_filter_def *)fdef)->filter_name : NULL;
140 return name ? git__strcmp(key, name) : -1;
141 }
142
143 static int filter_def_filter_key_check(const void *key, const void *fdef)
144 {
145 const void *filter = fdef ? ((const git_filter_def *)fdef)->filter : NULL;
146 return (key == filter) ? 0 : -1;
147 }
148
149 /* Note: callers must lock the registry before calling this function */
150 static int filter_registry_insert(
151 const char *name, git_filter *filter, int priority)
152 {
153 git_filter_def *fdef;
154 size_t nattr = 0, nmatch = 0, alloc_len;
155 git_buf attrs = GIT_BUF_INIT;
156
157 if (filter_def_scan_attrs(&attrs, &nattr, &nmatch, filter->attributes) < 0)
158 return -1;
159
160 GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, nattr, 2);
161 GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, alloc_len, sizeof(char *));
162 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 fdef->filter = filter;
171 fdef->priority = priority;
172 fdef->nattrs = nattr;
173 fdef->nmatches = nmatch;
174 fdef->attrdata = git_buf_detach(&attrs);
175
176 filter_def_set_attrs(fdef);
177
178 if (git_vector_insert(&filter_registry.filters, fdef) < 0) {
179 git__free(fdef->filter_name);
180 git__free(fdef->attrdata);
181 git__free(fdef);
182 return -1;
183 }
184
185 git_vector_sort(&filter_registry.filters);
186 return 0;
187 }
188
189 int git_filter_global_init(void)
190 {
191 git_filter *crlf = NULL, *ident = NULL;
192 int error = 0;
193
194 if (git_rwlock_init(&filter_registry.lock) < 0)
195 return -1;
196
197 if ((error = git_vector_init(&filter_registry.filters, 2,
198 filter_def_priority_cmp)) < 0)
199 goto done;
200
201 if ((crlf = git_crlf_filter_new()) == NULL ||
202 filter_registry_insert(
203 GIT_FILTER_CRLF, crlf, GIT_FILTER_CRLF_PRIORITY) < 0 ||
204 (ident = git_ident_filter_new()) == NULL ||
205 filter_registry_insert(
206 GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
207 error = -1;
208
209 git__on_shutdown(git_filter_global_shutdown);
210
211 done:
212 if (error) {
213 git_filter_free(crlf);
214 git_filter_free(ident);
215 }
216
217 return error;
218 }
219
220 static void git_filter_global_shutdown(void)
221 {
222 size_t pos;
223 git_filter_def *fdef;
224
225 if (git_rwlock_wrlock(&filter_registry.lock) < 0)
226 return;
227
228 git_vector_foreach(&filter_registry.filters, pos, fdef) {
229 if (fdef->filter && fdef->filter->shutdown) {
230 fdef->filter->shutdown(fdef->filter);
231 fdef->initialized = false;
232 }
233
234 git__free(fdef->filter_name);
235 git__free(fdef->attrdata);
236 git__free(fdef);
237 }
238
239 git_vector_free(&filter_registry.filters);
240
241 git_rwlock_wrunlock(&filter_registry.lock);
242 git_rwlock_free(&filter_registry.lock);
243 }
244
245 /* Note: callers must lock the registry before calling this function */
246 static int filter_registry_find(size_t *pos, const char *name)
247 {
248 return git_vector_search2(
249 pos, &filter_registry.filters, filter_def_name_key_check, name);
250 }
251
252 /* Note: callers must lock the registry before calling this function */
253 static git_filter_def *filter_registry_lookup(size_t *pos, const char *name)
254 {
255 git_filter_def *fdef = NULL;
256
257 if (!filter_registry_find(pos, name))
258 fdef = git_vector_get(&filter_registry.filters, *pos);
259
260 return fdef;
261 }
262
263
264 int git_filter_register(
265 const char *name, git_filter *filter, int priority)
266 {
267 int error;
268
269 assert(name && filter);
270
271 if (git_rwlock_wrlock(&filter_registry.lock) < 0) {
272 git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
273 return -1;
274 }
275
276 if (!filter_registry_find(NULL, name)) {
277 git_error_set(
278 GIT_ERROR_FILTER, "attempt to reregister existing filter '%s'", name);
279 error = GIT_EEXISTS;
280 goto done;
281 }
282
283 error = filter_registry_insert(name, filter, priority);
284
285 done:
286 git_rwlock_wrunlock(&filter_registry.lock);
287 return error;
288 }
289
290 int git_filter_unregister(const char *name)
291 {
292 size_t pos;
293 git_filter_def *fdef;
294 int error = 0;
295
296 assert(name);
297
298 /* cannot unregister default filters */
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 return -1;
302 }
303
304 if (git_rwlock_wrlock(&filter_registry.lock) < 0) {
305 git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
306 return -1;
307 }
308
309 if ((fdef = filter_registry_lookup(&pos, name)) == NULL) {
310 git_error_set(GIT_ERROR_FILTER, "cannot find filter '%s' to unregister", name);
311 error = GIT_ENOTFOUND;
312 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 fdef->initialized = false;
320 }
321
322 git__free(fdef->filter_name);
323 git__free(fdef->attrdata);
324 git__free(fdef);
325
326 done:
327 git_rwlock_wrunlock(&filter_registry.lock);
328 return error;
329 }
330
331 static int filter_initialize(git_filter_def *fdef)
332 {
333 int error = 0;
334
335 if (!fdef->initialized && fdef->filter && fdef->filter->initialize) {
336 if ((error = fdef->filter->initialize(fdef->filter)) < 0)
337 return error;
338 }
339
340 fdef->initialized = true;
341 return 0;
342 }
343
344 git_filter *git_filter_lookup(const char *name)
345 {
346 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 if ((fdef = filter_registry_lookup(&pos, name)) == NULL ||
356 (!fdef->initialized && filter_initialize(fdef) < 0))
357 goto done;
358
359 filter = fdef->filter;
360
361 done:
362 git_rwlock_rdunlock(&filter_registry.lock);
363 return filter;
364 }
365
366 void git_filter_free(git_filter *filter)
367 {
368 git__free(filter);
369 }
370
371 git_repository *git_filter_source_repo(const git_filter_source *src)
372 {
373 return src->repo;
374 }
375
376 const char *git_filter_source_path(const git_filter_source *src)
377 {
378 return src->path;
379 }
380
381 uint16_t git_filter_source_filemode(const git_filter_source *src)
382 {
383 return src->filemode;
384 }
385
386 const git_oid *git_filter_source_id(const git_filter_source *src)
387 {
388 return git_oid_is_zero(&src->oid) ? NULL : &src->oid;
389 }
390
391 git_filter_mode_t git_filter_source_mode(const git_filter_source *src)
392 {
393 return src->mode;
394 }
395
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 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_filter_list), pathlen);
408 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
409
410 fl = git__calloc(1, alloclen);
411 GIT_ERROR_CHECK_ALLOC(fl);
412
413 if (src->path)
414 memcpy(fl->path, src->path, pathlen);
415 fl->source.repo = src->repo;
416 fl->source.path = fl->path;
417 fl->source.mode = src->mode;
418 fl->source.flags = src->flags;
419
420 *out = fl;
421 return 0;
422 }
423
424 static int filter_list_check_attributes(
425 const char ***out,
426 git_repository *repo,
427 git_attr_session *attr_session,
428 git_filter_def *fdef,
429 const git_filter_source *src)
430 {
431 const char **strs = git__calloc(fdef->nattrs, sizeof(const char *));
432 uint32_t flags = 0;
433 size_t i;
434 int error;
435
436 GIT_ERROR_CHECK_ALLOC(strs);
437
438 if ((src->flags & GIT_FILTER_NO_SYSTEM_ATTRIBUTES) != 0)
439 flags |= GIT_ATTR_CHECK_NO_SYSTEM;
440
441 if ((src->flags & GIT_FILTER_ATTRIBUTES_FROM_HEAD) != 0)
442 flags |= GIT_ATTR_CHECK_INCLUDE_HEAD;
443
444 error = git_attr_get_many_with_session(
445 strs, repo, attr_session, flags, src->path, fdef->nattrs, fdef->attrs);
446
447 /* if no values were found but no matches are needed, it's okay! */
448 if (error == GIT_ENOTFOUND && !fdef->nmatches) {
449 git_error_clear();
450 git__free((void *)strs);
451 return 0;
452 }
453
454 for (i = 0; !error && i < fdef->nattrs; ++i) {
455 const char *want = fdef->attrs[fdef->nattrs + i];
456 git_attr_value_t want_type, found_type;
457
458 if (!want)
459 continue;
460
461 want_type = git_attr_value(want);
462 found_type = git_attr_value(strs[i]);
463
464 if (want_type != found_type)
465 error = GIT_ENOTFOUND;
466 else if (want_type == GIT_ATTR_VALUE_STRING &&
467 strcmp(want, strs[i]) &&
468 strcmp(want, "*"))
469 error = GIT_ENOTFOUND;
470 }
471
472 if (error)
473 git__free((void *)strs);
474 else
475 *out = strs;
476
477 return error;
478 }
479
480 int git_filter_list_new(
481 git_filter_list **out,
482 git_repository *repo,
483 git_filter_mode_t mode,
484 uint32_t flags)
485 {
486 git_filter_source src = { 0 };
487 src.repo = repo;
488 src.path = NULL;
489 src.mode = mode;
490 src.flags = flags;
491 return filter_list_new(out, &src);
492 }
493
494 int git_filter_list__load_ext(
495 git_filter_list **filters,
496 git_repository *repo,
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 int error = 0;
503 git_filter_list *fl = NULL;
504 git_filter_source src = { 0 };
505 git_filter_entry *fe;
506 size_t idx;
507 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 src.repo = repo;
515 src.path = path;
516 src.mode = mode;
517 src.flags = filter_opts->flags;
518
519 if (blob)
520 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 void *payload = NULL;
525
526 if (!fdef || !fdef->filter)
527 continue;
528
529 if (fdef->nattrs > 0) {
530 error = filter_list_check_attributes(
531 &values, repo, filter_opts->attr_session, fdef, &src);
532
533 if (error == GIT_ENOTFOUND) {
534 error = 0;
535 continue;
536 } else if (error < 0)
537 break;
538 }
539
540 if (!fdef->initialized && (error = filter_initialize(fdef)) < 0)
541 break;
542
543 if (fdef->filter->check)
544 error = fdef->filter->check(
545 fdef->filter, &payload, &src, values);
546
547 git__free((void *)values);
548
549 if (error == GIT_PASSTHROUGH)
550 error = 0;
551 else if (error < 0)
552 break;
553 else {
554 if (!fl) {
555 if ((error = filter_list_new(&fl, &src)) < 0)
556 break;
557
558 fl->temp_buf = filter_opts->temp_buf;
559 }
560
561 fe = git_array_alloc(fl->filters);
562 GIT_ERROR_CHECK_ALLOC(fe);
563
564 fe->filter = fdef->filter;
565 fe->filter_name = fdef->filter_name;
566 fe->payload = payload;
567 }
568 }
569
570 git_rwlock_rdunlock(&filter_registry.lock);
571
572 if (error && fl != NULL) {
573 git_array_clear(fl->filters);
574 git__free(fl);
575 fl = NULL;
576 }
577
578 *filters = fl;
579 return error;
580 }
581
582 int git_filter_list_load(
583 git_filter_list **filters,
584 git_repository *repo,
585 git_blob *blob, /* can be NULL */
586 const char *path,
587 git_filter_mode_t mode,
588 uint32_t flags)
589 {
590 git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;
591
592 filter_opts.flags = flags;
593
594 return git_filter_list__load_ext(
595 filters, repo, blob, path, mode, &filter_opts);
596 }
597
598 void git_filter_list_free(git_filter_list *fl)
599 {
600 uint32_t i;
601
602 if (!fl)
603 return;
604
605 for (i = 0; i < git_array_size(fl->filters); ++i) {
606 git_filter_entry *fe = git_array_get(fl->filters, i);
607 if (fe->filter->cleanup)
608 fe->filter->cleanup(fe->filter, fe->payload);
609 }
610
611 git_array_clear(fl->filters);
612 git__free(fl);
613 }
614
615 int git_filter_list_contains(
616 git_filter_list *fl,
617 const char *name)
618 {
619 size_t i;
620
621 assert(name);
622
623 if (!fl)
624 return 0;
625
626 for (i = 0; i < fl->filters.size; i++) {
627 if (strcmp(fl->filters.ptr[i].filter_name, name) == 0)
628 return 1;
629 }
630
631 return 0;
632 }
633
634 int git_filter_list_push(
635 git_filter_list *fl, git_filter *filter, void *payload)
636 {
637 int error = 0;
638 size_t pos;
639 git_filter_def *fdef = NULL;
640 git_filter_entry *fe;
641
642 assert(fl && filter);
643
644 if (git_rwlock_rdlock(&filter_registry.lock) < 0) {
645 git_error_set(GIT_ERROR_OS, "failed to lock filter registry");
646 return -1;
647 }
648
649 if (git_vector_search2(
650 &pos, &filter_registry.filters,
651 filter_def_filter_key_check, filter) == 0)
652 fdef = git_vector_get(&filter_registry.filters, pos);
653
654 git_rwlock_rdunlock(&filter_registry.lock);
655
656 if (fdef == NULL) {
657 git_error_set(GIT_ERROR_FILTER, "cannot use an unregistered filter");
658 return -1;
659 }
660
661 if (!fdef->initialized && (error = filter_initialize(fdef)) < 0)
662 return error;
663
664 fe = git_array_alloc(fl->filters);
665 GIT_ERROR_CHECK_ALLOC(fe);
666 fe->filter = filter;
667 fe->payload = payload;
668
669 return 0;
670 }
671
672 size_t git_filter_list_length(const git_filter_list *fl)
673 {
674 return fl ? git_array_size(fl->filters) : 0;
675 }
676
677 struct buf_stream {
678 git_writestream parent;
679 git_buf *target;
680 bool complete;
681 };
682
683 static int buf_stream_write(
684 git_writestream *s, const char *buffer, size_t len)
685 {
686 struct buf_stream *buf_stream = (struct buf_stream *)s;
687 assert(buf_stream);
688
689 assert(buf_stream->complete == 0);
690
691 return git_buf_put(buf_stream->target, buffer, len);
692 }
693
694 static int buf_stream_close(git_writestream *s)
695 {
696 struct buf_stream *buf_stream = (struct buf_stream *)s;
697 assert(buf_stream);
698
699 assert(buf_stream->complete == 0);
700 buf_stream->complete = 1;
701
702 return 0;
703 }
704
705 static void buf_stream_free(git_writestream *s)
706 {
707 GIT_UNUSED(s);
708 }
709
710 static void buf_stream_init(struct buf_stream *writer, git_buf *target)
711 {
712 memset(writer, 0, sizeof(struct buf_stream));
713
714 writer->parent.write = buf_stream_write;
715 writer->parent.close = buf_stream_close;
716 writer->parent.free = buf_stream_free;
717 writer->target = target;
718
719 git_buf_clear(target);
720 }
721
722 int git_filter_list_apply_to_data(
723 git_buf *tgt, git_filter_list *filters, git_buf *src)
724 {
725 struct buf_stream writer;
726 int error;
727
728 git_buf_sanitize(tgt);
729 git_buf_sanitize(src);
730
731 if (!filters) {
732 git_buf_attach_notowned(tgt, src->ptr, src->size);
733 return 0;
734 }
735
736 buf_stream_init(&writer, tgt);
737
738 if ((error = git_filter_list_stream_data(filters, src,
739 &writer.parent)) < 0)
740 return error;
741
742 assert(writer.complete);
743 return error;
744 }
745
746 int git_filter_list_apply_to_file(
747 git_buf *out,
748 git_filter_list *filters,
749 git_repository *repo,
750 const char *path)
751 {
752 struct buf_stream writer;
753 int error;
754
755 buf_stream_init(&writer, out);
756
757 if ((error = git_filter_list_stream_file(
758 filters, repo, path, &writer.parent)) < 0)
759 return error;
760
761 assert(writer.complete);
762 return error;
763 }
764
765 static int buf_from_blob(git_buf *out, git_blob *blob)
766 {
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 return -1;
772 }
773
774 git_buf_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize);
775 return 0;
776 }
777
778 int git_filter_list_apply_to_blob(
779 git_buf *out,
780 git_filter_list *filters,
781 git_blob *blob)
782 {
783 struct buf_stream writer;
784 int error;
785
786 buf_stream_init(&writer, out);
787
788 if ((error = git_filter_list_stream_blob(
789 filters, blob, &writer.parent)) < 0)
790 return error;
791
792 assert(writer.complete);
793 return error;
794 }
795
796 struct proxy_stream {
797 git_writestream parent;
798 git_filter *filter;
799 const git_filter_source *source;
800 void **payload;
801 git_buf input;
802 git_buf temp_buf;
803 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 struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
811 assert(proxy_stream);
812
813 return git_buf_put(&proxy_stream->input, buffer, len);
814 }
815
816 static int proxy_stream_close(git_writestream *s)
817 {
818 struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
819 git_buf *writebuf;
820 git_error_state error_state = {0};
821 int error;
822
823 assert(proxy_stream);
824
825 error = proxy_stream->filter->apply(
826 proxy_stream->filter,
827 proxy_stream->payload,
828 proxy_stream->output,
829 &proxy_stream->input,
830 proxy_stream->source);
831
832 if (error == GIT_PASSTHROUGH) {
833 writebuf = &proxy_stream->input;
834 } else if (error == 0) {
835 git_buf_sanitize(proxy_stream->output);
836 writebuf = proxy_stream->output;
837 } else {
838 /* close stream before erroring out taking care
839 * 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 return error;
844 }
845
846 if ((error = proxy_stream->target->write(
847 proxy_stream->target, writebuf->ptr, writebuf->size)) == 0)
848 error = proxy_stream->target->close(proxy_stream->target);
849
850 return error;
851 }
852
853 static void proxy_stream_free(git_writestream *s)
854 {
855 struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
856 assert(proxy_stream);
857
858 git_buf_dispose(&proxy_stream->input);
859 git_buf_dispose(&proxy_stream->temp_buf);
860 git__free(proxy_stream);
861 }
862
863 static int proxy_stream_init(
864 git_writestream **out,
865 git_filter *filter,
866 git_buf *temp_buf,
867 void **payload,
868 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 proxy_stream->parent.write = proxy_stream_write;
875 proxy_stream->parent.close = proxy_stream_close;
876 proxy_stream->parent.free = proxy_stream_free;
877 proxy_stream->filter = filter;
878 proxy_stream->payload = payload;
879 proxy_stream->source = source;
880 proxy_stream->target = target;
881 proxy_stream->output = temp_buf ? temp_buf : &proxy_stream->temp_buf;
882
883 if (temp_buf)
884 git_buf_clear(temp_buf);
885
886 *out = (git_writestream *)proxy_stream;
887 return 0;
888 }
889
890 static int stream_list_init(
891 git_writestream **out,
892 git_vector *streams,
893 git_filter_list *filters,
894 git_writestream *target)
895 {
896 git_writestream *last_stream = target;
897 size_t i;
898 int error = 0;
899
900 *out = NULL;
901
902 if (!filters) {
903 *out = target;
904 return 0;
905 }
906
907 /* Create filters last to first to get the chaining direction */
908 for (i = 0; i < git_array_size(filters->filters); ++i) {
909 size_t filter_idx = (filters->source.mode == GIT_FILTER_TO_WORKTREE) ?
910 git_array_size(filters->filters) - 1 - i : i;
911 git_filter_entry *fe = git_array_get(filters->filters, filter_idx);
912 git_writestream *filter_stream;
913
914 assert(fe->filter->stream || fe->filter->apply);
915
916 /* If necessary, create a stream that proxies the traditional
917 * application.
918 */
919 if (fe->filter->stream)
920 error = fe->filter->stream(&filter_stream, fe->filter,
921 &fe->payload, &filters->source, last_stream);
922 else
923 /* Create a stream that proxies the one-shot apply */
924 error = proxy_stream_init(&filter_stream, fe->filter,
925 filters->temp_buf, &fe->payload, &filters->source,
926 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 out:
936 if (error)
937 last_stream->close(last_stream);
938 else
939 *out = last_stream;
940
941 return error;
942 }
943
944 static void filter_streams_free(git_vector *streams)
945 {
946 git_writestream *stream;
947 size_t i;
948
949 git_vector_foreach(streams, i, stream)
950 stream->free(stream);
951 git_vector_free(streams);
952 }
953
954 int git_filter_list_stream_file(
955 git_filter_list *filters,
956 git_repository *repo,
957 const char *path,
958 git_writestream *target)
959 {
960 char buf[FILTERIO_BUFSIZE];
961 git_buf abspath = GIT_BUF_INIT;
962 const char *base = repo ? git_repository_workdir(repo) : NULL;
963 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 if ((error = stream_list_init(
969 &stream_start, &filter_streams, filters, target)) < 0 ||
970 (error = git_path_join_unrooted(&abspath, path, base, NULL)) < 0)
971 goto done;
972 initialized = 1;
973
974 if ((fd = git_futils_open_ro(abspath.ptr)) < 0) {
975 error = fd;
976 goto done;
977 }
978
979 while ((readlen = p_read(fd, buf, sizeof(buf))) > 0) {
980 if ((error = stream_start->write(stream_start, buf, readlen)) < 0)
981 goto done;
982 }
983
984 if (readlen < 0)
985 error = -1;
986
987 done:
988 if (initialized)
989 error |= stream_start->close(stream_start);
990
991 if (fd >= 0)
992 p_close(fd);
993 filter_streams_free(&filter_streams);
994 git_buf_dispose(&abspath);
995 return error;
996 }
997
998 int git_filter_list_stream_data(
999 git_filter_list *filters,
1000 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 git_buf_sanitize(data);
1008
1009 if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0)
1010 goto out;
1011 initialized = 1;
1012
1013 if ((error = stream_start->write(
1014 stream_start, data->ptr, data->size)) < 0)
1015 goto out;
1016
1017 out:
1018 if (initialized)
1019 error |= stream_start->close(stream_start);
1020
1021 filter_streams_free(&filter_streams);
1022 return error;
1023 }
1024
1025 int git_filter_list_stream_blob(
1026 git_filter_list *filters,
1027 git_blob *blob,
1028 git_writestream *target)
1029 {
1030 git_buf in = GIT_BUF_INIT;
1031
1032 if (buf_from_blob(&in, blob) < 0)
1033 return -1;
1034
1035 if (filters)
1036 git_oid_cpy(&filters->source.oid, git_blob_id(blob));
1037
1038 return git_filter_list_stream_data(filters, &in, target);
1039 }
1040
1041 int git_filter_init(git_filter *filter, unsigned int version)
1042 {
1043 GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT);
1044 return 0;
1045 }