lockfile: add accessors get_lock_file_fd() and get_lock_file_fp()

We are about to move those members, so change client code to read them
through accessor functions.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michael Haggerty 2015-08-10 11:47:38 +02:00 committed by Junio C Hamano
parent e54c347c1c
commit c99a4c2db3
5 changed files with 26 additions and 7 deletions

View File

@ -52,7 +52,7 @@ static void print_entry(struct credential *c)
static void print_line(struct strbuf *buf) static void print_line(struct strbuf *buf)
{ {
strbuf_addch(buf, '\n'); strbuf_addch(buf, '\n');
write_or_die(credential_lock.fd, buf->buf, buf->len); write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len);
} }
static void rewrite_credential_file(const char *fn, struct credential *c, static void rewrite_credential_file(const char *fn, struct credential *c,

View File

@ -364,6 +364,20 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
return lk->fp; return lk->fp;
} }
int get_lock_file_fd(struct lock_file *lk)
{
if (!lk->active)
die("BUG: get_lock_file_fd() called for unlocked object");
return lk->fd;
}
FILE *get_lock_file_fp(struct lock_file *lk)
{
if (!lk->active)
die("BUG: get_lock_file_fp() called for unlocked object");
return lk->fp;
}
char *get_locked_file_path(struct lock_file *lk) char *get_locked_file_path(struct lock_file *lk)
{ {
if (!lk->active) if (!lk->active)

View File

@ -203,6 +203,9 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);
*/ */
extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode); extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode);
extern int get_lock_file_fd(struct lock_file *lk);
extern FILE *get_lock_file_fp(struct lock_file *lk);
/* /*
* Return the path of the file that is locked by the specified * Return the path of the file that is locked by the specified
* lock_file object. The caller must free the memory. * lock_file object. The caller must free the memory.

View File

@ -2112,7 +2112,7 @@ static int commit_locked_index(struct lock_file *lk)
static int do_write_locked_index(struct index_state *istate, struct lock_file *lock, static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
unsigned flags) unsigned flags)
{ {
int ret = do_write_index(istate, lock->fd, 0); int ret = do_write_index(istate, get_lock_file_fd(lock), 0);
if (ret) if (ret)
return ret; return ret;
assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) != assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=

12
refs.c
View File

@ -3162,6 +3162,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
{ {
static char term = '\n'; static char term = '\n';
struct object *o; struct object *o;
int fd;
o = parse_object(sha1); o = parse_object(sha1);
if (!o) { if (!o) {
@ -3178,8 +3179,9 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if (write_in_full(lock->lk->fd, sha1_to_hex(sha1), 40) != 40 || fd = get_lock_file_fd(lock->lk);
write_in_full(lock->lk->fd, &term, 1) != 1 || if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
write_in_full(fd, &term, 1) != 1 ||
close_ref(lock) < 0) { close_ref(lock) < 0) {
int save_errno = errno; int save_errno = errno;
error("Couldn't write %s", lock->lk->filename.buf); error("Couldn't write %s", lock->lk->filename.buf);
@ -4264,10 +4266,10 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
status |= error("couldn't write %s: %s", log_file, status |= error("couldn't write %s: %s", log_file,
strerror(errno)); strerror(errno));
} else if (update && } else if (update &&
(write_in_full(lock->lk->fd, (write_in_full(get_lock_file_fd(lock->lk),
sha1_to_hex(cb.last_kept_sha1), 40) != 40 || sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
write_str_in_full(lock->lk->fd, "\n") != 1 || write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
close_ref(lock) < 0)) { close_ref(lock) < 0)) {
status |= error("couldn't write %s", status |= error("couldn't write %s",
lock->lk->filename.buf); lock->lk->filename.buf);
rollback_lock_file(&reflog_lock); rollback_lock_file(&reflog_lock);