lockfile: add accessor get_lock_file_path()

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 10, 2015 at 11:47 UTC b4fb09e4da53bfc6c720337142af5db3204736d5
6 files changed +34 -20
builtin/commit.c
+8 -7
@@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
324 struct string_list partial;
325 struct pathspec pathspec;
326 int refresh_flags = REFRESH_QUIET;
327 + const char *ret;
328
329 if (is_status)
330 refresh_flags |= REFRESH_UNMERGED;
@@ -344,7 +345,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
345 die(_("unable to create temporary index"));
346
347 old_index_env = getenv(INDEX_ENVIRONMENT);
347 - setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
348 + setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
349
350 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
351 die(_("interactive add failed"));
@@ -355,7 +356,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
356 unsetenv(INDEX_ENVIRONMENT);
357
358 discard_cache();
358 - read_cache_from(index_lock.filename.buf);
359 + read_cache_from(get_lock_file_path(&index_lock));
360 if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
361 if (reopen_lock_file(&index_lock) < 0)
362 die(_("unable to write index file"));
@@ -365,7 +366,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
366 warning(_("Failed to update main cache tree"));
367
368 commit_style = COMMIT_NORMAL;
368 - return index_lock.filename.buf;
369 + return get_lock_file_path(&index_lock);
370 }
371
372 /*
@@ -388,7 +389,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
389 if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
390 die(_("unable to write new_index file"));
391 commit_style = COMMIT_NORMAL;
391 - return index_lock.filename.buf;
392 + return get_lock_file_path(&index_lock);
393 }
394
395 /*
@@ -475,9 +476,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
476 die(_("unable to write temporary index file"));
477
478 discard_cache();
478 - read_cache_from(false_lock.filename.buf);
479 -
480 - return false_lock.filename.buf;
479 + ret = get_lock_file_path(&false_lock);
480 + read_cache_from(ret);
481 + return ret;
482 }
483
484 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
config.c
+7 -7
@@ -2056,9 +2056,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
2056 MAP_PRIVATE, in_fd, 0);
2057 close(in_fd);
2058
2059 - if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
2059 + if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
2060 error("chmod on %s failed: %s",
2061 - lock->filename.buf, strerror(errno));
2061 + get_lock_file_path(lock), strerror(errno));
2062 ret = CONFIG_NO_WRITE;
2063 goto out_free;
2064 }
@@ -2138,7 +2138,7 @@ out_free:
2138 return ret;
2139
2140 write_err_out:
2141 - ret = write_error(lock->filename.buf);
2141 + ret = write_error(get_lock_file_path(lock));
2142 goto out_free;
2143
2144 }
@@ -2239,9 +2239,9 @@ int git_config_rename_section_in_file(const char *config_filename,
2239
2240 fstat(fileno(config_file), &st);
2241
2242 - if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
2242 + if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
2243 ret = error("chmod on %s failed: %s",
2244 - lock->filename.buf, strerror(errno));
2244 + get_lock_file_path(lock), strerror(errno));
2245 goto out;
2246 }
2247
@@ -2262,7 +2262,7 @@ int git_config_rename_section_in_file(const char *config_filename,
2262 }
2263 store.baselen = strlen(new_name);
2264 if (!store_write_section(out_fd, new_name)) {
2265 - ret = write_error(lock->filename.buf);
2265 + ret = write_error(get_lock_file_path(lock));
2266 goto out;
2267 }
2268 /*
@@ -2288,7 +2288,7 @@ int git_config_rename_section_in_file(const char *config_filename,
2288 continue;
2289 length = strlen(output);
2290 if (write_in_full(out_fd, output, length) != length) {
2291 - ret = write_error(lock->filename.buf);
2291 + ret = write_error(get_lock_file_path(lock));
2292 goto out;
2293 }
2294 }
lockfile.c
+7
@@ -364,6 +364,13 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
364 return lk->fp;
365 }
366
367 +const char *get_lock_file_path(struct lock_file *lk)
368 +{
369 + if (!lk->active)
370 + die("BUG: get_lock_file_path() called for unlocked object");
371 + return lk->filename.buf;
372 +}
373 +
374 int get_lock_file_fd(struct lock_file *lk)
375 {
376 if (!lk->active)
lockfile.h
+6
@@ -203,6 +203,12 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);
203 */
204 extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode);
205
206 +/*
207 + * Return the path of the lockfile. The return value is a pointer to a
208 + * field within the lock_file object and should not be freed.
209 + */
210 +extern const char *get_lock_file_path(struct lock_file *lk);
211 +
212 extern int get_lock_file_fd(struct lock_file *lk);
213 extern FILE *get_lock_file_fp(struct lock_file *lk);
214
refs.c
+3 -3
@@ -3184,7 +3184,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
3184 write_in_full(fd, &term, 1) != 1 ||
3185 close_ref(lock) < 0) {
3186 int save_errno = errno;
3187 - error("Couldn't write %s", lock->lk->filename.buf);
3187 + error("Couldn't write %s", get_lock_file_path(lock->lk));
3188 unlock_ref(lock);
3189 errno = save_errno;
3190 return -1;
@@ -4241,7 +4241,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
4241 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
4242 if (!cb.newlog) {
4243 error("cannot fdopen %s (%s)",
4244 - reflog_lock.filename.buf, strerror(errno));
4244 + get_lock_file_path(&reflog_lock), strerror(errno));
4245 goto failure;
4246 }
4247 }
@@ -4271,7 +4271,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
4271 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
4272 close_ref(lock) < 0)) {
4273 status |= error("couldn't write %s",
4274 - lock->lk->filename.buf);
4274 + get_lock_file_path(lock->lk));
4275 rollback_lock_file(&reflog_lock);
4276 } else if (commit_lock_file(&reflog_lock)) {
4277 status |= error("unable to commit reflog '%s' (%s)",
shallow.c
+3 -3
@@ -267,8 +267,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
267 if (write_shallow_commits(&sb, 0, extra)) {
268 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
269 die_errno("failed to write to %s",
270 - shallow_lock->filename.buf);
271 - *alternate_shallow_file = shallow_lock->filename.buf;
270 + get_lock_file_path(shallow_lock));
271 + *alternate_shallow_file = get_lock_file_path(shallow_lock);
272 } else
273 /*
274 * is_repository_shallow() sees empty string as "no
@@ -314,7 +314,7 @@ void prune_shallow(int show_only)
314 if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
315 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
316 die_errno("failed to write to %s",
317 - shallow_lock.filename.buf);
317 + get_lock_file_path(&shallow_lock));
318 commit_lock_file(&shallow_lock);
319 } else {
320 unlink(git_path("shallow"));