@matheusbsbs / matheus-bagliano / commits / c33d5174d6
Fork 0

refs.c: convert it to use lockfile interface.

This updates the ref locking code to use creat-rename locking code we use for the index file, so that it can borrow the code to clean things up upon signals and program termination. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jun 6, 2006 at 13:54 UTC c33d5174d692b1861e0b2f656e51b80b2b00a23c
2 files changed +11 -12
refs.c
+10 -11
@@ -301,19 +301,18 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
301 unlock_ref(lock);
302 return NULL;
303 }
304 + lock->lk = xcalloc(1, sizeof(struct lock_file));
305
306 lock->ref_file = strdup(path);
306 - lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
307 lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
308 lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
309
310 - if (safe_create_leading_directories(lock->lock_file))
311 - die("unable to create directory for %s", lock->lock_file);
312 - lock->lock_fd = open(lock->lock_file,
313 - O_WRONLY | O_CREAT | O_EXCL, 0666);
310 + if (safe_create_leading_directories(lock->ref_file))
311 + die("unable to create directory for %s", lock->ref_file);
312 + lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file);
313 if (lock->lock_fd < 0) {
314 error("Couldn't open lock file %s: %s",
316 - lock->lock_file, strerror(errno));
315 + lock->lk->filename, strerror(errno));
316 unlock_ref(lock);
317 return NULL;
318 }
@@ -341,12 +340,12 @@ void unlock_ref (struct ref_lock *lock)
340 {
341 if (lock->lock_fd >= 0) {
342 close(lock->lock_fd);
344 - unlink(lock->lock_file);
343 + /* Do not free lock->lk -- atexit() still looks at them */
344 + if (lock->lk)
345 + rollback_lock_file(lock->lk);
346 }
347 if (lock->ref_file)
348 free(lock->ref_file);
348 - if (lock->lock_file)
349 - free(lock->lock_file);
349 if (lock->log_file)
350 free(lock->log_file);
351 free(lock);
@@ -415,7 +414,7 @@ int write_ref_sha1(struct ref_lock *lock,
414 if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
415 write(lock->lock_fd, &term, 1) != 1
416 || close(lock->lock_fd) < 0) {
418 - error("Couldn't write %s", lock->lock_file);
417 + error("Couldn't write %s", lock->lk->filename);
418 unlock_ref(lock);
419 return -1;
420 }
@@ -423,7 +422,7 @@ int write_ref_sha1(struct ref_lock *lock,
422 unlock_ref(lock);
423 return -1;
424 }
426 - if (rename(lock->lock_file, lock->ref_file) < 0) {
425 + if (commit_lock_file(lock->lk)) {
426 error("Couldn't set %s", lock->ref_file);
427 unlock_ref(lock);
428 return -1;
refs.h
+1 -1
@@ -3,8 +3,8 @@
3
4 struct ref_lock {
5 char *ref_file;
6 - char *lock_file;
6 char *log_file;
7 + struct lock_file *lk;
8 unsigned char old_sha1[20];
9 int lock_fd;
10 int force_write;