builtin/apply: add 'lock_file' pointer into 'struct apply_state'

We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c keeps a linked list of all created lock_file structures. Also 'struct apply_state' users might later want the same 'struct lock_file' instance to be reused by different series of calls to the apply api. So let's add a 'struct lock_file *lock_file' pointer into 'struct apply_state' and have the user of 'struct apply_state' allocate memory for the actual 'struct lock_file' instance. Let's also add an argument to init_apply_state(), so that the caller can easily supply a pointer to the allocated instance. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jun 3, 2016 at 18:58 UTC 8f31fac365c312aa9109a7a1fc1014e56ed473d2
1 file changed +15 -4
builtin/apply.c
+15 -4
@@ -52,6 +52,12 @@ struct apply_state {
52 const char *prefix;
53 int prefix_length;
54
55 + /*
56 + * Since lockfile.c keeps a linked list of all created
57 + * lock_file structures, it isn't safe to free(lock_file).
58 + */
59 + struct lock_file *lock_file;
60 +
61 /* These control what gets looked at and modified */
62 int apply; /* this is not a dry-run */
63 int cached; /* apply to the index only */
@@ -4547,7 +4553,7 @@ static int apply_patch(struct apply_state *state,
4553
4554 state->update_index = state->check_index && state->apply;
4555 if (state->update_index && newfd < 0)
4550 - newfd = hold_locked_index(&lock_file, 1);
4556 + newfd = hold_locked_index(state->lock_file, 1);
4557
4558 if (state->check_index) {
4559 if (read_cache() < 0)
@@ -4648,11 +4654,14 @@ static int option_parse_directory(const struct option *opt,
4654 return 0;
4655 }
4656
4651 -static void init_apply_state(struct apply_state *state, const char *prefix)
4657 +static void init_apply_state(struct apply_state *state,
4658 + const char *prefix,
4659 + struct lock_file *lock_file)
4660 {
4661 memset(state, 0, sizeof(*state));
4662 state->prefix = prefix;
4663 state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4664 + state->lock_file = lock_file;
4665 state->apply = 1;
4666 state->line_termination = '\n';
4667 state->p_value = 1;
@@ -4705,6 +4714,8 @@ static void check_apply_state(struct apply_state *state, int force_apply)
4714 }
4715 if (state->check_index)
4716 state->unsafe_paths = 0;
4717 + if (!state->lock_file)
4718 + die("BUG: state->lock_file should not be NULL");
4719 }
4720
4721 static int apply_all_patches(struct apply_state *state,
@@ -4769,7 +4780,7 @@ static int apply_all_patches(struct apply_state *state,
4780 }
4781
4782 if (state->update_index) {
4772 - if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
4783 + if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
4784 die(_("Unable to write new index file"));
4785 }
4786
@@ -4852,7 +4863,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4863 OPT_END()
4864 };
4865
4855 - init_apply_state(&state, prefix);
4866 + init_apply_state(&state, prefix, &lock_file);
4867
4868 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4869 apply_usage, 0);