From b4fb09e4da53bfc6c720337142af5db3204736d5 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 10 Aug 2015 11:47:39 +0200 Subject: [PATCH] lockfile: add accessor get_lock_file_path() Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- builtin/commit.c | 15 ++++++++------- config.c | 14 +++++++------- lockfile.c | 7 +++++++ lockfile.h | 6 ++++++ refs.c | 6 +++--- shallow.c | 6 +++--- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 254477fd1d..96aee0c0cc 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix struct string_list partial; struct pathspec pathspec; int refresh_flags = REFRESH_QUIET; + const char *ret; if (is_status) refresh_flags |= REFRESH_UNMERGED; @@ -344,7 +345,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix die(_("unable to create temporary index")); old_index_env = getenv(INDEX_ENVIRONMENT); - setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1); + setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1); if (interactive_add(argc, argv, prefix, patch_interactive) != 0) die(_("interactive add failed")); @@ -355,7 +356,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix unsetenv(INDEX_ENVIRONMENT); discard_cache(); - read_cache_from(index_lock.filename.buf); + read_cache_from(get_lock_file_path(&index_lock)); if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) { if (reopen_lock_file(&index_lock) < 0) die(_("unable to write index file")); @@ -365,7 +366,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix warning(_("Failed to update main cache tree")); commit_style = COMMIT_NORMAL; - return index_lock.filename.buf; + return get_lock_file_path(&index_lock); } /* @@ -388,7 +389,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK)) die(_("unable to write new_index file")); commit_style = COMMIT_NORMAL; - return index_lock.filename.buf; + return get_lock_file_path(&index_lock); } /* @@ -475,9 +476,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix die(_("unable to write temporary index file")); discard_cache(); - read_cache_from(false_lock.filename.buf); - - return false_lock.filename.buf; + ret = get_lock_file_path(&false_lock); + read_cache_from(ret); + return ret; } static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, diff --git a/config.c b/config.c index ab46462e15..adf8b53036 100644 --- a/config.c +++ b/config.c @@ -2056,9 +2056,9 @@ int git_config_set_multivar_in_file(const char *config_filename, MAP_PRIVATE, in_fd, 0); close(in_fd); - if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) { + if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) { error("chmod on %s failed: %s", - lock->filename.buf, strerror(errno)); + get_lock_file_path(lock), strerror(errno)); ret = CONFIG_NO_WRITE; goto out_free; } @@ -2138,7 +2138,7 @@ int git_config_set_multivar_in_file(const char *config_filename, return ret; write_err_out: - ret = write_error(lock->filename.buf); + ret = write_error(get_lock_file_path(lock)); goto out_free; } @@ -2239,9 +2239,9 @@ int git_config_rename_section_in_file(const char *config_filename, fstat(fileno(config_file), &st); - if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) { + if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) { ret = error("chmod on %s failed: %s", - lock->filename.buf, strerror(errno)); + get_lock_file_path(lock), strerror(errno)); goto out; } @@ -2262,7 +2262,7 @@ int git_config_rename_section_in_file(const char *config_filename, } store.baselen = strlen(new_name); if (!store_write_section(out_fd, new_name)) { - ret = write_error(lock->filename.buf); + ret = write_error(get_lock_file_path(lock)); goto out; } /* @@ -2288,7 +2288,7 @@ int git_config_rename_section_in_file(const char *config_filename, continue; length = strlen(output); if (write_in_full(out_fd, output, length) != length) { - ret = write_error(lock->filename.buf); + ret = write_error(get_lock_file_path(lock)); goto out; } } diff --git a/lockfile.c b/lockfile.c index df9c704f51..5e954baf07 100644 --- a/lockfile.c +++ b/lockfile.c @@ -364,6 +364,13 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode) return lk->fp; } +const char *get_lock_file_path(struct lock_file *lk) +{ + if (!lk->active) + die("BUG: get_lock_file_path() called for unlocked object"); + return lk->filename.buf; +} + int get_lock_file_fd(struct lock_file *lk) { if (!lk->active) diff --git a/lockfile.h b/lockfile.h index d9dfbc999c..a204ab6f60 100644 --- a/lockfile.h +++ b/lockfile.h @@ -203,6 +203,12 @@ extern NORETURN void unable_to_lock_die(const char *path, int err); */ extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode); +/* + * Return the path of the lockfile. The return value is a pointer to a + * field within the lock_file object and should not be freed. + */ +extern const char *get_lock_file_path(struct lock_file *lk); + extern int get_lock_file_fd(struct lock_file *lk); extern FILE *get_lock_file_fp(struct lock_file *lk); diff --git a/refs.c b/refs.c index 0f49a6210c..bf68015b5d 100644 --- a/refs.c +++ b/refs.c @@ -3184,7 +3184,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock, write_in_full(fd, &term, 1) != 1 || close_ref(lock) < 0) { int save_errno = errno; - error("Couldn't write %s", lock->lk->filename.buf); + error("Couldn't write %s", get_lock_file_path(lock->lk)); unlock_ref(lock); errno = save_errno; return -1; @@ -4241,7 +4241,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1, cb.newlog = fdopen_lock_file(&reflog_lock, "w"); if (!cb.newlog) { error("cannot fdopen %s (%s)", - reflog_lock.filename.buf, strerror(errno)); + get_lock_file_path(&reflog_lock), strerror(errno)); goto failure; } } @@ -4271,7 +4271,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1, write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 || close_ref(lock) < 0)) { status |= error("couldn't write %s", - lock->lk->filename.buf); + get_lock_file_path(lock->lk)); rollback_lock_file(&reflog_lock); } else if (commit_lock_file(&reflog_lock)) { status |= error("unable to commit reflog '%s' (%s)", diff --git a/shallow.c b/shallow.c index 257d8115c7..7973e74196 100644 --- a/shallow.c +++ b/shallow.c @@ -267,8 +267,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock, if (write_shallow_commits(&sb, 0, extra)) { if (write_in_full(fd, sb.buf, sb.len) != sb.len) die_errno("failed to write to %s", - shallow_lock->filename.buf); - *alternate_shallow_file = shallow_lock->filename.buf; + get_lock_file_path(shallow_lock)); + *alternate_shallow_file = get_lock_file_path(shallow_lock); } else /* * is_repository_shallow() sees empty string as "no @@ -314,7 +314,7 @@ void prune_shallow(int show_only) if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) { if (write_in_full(fd, sb.buf, sb.len) != sb.len) die_errno("failed to write to %s", - shallow_lock.filename.buf); + get_lock_file_path(&shallow_lock)); commit_lock_file(&shallow_lock); } else { unlink(git_path("shallow"));