use lockfile.c routines in git_commit_set_multivar()
Changed git_commit_set_multivar() to use the routines provided by lockfile.c to reduce code duplication and ensure consistent behavior. Signed-off-by: Bradford C. Smith <bradford.carl.smith@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Bradford C. Smith committed
Jul 26, 2007 at 12:55 UTC
6cbf973c9a6f06e9c45b3f6a76f0ceae8e7fd291
1 file changed
+18
-12
config.c
+18
-12
@@ -715,7 +715,7 @@ int git_config_set_multivar(const char* key, const char* value,
715
int fd = -1, in_fd;
716
int ret;
717
char* config_filename;
718
- char* lock_file;
718
+ struct lock_file *lock = NULL;
719
const char* last_dot = strrchr(key, '.');
720
721
config_filename = getenv(CONFIG_ENVIRONMENT);
@@ -725,7 +725,6 @@ int git_config_set_multivar(const char* key, const char* value,
725
config_filename = git_path("config");
726
}
727
config_filename = xstrdup(config_filename);
728
- lock_file = xstrdup(mkpath("%s.lock", config_filename));
728
729
/*
730
* Since "key" actually contains the section name and the real
@@ -770,11 +769,12 @@ int git_config_set_multivar(const char* key, const char* value,
769
store.key[i] = 0;
770
771
/*
773
- * The lock_file serves a purpose in addition to locking: the new
772
+ * The lock serves a purpose in addition to locking: the new
773
* contents of .git/config will be written into it.
774
*/
776
- fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
777
- if (fd < 0 || adjust_shared_perm(lock_file)) {
775
+ lock = xcalloc(sizeof(struct lock_file), 1);
776
+ fd = hold_lock_file_for_update(lock, config_filename, 0);
777
+ if (fd < 0) {
778
fprintf(stderr, "could not lock config file\n");
779
free(store.key);
780
ret = -1;
@@ -914,25 +914,31 @@ int git_config_set_multivar(const char* key, const char* value,
914
goto write_err_out;
915
916
munmap(contents, contents_sz);
917
- unlink(config_filename);
917
}
918
920
- if (rename(lock_file, config_filename) < 0) {
921
- fprintf(stderr, "Could not rename the lock file?\n");
919
+ if (close(fd) || commit_lock_file(lock) < 0) {
920
+ fprintf(stderr, "Cannot commit config file!\n");
921
ret = 4;
922
goto out_free;
923
}
924
925
+ /* fd is closed, so don't try to close it below. */
926
+ fd = -1;
927
+ /*
928
+ * lock is committed, so don't try to roll it back below.
929
+ * NOTE: Since lockfile.c keeps a linked list of all created
930
+ * lock_file structures, it isn't safe to free(lock). It's
931
+ * better to just leave it hanging around.
932
+ */
933
+ lock = NULL;
934
ret = 0;
935
936
out_free:
937
if (0 <= fd)
938
close(fd);
939
+ if (lock)
940
+ rollback_lock_file(lock);
941
free(config_filename);
932
- if (lock_file) {
933
- unlink(lock_file);
934
- free(lock_file);
935
- }
942
return ret;
943
944
write_err_out: