lock_file(): always initialize and register lock_file object
[gitweb.git] / lockfile.c
index f4ce79bc2733b44d85ddb60c65bb710536747330..81143e55ad9fde888de1875d039439b611124375 100644 (file)
@@ -129,6 +129,22 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
         */
        static const size_t max_path_len = sizeof(lk->filename) - 5;
 
+       if (!lock_file_list) {
+               /* One-time initialization */
+               sigchain_push_common(remove_lock_file_on_signal);
+               atexit(remove_lock_file);
+       }
+
+       if (!lk->on_list) {
+               /* Initialize *lk and add it to lock_file_list: */
+               lk->fd = -1;
+               lk->owner = 0;
+               lk->filename[0] = 0;
+               lk->next = lock_file_list;
+               lock_file_list = lk;
+               lk->on_list = 1;
+       }
+
        if (strlen(path) >= max_path_len) {
                errno = ENAMETOOLONG;
                return -1;
@@ -139,16 +155,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
        strcat(lk->filename, ".lock");
        lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
        if (0 <= lk->fd) {
-               if (!lock_file_list) {
-                       sigchain_push_common(remove_lock_file_on_signal);
-                       atexit(remove_lock_file);
-               }
                lk->owner = getpid();
-               if (!lk->on_list) {
-                       lk->next = lock_file_list;
-                       lock_file_list = lk;
-                       lk->on_list = 1;
-               }
                if (adjust_shared_perm(lk->filename)) {
                        int save_errno = errno;
                        error("cannot fix permission bits on %s",