From: Christian Couder Date: Wed, 11 May 2016 13:17:33 GMT Subject: [PATCH v2 82/94] apply: roll back index lock file in case of error Message-ID: <20160511131745.2914-83-chriscool@tuxfamily.org> In-Reply-To: <20160511131745.2914-1-chriscool@tuxfamily.org> According to the lockfile API, when finished with a lockfile, one should either commit it or roll it back. This is even more important now that the same lockfile can be passed to init_apply_state() many times to be reused by series of calls to the apply lib functions. Helped-by: Johannes Schindelin Signed-off-by: Christian Couder --- apply.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/apply.c b/apply.c index 3285bf7..7480ae8 100644 --- a/apply.c +++ b/apply.c @@ -4739,7 +4739,7 @@ int apply_all_patches(struct apply_state *state, if (!strcmp(arg, "-")) { res = apply_patch(state, 0, "", options); if (res < 0) - return -1; + goto rollback_end; errs |= res; read_stdin = 0; continue; @@ -4749,21 +4749,23 @@ int apply_all_patches(struct apply_state *state, arg); fd = open(arg, O_RDONLY); - if (fd < 0) - return error(_("can't open patch '%s': %s"), arg, strerror(errno)); + if (fd < 0) { + error(_("can't open patch '%s': %s"), arg, strerror(errno)); + goto rollback_end; + } read_stdin = 0; set_default_whitespace_mode(state); res = apply_patch(state, fd, arg, options); close(fd); if (res < 0) - return -1; + goto rollback_end; errs |= res; } set_default_whitespace_mode(state); if (read_stdin) { res = apply_patch(state, 0, "", options); if (res < 0) - return -1; + goto rollback_end; errs |= res; } @@ -4777,11 +4779,13 @@ int apply_all_patches(struct apply_state *state, squelched), squelched); } - if (state->ws_error_action == die_on_ws_error) - return error(Q_("%d line adds whitespace errors.", - "%d lines add whitespace errors.", - state->whitespace_error), - state->whitespace_error); + if (state->ws_error_action == die_on_ws_error) { + error(Q_("%d line adds whitespace errors.", + "%d lines add whitespace errors.", + state->whitespace_error), + state->whitespace_error); + goto rollback_end; + } if (state->applied_after_fixing_ws && state->apply) warning("%d line%s applied after" " fixing whitespace errors.", @@ -4802,5 +4806,12 @@ int apply_all_patches(struct apply_state *state, } return !!errs; + +rollback_end: + if (state->newfd >= 0) { + rollback_lock_file(state->lock_file); + state->newfd = -1; + } + return -1; } -- 2.8.2.490.g3dabe57