convert: update generic functions to only use generic data structures
Update all functions that are going to be moved into a reusable module so that they only work with the reusable data structures. Move code that is specific to the filter out into the filter specific functions. Signed-off-by: Ben Peart <benpeart@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ben Peart committed
May 5, 2017 at 11:27 UTC
7ddb9b2ca9f5a1ebb8a13744575ec3e6879c5f7e
1 file changed
+23
-18
convert.c
+23
-18
@@ -556,7 +556,6 @@ static void kill_multi_file_filter(struct hashmap *hashmap, struct subprocess_en
556
finish_command(&entry->process);
557
558
hashmap_remove(hashmap, entry, NULL);
559
- free(entry);
559
}
560
561
static void stop_multi_file_filter(struct child_process *process)
@@ -570,14 +569,15 @@ static void stop_multi_file_filter(struct child_process *process)
569
finish_command(process);
570
}
571
573
-static int start_multi_file_filter_fn(struct cmd2process *entry)
572
+static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
573
{
574
int err;
575
+ struct cmd2process *entry = (struct cmd2process *)subprocess;
576
struct string_list cap_list = STRING_LIST_INIT_NODUP;
577
char *cap_buf;
578
const char *cap_name;
579
- struct child_process *process = &entry->subprocess.process;
580
- const char *cmd = entry->subprocess.cmd;
579
+ struct child_process *process = &subprocess->process;
580
+ const char *cmd = subprocess->cmd;
581
582
sigchain_push(SIGPIPE, SIG_IGN);
583
@@ -629,17 +629,16 @@ done:
629
return err;
630
}
631
632
-static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, const char *cmd)
632
+typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
633
+int start_multi_file_filter(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
634
+ subprocess_start_fn startfn)
635
{
636
int err;
635
- struct cmd2process *entry;
637
struct child_process *process;
638
const char *argv[] = { cmd, NULL };
639
639
- entry = xmalloc(sizeof(*entry));
640
- entry->subprocess.cmd = cmd;
641
- entry->supported_capabilities = 0;
642
- process = &entry->subprocess.process;
640
+ entry->cmd = cmd;
641
+ process = &entry->process;
642
643
child_process_init(process);
644
process->argv = argv;
@@ -649,22 +648,23 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons
648
process->clean_on_exit = 1;
649
process->clean_on_exit_handler = stop_multi_file_filter;
650
652
- if (start_command(process)) {
651
+ err = start_command(process);
652
+ if (err) {
653
error("cannot fork to run external filter '%s'", cmd);
654
- return NULL;
654
+ return err;
655
}
656
657
hashmap_entry_init(entry, strhash(cmd));
658
659
- err = start_multi_file_filter_fn(entry);
659
+ err = startfn(entry);
660
if (err) {
661
error("initialization for external filter '%s' failed", cmd);
662
- kill_multi_file_filter(hashmap, &entry->subprocess);
663
- return NULL;
662
+ kill_multi_file_filter(hashmap, entry);
663
+ return err;
664
}
665
666
hashmap_add(hashmap, entry);
667
- return entry;
667
+ return 0;
668
}
669
670
static int apply_multi_file_filter(const char *path, const char *src, size_t len,
@@ -689,9 +689,13 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
689
fflush(NULL);
690
691
if (!entry) {
692
- entry = start_multi_file_filter(&cmd_process_map, cmd);
693
- if (!entry)
692
+ entry = xmalloc(sizeof(*entry));
693
+ entry->supported_capabilities = 0;
694
+
695
+ if (start_multi_file_filter(&cmd_process_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
696
+ free(entry);
697
return 0;
698
+ }
699
}
700
process = &entry->subprocess.process;
701
@@ -765,6 +769,7 @@ done:
769
*/
770
error("external filter '%s' failed", cmd);
771
kill_multi_file_filter(&cmd_process_map, &entry->subprocess);
772
+ free(entry);
773
}
774
} else {
775
strbuf_swap(dst, &nbuf);