lockfile.h: extract new header file for the functions in lockfile.c

Move the interface declaration for the functions in lockfile.c from cache.h to a new file, lockfile.h. Add #includes where necessary (and remove some redundant includes of cache.h by files that already include builtin.h). Move the documentation of the lock_file state diagram from lockfile.c to the new header file. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC 697cc8efd944a32ca472337cd6640004c474b788
35 files changed +118 -83
builtin/add.c
+1
@@ -5,6 +5,7 @@
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 +#include "lockfile.h"
9 #include "dir.h"
10 #include "pathspec.h"
11 #include "exec_cmd.h"
builtin/apply.c
+1
@@ -7,6 +7,7 @@
7 *
8 */
9 #include "cache.h"
10 +#include "lockfile.h"
11 #include "cache-tree.h"
12 #include "quote.h"
13 #include "blob.h"
builtin/checkout-index.c
+1 -1
@@ -5,7 +5,7 @@
5 *
6 */
7 #include "builtin.h"
8 -#include "cache.h"
8 +#include "lockfile.h"
9 #include "quote.h"
10 #include "cache-tree.h"
11 #include "parse-options.h"
builtin/checkout.c
+1 -1
@@ -1,5 +1,5 @@
1 -#include "cache.h"
1 #include "builtin.h"
2 +#include "lockfile.h"
3 #include "parse-options.h"
4 #include "refs.h"
5 #include "commit.h"
builtin/clone.c
+1
@@ -9,6 +9,7 @@
9 */
10
11 #include "builtin.h"
12 +#include "lockfile.h"
13 #include "parse-options.h"
14 #include "fetch-pack.h"
15 #include "refs.h"
builtin/commit.c
+1
@@ -6,6 +6,7 @@
6 */
7
8 #include "cache.h"
9 +#include "lockfile.h"
10 #include "cache-tree.h"
11 #include "color.h"
12 #include "dir.h"
builtin/describe.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
builtin/diff.c
+1
@@ -4,6 +4,7 @@
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #include "cache.h"
7 +#include "lockfile.h"
8 #include "color.h"
9 #include "commit.h"
10 #include "blob.h"
builtin/gc.c
+1 -1
@@ -11,7 +11,7 @@
11 */
12
13 #include "builtin.h"
14 -#include "cache.h"
14 +#include "lockfile.h"
15 #include "parse-options.h"
16 #include "run-command.h"
17 #include "sigchain.h"
builtin/merge.c
+1
@@ -9,6 +9,7 @@
9 #include "cache.h"
10 #include "parse-options.h"
11 #include "builtin.h"
12 +#include "lockfile.h"
13 #include "run-command.h"
14 #include "diff.h"
15 #include "refs.h"
builtin/mv.c
+1 -1
@@ -3,8 +3,8 @@
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 -#include "cache.h"
6 #include "builtin.h"
7 +#include "lockfile.h"
8 #include "dir.h"
9 #include "cache-tree.h"
10 #include "string-list.h"
builtin/read-tree.c
+1
@@ -5,6 +5,7 @@
5 */
6
7 #include "cache.h"
8 +#include "lockfile.h"
9 #include "object.h"
10 #include "tree.h"
11 #include "tree-walk.h"
builtin/receive-pack.c
+1
@@ -1,4 +1,5 @@
1 #include "builtin.h"
2 +#include "lockfile.h"
3 #include "pack.h"
4 #include "refs.h"
5 #include "pkt-line.h"
builtin/reflog.c
+1 -1
@@ -1,5 +1,5 @@
1 -#include "cache.h"
1 #include "builtin.h"
2 +#include "lockfile.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "dir.h"
builtin/reset.c
+1
@@ -8,6 +8,7 @@
8 * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
9 */
10 #include "builtin.h"
11 +#include "lockfile.h"
12 #include "tag.h"
13 #include "object.h"
14 #include "commit.h"
builtin/rm.c
+1 -1
@@ -3,8 +3,8 @@
3 *
4 * Copyright (C) Linus Torvalds 2006
5 */
6 -#include "cache.h"
6 #include "builtin.h"
7 +#include "lockfile.h"
8 #include "dir.h"
9 #include "cache-tree.h"
10 #include "tree-walk.h"
builtin/update-index.c
+1
@@ -4,6 +4,7 @@
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 +#include "lockfile.h"
8 #include "quote.h"
9 #include "cache-tree.h"
10 #include "tree-walk.h"
bundle.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "bundle.h"
4 #include "object.h"
5 #include "commit.h"
cache-tree.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "tree.h"
4 #include "tree-walk.h"
5 #include "cache-tree.h"
cache.h
+1 -26
@@ -570,36 +570,11 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
570 #define REFRESH_IN_PORCELAIN 0x0020 /* user friendly output, not "needs update" */
571 extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
572
573 -/* String appended to a filename to derive the lockfile name: */
574 -#define LOCK_SUFFIX ".lock"
575 -#define LOCK_SUFFIX_LEN 5
576 -
577 -struct lock_file {
578 - struct lock_file *volatile next;
579 - volatile sig_atomic_t active;
580 - volatile int fd;
581 - volatile pid_t owner;
582 - char on_list;
583 - struct strbuf filename;
584 -};
585 -#define LOCK_DIE_ON_ERROR 1
586 -#define LOCK_NO_DEREF 2
587 -extern int unable_to_lock_error(const char *path, int err);
588 -extern void unable_to_lock_message(const char *path, int err,
589 - struct strbuf *buf);
590 -extern NORETURN void unable_to_lock_die(const char *path, int err);
591 -extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
592 -extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
593 -extern char *get_locked_file_path(struct lock_file *);
594 -extern int commit_lock_file_to(struct lock_file *, const char *path);
595 -extern int commit_lock_file(struct lock_file *);
596 -extern int reopen_lock_file(struct lock_file *);
573 extern void update_index_if_able(struct index_state *, struct lock_file *);
574
575 extern int hold_locked_index(struct lock_file *, int);
576 extern void set_alternate_index_output(const char *);
601 -extern int close_lock_file(struct lock_file *);
602 -extern void rollback_lock_file(struct lock_file *);
577 +
578 extern int delete_ref(const char *, const unsigned char *sha1, int delopt);
579
580 /* Environment bits from configuration mechanism */
config.c
+1
@@ -6,6 +6,7 @@
6 *
7 */
8 #include "cache.h"
9 +#include "lockfile.h"
10 #include "exec_cmd.h"
11 #include "strbuf.h"
12 #include "quote.h"
credential-store.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "credential.h"
4 #include "string-list.h"
5 #include "parse-options.h"
fast-import.c
+1
@@ -153,6 +153,7 @@ Format of STDIN stream:
153
154 #include "builtin.h"
155 #include "cache.h"
156 +#include "lockfile.h"
157 #include "object.h"
158 #include "blob.h"
159 #include "tree.h"
fetch-pack.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "commit.h"
lockfile.c
+1 -51
@@ -2,59 +2,9 @@
2 * Copyright (c) 2005, Junio C Hamano
3 */
4 #include "cache.h"
5 +#include "lockfile.h"
6 #include "sigchain.h"
7
7 -/*
8 - * File write-locks as used by Git.
9 - *
10 - * For an overview of how to use the lockfile API, please see
11 - *
12 - * Documentation/technical/api-lockfile.txt
13 - *
14 - * This module keeps track of all locked files in lock_file_list for
15 - * use at cleanup. This list and the lock_file objects that comprise
16 - * it must be kept in self-consistent states at all time, because the
17 - * program can be interrupted any time by a signal, in which case the
18 - * signal handler will walk through the list attempting to clean up
19 - * any open lock files.
20 - *
21 - * A lockfile is owned by the process that created it. The lock_file
22 - * object has an "owner" field that records its owner. This field is
23 - * used to prevent a forked process from closing a lockfile created by
24 - * its parent.
25 - *
26 - * The possible states of a lock_file object are as follows:
27 - *
28 - * - Uninitialized. In this state the object's on_list field must be
29 - * zero but the rest of its contents need not be initialized. As
30 - * soon as the object is used in any way, it is irrevocably
31 - * registered in the lock_file_list, and on_list is set.
32 - *
33 - * - Locked, lockfile open (after hold_lock_file_for_update(),
34 - * hold_lock_file_for_append(), or reopen_lock_file()). In this
35 - * state:
36 - * - the lockfile exists
37 - * - active is set
38 - * - filename holds the filename of the lockfile
39 - * - fd holds a file descriptor open for writing to the lockfile
40 - * - owner holds the PID of the process that locked the file
41 - *
42 - * - Locked, lockfile closed (after successful close_lock_file()).
43 - * Same as the previous state, except that the lockfile is closed
44 - * and fd is -1.
45 - *
46 - * - Unlocked (after commit_lock_file(), commit_lock_file_to(),
47 - * rollback_lock_file(), a failed attempt to lock, or a failed
48 - * close_lock_file()). In this state:
49 - * - active is unset
50 - * - filename is empty (usually, though there are transitory
51 - * states in which this condition doesn't hold). Client code should
52 - * *not* rely on the filename being empty in this state.
53 - * - fd is -1
54 - * - the object is left registered in the lock_file_list, and
55 - * on_list is set.
56 - */
57 -
8 static struct lock_file *volatile lock_file_list;
9
10 static void remove_lock_files(void)
lockfile.h new
+84
@@ -0,0 +1,84 @@
1 +#ifndef LOCKFILE_H
2 +#define LOCKFILE_H
3 +
4 +/*
5 + * File write-locks as used by Git.
6 + *
7 + * For an overview of how to use the lockfile API, please see
8 + *
9 + * Documentation/technical/api-lockfile.txt
10 + *
11 + * This module keeps track of all locked files in lock_file_list for
12 + * use at cleanup. This list and the lock_file objects that comprise
13 + * it must be kept in self-consistent states at all time, because the
14 + * program can be interrupted any time by a signal, in which case the
15 + * signal handler will walk through the list attempting to clean up
16 + * any open lock files.
17 + *
18 + * A lockfile is owned by the process that created it. The lock_file
19 + * object has an "owner" field that records its owner. This field is
20 + * used to prevent a forked process from closing a lockfile created by
21 + * its parent.
22 + *
23 + * The possible states of a lock_file object are as follows:
24 + *
25 + * - Uninitialized. In this state the object's on_list field must be
26 + * zero but the rest of its contents need not be initialized. As
27 + * soon as the object is used in any way, it is irrevocably
28 + * registered in the lock_file_list, and on_list is set.
29 + *
30 + * - Locked, lockfile open (after hold_lock_file_for_update(),
31 + * hold_lock_file_for_append(), or reopen_lock_file()). In this
32 + * state:
33 + * - the lockfile exists
34 + * - active is set
35 + * - filename holds the filename of the lockfile
36 + * - fd holds a file descriptor open for writing to the lockfile
37 + * - owner holds the PID of the process that locked the file
38 + *
39 + * - Locked, lockfile closed (after successful close_lock_file()).
40 + * Same as the previous state, except that the lockfile is closed
41 + * and fd is -1.
42 + *
43 + * - Unlocked (after commit_lock_file(), commit_lock_file_to(),
44 + * rollback_lock_file(), a failed attempt to lock, or a failed
45 + * close_lock_file()). In this state:
46 + * - active is unset
47 + * - filename is empty (usually, though there are transitory
48 + * states in which this condition doesn't hold). Client code should
49 + * *not* rely on the filename being empty in this state.
50 + * - fd is -1
51 + * - the object is left registered in the lock_file_list, and
52 + * on_list is set.
53 + */
54 +
55 +struct lock_file {
56 + struct lock_file *volatile next;
57 + volatile sig_atomic_t active;
58 + volatile int fd;
59 + volatile pid_t owner;
60 + char on_list;
61 + struct strbuf filename;
62 +};
63 +
64 +/* String appended to a filename to derive the lockfile name: */
65 +#define LOCK_SUFFIX ".lock"
66 +#define LOCK_SUFFIX_LEN 5
67 +
68 +#define LOCK_DIE_ON_ERROR 1
69 +#define LOCK_NO_DEREF 2
70 +
71 +extern int unable_to_lock_error(const char *path, int err);
72 +extern void unable_to_lock_message(const char *path, int err,
73 + struct strbuf *buf);
74 +extern NORETURN void unable_to_lock_die(const char *path, int err);
75 +extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
76 +extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
77 +extern char *get_locked_file_path(struct lock_file *);
78 +extern int commit_lock_file_to(struct lock_file *, const char *path);
79 +extern int commit_lock_file(struct lock_file *);
80 +extern int reopen_lock_file(struct lock_file *);
81 +extern int close_lock_file(struct lock_file *);
82 +extern void rollback_lock_file(struct lock_file *);
83 +
84 +#endif /* LOCKFILE_H */
merge-recursive.c
+1
@@ -5,6 +5,7 @@
5 */
6 #include "advice.h"
7 #include "cache.h"
8 +#include "lockfile.h"
9 #include "cache-tree.h"
10 #include "commit.h"
11 #include "blob.h"
merge.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "commit.h"
4 #include "run-command.h"
5 #include "resolve-undo.h"
read-cache.c
+1
@@ -5,6 +5,7 @@
5 */
6 #define NO_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 +#include "lockfile.h"
9 #include "cache-tree.h"
10 #include "refs.h"
11 #include "dir.h"
refs.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "refs.h"
4 #include "object.h"
5 #include "tag.h"
rerere.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "string-list.h"
4 #include "rerere.h"
5 #include "xdiff-interface.h"
sequencer.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "sequencer.h"
4 #include "dir.h"
5 #include "object.h"
sha1_file.c
+1
@@ -8,6 +8,7 @@
8 */
9 #include "cache.h"
10 #include "string-list.h"
11 +#include "lockfile.h"
12 #include "delta.h"
13 #include "pack.h"
14 #include "blob.h"
shallow.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "pkt-line.h"
test-scrap-cache-tree.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "lockfile.h"
3 #include "tree.h"
4 #include "cache-tree.h"
5