* [PATCH v6 01/39] unable_to_lock_die(): rename function from unable_to_lock_index_die()
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 02/39] api-lockfile: revise and expand the documentation Michael Haggerty
` (39 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
This function is used for other things besides the index, so rename it
accordingly.
Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Ronnie Sahlberg <sahlberg@google.com>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
builtin/update-index.c | 2 +-
cache.h | 2 +-
lockfile.c | 6 +++---
refs.c | 2 +-
4 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/builtin/update-index.c b/builtin/update-index.c
index e8c7fd4..6c95988 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -942,7 +942,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
if (newfd < 0) {
if (refresh_args.flags & REFRESH_QUIET)
exit(128);
- unable_to_lock_index_die(get_index_file(), lock_error);
+ unable_to_lock_die(get_index_file(), lock_error);
}
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die("Unable to write new index file");
diff --git a/cache.h b/cache.h
index af590d5..07682cb 100644
--- a/cache.h
+++ b/cache.h
@@ -582,7 +582,7 @@ struct lock_file {
extern int unable_to_lock_error(const char *path, int err);
extern void unable_to_lock_message(const char *path, int err,
struct strbuf *buf);
-extern NORETURN void unable_to_lock_index_die(const char *path, int err);
+extern NORETURN void unable_to_lock_die(const char *path, int err);
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
extern int commit_lock_file(struct lock_file *);
diff --git a/lockfile.c b/lockfile.c
index 2a800ce..f1ce154 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -185,7 +185,7 @@ int unable_to_lock_error(const char *path, int err)
return -1;
}
-NORETURN void unable_to_lock_index_die(const char *path, int err)
+NORETURN void unable_to_lock_die(const char *path, int err)
{
struct strbuf buf = STRBUF_INIT;
@@ -198,7 +198,7 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
{
int fd = lock_file(lk, path, flags);
if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
- unable_to_lock_index_die(path, errno);
+ unable_to_lock_die(path, errno);
return fd;
}
@@ -209,7 +209,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
fd = lock_file(lk, path, flags);
if (fd < 0) {
if (flags & LOCK_DIE_ON_ERROR)
- unable_to_lock_index_die(path, errno);
+ unable_to_lock_die(path, errno);
return fd;
}
diff --git a/refs.c b/refs.c
index 2ce5d69..8cb3539 100644
--- a/refs.c
+++ b/refs.c
@@ -2167,7 +2167,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
*/
goto retry;
else
- unable_to_lock_index_die(ref_file, errno);
+ unable_to_lock_die(ref_file, errno);
}
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 01/39] unable_to_lock_die(): rename function from unable_to_lock_index_die() Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 18:33 ` Junio C Hamano
2014-09-26 20:40 ` Junio C Hamano
2014-09-26 10:08 ` [PATCH v6 03/39] close_lock_file(): exit (successfully) if file is already closed Michael Haggerty
` (38 subsequent siblings)
40 siblings, 2 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Document a couple more functions and the flags argument as used by
hold_lock_file_for_update() and hold_lock_file_for_append().
Reorganize the document to make it more accessible.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 199 ++++++++++++++++++++++---------
1 file changed, 145 insertions(+), 54 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index dd89404..c14fca5 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -3,20 +3,124 @@ lockfile API
The lockfile API serves two purposes:
-* Mutual exclusion. When we write out a new index file, first
- we create a new file `$GIT_DIR/index.lock`, write the new
- contents into it, and rename it to the final destination
- `$GIT_DIR/index`. We try to create the `$GIT_DIR/index.lock`
- file with O_EXCL so that we can notice and fail when somebody
- else is already trying to update the index file.
-
-* Automatic cruft removal. After we create the "lock" file, we
- may decide to `die()`, and we would want to make sure that we
- remove the file that has not been committed to its final
- destination. This is done by remembering the lockfiles we
- created in a linked list and cleaning them up from an
- `atexit(3)` handler. Outstanding lockfiles are also removed
- when the program dies on a signal.
+* Mutual exclusion and atomic file updates. When we want to change a
+ file, we create a lockfile `<filename>.lock`, write the new file
+ contents into it, and then rename the lockfile to its final
+ destination `<filename>`. We create the `<filename>.lock` file with
+ `O_CREAT|O_EXCL` so that we can notice and fail if somebody else has
+ already locked the file.
+
+* Automatic cruft removal. If the program exits after we lock a file
+ but before the changes have been committed, we want to make sure
+ that we remove the lockfile. This is done by remembering the
+ lockfiles we have created in a linked list and setting up an
+ `atexit(3)` handler and a signal handler that clean up the
+ lockfiles. This mechanism ensures that outstanding lockfiles are
+ cleaned up if the program exits (including when `die()` is called)
+ or if the program dies on a signal.
+
+Please note that lockfiles only block other writers. Readers do not
+block, but they are guaranteed to see either the old contents of the
+file or the new contents of the file (assuming that the filesystem
+implements `rename(2)` atomically).
+
+
+Calling sequence
+----------------
+
+The caller:
+
+* Allocates a variable `struct lock_file lock` in the bss section or
+ heap, initialized to zeros. It cannot be an auto variable allocated
+ on the stack. Because the `lock_file` structure is used in an
+ `atexit(3)` handler, its storage has to stay throughout the life of
+ the program, even after the file changes have been committed or
+ rolled back.
+
+* Attempts to create a lockfile by passing that variable and the path
+ of the final destination (e.g. `$GIT_DIR/index`) to
+ `hold_lock_file_for_update` or `hold_lock_file_for_append`.
+
+* Writes new content for the destination file by writing to the file
+ descriptor returned by those functions (also available via
+ `lock->fd`).
+
+When finished writing, the caller can:
+
+* Close the file descriptor and rename the lockfile to its final
+ destination by calling `commit_lock_file`.
+
+* Close the file descriptor and remove the lockfile by calling
+ `rollback_lock_file`.
+
+* Close the file descriptor without removing or renaming the lockfile
+ by calling `close_lock_file`, and later call `commit_lock_file` or
+ `rollback_lock_file`.
+
+At this point, the `lock_file` object must not be freed or altered,
+because it is still registered in the `lock_file_list`. However, it
+may be reused; just pass it to another call of
+`hold_lock_file_for_update` or `hold_lock_file_for_append`.
+
+If the program exits before you have called one of `commit_lock_file`,
+`rollback_lock_file`, or `close_lock_file`, an `atexit(3)` handler
+will close and remove the lockfile, essentially rolling back any
+uncommitted changes.
+
+If you need to close the file descriptor you obtained from a
+`hold_lock_file_*` function yourself, do so by calling
+`close_lock_file`. You should never call `close(2)` yourself!
+Otherwise the `struct lock_file` structure would still think that the
+file descriptor needs to be closed, and a later call to
+`commit_lock_file` or `rollback_lock_file` or program exit would
+result in duplicate calls to `close(2)`. Worse yet, if you `close(2)`
+and then later open another file descriptor for a completely different
+purpose, then a call to `commit_lock_file` or `rollback_lock_file`
+might close that unrelated file descriptor.
+
+
+Error handling
+--------------
+
+The `hold_lock_file_*` functions return a file descriptor on success
+or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see below). On
+errors, `errno` describes the reason for failure. Errors can be
+handled by passing `errno` to one of the following helper functions:
+
+unable_to_lock_message::
+
+ Append an appropriate error message to a `strbuf`.
+
+unable_to_lock_error::
+
+ Emit an appropriate error message using `error()`.
+
+unable_to_lock_die::
+
+ Emit an appropriate error message and `die()`.
+
+
+Flags
+-----
+
+The following flags can be passed to `hold_lock_file_for_update` or
+`hold_lock_file_for_append`:
+
+LOCK_NODEREF::
+
+ Usually symbolic links in the destination path are resolved
+ and the lockfile is created by adding ".lock" to the resolved
+ path. If `LOCK_NODEREF` is set, then the lockfile is created
+ by adding ".lock" to the path argument itself. This option is
+ used, for example, when locking a symbolic reference, which
+ for backwards-compatibility reasons can be a symbolic link
+ containing the name of the referred-to-reference.
+
+LOCK_DIE_ON_ERROR::
+
+ If a lock is already taken for the file, `die()` with an error
+ message. If this option is not specified, trying to lock a
+ file that is already locked returns -1 to the caller.
The functions
@@ -24,51 +128,38 @@ The functions
hold_lock_file_for_update::
- Take a pointer to `struct lock_file`, the filename of
- the final destination (e.g. `$GIT_DIR/index`) and a flag
- `die_on_error`. Attempt to create a lockfile for the
- destination and return the file descriptor for writing
- to the file. If `die_on_error` flag is true, it dies if
- a lock is already taken for the file; otherwise it
- returns a negative integer to the caller on failure.
+ Take a pointer to `struct lock_file`, the path of the file to
+ be locked (e.g. `$GIT_DIR/index`) and a flags argument (see
+ above). Attempt to create a lockfile for the destination and
+ return the file descriptor for writing to the file.
+
+hold_lock_file_for_append::
+
+ Like `hold_lock_file_for_update`, but before returning copy
+ the existing contents of the file (if any) to the lockfile and
+ position its write pointer at the end of the file.
commit_lock_file::
- Take a pointer to the `struct lock_file` initialized
- with an earlier call to `hold_lock_file_for_update()`,
- close the file descriptor and rename the lockfile to its
- final destination. Returns 0 upon success, a negative
- value on failure to close(2) or rename(2).
+ Take a pointer to the `struct lock_file` initialized with an
+ earlier call to `hold_lock_file_for_update` or
+ `hold_lock_file_for_append`, close the file descriptor and
+ rename the lockfile to its final destination. Return 0 upon
+ success or a negative value on failure to `close(2)` or
+ `rename(2)`.
rollback_lock_file::
- Take a pointer to the `struct lock_file` initialized
- with an earlier call to `hold_lock_file_for_update()`,
- close the file descriptor and remove the lockfile.
+ Take a pointer to the `struct lock_file` initialized with an
+ earlier call to `hold_lock_file_for_update` or
+ `hold_lock_file_for_append`, close the file descriptor and
+ remove the lockfile.
close_lock_file::
- Take a pointer to the `struct lock_file` initialized
- with an earlier call to `hold_lock_file_for_update()`,
- and close the file descriptor. Returns 0 upon success,
- a negative value on failure to close(2).
-
-Because the structure is used in an `atexit(3)` handler, its
-storage has to stay throughout the life of the program. It
-cannot be an auto variable allocated on the stack.
-
-Call `commit_lock_file()` or `rollback_lock_file()` when you are
-done writing to the file descriptor. If you do not call either
-and simply `exit(3)` from the program, an `atexit(3)` handler
-will close and remove the lockfile.
-
-If you need to close the file descriptor you obtained from
-`hold_lock_file_for_update` function yourself, do so by calling
-`close_lock_file()`. You should never call `close(2)` yourself!
-Otherwise the `struct
-lock_file` structure still remembers that the file descriptor
-needs to be closed, and a later call to `commit_lock_file()` or
-`rollback_lock_file()` will result in duplicate calls to
-`close(2)`. Worse yet, if you `close(2)`, open another file
-descriptor for completely different purpose, and then call
-`commit_lock_file()` or `rollback_lock_file()`, they may close
-that unrelated file descriptor.
+
+ Take a pointer to the `struct lock_file` initialized with an
+ earlier call to `hold_lock_file_for_update` or
+ `hold_lock_file_for_append`, and close the file descriptor.
+ Return 0 upon success or a negative value on failure to
+ close(2). Usually `commit_lock_file` or `rollback_lock_file`
+ should be called after `close_lock_file`.
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-26 10:08 ` [PATCH v6 02/39] api-lockfile: revise and expand the documentation Michael Haggerty
@ 2014-09-26 18:33 ` Junio C Hamano
2014-09-30 10:53 ` Michael Haggerty
2014-09-26 20:40 ` Junio C Hamano
1 sibling, 1 reply; 55+ messages in thread
From: Junio C Hamano @ 2014-09-26 18:33 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> Document a couple more functions and the flags argument as used by
> hold_lock_file_for_update() and hold_lock_file_for_append().
> Reorganize the document to make it more accessible.
>
> Helped-by: Jonathan Nieder <jrnieder@gmail.com>
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
> Documentation/technical/api-lockfile.txt | 199 ++++++++++++++++++++++---------
> 1 file changed, 145 insertions(+), 54 deletions(-)
Nicely done.
> +* Mutual exclusion and atomic file updates. When we want to change a
> + file, we create a lockfile `<filename>.lock`, write the new file
> + contents into it, and then rename the lockfile to its final
> + destination `<filename>`. We create the `<filename>.lock` file with
> + `O_CREAT|O_EXCL` so that we can notice and fail if somebody else has
> + already locked the file.
You may want to say
then atomically rename the lockfile to its final destination
to commit the changes and unlock the file.
here; that way, your mention of "commit" in the next paragraph would
become easier to understand.
> +* Automatic cruft removal. If the program exits after we lock a file
> + but before the changes have been committed, we want to make sure
> + that we remove the lockfile.
> +Calling sequence
> +----------------
> +
> +The caller:
> +
> +* Allocates a variable `struct lock_file lock` in the bss section or
> + heap, initialized to zeros. It cannot be an auto variable allocated
> + on the stack. Because the `lock_file` structure is used in an
> + `atexit(3)` handler, its storage has to stay throughout the life of
> + the program, even after the file changes have been committed or
> + rolled back.
It is easier to read if you pushed the second sentence (which is a
rephrase of the first one) and third sentence (which explains why
the second sentence is true) out of line as a side-note, I think, or
probably remove them from here (see below).
> +* Attempts to create a lockfile by passing that variable and the path
> + of the final destination (e.g. `$GIT_DIR/index`) to
> + `hold_lock_file_for_update` or `hold_lock_file_for_append`.
> +
> +* Writes new content for the destination file by writing to the file
> + descriptor returned by those functions (also available via
> + `lock->fd`).
> +
> +When finished writing, the caller can:
> +
> +* Close the file descriptor and rename the lockfile to its final
> + destination by calling `commit_lock_file`.
> +
> +* Close the file descriptor and remove the lockfile by calling
> + `rollback_lock_file`.
> +
> +* Close the file descriptor without removing or renaming the lockfile
> + by calling `close_lock_file`, and later call `commit_lock_file` or
> + `rollback_lock_file`.
> +
> +At this point, the `lock_file` object must not be freed or altered,
> +because it is still registered in the `lock_file_list`. However, it
> +may be reused; just pass it to another call of
> +`hold_lock_file_for_update` or `hold_lock_file_for_append`.
It feels a bit conflicting between "must not be freed or ALTERED"
and "it may be REUSED". Drop "or altered" to reduce confusion? And
this repeats the third sentence I suggested to remove from the first
paragraph (above 'see below' refers here).
Is it allowed to tell the name of this lock_file to other people and
let them read from it? The answer is yes but it is not obvious from
this description.
Also mention how the above interact with reopen_lock_file() here?
> +If the program exits before you have called one of `commit_lock_file`,
> +`rollback_lock_file`, or `close_lock_file`, an `atexit(3)` handler
> +will close and remove the lockfile, essentially rolling back any
> +uncommitted changes.
s/essentially //;
> +Error handling
> +--------------
> +
> +The `hold_lock_file_*` functions return a file descriptor on success
> +or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see below). On
> +errors, `errno` describes the reason for failure. Errors can be
> +handled by passing `errno` to one of the following helper functions:
s/handled by/reported by/; probably. None of these will help you
"handle" errors in the sense to (attempt to) recover from it.
> +unable_to_lock_message::
> +
> + Append an appropriate error message to a `strbuf`.
> +
> +unable_to_lock_error::
> +
> + Emit an appropriate error message using `error()`.
> +
> +unable_to_lock_die::
> +
> + Emit an appropriate error message and `die()`.
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-26 18:33 ` Junio C Hamano
@ 2014-09-30 10:53 ` Michael Haggerty
2014-09-30 17:39 ` Junio C Hamano
0 siblings, 1 reply; 55+ messages in thread
From: Michael Haggerty @ 2014-09-30 10:53 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
On 09/26/2014 08:33 PM, Junio C Hamano wrote:
> Michael Haggerty <mhagger@alum.mit.edu> writes:
>
>> Document a couple more functions and the flags argument as used by
>> hold_lock_file_for_update() and hold_lock_file_for_append().
>> Reorganize the document to make it more accessible.
>>
>> Helped-by: Jonathan Nieder <jrnieder@gmail.com>
>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
>> ---
>> Documentation/technical/api-lockfile.txt | 199 ++++++++++++++++++++++---------
>> 1 file changed, 145 insertions(+), 54 deletions(-)
>
> Nicely done.
>
>> +* Mutual exclusion and atomic file updates. When we want to change a
>> + file, we create a lockfile `<filename>.lock`, write the new file
>> + contents into it, and then rename the lockfile to its final
>> + destination `<filename>`. We create the `<filename>.lock` file with
>> + `O_CREAT|O_EXCL` so that we can notice and fail if somebody else has
>> + already locked the file.
>
>
> You may want to say
>
> then atomically rename the lockfile to its final destination
> to commit the changes and unlock the file.
>
> here; that way, your mention of "commit" in the next paragraph would
> become easier to understand.
Good; will change.
>> [...]
>> +Calling sequence
>> +----------------
>> +
>> +The caller:
>> +
>> +* Allocates a variable `struct lock_file lock` in the bss section or
>> + heap, initialized to zeros. It cannot be an auto variable allocated
>> + on the stack. Because the `lock_file` structure is used in an
>> + `atexit(3)` handler, its storage has to stay throughout the life of
>> + the program, even after the file changes have been committed or
>> + rolled back.
>
> It is easier to read if you pushed the second sentence (which is a
> rephrase of the first one) and third sentence (which explains why
> the second sentence is true) out of line as a side-note, I think, or
> probably remove them from here (see below).
I was being repetitive because I didn't want the docs to depend on the
user remembering what the "bss" section is (which, technically, is also
not part of the C standard). I think a better way would be to just not
mention "bss section" at all and reword the rest. Maybe something like
The caller:
* Allocates a variable `struct lock_file lock`, initialized to zeros.
Because the `lock_file` structure is used in an `atexit(3)` handler,
its storage has to remain valid throughout the life of the program;
e.g., it should be a static variable or space allocated on the heap.
Better?
>> +* Attempts to create a lockfile by passing that variable and the path
>> + of the final destination (e.g. `$GIT_DIR/index`) to
>> + `hold_lock_file_for_update` or `hold_lock_file_for_append`.
>> +
>> +* Writes new content for the destination file by writing to the file
>> + descriptor returned by those functions (also available via
>> + `lock->fd`).
>> +
>> +When finished writing, the caller can:
>> +
>> +* Close the file descriptor and rename the lockfile to its final
>> + destination by calling `commit_lock_file`.
>> +
>> +* Close the file descriptor and remove the lockfile by calling
>> + `rollback_lock_file`.
>> +
>> +* Close the file descriptor without removing or renaming the lockfile
>> + by calling `close_lock_file`, and later call `commit_lock_file` or
>> + `rollback_lock_file`.
>> +
>> +At this point, the `lock_file` object must not be freed or altered,
>> +because it is still registered in the `lock_file_list`. However, it
>> +may be reused; just pass it to another call of
>> +`hold_lock_file_for_update` or `hold_lock_file_for_append`.
>
> It feels a bit conflicting between "must not be freed or ALTERED"
> and "it may be REUSED". Drop "or altered" to reduce confusion? And
> this repeats the third sentence I suggested to remove from the first
> paragraph (above 'see below' refers here).
The purpose of "or altered" is to make sure that users don't imagine
that they should memset() the structure to zeros or something before
reusing it (especially since the "caller" instructions earlier say that
the object should be "initialized to zeros").
Would it help if I change
s/altered/altered by the caller/
?
I will also drop "because it is still registered in the `lock_file_list`".
> Is it allowed to tell the name of this lock_file to other people and
> let them read from it? The answer is yes but it is not obvious from
> this description.
>
> Also mention how the above interact with reopen_lock_file() here?
I'll take a stab at it, though TBH I haven't really studied the use case
for reopen_lock_file(). You might be better able to provide insight into
this topic.
>> +If the program exits before you have called one of `commit_lock_file`,
>> +`rollback_lock_file`, or `close_lock_file`, an `atexit(3)` handler
>> +will close and remove the lockfile, essentially rolling back any
>> +uncommitted changes.
>
> s/essentially //;
Done.
>> +Error handling
>> +--------------
>> +
>> +The `hold_lock_file_*` functions return a file descriptor on success
>> +or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see below). On
>> +errors, `errno` describes the reason for failure. Errors can be
>> +handled by passing `errno` to one of the following helper functions:
>
> s/handled by/reported by/; probably. None of these will help you
> "handle" errors in the sense to (attempt to) recover from it.
Done.
Thanks for your suggestions!
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-30 10:53 ` Michael Haggerty
@ 2014-09-30 17:39 ` Junio C Hamano
2014-10-01 7:37 ` Michael Haggerty
0 siblings, 1 reply; 55+ messages in thread
From: Junio C Hamano @ 2014-09-30 17:39 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> I was being repetitive because I didn't want the docs to depend on the
> user remembering what the "bss" section is (which, technically, is also
> not part of the C standard). I think a better way would be to just not
> mention "bss section" at all and reword the rest. Maybe something like
>
> The caller:
>
> * Allocates a variable `struct lock_file lock`, initialized to zeros.
> Because the `lock_file` structure is used in an `atexit(3)` handler,
> its storage has to remain valid throughout the life of the program;
> e.g., it should be a static variable or space allocated on the heap.
>
> Better?
Somewhat. I like that you droped "BSS", though.
Allocates a 'struct lock_file' either as a static variable
or on the heap, initialized to zeros. Once you use the
structure to call hold_lock_file_* family of functions, it
belongs to the lockfile subsystem and its storage must
remain valid throughout the life of the program (i.e. you
cannot use an on-stack variable to hold this structure).
>> It feels a bit conflicting between "must not be freed or ALTERED"
>> and "it may be REUSED". Drop "or altered" to reduce confusion? And
>> this repeats the third sentence I suggested to remove from the first
>> paragraph (above 'see below' refers here).
>
> The purpose of "or altered" is to make sure that users don't imagine
> that they should memset() the structure to zeros or something before
> reusing it (especially since the "caller" instructions earlier say that
> the object should be "initialized to zeros").
>
> Would it help if I change
>
> s/altered/altered by the caller/
>
> ?
The fundamental rule is that callers outside the lockfile system must
not touch individual members of "struct lock_file" that is "live".
They must not free, they must not alter, they must not do anything
other than calling the lockfile API functions.
While it is natural that the readers would understand such a rule
must be followed for a lockfile that is in either the initialized,
locked, closed-but-not-committed state, I agree that it is not just
possible but likely that people misunderstand and think that once a
lockfile is committed or rolled back it no longer has to follow that
rule. We would want to make sure readers do not fall into such a
misunderstanding.
I dunno. Your "if you memset it to NULs, you will break the linked
list of the lock and the whole lockfile system and the element
cannot even be reused." may be the most important thing you may have
wanted to say, but it is not conveyed by that change at all, at
least to me.
A small voice in the back of my skull keeps telling me that a rule
that is hard to document and explain is a rule that does not make
sense. Is it possible to allow commit and rollback to safely remove
the structure from the atexit(3) list (aka "no longer owned by the
lockfile subsystem")?
>> Is it allowed to tell the name of this lock_file to other people and
>> let them read from it? The answer is yes but it is not obvious from
>> this description.
>>
>> Also mention how the above interact with reopen_lock_file() here?
>
> I'll take a stab at it, though TBH I haven't really studied the use case
> for reopen_lock_file(). You might be better able to provide insight into
> this topic.
You would want to be able to do this:
- hold a lock on a file (say, the index);
- update it in preparation to commit it;
- write it out and make sure the contents is on the disk platter,
in preparation to call another program and allow it (and nobody
else) to inspect the contents you wrote, while still holding the
lock yourself. In our set of API functions, close_lock_file is
what lets us do this.
- further update it, write it out and commit. We need to read it
first, open(2) it to write, write(2), and commit_lock_file().
The set of API functions you described in the document, there is no
way to say "I already have a lock on that file, just let me open(2)
for writing because I have further updates" and that is where reopen
comes in.
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-30 17:39 ` Junio C Hamano
@ 2014-10-01 7:37 ` Michael Haggerty
0 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-10-01 7:37 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
On 09/30/2014 07:39 PM, Junio C Hamano wrote:
> Michael Haggerty <mhagger@alum.mit.edu> writes:
>
>> I was being repetitive because I didn't want the docs to depend on the
>> user remembering what the "bss" section is (which, technically, is also
>> not part of the C standard). I think a better way would be to just not
>> mention "bss section" at all and reword the rest. Maybe something like
>>
>> The caller:
>>
>> * Allocates a variable `struct lock_file lock`, initialized to zeros.
>> Because the `lock_file` structure is used in an `atexit(3)` handler,
>> its storage has to remain valid throughout the life of the program;
>> e.g., it should be a static variable or space allocated on the heap.
>>
>> Better?
>
> Somewhat. I like that you droped "BSS", though.
>
> Allocates a 'struct lock_file' either as a static variable
> or on the heap, initialized to zeros. Once you use the
> structure to call hold_lock_file_* family of functions, it
> belongs to the lockfile subsystem and its storage must
> remain valid throughout the life of the program (i.e. you
> cannot use an on-stack variable to hold this structure).
OK, I'll use that.
>>> It feels a bit conflicting between "must not be freed or ALTERED"
>>> and "it may be REUSED". Drop "or altered" to reduce confusion? And
>>> this repeats the third sentence I suggested to remove from the first
>>> paragraph (above 'see below' refers here).
>>
>> The purpose of "or altered" is to make sure that users don't imagine
>> that they should memset() the structure to zeros or something before
>> reusing it (especially since the "caller" instructions earlier say that
>> the object should be "initialized to zeros").
>>
>> Would it help if I change
>>
>> s/altered/altered by the caller/
>>
>> ?
>
> The fundamental rule is that callers outside the lockfile system must
> not touch individual members of "struct lock_file" that is "live".
> They must not free, they must not alter, they must not do anything
> other than calling the lockfile API functions.
>
> While it is natural that the readers would understand such a rule
> must be followed for a lockfile that is in either the initialized,
> locked, closed-but-not-committed state, I agree that it is not just
> possible but likely that people misunderstand and think that once a
> lockfile is committed or rolled back it no longer has to follow that
> rule. We would want to make sure readers do not fall into such a
> misunderstanding.
>
> I dunno. Your "if you memset it to NULs, you will break the linked
> list of the lock and the whole lockfile system and the element
> cannot even be reused." may be the most important thing you may have
> wanted to say, but it is not conveyed by that change at all, at
> least to me.
>
> A small voice in the back of my skull keeps telling me that a rule
> that is hard to document and explain is a rule that does not make
> sense. Is it possible to allow commit and rollback to safely remove
> the structure from the atexit(3) list (aka "no longer owned by the
> lockfile subsystem")?
Certainly it is possible. One would probably make lock_file an opaque
data structure, always allocated on the heap within the lockfile
subsystem, and maybe with a free list to reduce memory allocations.
But it would be a lot of work and I don't see a commensurate payoff.
There are not *that* many callers of the lockfile API.
>>> Is it allowed to tell the name of this lock_file to other people and
>>> let them read from it? The answer is yes but it is not obvious from
>>> this description.
>>>
>>> Also mention how the above interact with reopen_lock_file() here?
>>
>> I'll take a stab at it, though TBH I haven't really studied the use case
>> for reopen_lock_file(). You might be better able to provide insight into
>> this topic.
>
> You would want to be able to do this:
>
> - hold a lock on a file (say, the index);
>
> - update it in preparation to commit it;
>
> - write it out and make sure the contents is on the disk platter,
> in preparation to call another program and allow it (and nobody
> else) to inspect the contents you wrote, while still holding the
> lock yourself. In our set of API functions, close_lock_file is
> what lets us do this.
>
> - further update it, write it out and commit. We need to read it
> first, open(2) it to write, write(2), and commit_lock_file().
>
> The set of API functions you described in the document, there is no
> way to say "I already have a lock on that file, just let me open(2)
> for writing because I have further updates" and that is where reopen
> comes in.
Thanks for the clarification. My upcoming reroll will document
reopen_lock_file().
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-26 10:08 ` [PATCH v6 02/39] api-lockfile: revise and expand the documentation Michael Haggerty
2014-09-26 18:33 ` Junio C Hamano
@ 2014-09-26 20:40 ` Junio C Hamano
2014-09-30 13:41 ` Michael Haggerty
1 sibling, 1 reply; 55+ messages in thread
From: Junio C Hamano @ 2014-09-26 20:40 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> +If you need to close the file descriptor you obtained from a
> +`hold_lock_file_*` function yourself, do so by calling
> +`close_lock_file`. You should never call `close(2)` yourself!
This is sometimes untenable, isn't it? A caller may want to
freopen(3) a stream on the fd to write into it and then fclose(3);
which would not know to call close_lock_file() but calls close(2).
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-26 20:40 ` Junio C Hamano
@ 2014-09-30 13:41 ` Michael Haggerty
2014-09-30 16:15 ` Jeff King
0 siblings, 1 reply; 55+ messages in thread
From: Michael Haggerty @ 2014-09-30 13:41 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
On 09/26/2014 10:40 PM, Junio C Hamano wrote:
> Michael Haggerty <mhagger@alum.mit.edu> writes:
>
>> +If you need to close the file descriptor you obtained from a
>> +`hold_lock_file_*` function yourself, do so by calling
>> +`close_lock_file`. You should never call `close(2)` yourself!
>
> This is sometimes untenable, isn't it? A caller may want to
> freopen(3) a stream on the fd to write into it and then fclose(3);
> which would not know to call close_lock_file() but calls close(2).
You are absolutely correct. I carried this text over from the old
version, where it was just as inaccurate.
I didn't fix it because IMO the correct fix is to add a stdio-oriented
entry point to the lockfile API, and teach the lockfile code to handle
closing the FILE correctly when necessary.
But I didn't want to add even more changes to this patch series, so I am
working on this as a separate patch series. I hope to submit it soon.
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-30 13:41 ` Michael Haggerty
@ 2014-09-30 16:15 ` Jeff King
2014-09-30 17:47 ` Junio C Hamano
0 siblings, 1 reply; 55+ messages in thread
From: Jeff King @ 2014-09-30 16:15 UTC (permalink / raw)
To: Michael Haggerty
Cc: Junio C Hamano, Johannes Sixt, Torsten Bögershausen,
Ronnie Sahlberg, Jonathan Nieder, git
On Tue, Sep 30, 2014 at 03:41:55PM +0200, Michael Haggerty wrote:
> I didn't fix it because IMO the correct fix is to add a stdio-oriented
> entry point to the lockfile API, and teach the lockfile code to handle
> closing the FILE correctly when necessary.
I think so, too, after our discussion[1] surrounding 9540ce5 (refs: write
packed_refs file using stdio, 2014-09-10).
> But I didn't want to add even more changes to this patch series, so I am
> working on this as a separate patch series. I hope to submit it soon.
Yay.
-Peff
[1] http://thread.gmane.org/gmane.comp.version-control.git/256729/focus=256734
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-30 16:15 ` Jeff King
@ 2014-09-30 17:47 ` Junio C Hamano
2014-10-01 8:11 ` Michael Haggerty
0 siblings, 1 reply; 55+ messages in thread
From: Junio C Hamano @ 2014-09-30 17:47 UTC (permalink / raw)
To: Jeff King
Cc: Michael Haggerty, Johannes Sixt, Torsten Bögershausen,
Ronnie Sahlberg, Jonathan Nieder, git
Jeff King <peff@peff.net> writes:
> On Tue, Sep 30, 2014 at 03:41:55PM +0200, Michael Haggerty wrote:
>
>> I didn't fix it because IMO the correct fix is to add a stdio-oriented
>> entry point to the lockfile API, and teach the lockfile code to handle
>> closing the FILE correctly when necessary.
>
> I think so, too, after our discussion[1] surrounding 9540ce5 (refs: write
> packed_refs file using stdio, 2014-09-10).
Yeah, but we already write packed-refs via stdio, so the stdio
oriented lockfile API entry points can no longer be just on the
mythical todo list but needs to become reality before we can merge
this topic sanely.
>> But I didn't want to add even more changes to this patch series, so I am
>> working on this as a separate patch series. I hope to submit it soon.
>
> Yay.
Yay.
Thanks.
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 02/39] api-lockfile: revise and expand the documentation
2014-09-30 17:47 ` Junio C Hamano
@ 2014-10-01 8:11 ` Michael Haggerty
0 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-10-01 8:11 UTC (permalink / raw)
To: Junio C Hamano, Jeff King
Cc: Johannes Sixt, Torsten Bögershausen, Ronnie Sahlberg,
Jonathan Nieder, git
On 09/30/2014 07:47 PM, Junio C Hamano wrote:
> Jeff King <peff@peff.net> writes:
>
>> On Tue, Sep 30, 2014 at 03:41:55PM +0200, Michael Haggerty wrote:
>>
>>> I didn't fix it because IMO the correct fix is to add a stdio-oriented
>>> entry point to the lockfile API, and teach the lockfile code to handle
>>> closing the FILE correctly when necessary.
>>
>> I think so, too, after our discussion[1] surrounding 9540ce5 (refs: write
>> packed_refs file using stdio, 2014-09-10).
>
> Yeah, but we already write packed-refs via stdio, so the stdio
> oriented lockfile API entry points can no longer be just on the
> mythical todo list but needs to become reality before we can merge
> this topic sanely.
That's not the fault of this topic, which just moves the text of the
"rule" to a different place in the file. And neither is it the fault of
Peff's change to write packed-refs via stdio. It is the fault of
60b9004 Use atomic updates to the fast-import mark file (2007-03-08)
which also fdopen()ed then fclose()d a lock_file::fd, and of
0c0478c Document lockfile API (2008-01-16)
which documented the "rule" that had already been broken.
>>> But I didn't want to add even more changes to this patch series, so I am
>>> working on this as a separate patch series. I hope to submit it soon.
>>
>> Yay.
>
> Yay.
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
^ permalink raw reply [flat|nested] 55+ messages in thread
* [PATCH v6 03/39] close_lock_file(): exit (successfully) if file is already closed
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 01/39] unable_to_lock_die(): rename function from unable_to_lock_index_die() Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 02/39] api-lockfile: revise and expand the documentation Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 04/39] rollback_lock_file(): do not clear filename redundantly Michael Haggerty
` (37 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Suggested-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 6 +++++-
read-cache.c | 2 +-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index f1ce154..d02c3bf 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -233,6 +233,10 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
int close_lock_file(struct lock_file *lk)
{
int fd = lk->fd;
+
+ if (fd < 0)
+ return 0;
+
lk->fd = -1;
return close(fd);
}
@@ -251,7 +255,7 @@ int commit_lock_file(struct lock_file *lk)
{
char result_file[PATH_MAX];
size_t i;
- if (lk->fd >= 0 && close_lock_file(lk))
+ if (close_lock_file(lk))
return -1;
strcpy(result_file, lk->filename);
i = strlen(result_file) - 5; /* .lock */
diff --git a/read-cache.c b/read-cache.c
index 2fc1182..5ffb1d7 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2042,7 +2042,7 @@ void set_alternate_index_output(const char *name)
static int commit_locked_index(struct lock_file *lk)
{
if (alternate_index_output) {
- if (lk->fd >= 0 && close_lock_file(lk))
+ if (close_lock_file(lk))
return -1;
if (rename(lk->filename, alternate_index_output))
return -1;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 04/39] rollback_lock_file(): do not clear filename redundantly
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (2 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 03/39] close_lock_file(): exit (successfully) if file is already closed Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 05/39] rollback_lock_file(): exit early if lock is not active Michael Haggerty
` (36 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
It is only necessary to clear the lock_file's filename field if it was
not already clear.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Ronnie Sahlberg <sahlberg@google.com>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lockfile.c b/lockfile.c
index d02c3bf..5330d6a 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -280,6 +280,6 @@ void rollback_lock_file(struct lock_file *lk)
if (lk->fd >= 0)
close(lk->fd);
unlink_or_warn(lk->filename);
+ lk->filename[0] = 0;
}
- lk->filename[0] = 0;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 05/39] rollback_lock_file(): exit early if lock is not active
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (3 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 04/39] rollback_lock_file(): do not clear filename redundantly Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 06/39] rollback_lock_file(): set fd to -1 Michael Haggerty
` (35 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Eliminate a layer of nesting.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Ronnie Sahlberg <sahlberg@google.com>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 5330d6a..e55149a 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -276,10 +276,11 @@ int hold_locked_index(struct lock_file *lk, int die_on_error)
void rollback_lock_file(struct lock_file *lk)
{
- if (lk->filename[0]) {
- if (lk->fd >= 0)
- close(lk->fd);
- unlink_or_warn(lk->filename);
- lk->filename[0] = 0;
- }
+ if (!lk->filename[0])
+ return;
+
+ if (lk->fd >= 0)
+ close(lk->fd);
+ unlink_or_warn(lk->filename);
+ lk->filename[0] = 0;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 06/39] rollback_lock_file(): set fd to -1
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (4 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 05/39] rollback_lock_file(): exit early if lock is not active Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 07/39] lockfile: unlock file if lockfile permissions cannot be adjusted Michael Haggerty
` (34 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
When rolling back the lockfile, call close_lock_file() so that the
lock_file's fd field gets set back to -1. This keeps the lock_file
object in a valid state, which is important because these objects are
allowed to be reused. It also makes it unnecessary to check whether
the file has already been closed, because close_lock_file() takes care
of that.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index e55149a..3df1e83 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -279,8 +279,7 @@ void rollback_lock_file(struct lock_file *lk)
if (!lk->filename[0])
return;
- if (lk->fd >= 0)
- close(lk->fd);
+ close_lock_file(lk);
unlink_or_warn(lk->filename);
lk->filename[0] = 0;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 07/39] lockfile: unlock file if lockfile permissions cannot be adjusted
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (5 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 06/39] rollback_lock_file(): set fd to -1 Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 08/39] hold_lock_file_for_append(): release lock on errors Michael Haggerty
` (33 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
If the call to adjust_shared_perm() fails, lock_file returns -1, which
to the caller looks like any other failure to lock the file. So in
this case, roll back the lockfile before returning so that the lock
file is deleted immediately and the lockfile object is left in a
predictable state (namely, unlocked). Previously, the lockfile was
retained until process cleanup in this situation.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/lockfile.c b/lockfile.c
index 3df1e83..d74de8d 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -153,6 +153,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
int save_errno = errno;
error("cannot fix permission bits on %s",
lk->filename);
+ rollback_lock_file(lk);
errno = save_errno;
return -1;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 08/39] hold_lock_file_for_append(): release lock on errors
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (6 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 07/39] lockfile: unlock file if lockfile permissions cannot be adjusted Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 09/39] lock_file(): always initialize and register lock_file object Michael Haggerty
` (32 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
If there is an error copying the old contents to the lockfile, roll
back the lockfile before exiting so that the lockfile is not held
until process cleanup.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index d74de8d..f4ce79b 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -219,13 +219,13 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
if (errno != ENOENT) {
if (flags & LOCK_DIE_ON_ERROR)
die("cannot open '%s' for copying", path);
- close(fd);
+ rollback_lock_file(lk);
return error("cannot open '%s' for copying", path);
}
} else if (copy_fd(orig_fd, fd)) {
if (flags & LOCK_DIE_ON_ERROR)
exit(128);
- close(fd);
+ rollback_lock_file(lk);
return -1;
}
return fd;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 09/39] lock_file(): always initialize and register lock_file object
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (7 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 08/39] hold_lock_file_for_append(): release lock on errors Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 10/39] lockfile.c: document the various states of lock_file objects Michael Haggerty
` (31 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
The purpose of this patch is to make the state diagram for lock_file
objects simpler and deterministic.
If locking fails, lock_file() sometimes leaves the lock_file object
partly initialized, but sometimes not. It sometimes registers the
object in lock_file_list, but sometimes not. This makes the state
diagram for lock_file objects effectively indeterministic and hard to
reason about. A future patch will also change the filename field into
a strbuf, which needs more involved initialization, so it will become
even more important that the state of a lock_file object is
well-defined after a failed attempt to lock.
The ambiguity doesn't currently have any ill effects, because
lock_file objects cannot be removed from the lock_file_list anyway.
But to make it easier to document and reason about the code, make this
behavior inconsistent: *always* initialize the lock_file object and
*always* register it in lock_file_list the first time it is used,
regardless of whether an error occurs.
While we're at it, make sure that all of the lock_file fields are
initialized to values appropriate for an unlocked object; the caller
is only responsible for making sure that on_list is set to zero before
the first time it is used.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index f4ce79b..81143e5 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -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",
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 10/39] lockfile.c: document the various states of lock_file objects
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (8 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 09/39] lock_file(): always initialize and register lock_file object Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 11/39] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN Michael Haggerty
` (30 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Document the valid states of lock_file objects, how they get into each
state, and how the state is encoded in the object's fields.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Ronnie Sahlberg <sahlberg@google.com>
---
lockfile.c | 42 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/lockfile.c b/lockfile.c
index 81143e5..2680dc9 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -4,6 +4,48 @@
#include "cache.h"
#include "sigchain.h"
+/*
+ * File write-locks as used by Git.
+ *
+ * For an overview of how to use the lockfile API, please see
+ *
+ * Documentation/technical/api-lockfile.txt
+ *
+ * This module keeps track of all locked files in lock_file_list for
+ * use at cleanup. This list and the lock_file objects that comprise
+ * it must be kept in self-consistent states at all time, because the
+ * program can be interrupted any time by a signal, in which case the
+ * signal handler will walk through the list attempting to clean up
+ * any open lock files.
+ *
+ * A lockfile is owned by the process that created it. The lock_file
+ * object has an "owner" field that records its owner. This field is
+ * used to prevent a forked process from closing a lockfile created by
+ * its parent.
+ *
+ * A lock_file object can be in several states:
+ *
+ * - Uninitialized. In this state the object's on_list field must be
+ * zero but the rest of its contents need not be initialized. As
+ * soon as the object is used in any way, it is irrevocably
+ * registered in the lock_file_list, and on_list is set.
+ *
+ * - Locked, lockfile open (after hold_lock_file_for_update(),
+ * hold_lock_file_for_append(), or reopen_lock_file()). In this
+ * state, the lockfile exists, filename holds the filename of the
+ * lockfile, fd holds a file descriptor open for writing to the
+ * lockfile, and owner holds the PID of the process that locked the
+ * file.
+ *
+ * - Locked, lockfile closed (after close_lock_file()). Same as the
+ * previous state, except that the lockfile is closed and fd is -1.
+ *
+ * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a
+ * failed attempt to lock). In this state, filename[0] == '\0' and
+ * fd is -1. The object is left registered in the lock_file_list,
+ * and on_list is set.
+ */
+
static struct lock_file *lock_file_list;
static void remove_lock_file(void)
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 11/39] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (9 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 10/39] lockfile.c: document the various states of lock_file objects Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 12/39] delete_ref_loose(): don't muck around in the lock_file's filename Michael Haggerty
` (29 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
There are a few places that use these values, so define constants for
them.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
cache.h | 4 ++++
lockfile.c | 11 ++++++-----
refs.c | 7 ++++---
3 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/cache.h b/cache.h
index 07682cb..9f09a89 100644
--- a/cache.h
+++ b/cache.h
@@ -570,6 +570,10 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
#define REFRESH_IN_PORCELAIN 0x0020 /* user friendly output, not "needs update" */
extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
+/* String appended to a filename to derive the lockfile name: */
+#define LOCK_SUFFIX ".lock"
+#define LOCK_SUFFIX_LEN 5
+
struct lock_file {
struct lock_file *next;
int fd;
diff --git a/lockfile.c b/lockfile.c
index 2680dc9..23847fc 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -166,10 +166,11 @@ static char *resolve_symlink(char *p, size_t s)
static int lock_file(struct lock_file *lk, const char *path, int flags)
{
/*
- * subtract 5 from size to make sure there's room for adding
- * ".lock" for the lock file name
+ * subtract LOCK_SUFFIX_LEN from size to make sure there's
+ * room for adding ".lock" for the lock file name:
*/
- static const size_t max_path_len = sizeof(lk->filename) - 5;
+ static const size_t max_path_len = sizeof(lk->filename) -
+ LOCK_SUFFIX_LEN;
if (!lock_file_list) {
/* One-time initialization */
@@ -194,7 +195,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
strcpy(lk->filename, path);
if (!(flags & LOCK_NODEREF))
resolve_symlink(lk->filename, max_path_len);
- strcat(lk->filename, ".lock");
+ strcat(lk->filename, LOCK_SUFFIX);
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
if (0 <= lk->fd) {
lk->owner = getpid();
@@ -308,7 +309,7 @@ int commit_lock_file(struct lock_file *lk)
if (close_lock_file(lk))
return -1;
strcpy(result_file, lk->filename);
- i = strlen(result_file) - 5; /* .lock */
+ i = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */
result_file[i] = 0;
if (rename(lk->filename, result_file))
return -1;
diff --git a/refs.c b/refs.c
index 8cb3539..53cd4fb 100644
--- a/refs.c
+++ b/refs.c
@@ -79,7 +79,8 @@ out:
if (refname[1] == '\0')
return -1; /* Component equals ".". */
}
- if (cp - refname >= 5 && !memcmp(cp - 5, ".lock", 5))
+ if (cp - refname >= LOCK_SUFFIX_LEN &&
+ !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
return -1; /* Refname ends with ".lock". */
return cp - refname;
}
@@ -2551,11 +2552,11 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
{
if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
/* loose */
- int err, i = strlen(lock->lk->filename) - 5; /* .lock */
+ int err, i = strlen(lock->lk->filename) - LOCK_SUFFIX_LEN;
lock->lk->filename[i] = 0;
err = unlink_or_warn(lock->lk->filename);
- lock->lk->filename[i] = '.';
+ lock->lk->filename[i] = LOCK_SUFFIX[0];
if (err && errno != ENOENT)
return 1;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 12/39] delete_ref_loose(): don't muck around in the lock_file's filename
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (10 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 11/39] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 13/39] prepare_index(): declare return value to be (const char *) Michael Haggerty
` (28 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
It's bad manners. Especially since there could be a signal during the
call to unlink_or_warn(), in which case the signal handler will see
the wrong filename and delete the reference file, leaving the lockfile
behind.
So make our own copy to work with.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
refs.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/refs.c b/refs.c
index 53cd4fb..11c76ec 100644
--- a/refs.c
+++ b/refs.c
@@ -2551,12 +2551,15 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
static int delete_ref_loose(struct ref_lock *lock, int flag)
{
if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
- /* loose */
- int err, i = strlen(lock->lk->filename) - LOCK_SUFFIX_LEN;
-
- lock->lk->filename[i] = 0;
- err = unlink_or_warn(lock->lk->filename);
- lock->lk->filename[i] = LOCK_SUFFIX[0];
+ /*
+ * loose. The loose file name is the same as the
+ * lockfile name, minus ".lock":
+ */
+ char *loose_filename = xmemdupz(
+ lock->lk->filename,
+ strlen(lock->lk->filename) - LOCK_SUFFIX_LEN);
+ int err = unlink_or_warn(loose_filename);
+ free(loose_filename);
if (err && errno != ENOENT)
return 1;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 13/39] prepare_index(): declare return value to be (const char *)
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (11 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 12/39] delete_ref_loose(): don't muck around in the lock_file's filename Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 14/39] write_packed_entry_fn(): convert cb_data into a (const int *) Michael Haggerty
` (27 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Declare the return value to be const to make it clear that we aren't
giving callers permission to write over the string that it points at.
(The return value is the filename field of a struct lock_file, which
can be used by a signal handler at any time and therefore shouldn't be
tampered with.)
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
builtin/commit.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index b0fe784..70f5935 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -315,8 +315,8 @@ static void refresh_cache_or_die(int refresh_flags)
die_resolve_conflict("commit");
}
-static char *prepare_index(int argc, const char **argv, const char *prefix,
- const struct commit *current_head, int is_status)
+static const char *prepare_index(int argc, const char **argv, const char *prefix,
+ const struct commit *current_head, int is_status)
{
struct string_list partial;
struct pathspec pathspec;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 14/39] write_packed_entry_fn(): convert cb_data into a (const int *)
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (12 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 13/39] prepare_index(): declare return value to be (const char *) Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 15/39] lock_file(): exit early if lockfile cannot be opened Michael Haggerty
` (26 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
This makes it obvious that we have no plans to change the integer
pointed to, which is actually the fd field from a struct lock_file.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Ronnie Sahlberg <sahlberg@google.com>
---
refs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/refs.c b/refs.c
index 11c76ec..4f313bc 100644
--- a/refs.c
+++ b/refs.c
@@ -2217,7 +2217,7 @@ static void write_packed_entry(int fd, char *refname, unsigned char *sha1,
*/
static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
{
- int *fd = cb_data;
+ const int *fd = cb_data;
enum peel_status peel_status = peel_entry(entry, 0);
if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 15/39] lock_file(): exit early if lockfile cannot be opened
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (13 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 14/39] write_packed_entry_fn(): convert cb_data into a (const int *) Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 16/39] remove_lock_file(): call rollback_lock_file() Michael Haggerty
` (25 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
This is a bit easier to read than the old version, which nested part
of the non-error code in an "if" block.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Ronnie Sahlberg <sahlberg@google.com>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 23 +++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 23847fc..a8f32e5 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -197,19 +197,18 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
resolve_symlink(lk->filename, max_path_len);
strcat(lk->filename, LOCK_SUFFIX);
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
- if (0 <= lk->fd) {
- lk->owner = getpid();
- if (adjust_shared_perm(lk->filename)) {
- int save_errno = errno;
- error("cannot fix permission bits on %s",
- lk->filename);
- rollback_lock_file(lk);
- errno = save_errno;
- return -1;
- }
- }
- else
+ if (lk->fd < 0) {
lk->filename[0] = 0;
+ return -1;
+ }
+ lk->owner = getpid();
+ if (adjust_shared_perm(lk->filename)) {
+ int save_errno = errno;
+ error("cannot fix permission bits on %s", lk->filename);
+ rollback_lock_file(lk);
+ errno = save_errno;
+ return -1;
+ }
return lk->fd;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 16/39] remove_lock_file(): call rollback_lock_file()
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (14 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 15/39] lock_file(): exit early if lockfile cannot be opened Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 17/39] commit_lock_file(): inline temporary variable Michael Haggerty
` (24 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
It does just what we need.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
lockfile.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index a8f32e5..f8205f6 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -53,12 +53,8 @@ static void remove_lock_file(void)
pid_t me = getpid();
while (lock_file_list) {
- if (lock_file_list->owner == me &&
- lock_file_list->filename[0]) {
- if (lock_file_list->fd >= 0)
- close(lock_file_list->fd);
- unlink_or_warn(lock_file_list->filename);
- }
+ if (lock_file_list->owner == me)
+ rollback_lock_file(lock_file_list);
lock_file_list = lock_file_list->next;
}
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 17/39] commit_lock_file(): inline temporary variable
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (15 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 16/39] remove_lock_file(): call rollback_lock_file() Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 18/39] commit_lock_file(): die() if called for unlocked lockfile object Michael Haggerty
` (23 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index f8205f6..e148227 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -300,12 +300,14 @@ int reopen_lock_file(struct lock_file *lk)
int commit_lock_file(struct lock_file *lk)
{
char result_file[PATH_MAX];
- size_t i;
+
if (close_lock_file(lk))
return -1;
+
strcpy(result_file, lk->filename);
- i = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */
- result_file[i] = 0;
+ /* remove ".lock": */
+ result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
+
if (rename(lk->filename, result_file))
return -1;
lk->filename[0] = 0;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 18/39] commit_lock_file(): die() if called for unlocked lockfile object
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (16 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 17/39] commit_lock_file(): inline temporary variable Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 19/39] close_lock_file(): if close fails, roll back Michael Haggerty
` (22 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
It was previously a bug to call commit_lock_file() with a lock_file
object that was not active (an illegal access would happen within the
function). It was presumably never done, but this would be an easy
programming error to overlook. So before continuing, do a consistency
check that the lock_file object really is locked.
Helped-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 3 ++-
lockfile.c | 3 +++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index c14fca5..0f81e53 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -146,7 +146,8 @@ commit_lock_file::
`hold_lock_file_for_append`, close the file descriptor and
rename the lockfile to its final destination. Return 0 upon
success or a negative value on failure to `close(2)` or
- `rename(2)`.
+ `rename(2)`. It is a bug to call `commit_lock_file()` for a
+ `lock_file` object that is not currently locked.
rollback_lock_file::
diff --git a/lockfile.c b/lockfile.c
index e148227..c897dd8 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -301,6 +301,9 @@ int commit_lock_file(struct lock_file *lk)
{
char result_file[PATH_MAX];
+ if (!lk->filename[0])
+ die("BUG: attempt to commit unlocked object");
+
if (close_lock_file(lk))
return -1;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 19/39] close_lock_file(): if close fails, roll back
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (17 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 18/39] commit_lock_file(): die() if called for unlocked lockfile object Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 20/39] commit_lock_file(): rollback lock file on failure to rename Michael Haggerty
` (21 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
If closing an open lockfile fails, then we cannot be sure of the
contents of the lockfile, so there is nothing sensible to do but
delete it. This change also insures that the lock_file object is left
in a defined state in this error path (namely, unlocked).
The only caller that is ultimately affected by this change is
try_merge_strategy() -> write_locked_index(), which can call
close_lock_file() via various execution paths. This caller uses a
static lock_file object which previously could have been reused after
a failed close_lock_file() even though it was still in locked state.
This change causes the lock_file object to be unlocked on failure,
thus fixing this error-handling path.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 7 ++++---
lockfile.c | 28 ++++++++++++++++++----------
2 files changed, 22 insertions(+), 13 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index 0f81e53..7f04db2 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -161,6 +161,7 @@ close_lock_file::
Take a pointer to the `struct lock_file` initialized with an
earlier call to `hold_lock_file_for_update` or
`hold_lock_file_for_append`, and close the file descriptor.
- Return 0 upon success or a negative value on failure to
- close(2). Usually `commit_lock_file` or `rollback_lock_file`
- should be called after `close_lock_file`.
+ Return 0 upon success. On failure to `close(2)`, return a
+ negative value and rollback the lock file. Usually
+ `commit_lock_file` or `rollback_lock_file` should eventually
+ be called if `close_lock_file` succeeds.
diff --git a/lockfile.c b/lockfile.c
index c897dd8..1d18c67 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -37,13 +37,14 @@
* lockfile, and owner holds the PID of the process that locked the
* file.
*
- * - Locked, lockfile closed (after close_lock_file()). Same as the
- * previous state, except that the lockfile is closed and fd is -1.
+ * - Locked, lockfile closed (after successful close_lock_file()).
+ * Same as the previous state, except that the lockfile is closed
+ * and fd is -1.
*
- * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a
- * failed attempt to lock). In this state, filename[0] == '\0' and
- * fd is -1. The object is left registered in the lock_file_list,
- * and on_list is set.
+ * - Unlocked (after commit_lock_file(), rollback_lock_file(), a
+ * failed attempt to lock, or a failed close_lock_file()). In this
+ * state, filename[0] == '\0' and fd is -1. The object is left
+ * registered in the lock_file_list, and on_list is set.
*/
static struct lock_file *lock_file_list;
@@ -284,7 +285,13 @@ int close_lock_file(struct lock_file *lk)
return 0;
lk->fd = -1;
- return close(fd);
+ if (close(fd)) {
+ int save_errno = errno;
+ rollback_lock_file(lk);
+ errno = save_errno;
+ return -1;
+ }
+ return 0;
}
int reopen_lock_file(struct lock_file *lk)
@@ -330,7 +337,8 @@ void rollback_lock_file(struct lock_file *lk)
if (!lk->filename[0])
return;
- close_lock_file(lk);
- unlink_or_warn(lk->filename);
- lk->filename[0] = 0;
+ if (!close_lock_file(lk)) {
+ unlink_or_warn(lk->filename);
+ lk->filename[0] = 0;
+ }
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 20/39] commit_lock_file(): rollback lock file on failure to rename
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (18 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 19/39] close_lock_file(): if close fails, roll back Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 21/39] api-lockfile: document edge cases Michael Haggerty
` (20 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
If rename() fails, call rollback_lock_file() to delete the lock file
(in case it is still present) and reset the filename field to the
empty string so that the lockfile object is left in a valid state.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lockfile.c b/lockfile.c
index 1d18c67..728ce49 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -318,8 +318,13 @@ int commit_lock_file(struct lock_file *lk)
/* remove ".lock": */
result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
- if (rename(lk->filename, result_file))
+ if (rename(lk->filename, result_file)) {
+ int save_errno = errno;
+ rollback_lock_file(lk);
+ errno = save_errno;
return -1;
+ }
+
lk->filename[0] = 0;
return 0;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 21/39] api-lockfile: document edge cases
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (19 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 20/39] commit_lock_file(): rollback lock file on failure to rename Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 22/39] dump_marks(): remove a redundant call to rollback_lock_file() Michael Haggerty
` (19 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
* Document the behavior of commit_lock_file() when it fails, namely
that it rolls back the lock_file object and sets errno
appropriately.
* Document the behavior of rollback_lock_file() when called for a
lock_file object that has already been committed or rolled back,
namely that it is a NOOP.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index 7f04db2..d7882df 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -99,6 +99,10 @@ unable_to_lock_die::
Emit an appropriate error message and `die()`.
+Similarly, `commit_lock_file` and `close_lock_file` return 0 on
+success. On failure they set `errno` appropriately, do their best to
+roll back the lockfile, and return -1.
+
Flags
-----
@@ -143,18 +147,22 @@ commit_lock_file::
Take a pointer to the `struct lock_file` initialized with an
earlier call to `hold_lock_file_for_update` or
- `hold_lock_file_for_append`, close the file descriptor and
+ `hold_lock_file_for_append`, close the file descriptor, and
rename the lockfile to its final destination. Return 0 upon
- success or a negative value on failure to `close(2)` or
- `rename(2)`. It is a bug to call `commit_lock_file()` for a
- `lock_file` object that is not currently locked.
+ success. On failure, roll back the lock file and return -1,
+ with `errno` set to the value from the failing call to
+ `close(2)` or `rename(2)`. It is a bug to call
+ `commit_lock_file` for a `lock_file` object that is not
+ currently locked.
rollback_lock_file::
Take a pointer to the `struct lock_file` initialized with an
earlier call to `hold_lock_file_for_update` or
`hold_lock_file_for_append`, close the file descriptor and
- remove the lockfile.
+ remove the lockfile. It is a NOOP to call
+ `rollback_lock_file()` for a `lock_file` object that has
+ already been committed or rolled back.
close_lock_file::
@@ -162,6 +170,6 @@ close_lock_file::
earlier call to `hold_lock_file_for_update` or
`hold_lock_file_for_append`, and close the file descriptor.
Return 0 upon success. On failure to `close(2)`, return a
- negative value and rollback the lock file. Usually
+ negative value and roll back the lock file. Usually
`commit_lock_file` or `rollback_lock_file` should eventually
be called if `close_lock_file` succeeds.
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 22/39] dump_marks(): remove a redundant call to rollback_lock_file()
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (20 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 21/39] api-lockfile: document edge cases Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 23/39] git_config_set_multivar_in_file(): avoid " Michael Haggerty
` (18 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
When commit_lock_file() fails, it now always calls
rollback_lock_file() internally, so there is no need to call that
function here.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
fast-import.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/fast-import.c b/fast-import.c
index 487f1f8..61ac7e0 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1832,10 +1832,8 @@ static void dump_marks(void)
}
if (commit_lock_file(&mark_lock)) {
- int saved_errno = errno;
- rollback_lock_file(&mark_lock);
failure |= error("Unable to commit marks file %s: %s",
- export_marks_file, strerror(saved_errno));
+ export_marks_file, strerror(errno));
return;
}
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 23/39] git_config_set_multivar_in_file(): avoid call to rollback_lock_file()
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (21 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 22/39] dump_marks(): remove a redundant call to rollback_lock_file() Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 24/39] lockfile: avoid transitory invalid states Michael Haggerty
` (17 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
After commit_lock_file() is called, then the lock_file object is
necessarily either committed or rolled back. So there is no need to
call rollback_lock_file() again in either of these cases.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
config.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/config.c b/config.c
index a677eb6..123ed29 100644
--- a/config.c
+++ b/config.c
@@ -2083,6 +2083,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
if (commit_lock_file(lock) < 0) {
error("could not commit config file %s", config_filename);
ret = CONFIG_NO_WRITE;
+ lock = NULL;
goto out_free;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 24/39] lockfile: avoid transitory invalid states
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (22 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 23/39] git_config_set_multivar_in_file(): avoid " Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 25/39] struct lock_file: declare some fields volatile Michael Haggerty
` (16 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Because remove_lock_file() can be called any time by the signal
handler, it is important that any lock_file objects that are in the
lock_file_list are always in a valid state. And since lock_file
objects are often reused (but are never removed from lock_file_list),
that means we have to be careful whenever mutating a lock_file object
to always keep it in a well-defined state.
This was formerly not the case, because part of the state was encoded
by setting lk->filename to the empty string vs. a valid filename. It
is wrong to assume that this string can be updated atomically; for
example, even
strcpy(lk->filename, value)
is unsafe. But the old code was even more reckless; for example,
strcpy(lk->filename, path);
if (!(flags & LOCK_NODEREF))
resolve_symlink(lk->filename, max_path_len);
strcat(lk->filename, ".lock");
During the call to resolve_symlink(), lk->filename contained the name
of the file that was being locked, not the name of the lockfile. If a
signal were raised during that interval, then the signal handler would
have deleted the valuable file!
We could probably continue to use the filename field to encode the
state by being careful to write characters 1..N-1 of the filename
first, and then overwrite the NUL at filename[0] with the first
character of the filename, but that would be awkward and error-prone.
So, instead of using the filename field to determine whether the
lock_file object is active, add a new field "lock_file::active" for
this purpose. Be careful to set this field only when filename really
contains the name of a file that should be deleted on cleanup.
Helped-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
cache.h | 1 +
lockfile.c | 37 ++++++++++++++++++++++++++-----------
read-cache.c | 1 +
3 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/cache.h b/cache.h
index 9f09a89..81c70c7 100644
--- a/cache.h
+++ b/cache.h
@@ -576,6 +576,7 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
struct lock_file {
struct lock_file *next;
+ volatile sig_atomic_t active;
int fd;
pid_t owner;
char on_list;
diff --git a/lockfile.c b/lockfile.c
index 728ce49..d35ac44 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -23,7 +23,7 @@
* used to prevent a forked process from closing a lockfile created by
* its parent.
*
- * A lock_file object can be in several states:
+ * The possible states of a lock_file object are as follows:
*
* - Uninitialized. In this state the object's on_list field must be
* zero but the rest of its contents need not be initialized. As
@@ -32,19 +32,27 @@
*
* - Locked, lockfile open (after hold_lock_file_for_update(),
* hold_lock_file_for_append(), or reopen_lock_file()). In this
- * state, the lockfile exists, filename holds the filename of the
- * lockfile, fd holds a file descriptor open for writing to the
- * lockfile, and owner holds the PID of the process that locked the
- * file.
+ * state:
+ * - the lockfile exists
+ * - active is set
+ * - filename holds the filename of the lockfile
+ * - fd holds a file descriptor open for writing to the lockfile
+ * - owner holds the PID of the process that locked the file
*
* - Locked, lockfile closed (after successful close_lock_file()).
* Same as the previous state, except that the lockfile is closed
* and fd is -1.
*
* - Unlocked (after commit_lock_file(), rollback_lock_file(), a
- * failed attempt to lock, or a failed close_lock_file()). In this
- * state, filename[0] == '\0' and fd is -1. The object is left
- * registered in the lock_file_list, and on_list is set.
+ * failed attempt to lock, or a failed close_lock_file()). In this
+ * state:
+ * - active is unset
+ * - filename[0] == '\0' (usually, though there are transitory states
+ * in which this condition doesn't hold). Client code should *not*
+ * rely on this fact!
+ * - fd is -1
+ * - the object is left registered in the lock_file_list, and
+ * on_list is set.
*/
static struct lock_file *lock_file_list;
@@ -175,9 +183,13 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
atexit(remove_lock_file);
}
+ if (lk->active)
+ die("BUG: cannot lock_file(\"%s\") using active struct lock_file",
+ path);
if (!lk->on_list) {
/* Initialize *lk and add it to lock_file_list: */
lk->fd = -1;
+ lk->active = 0;
lk->owner = 0;
lk->filename[0] = 0;
lk->next = lock_file_list;
@@ -199,6 +211,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
return -1;
}
lk->owner = getpid();
+ lk->active = 1;
if (adjust_shared_perm(lk->filename)) {
int save_errno = errno;
error("cannot fix permission bits on %s", lk->filename);
@@ -298,7 +311,7 @@ int reopen_lock_file(struct lock_file *lk)
{
if (0 <= lk->fd)
die(_("BUG: reopen a lockfile that is still open"));
- if (!lk->filename[0])
+ if (!lk->active)
die(_("BUG: reopen a lockfile that has been committed"));
lk->fd = open(lk->filename, O_WRONLY);
return lk->fd;
@@ -308,7 +321,7 @@ int commit_lock_file(struct lock_file *lk)
{
char result_file[PATH_MAX];
- if (!lk->filename[0])
+ if (!lk->active)
die("BUG: attempt to commit unlocked object");
if (close_lock_file(lk))
@@ -325,6 +338,7 @@ int commit_lock_file(struct lock_file *lk)
return -1;
}
+ lk->active = 0;
lk->filename[0] = 0;
return 0;
}
@@ -339,11 +353,12 @@ int hold_locked_index(struct lock_file *lk, int die_on_error)
void rollback_lock_file(struct lock_file *lk)
{
- if (!lk->filename[0])
+ if (!lk->active)
return;
if (!close_lock_file(lk)) {
unlink_or_warn(lk->filename);
+ lk->active = 0;
lk->filename[0] = 0;
}
}
diff --git a/read-cache.c b/read-cache.c
index 5ffb1d7..af69f34 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2046,6 +2046,7 @@ static int commit_locked_index(struct lock_file *lk)
return -1;
if (rename(lk->filename, alternate_index_output))
return -1;
+ lk->active = 0;
lk->filename[0] = 0;
return 0;
} else {
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 25/39] struct lock_file: declare some fields volatile
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (23 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 24/39] lockfile: avoid transitory invalid states Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 26/39] try_merge_strategy(): remove redundant lock_file allocation Michael Haggerty
` (15 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
The function remove_lock_file_on_signal() is used as a signal handler.
It is not realistic to make the signal handler conform strictly to the
C standard, which is very restrictive about what a signal handler is
allowed to do. But let's increase the likelihood that it will work:
The lock_file_list global variable and several fields from struct
lock_file are used by the signal handler. Declare those values
"volatile" to (1) force the main process to write the values to RAM
promptly, and (2) prevent updates to these fields from being reordered
in a way that leaves an opportunity for a jump to the signal handler
while the object is in an inconsistent state.
We don't mark the filename field volatile because that would prevent
the use of strcpy(), and it is anyway unlikely that a compiler
re-orders a strcpy() call across other expressions. So in practice it
should be possible to get away without "volatile" in the "filename"
case.
Suggested-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
cache.h | 6 +++---
lockfile.c | 2 +-
refs.c | 5 +++--
3 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/cache.h b/cache.h
index 81c70c7..0e55bbe 100644
--- a/cache.h
+++ b/cache.h
@@ -575,10 +575,10 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
#define LOCK_SUFFIX_LEN 5
struct lock_file {
- struct lock_file *next;
+ struct lock_file *volatile next;
volatile sig_atomic_t active;
- int fd;
- pid_t owner;
+ volatile int fd;
+ volatile pid_t owner;
char on_list;
char filename[PATH_MAX];
};
diff --git a/lockfile.c b/lockfile.c
index d35ac44..89043f5 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -55,7 +55,7 @@
* on_list is set.
*/
-static struct lock_file *lock_file_list;
+static struct lock_file *volatile lock_file_list;
static void remove_lock_file(void)
{
diff --git a/refs.c b/refs.c
index 4f313bc..9971ac5 100644
--- a/refs.c
+++ b/refs.c
@@ -2259,15 +2259,16 @@ int commit_packed_refs(void)
get_packed_ref_cache(&ref_cache);
int error = 0;
int save_errno = 0;
+ int fd;
if (!packed_ref_cache->lock)
die("internal error: packed-refs not locked");
write_or_die(packed_ref_cache->lock->fd,
PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
+ fd = packed_ref_cache->lock->fd;
do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
- 0, write_packed_entry_fn,
- &packed_ref_cache->lock->fd);
+ 0, write_packed_entry_fn, &fd);
if (commit_lock_file(packed_ref_cache->lock)) {
save_errno = errno;
error = -1;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 26/39] try_merge_strategy(): remove redundant lock_file allocation
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (24 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 25/39] struct lock_file: declare some fields volatile Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object Michael Haggerty
` (14 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
By the time the "if" block is entered, the lock_file instance from the
main function block is no longer in use, so re-use that one instead of
allocating a second one.
Note that the "lock" variable in the "if" block shadowed the "lock"
variable at function scope, so the only change needed is to remove the
inner definition.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
builtin/merge.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index ec6fa93..cb8f709 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -668,7 +668,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
int clean, x;
struct commit *result;
- struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
struct commit_list *reversed = NULL;
struct merge_options o;
struct commit_list *j;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (25 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 26/39] try_merge_strategy(): remove redundant lock_file allocation Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 19:00 ` Junio C Hamano
2014-09-26 10:08 ` [PATCH v6 28/39] commit_lock_file(): use a strbuf to manage temporary space Michael Haggerty
` (13 subsequent siblings)
40 siblings, 1 reply; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Even the one lockfile object needn't be allocated each time the
function is called. Instead, define one statically-allocated
lock_file object and reuse it for every call.
Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
builtin/merge.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index cb8f709..c7fa3bc 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -656,14 +656,14 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
struct commit_list *remoteheads,
struct commit *head, const char *head_arg)
{
- struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
+ static struct lock_file lock;
- hold_locked_index(lock, 1);
+ hold_locked_index(&lock, 1);
refresh_cache(REFRESH_QUIET);
if (active_cache_changed &&
- write_locked_index(&the_index, lock, COMMIT_LOCK))
+ write_locked_index(&the_index, &lock, COMMIT_LOCK))
return error(_("Unable to write index."));
- rollback_lock_file(lock);
+ rollback_lock_file(&lock);
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
int clean, x;
@@ -695,13 +695,13 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
for (j = common; j; j = j->next)
commit_list_insert(j->item, &reversed);
- hold_locked_index(lock, 1);
+ hold_locked_index(&lock, 1);
clean = merge_recursive(&o, head,
remoteheads->item, reversed, &result);
if (active_cache_changed &&
- write_locked_index(&the_index, lock, COMMIT_LOCK))
+ write_locked_index(&the_index, &lock, COMMIT_LOCK))
die (_("unable to write %s"), get_index_file());
- rollback_lock_file(lock);
+ rollback_lock_file(&lock);
return clean ? 0 : 1;
} else {
return try_merge_command(strategy, xopts_nr, xopts,
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* Re: [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object
2014-09-26 10:08 ` [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object Michael Haggerty
@ 2014-09-26 19:00 ` Junio C Hamano
2014-09-30 14:04 ` Michael Haggerty
0 siblings, 1 reply; 55+ messages in thread
From: Junio C Hamano @ 2014-09-26 19:00 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> Even the one lockfile object needn't be allocated each time the
> function is called. Instead, define one statically-allocated
> lock_file object and reuse it for every call.
>
> Suggested-by: Jeff King <peff@peff.net>
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
> ...
> - hold_locked_index(lock, 1);
> + hold_locked_index(&lock, 1);
> refresh_cache(REFRESH_QUIET);
> if (active_cache_changed &&
> - write_locked_index(&the_index, lock, COMMIT_LOCK))
> + write_locked_index(&the_index, &lock, COMMIT_LOCK))
I wondered if the next step would be to lose the "lock" parameter
from {hold,write}_locked_index() and have them work on a
process-global lock, but that would not work well.
The reason why this patch works is because we are only working with
a single destination (i.e. $GIT_INDEX_FILE typically .git/index),
right?
Interesting.
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object
2014-09-26 19:00 ` Junio C Hamano
@ 2014-09-30 14:04 ` Michael Haggerty
2014-09-30 18:08 ` Junio C Hamano
0 siblings, 1 reply; 55+ messages in thread
From: Michael Haggerty @ 2014-09-30 14:04 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
On 09/26/2014 09:00 PM, Junio C Hamano wrote:
> Michael Haggerty <mhagger@alum.mit.edu> writes:
>
>> Even the one lockfile object needn't be allocated each time the
>> function is called. Instead, define one statically-allocated
>> lock_file object and reuse it for every call.
>>
>> Suggested-by: Jeff King <peff@peff.net>
>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
>> ---
>> ...
>> - hold_locked_index(lock, 1);
>> + hold_locked_index(&lock, 1);
>> refresh_cache(REFRESH_QUIET);
>> if (active_cache_changed &&
>> - write_locked_index(&the_index, lock, COMMIT_LOCK))
>> + write_locked_index(&the_index, &lock, COMMIT_LOCK))
>
> I wondered if the next step would be to lose the "lock" parameter
> from {hold,write}_locked_index() and have them work on a
> process-global lock, but that would not work well.
>
> The reason why this patch works is because we are only working with
> a single destination (i.e. $GIT_INDEX_FILE typically .git/index),
> right?
>
> Interesting.
Ummm, this patch wasn't supposed to be interesting. If it is then maybe
I made a mistake...
My reasoning was that after the lock is acquired, it is released
unconditionally before the function exits. Therefore, it should be no
problem to reuse it each time the function is called.
Of course, one gap in this argument is the possibility that this
function calls itself recursively. The fact that it calls
merge_recursive() should have alerted me to this possibility. So let me
check...
* try_merge_strategy() is only called by cmd_merge()
* cmd_merge() is only called by the command dispatcher
So I don't see a way for this function to call itself recursively within
a single process.
A second possible gap in this argument would be if this function can be
called from multiple threads. But hardly any of our code is thread-safe,
so I hardly think that is likely.
What am I missing?
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object
2014-09-30 14:04 ` Michael Haggerty
@ 2014-09-30 18:08 ` Junio C Hamano
0 siblings, 0 replies; 55+ messages in thread
From: Junio C Hamano @ 2014-09-30 18:08 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> On 09/26/2014 09:00 PM, Junio C Hamano wrote:
>> Michael Haggerty <mhagger@alum.mit.edu> writes:
>>
>>> Even the one lockfile object needn't be allocated each time the
>>> function is called. Instead, define one statically-allocated
>>> lock_file object and reuse it for every call.
>>>
>>> Suggested-by: Jeff King <peff@peff.net>
>>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
>>> ---
>>> ...
>>> - hold_locked_index(lock, 1);
>>> + hold_locked_index(&lock, 1);
>>> refresh_cache(REFRESH_QUIET);
>>> if (active_cache_changed &&
>>> - write_locked_index(&the_index, lock, COMMIT_LOCK))
>>> + write_locked_index(&the_index, &lock, COMMIT_LOCK))
>>
>> I wondered if the next step would be to lose the "lock" parameter
>> from {hold,write}_locked_index() and have them work on a
>> process-global lock, but that would not work well.
>>
>> The reason why this patch works is because we are only working with
>> a single destination (i.e. $GIT_INDEX_FILE typically .git/index),
>> right?
>>
>> Interesting.
>
> Ummm, this patch wasn't supposed to be interesting. If it is then maybe
> I made a mistake...
Well, Interesting is very different from Questionable.
This lock can never have multiple active instances, as you
mentioned.
We didn't have to change this for so long since the function was
written; that probably is because this sequence:
lock = hold_lock();
use(lock);
commit(lock); /* or rollback(lock) */
is so obviously natural to callers of the API, and commit/rollback
looks very much like a declaration that we are done with the object
and its resource can be freed.
This change makes sense because the API does not actually allow us
to free the resource held to use this lock, so reducing the number
of "struct lock_file" ever used during the life of the program makes
difference, especially when you use many, even when not many of them
you need to hold at the same time.
Which was counter-intuitive to me, and the realization did not hit
me until I thought about it, which made it an "Interesting" change.
It may at the same be suggesting that "once in the lockfile subsystem,
the resource becomes reclaimable" may be something we would want to
fix, though.
^ permalink raw reply [flat|nested] 55+ messages in thread
* [PATCH v6 28/39] commit_lock_file(): use a strbuf to manage temporary space
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (26 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 27/39] try_merge_strategy(): use a statically-allocated lock_file object Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 29/39] Change lock_file::filename into a strbuf Michael Haggerty
` (12 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Avoid relying on the filename length restrictions that are currently
checked by lock_file().
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 89043f5..1dd118f 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -319,7 +319,8 @@ int reopen_lock_file(struct lock_file *lk)
int commit_lock_file(struct lock_file *lk)
{
- char result_file[PATH_MAX];
+ static struct strbuf result_file = STRBUF_INIT;
+ int err;
if (!lk->active)
die("BUG: attempt to commit unlocked object");
@@ -327,11 +328,12 @@ int commit_lock_file(struct lock_file *lk)
if (close_lock_file(lk))
return -1;
- strcpy(result_file, lk->filename);
/* remove ".lock": */
- result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
-
- if (rename(lk->filename, result_file)) {
+ strbuf_add(&result_file, lk->filename,
+ strlen(lk->filename) - LOCK_SUFFIX_LEN);
+ err = rename(lk->filename, result_file.buf);
+ strbuf_reset(&result_file);
+ if (err) {
int save_errno = errno;
rollback_lock_file(lk);
errno = save_errno;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 29/39] Change lock_file::filename into a strbuf
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (27 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 28/39] commit_lock_file(): use a strbuf to manage temporary space Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 30/39] resolve_symlink(): use a strbuf for internal scratch space Michael Haggerty
` (11 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
For now, we still make sure to allocate at least PATH_MAX characters
for the strbuf because resolve_symlink() doesn't know how to expand
the space for its return value. (That will be fixed in a moment.)
Another alternative would be to just use a strbuf as scratch space in
lock_file() but then store a pointer to the naked string in struct
lock_file. But lock_file objects are often reused. By reusing the
same strbuf, we can avoid having to reallocate the string most times
when a lock_file object is reused.
Helped-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
builtin/commit.c | 12 ++++++------
builtin/reflog.c | 2 +-
cache.h | 2 +-
config.c | 14 +++++++-------
lockfile.c | 53 ++++++++++++++++++++++++-----------------------------
read-cache.c | 4 ++--
refs.c | 6 +++---
shallow.c | 6 +++---
8 files changed, 47 insertions(+), 52 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 70f5935..f55e809 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -341,7 +341,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
die(_("unable to create temporary index"));
old_index_env = getenv(INDEX_ENVIRONMENT);
- setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
+ setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
die(_("interactive add failed"));
@@ -352,7 +352,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
unsetenv(INDEX_ENVIRONMENT);
discard_cache();
- read_cache_from(index_lock.filename);
+ read_cache_from(index_lock.filename.buf);
if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
if (reopen_lock_file(&index_lock) < 0)
die(_("unable to write index file"));
@@ -362,7 +362,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
warning(_("Failed to update main cache tree"));
commit_style = COMMIT_NORMAL;
- return index_lock.filename;
+ return index_lock.filename.buf;
}
/*
@@ -385,7 +385,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
die(_("unable to write new_index file"));
commit_style = COMMIT_NORMAL;
- return index_lock.filename;
+ return index_lock.filename.buf;
}
/*
@@ -472,9 +472,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
die(_("unable to write temporary index file"));
discard_cache();
- read_cache_from(false_lock.filename);
+ read_cache_from(false_lock.filename.buf);
- return false_lock.filename;
+ return false_lock.filename.buf;
}
static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
diff --git a/builtin/reflog.c b/builtin/reflog.c
index e8a8fb1..7c78b15 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -431,7 +431,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
write_str_in_full(lock->lock_fd, "\n") != 1 ||
close_ref(lock) < 0)) {
status |= error("Couldn't write %s",
- lock->lk->filename);
+ lock->lk->filename.buf);
unlink(newlog_path);
} else if (rename(newlog_path, log_file)) {
status |= error("cannot rename %s to %s",
diff --git a/cache.h b/cache.h
index 0e55bbe..433fae5 100644
--- a/cache.h
+++ b/cache.h
@@ -580,7 +580,7 @@ struct lock_file {
volatile int fd;
volatile pid_t owner;
char on_list;
- char filename[PATH_MAX];
+ struct strbuf filename;
};
#define LOCK_DIE_ON_ERROR 1
#define LOCK_NODEREF 2
diff --git a/config.c b/config.c
index 123ed29..2110779 100644
--- a/config.c
+++ b/config.c
@@ -2024,9 +2024,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
MAP_PRIVATE, in_fd, 0);
close(in_fd);
- if (chmod(lock->filename, st.st_mode & 07777) < 0) {
+ if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
error("chmod on %s failed: %s",
- lock->filename, strerror(errno));
+ lock->filename.buf, strerror(errno));
ret = CONFIG_NO_WRITE;
goto out_free;
}
@@ -2106,7 +2106,7 @@ out_free:
return ret;
write_err_out:
- ret = write_error(lock->filename);
+ ret = write_error(lock->filename.buf);
goto out_free;
}
@@ -2207,9 +2207,9 @@ int git_config_rename_section_in_file(const char *config_filename,
fstat(fileno(config_file), &st);
- if (chmod(lock->filename, st.st_mode & 07777) < 0) {
+ if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
ret = error("chmod on %s failed: %s",
- lock->filename, strerror(errno));
+ lock->filename.buf, strerror(errno));
goto out;
}
@@ -2230,7 +2230,7 @@ int git_config_rename_section_in_file(const char *config_filename,
}
store.baselen = strlen(new_name);
if (!store_write_section(out_fd, new_name)) {
- ret = write_error(lock->filename);
+ ret = write_error(lock->filename.buf);
goto out;
}
/*
@@ -2256,7 +2256,7 @@ int git_config_rename_section_in_file(const char *config_filename,
continue;
length = strlen(output);
if (write_in_full(out_fd, output, length) != length) {
- ret = write_error(lock->filename);
+ ret = write_error(lock->filename.buf);
goto out;
}
}
diff --git a/lockfile.c b/lockfile.c
index 1dd118f..85c8648 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -47,9 +47,9 @@
* failed attempt to lock, or a failed close_lock_file()). In this
* state:
* - active is unset
- * - filename[0] == '\0' (usually, though there are transitory states
- * in which this condition doesn't hold). Client code should *not*
- * rely on this fact!
+ * - filename is empty (usually, though there are transitory
+ * states in which this condition doesn't hold). Client code should
+ * *not* rely on the filename being empty in this state.
* - fd is -1
* - the object is left registered in the lock_file_list, and
* on_list is set.
@@ -170,13 +170,6 @@ static char *resolve_symlink(char *p, size_t s)
/* Make sure errno contains a meaningful value on error */
static int lock_file(struct lock_file *lk, const char *path, int flags)
{
- /*
- * subtract LOCK_SUFFIX_LEN from size to make sure there's
- * room for adding ".lock" for the lock file name:
- */
- static const size_t max_path_len = sizeof(lk->filename) -
- LOCK_SUFFIX_LEN;
-
if (!lock_file_list) {
/* One-time initialization */
sigchain_push_common(remove_lock_file_on_signal);
@@ -191,30 +184,32 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
lk->fd = -1;
lk->active = 0;
lk->owner = 0;
- lk->filename[0] = 0;
+ strbuf_init(&lk->filename, PATH_MAX);
lk->next = lock_file_list;
lock_file_list = lk;
lk->on_list = 1;
+ } else if (lk->filename.len) {
+ /* This shouldn't happen, but better safe than sorry. */
+ die("BUG: lock_file(\"%s\") called with improperly-reset lock_file object",
+ path);
}
- if (strlen(path) >= max_path_len) {
- errno = ENAMETOOLONG;
- return -1;
+ strbuf_addstr(&lk->filename, path);
+ if (!(flags & LOCK_NODEREF)) {
+ resolve_symlink(lk->filename.buf, lk->filename.alloc);
+ strbuf_setlen(&lk->filename, strlen(lk->filename.buf));
}
- strcpy(lk->filename, path);
- if (!(flags & LOCK_NODEREF))
- resolve_symlink(lk->filename, max_path_len);
- strcat(lk->filename, LOCK_SUFFIX);
- lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
+ strbuf_addstr(&lk->filename, LOCK_SUFFIX);
+ lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
if (lk->fd < 0) {
- lk->filename[0] = 0;
+ strbuf_reset(&lk->filename);
return -1;
}
lk->owner = getpid();
lk->active = 1;
- if (adjust_shared_perm(lk->filename)) {
+ if (adjust_shared_perm(lk->filename.buf)) {
int save_errno = errno;
- error("cannot fix permission bits on %s", lk->filename);
+ error("cannot fix permission bits on %s", lk->filename.buf);
rollback_lock_file(lk);
errno = save_errno;
return -1;
@@ -313,7 +308,7 @@ int reopen_lock_file(struct lock_file *lk)
die(_("BUG: reopen a lockfile that is still open"));
if (!lk->active)
die(_("BUG: reopen a lockfile that has been committed"));
- lk->fd = open(lk->filename, O_WRONLY);
+ lk->fd = open(lk->filename.buf, O_WRONLY);
return lk->fd;
}
@@ -329,9 +324,9 @@ int commit_lock_file(struct lock_file *lk)
return -1;
/* remove ".lock": */
- strbuf_add(&result_file, lk->filename,
- strlen(lk->filename) - LOCK_SUFFIX_LEN);
- err = rename(lk->filename, result_file.buf);
+ strbuf_add(&result_file, lk->filename.buf,
+ lk->filename.len - LOCK_SUFFIX_LEN);
+ err = rename(lk->filename.buf, result_file.buf);
strbuf_reset(&result_file);
if (err) {
int save_errno = errno;
@@ -341,7 +336,7 @@ int commit_lock_file(struct lock_file *lk)
}
lk->active = 0;
- lk->filename[0] = 0;
+ strbuf_reset(&lk->filename);
return 0;
}
@@ -359,8 +354,8 @@ void rollback_lock_file(struct lock_file *lk)
return;
if (!close_lock_file(lk)) {
- unlink_or_warn(lk->filename);
+ unlink_or_warn(lk->filename.buf);
lk->active = 0;
- lk->filename[0] = 0;
+ strbuf_reset(&lk->filename);
}
}
diff --git a/read-cache.c b/read-cache.c
index af69f34..91bf876 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2044,10 +2044,10 @@ static int commit_locked_index(struct lock_file *lk)
if (alternate_index_output) {
if (close_lock_file(lk))
return -1;
- if (rename(lk->filename, alternate_index_output))
+ if (rename(lk->filename.buf, alternate_index_output))
return -1;
lk->active = 0;
- lk->filename[0] = 0;
+ strbuf_reset(&lk->filename);
return 0;
} else {
return commit_lock_file(lk);
diff --git a/refs.c b/refs.c
index 9971ac5..c6e15f9a 100644
--- a/refs.c
+++ b/refs.c
@@ -2557,8 +2557,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
* lockfile name, minus ".lock":
*/
char *loose_filename = xmemdupz(
- lock->lk->filename,
- strlen(lock->lk->filename) - LOCK_SUFFIX_LEN);
+ lock->lk->filename.buf,
+ lock->lk->filename.len - LOCK_SUFFIX_LEN);
int err = unlink_or_warn(loose_filename);
free(loose_filename);
if (err && errno != ENOENT)
@@ -2922,7 +2922,7 @@ int write_ref_sha1(struct ref_lock *lock,
write_in_full(lock->lock_fd, &term, 1) != 1 ||
close_ref(lock) < 0) {
int save_errno = errno;
- error("Couldn't write %s", lock->lk->filename);
+ error("Couldn't write %s", lock->lk->filename.buf);
unlock_ref(lock);
errno = save_errno;
return -1;
diff --git a/shallow.c b/shallow.c
index de07709..33f3c7f 100644
--- a/shallow.c
+++ b/shallow.c
@@ -269,8 +269,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
if (write_shallow_commits(&sb, 0, extra)) {
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
die_errno("failed to write to %s",
- shallow_lock->filename);
- *alternate_shallow_file = shallow_lock->filename;
+ shallow_lock->filename.buf);
+ *alternate_shallow_file = shallow_lock->filename.buf;
} else
/*
* is_repository_shallow() sees empty string as "no
@@ -316,7 +316,7 @@ void prune_shallow(int show_only)
if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
die_errno("failed to write to %s",
- shallow_lock.filename);
+ shallow_lock.filename.buf);
commit_lock_file(&shallow_lock);
} else {
unlink(git_path("shallow"));
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 30/39] resolve_symlink(): use a strbuf for internal scratch space
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (28 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 29/39] Change lock_file::filename into a strbuf Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 31/39] resolve_symlink(): take a strbuf parameter Michael Haggerty
` (10 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Aside from shortening and simplifying the code, this removes another
place where the path name length is arbitrarily limited.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 33 ++++++++++++---------------------
1 file changed, 12 insertions(+), 21 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 85c8648..cc9b9cb 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -126,44 +126,35 @@ static char *last_path_elm(char *p)
static char *resolve_symlink(char *p, size_t s)
{
int depth = MAXDEPTH;
+ static struct strbuf link = STRBUF_INIT;
while (depth--) {
- char link[PATH_MAX];
- int link_len = readlink(p, link, sizeof(link));
- if (link_len < 0) {
- /* not a symlink anymore */
- return p;
- }
- else if (link_len < sizeof(link))
- /* readlink() never null-terminates */
- link[link_len] = '\0';
- else {
- warning("%s: symlink too long", p);
- return p;
- }
+ if (strbuf_readlink(&link, p, strlen(p)) < 0)
+ break;
- if (is_absolute_path(link)) {
+ if (is_absolute_path(link.buf)) {
/* absolute path simply replaces p */
- if (link_len < s)
- strcpy(p, link);
+ if (link.len < s)
+ strcpy(p, link.buf);
else {
warning("%s: symlink too long", p);
- return p;
+ break;
}
} else {
/*
- * link is a relative path, so I must replace the
+ * link is a relative path, so replace the
* last element of p with it.
*/
char *r = (char *)last_path_elm(p);
- if (r - p + link_len < s)
- strcpy(r, link);
+ if (r - p + link.len < s)
+ strcpy(r, link.buf);
else {
warning("%s: symlink too long", p);
- return p;
+ break;
}
}
}
+ strbuf_reset(&link);
return p;
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 31/39] resolve_symlink(): take a strbuf parameter
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (29 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 30/39] resolve_symlink(): use a strbuf for internal scratch space Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 32/39] trim_last_path_component(): replace last_path_elm() Michael Haggerty
` (9 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Change resolve_symlink() to take a strbuf rather than a string as
parameter. This simplifies the code and removes an arbitrary pathname
length restriction. It also means that lock_file's filename field no
longer needs to be initialized to a large size.
Helped-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 57 ++++++++++++++++++++++-----------------------------------
1 file changed, 22 insertions(+), 35 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index cc9b9cb..5f5bcff 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -109,58 +109,47 @@ static char *last_path_elm(char *p)
#define MAXDEPTH 5
/*
- * p = path that may be a symlink
- * s = full size of p
+ * path contains a path that might be a symlink.
*
- * If p is a symlink, attempt to overwrite p with a path to the real
- * file or directory (which may or may not exist), following a chain of
- * symlinks if necessary. Otherwise, leave p unmodified.
+ * If path is a symlink, attempt to overwrite it with a path to the
+ * real file or directory (which may or may not exist), following a
+ * chain of symlinks if necessary. Otherwise, leave path unmodified.
*
- * This is a best-effort routine. If an error occurs, p will either be
- * left unmodified or will name a different symlink in a symlink chain
- * that started with p's initial contents.
- *
- * Always returns p.
+ * This is a best-effort routine. If an error occurs, path will
+ * either be left unmodified or will name a different symlink in a
+ * symlink chain that started with the original path.
*/
-
-static char *resolve_symlink(char *p, size_t s)
+static void resolve_symlink(struct strbuf *path)
{
int depth = MAXDEPTH;
static struct strbuf link = STRBUF_INIT;
while (depth--) {
- if (strbuf_readlink(&link, p, strlen(p)) < 0)
+ if (strbuf_readlink(&link, path->buf, path->len) < 0)
break;
- if (is_absolute_path(link.buf)) {
+ if (is_absolute_path(link.buf))
/* absolute path simply replaces p */
- if (link.len < s)
- strcpy(p, link.buf);
- else {
- warning("%s: symlink too long", p);
- break;
- }
- } else {
+ strbuf_reset(path);
+ else {
/*
* link is a relative path, so replace the
* last element of p with it.
*/
- char *r = (char *)last_path_elm(p);
- if (r - p + link.len < s)
- strcpy(r, link.buf);
- else {
- warning("%s: symlink too long", p);
- break;
- }
+ char *r = last_path_elm(path->buf);
+ strbuf_setlen(path, r - path->buf);
}
+
+ strbuf_addbuf(path, &link);
}
strbuf_reset(&link);
- return p;
}
/* Make sure errno contains a meaningful value on error */
static int lock_file(struct lock_file *lk, const char *path, int flags)
{
+ size_t pathlen = strlen(path);
+
if (!lock_file_list) {
/* One-time initialization */
sigchain_push_common(remove_lock_file_on_signal);
@@ -175,7 +164,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
lk->fd = -1;
lk->active = 0;
lk->owner = 0;
- strbuf_init(&lk->filename, PATH_MAX);
+ strbuf_init(&lk->filename, pathlen + LOCK_SUFFIX_LEN);
lk->next = lock_file_list;
lock_file_list = lk;
lk->on_list = 1;
@@ -185,11 +174,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
path);
}
- strbuf_addstr(&lk->filename, path);
- if (!(flags & LOCK_NODEREF)) {
- resolve_symlink(lk->filename.buf, lk->filename.alloc);
- strbuf_setlen(&lk->filename, strlen(lk->filename.buf));
- }
+ strbuf_add(&lk->filename, path, pathlen);
+ if (!(flags & LOCK_NODEREF))
+ resolve_symlink(&lk->filename);
strbuf_addstr(&lk->filename, LOCK_SUFFIX);
lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
if (lk->fd < 0) {
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 32/39] trim_last_path_component(): replace last_path_elm()
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (30 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 31/39] resolve_symlink(): take a strbuf parameter Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 33/39] Extract a function commit_lock_file_to() Michael Haggerty
` (8 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Rewrite last_path_elm() to take a strbuf parameter and to trim off the
last path name element in place rather than returning a pointer to the
beginning of the last path name element. This simplifies the function
a bit and makes it integrate better with its caller, which is now also
strbuf-based. Rename the function accordingly and a bit less tersely.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 38 ++++++++++++++++----------------------
1 file changed, 16 insertions(+), 22 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 5f5bcff..56ad7e8 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -76,32 +76,28 @@ static void remove_lock_file_on_signal(int signo)
}
/*
- * p = absolute or relative path name
+ * path = absolute or relative path name
*
- * Return a pointer into p showing the beginning of the last path name
- * element. If p is empty or the root directory ("/"), just return p.
+ * Remove the last path name element from path (leaving the preceding
+ * "/", if any). If path is empty or the root directory ("/"), set
+ * path to the empty string.
*/
-static char *last_path_elm(char *p)
+static void trim_last_path_component(struct strbuf *path)
{
- /* r starts pointing to null at the end of the string */
- char *r = strchr(p, '\0');
-
- if (r == p)
- return p; /* just return empty string */
-
- r--; /* back up to last non-null character */
+ int i = path->len;
/* back up past trailing slashes, if any */
- while (r > p && *r == '/')
- r--;
+ while (i && path->buf[i - 1] == '/')
+ i--;
/*
- * then go backwards until I hit a slash, or the beginning of
- * the string
+ * then go backwards until a slash, or the beginning of the
+ * string
*/
- while (r > p && *(r-1) != '/')
- r--;
- return r;
+ while (i && path->buf[i - 1] != '/')
+ i--;
+
+ strbuf_setlen(path, i);
}
@@ -131,14 +127,12 @@ static void resolve_symlink(struct strbuf *path)
if (is_absolute_path(link.buf))
/* absolute path simply replaces p */
strbuf_reset(path);
- else {
+ else
/*
* link is a relative path, so replace the
* last element of p with it.
*/
- char *r = last_path_elm(path->buf);
- strbuf_setlen(path, r - path->buf);
- }
+ trim_last_path_component(path);
strbuf_addbuf(path, &link);
}
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 33/39] Extract a function commit_lock_file_to()
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (31 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 32/39] trim_last_path_component(): replace last_path_elm() Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 34/39] Rename LOCK_NODEREF to LOCK_NO_DEREF Michael Haggerty
` (7 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
commit_locked_index(), when writing to an alternate index file,
duplicates (poorly) the code in commit_lock_file(). And anyway, it
shouldn't have to know so much about the internal workings of lockfile
objects. So extract a new function commit_lock_file_to() that does the
work common to the two functions, and call it from both
commit_lock_file() and commit_locked_index().
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 36 ++++++++++++++++------------
cache.h | 1 +
lockfile.c | 40 +++++++++++++++++++++-----------
read-cache.c | 13 +++--------
4 files changed, 51 insertions(+), 39 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index d7882df..0b8db64 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -48,14 +48,14 @@ The caller:
When finished writing, the caller can:
* Close the file descriptor and rename the lockfile to its final
- destination by calling `commit_lock_file`.
+ destination by calling `commit_lock_file` or `commit_lock_file_to`.
* Close the file descriptor and remove the lockfile by calling
`rollback_lock_file`.
* Close the file descriptor without removing or renaming the lockfile
- by calling `close_lock_file`, and later call `commit_lock_file` or
- `rollback_lock_file`.
+ by calling `close_lock_file`, and later call `commit_lock_file`,
+ `commit_lock_file_to`, or `rollback_lock_file`.
At this point, the `lock_file` object must not be freed or altered,
because it is still registered in the `lock_file_list`. However, it
@@ -63,20 +63,19 @@ may be reused; just pass it to another call of
`hold_lock_file_for_update` or `hold_lock_file_for_append`.
If the program exits before you have called one of `commit_lock_file`,
-`rollback_lock_file`, or `close_lock_file`, an `atexit(3)` handler
-will close and remove the lockfile, essentially rolling back any
-uncommitted changes.
+`commit_lock_file_to`, `rollback_lock_file`, or `close_lock_file`, an
+`atexit(3)` handler will close and remove the lockfile, essentially
+rolling back any uncommitted changes.
If you need to close the file descriptor you obtained from a
`hold_lock_file_*` function yourself, do so by calling
`close_lock_file`. You should never call `close(2)` yourself!
Otherwise the `struct lock_file` structure would still think that the
-file descriptor needs to be closed, and a later call to
-`commit_lock_file` or `rollback_lock_file` or program exit would
+file descriptor needs to be closed, and a commit or rollback would
result in duplicate calls to `close(2)`. Worse yet, if you `close(2)`
and then later open another file descriptor for a completely different
-purpose, then a call to `commit_lock_file` or `rollback_lock_file`
-might close that unrelated file descriptor.
+purpose, then a commit or rollback might close that unrelated file
+descriptor.
Error handling
@@ -99,9 +98,9 @@ unable_to_lock_die::
Emit an appropriate error message and `die()`.
-Similarly, `commit_lock_file` and `close_lock_file` return 0 on
-success. On failure they set `errno` appropriately, do their best to
-roll back the lockfile, and return -1.
+Similarly, `commit_lock_file`, `commit_lock_file_to`, and
+`close_lock_file` return 0 on success. On failure they set `errno`
+appropriately, do their best to roll back the lockfile, and return -1.
Flags
@@ -155,6 +154,12 @@ commit_lock_file::
`commit_lock_file` for a `lock_file` object that is not
currently locked.
+commit_lock_file_to::
+
+ Like `commit_lock_file()`, except that it takes an explicit
+ `path` argument to which the lockfile should be renamed. The
+ `path` must be on the same filesystem as the lock file.
+
rollback_lock_file::
Take a pointer to the `struct lock_file` initialized with an
@@ -171,5 +176,6 @@ close_lock_file::
`hold_lock_file_for_append`, and close the file descriptor.
Return 0 upon success. On failure to `close(2)`, return a
negative value and roll back the lock file. Usually
- `commit_lock_file` or `rollback_lock_file` should eventually
- be called if `close_lock_file` succeeds.
+ `commit_lock_file`, `commit_lock_file_to`, or
+ `rollback_lock_file` should eventually be called if
+ `close_lock_file` succeeds.
diff --git a/cache.h b/cache.h
index 433fae5..30883b3 100644
--- a/cache.h
+++ b/cache.h
@@ -590,6 +590,7 @@ extern void unable_to_lock_message(const char *path, int err,
extern NORETURN void unable_to_lock_die(const char *path, int err);
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
+extern int commit_lock_file_to(struct lock_file *, const char *path);
extern int commit_lock_file(struct lock_file *);
extern int reopen_lock_file(struct lock_file *);
extern void update_index_if_able(struct index_state *, struct lock_file *);
diff --git a/lockfile.c b/lockfile.c
index 56ad7e8..cf7f4d0 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -43,9 +43,9 @@
* Same as the previous state, except that the lockfile is closed
* and fd is -1.
*
- * - Unlocked (after commit_lock_file(), rollback_lock_file(), a
- * failed attempt to lock, or a failed close_lock_file()). In this
- * state:
+ * - Unlocked (after commit_lock_file(), commit_lock_file_to(),
+ * rollback_lock_file(), a failed attempt to lock, or a failed
+ * close_lock_file()). In this state:
* - active is unset
* - filename is empty (usually, though there are transitory
* states in which this condition doesn't hold). Client code should
@@ -284,23 +284,15 @@ int reopen_lock_file(struct lock_file *lk)
return lk->fd;
}
-int commit_lock_file(struct lock_file *lk)
+int commit_lock_file_to(struct lock_file *lk, const char *path)
{
- static struct strbuf result_file = STRBUF_INIT;
- int err;
-
if (!lk->active)
- die("BUG: attempt to commit unlocked object");
+ die("BUG: attempt to commit unlocked object to \"%s\"", path);
if (close_lock_file(lk))
return -1;
- /* remove ".lock": */
- strbuf_add(&result_file, lk->filename.buf,
- lk->filename.len - LOCK_SUFFIX_LEN);
- err = rename(lk->filename.buf, result_file.buf);
- strbuf_reset(&result_file);
- if (err) {
+ if (rename(lk->filename.buf, path)) {
int save_errno = errno;
rollback_lock_file(lk);
errno = save_errno;
@@ -312,6 +304,26 @@ int commit_lock_file(struct lock_file *lk)
return 0;
}
+int commit_lock_file(struct lock_file *lk)
+{
+ static struct strbuf result_file = STRBUF_INIT;
+ int err;
+
+ if (!lk->active)
+ die("BUG: attempt to commit unlocked object");
+
+ if (lk->filename.len <= LOCK_SUFFIX_LEN ||
+ strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
+ die("BUG: lockfile filename corrupt");
+
+ /* remove ".lock": */
+ strbuf_add(&result_file, lk->filename.buf,
+ lk->filename.len - LOCK_SUFFIX_LEN);
+ err = commit_lock_file_to(lk, result_file.buf);
+ strbuf_reset(&result_file);
+ return err;
+}
+
int hold_locked_index(struct lock_file *lk, int die_on_error)
{
return hold_lock_file_for_update(lk, get_index_file(),
diff --git a/read-cache.c b/read-cache.c
index 91bf876..e887e23 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2041,17 +2041,10 @@ void set_alternate_index_output(const char *name)
static int commit_locked_index(struct lock_file *lk)
{
- if (alternate_index_output) {
- if (close_lock_file(lk))
- return -1;
- if (rename(lk->filename.buf, alternate_index_output))
- return -1;
- lk->active = 0;
- strbuf_reset(&lk->filename);
- return 0;
- } else {
+ if (alternate_index_output)
+ return commit_lock_file_to(lk, alternate_index_output);
+ else
return commit_lock_file(lk);
- }
}
static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 34/39] Rename LOCK_NODEREF to LOCK_NO_DEREF
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (32 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 33/39] Extract a function commit_lock_file_to() Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 35/39] lockfile.c: rename static functions Michael Haggerty
` (6 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
This makes it harder to misread the name as LOCK_NODE_REF.
Suggested-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 4 ++--
cache.h | 2 +-
lockfile.c | 2 +-
refs.c | 2 +-
4 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index 0b8db64..64b250f 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -109,11 +109,11 @@ Flags
The following flags can be passed to `hold_lock_file_for_update` or
`hold_lock_file_for_append`:
-LOCK_NODEREF::
+LOCK_NO_DEREF::
Usually symbolic links in the destination path are resolved
and the lockfile is created by adding ".lock" to the resolved
- path. If `LOCK_NODEREF` is set, then the lockfile is created
+ path. If `LOCK_NO_DEREF` is set, then the lockfile is created
by adding ".lock" to the path argument itself. This option is
used, for example, when locking a symbolic reference, which
for backwards-compatibility reasons can be a symbolic link
diff --git a/cache.h b/cache.h
index 30883b3..ca64a42 100644
--- a/cache.h
+++ b/cache.h
@@ -583,7 +583,7 @@ struct lock_file {
struct strbuf filename;
};
#define LOCK_DIE_ON_ERROR 1
-#define LOCK_NODEREF 2
+#define LOCK_NO_DEREF 2
extern int unable_to_lock_error(const char *path, int err);
extern void unable_to_lock_message(const char *path, int err,
struct strbuf *buf);
diff --git a/lockfile.c b/lockfile.c
index cf7f4d0..a1cc08a 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -169,7 +169,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
}
strbuf_add(&lk->filename, path, pathlen);
- if (!(flags & LOCK_NODEREF))
+ if (!(flags & LOCK_NO_DEREF))
resolve_symlink(&lk->filename);
strbuf_addstr(&lk->filename, LOCK_SUFFIX);
lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
diff --git a/refs.c b/refs.c
index c6e15f9a..525ce4b 100644
--- a/refs.c
+++ b/refs.c
@@ -2134,7 +2134,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
lflags = 0;
if (flags & REF_NODEREF) {
refname = orig_refname;
- lflags |= LOCK_NODEREF;
+ lflags |= LOCK_NO_DEREF;
}
lock->ref_name = xstrdup(refname);
lock->orig_ref_name = xstrdup(orig_refname);
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 35/39] lockfile.c: rename static functions
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (33 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 34/39] Rename LOCK_NODEREF to LOCK_NO_DEREF Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 36/39] get_locked_file_path(): new function Michael Haggerty
` (5 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
* remove_lock_file() -> remove_lock_files()
* remove_lock_file_on_signal() -> remove_lock_files_on_signal()
Suggested-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index a1cc08a..0a8c3c8 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -57,7 +57,7 @@
static struct lock_file *volatile lock_file_list;
-static void remove_lock_file(void)
+static void remove_lock_files(void)
{
pid_t me = getpid();
@@ -68,9 +68,9 @@ static void remove_lock_file(void)
}
}
-static void remove_lock_file_on_signal(int signo)
+static void remove_lock_files_on_signal(int signo)
{
- remove_lock_file();
+ remove_lock_files();
sigchain_pop(signo);
raise(signo);
}
@@ -146,8 +146,8 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
if (!lock_file_list) {
/* One-time initialization */
- sigchain_push_common(remove_lock_file_on_signal);
- atexit(remove_lock_file);
+ sigchain_push_common(remove_lock_files_on_signal);
+ atexit(remove_lock_files);
}
if (lk->active)
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 36/39] get_locked_file_path(): new function
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (34 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 35/39] lockfile.c: rename static functions Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 37/39] hold_lock_file_for_append(): restore errno before returning Michael Haggerty
` (4 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Add a function to return the path of the file that is locked by a
lock_file object. This reduces the knowledge that callers have to have
about the lock_file layout.
Suggested-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Documentation/technical/api-lockfile.txt | 5 +++++
cache.h | 1 +
lockfile.c | 9 +++++++++
refs.c | 4 +---
4 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index 64b250f..74b67c2 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -142,6 +142,11 @@ hold_lock_file_for_append::
the existing contents of the file (if any) to the lockfile and
position its write pointer at the end of the file.
+get_locked_file_path::
+
+ Return the path of the file that is locked by the specified
+ lock_file object. The caller must free the memory.
+
commit_lock_file::
Take a pointer to the `struct lock_file` initialized with an
diff --git a/cache.h b/cache.h
index ca64a42..0f1201e 100644
--- a/cache.h
+++ b/cache.h
@@ -590,6 +590,7 @@ extern void unable_to_lock_message(const char *path, int err,
extern NORETURN void unable_to_lock_die(const char *path, int err);
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
+extern char *get_locked_file_path(struct lock_file *);
extern int commit_lock_file_to(struct lock_file *, const char *path);
extern int commit_lock_file(struct lock_file *);
extern int reopen_lock_file(struct lock_file *);
diff --git a/lockfile.c b/lockfile.c
index 0a8c3c8..c51c6ec 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -257,6 +257,15 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
return fd;
}
+char *get_locked_file_path(struct lock_file *lk)
+{
+ if (!lk->active)
+ die("BUG: get_locked_file_path() called for unlocked object");
+ if (lk->filename.len <= LOCK_SUFFIX_LEN)
+ die("BUG: get_locked_file_path() called for malformed lock object");
+ return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN);
+}
+
int close_lock_file(struct lock_file *lk)
{
int fd = lk->fd;
diff --git a/refs.c b/refs.c
index 525ce4b..5842dd0 100644
--- a/refs.c
+++ b/refs.c
@@ -2556,9 +2556,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
* loose. The loose file name is the same as the
* lockfile name, minus ".lock":
*/
- char *loose_filename = xmemdupz(
- lock->lk->filename.buf,
- lock->lk->filename.len - LOCK_SUFFIX_LEN);
+ char *loose_filename = get_locked_file_path(lock->lk);
int err = unlink_or_warn(loose_filename);
free(loose_filename);
if (err && errno != ENOENT)
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 37/39] hold_lock_file_for_append(): restore errno before returning
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (35 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 36/39] get_locked_file_path(): new function Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 38/39] Move read_index() definition to read-cache.c Michael Haggerty
` (3 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
Callers who don't pass LOCK_DIE_ON_ERROR might want to examine errno
to see what went wrong, so restore errno before returning.
In fact this function only has one caller, add_to_alternates_file(),
and it *does* use LOCK_DIE_ON_ERROR, but, you know, think of future
generations.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/lockfile.c b/lockfile.c
index c51c6ec..b2f5d36 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -243,15 +243,22 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
orig_fd = open(path, O_RDONLY);
if (orig_fd < 0) {
if (errno != ENOENT) {
+ int save_errno = errno;
+
if (flags & LOCK_DIE_ON_ERROR)
die("cannot open '%s' for copying", path);
rollback_lock_file(lk);
- return error("cannot open '%s' for copying", path);
+ error("cannot open '%s' for copying", path);
+ errno = save_errno;
+ return -1;
}
} else if (copy_fd(orig_fd, fd)) {
+ int save_errno = errno;
+
if (flags & LOCK_DIE_ON_ERROR)
exit(128);
rollback_lock_file(lk);
+ errno = save_errno;
return -1;
}
return fd;
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 38/39] Move read_index() definition to read-cache.c
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (36 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 37/39] hold_lock_file_for_append(): restore errno before returning Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 10:08 ` [PATCH v6 39/39] lockfile.h: extract new header file for the functions in lockfile.c Michael Haggerty
` (2 subsequent siblings)
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
lockfile.c contains the general API for locking any file. Code
specifically about the index file doesn't belong here.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
lockfile.c | 8 --------
read-cache.c | 8 ++++++++
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index b2f5d36..63f4e94 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -340,14 +340,6 @@ int commit_lock_file(struct lock_file *lk)
return err;
}
-int hold_locked_index(struct lock_file *lk, int die_on_error)
-{
- return hold_lock_file_for_update(lk, get_index_file(),
- die_on_error
- ? LOCK_DIE_ON_ERROR
- : 0);
-}
-
void rollback_lock_file(struct lock_file *lk)
{
if (!lk->active)
diff --git a/read-cache.c b/read-cache.c
index e887e23..9f137e7 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1367,6 +1367,14 @@ static int read_index_extension(struct index_state *istate,
return 0;
}
+int hold_locked_index(struct lock_file *lk, int die_on_error)
+{
+ return hold_lock_file_for_update(lk, get_index_file(),
+ die_on_error
+ ? LOCK_DIE_ON_ERROR
+ : 0);
+}
+
int read_index(struct index_state *istate)
{
return read_index_from(istate, get_index_file());
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* [PATCH v6 39/39] lockfile.h: extract new header file for the functions in lockfile.c
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (37 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 38/39] Move read_index() definition to read-cache.c Michael Haggerty
@ 2014-09-26 10:08 ` Michael Haggerty
2014-09-26 16:31 ` [PATCH v6 00/39] Lockfile correctness and refactoring Junio C Hamano
2014-09-26 19:10 ` Junio C Hamano
40 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-26 10:08 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git, Michael Haggerty
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>
---
builtin/add.c | 1 +
builtin/apply.c | 1 +
builtin/checkout-index.c | 2 +-
builtin/checkout.c | 2 +-
builtin/clone.c | 1 +
builtin/commit.c | 1 +
builtin/describe.c | 1 +
builtin/diff.c | 1 +
builtin/gc.c | 2 +-
builtin/merge.c | 1 +
builtin/mv.c | 2 +-
builtin/read-tree.c | 1 +
builtin/receive-pack.c | 1 +
builtin/reflog.c | 2 +-
builtin/reset.c | 1 +
builtin/rm.c | 2 +-
builtin/update-index.c | 1 +
bundle.c | 1 +
cache-tree.c | 1 +
cache.h | 27 +---------------
config.c | 1 +
credential-store.c | 1 +
fast-import.c | 1 +
fetch-pack.c | 1 +
lockfile.c | 52 +-----------------------------
lockfile.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
merge-recursive.c | 1 +
merge.c | 1 +
read-cache.c | 1 +
refs.c | 1 +
rerere.c | 1 +
sequencer.c | 1 +
sha1_file.c | 1 +
shallow.c | 1 +
test-scrap-cache-tree.c | 1 +
35 files changed, 118 insertions(+), 83 deletions(-)
create mode 100644 lockfile.h
diff --git a/builtin/add.c b/builtin/add.c
index 352b85e..ae6d3e2 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -5,6 +5,7 @@
*/
#include "cache.h"
#include "builtin.h"
+#include "lockfile.h"
#include "dir.h"
#include "pathspec.h"
#include "exec_cmd.h"
diff --git a/builtin/apply.c b/builtin/apply.c
index f204cca..21c8df2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -7,6 +7,7 @@
*
*/
#include "cache.h"
+#include "lockfile.h"
#include "cache-tree.h"
#include "quote.h"
#include "blob.h"
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 05edd9e..383dccf 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -5,7 +5,7 @@
*
*/
#include "builtin.h"
-#include "cache.h"
+#include "lockfile.h"
#include "quote.h"
#include "cache-tree.h"
#include "parse-options.h"
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 8afdf2b..570bb09 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1,5 +1,5 @@
-#include "cache.h"
#include "builtin.h"
+#include "lockfile.h"
#include "parse-options.h"
#include "refs.h"
#include "commit.h"
diff --git a/builtin/clone.c b/builtin/clone.c
index 3927edf..d3bf953 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -9,6 +9,7 @@
*/
#include "builtin.h"
+#include "lockfile.h"
#include "parse-options.h"
#include "fetch-pack.h"
#include "refs.h"
diff --git a/builtin/commit.c b/builtin/commit.c
index f55e809..c230018 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -6,6 +6,7 @@
*/
#include "cache.h"
+#include "lockfile.h"
#include "cache-tree.h"
#include "color.h"
#include "dir.h"
diff --git a/builtin/describe.c b/builtin/describe.c
index ee6a3b9..9103193 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "commit.h"
#include "tag.h"
#include "refs.h"
diff --git a/builtin/diff.c b/builtin/diff.c
index 0f247d2..4326fa5 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -4,6 +4,7 @@
* Copyright (c) 2006 Junio C Hamano
*/
#include "cache.h"
+#include "lockfile.h"
#include "color.h"
#include "commit.h"
#include "blob.h"
diff --git a/builtin/gc.c b/builtin/gc.c
index ced1456..005adbe 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -11,7 +11,7 @@
*/
#include "builtin.h"
-#include "cache.h"
+#include "lockfile.h"
#include "parse-options.h"
#include "run-command.h"
#include "sigchain.h"
diff --git a/builtin/merge.c b/builtin/merge.c
index c7fa3bc..58497e0 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -9,6 +9,7 @@
#include "cache.h"
#include "parse-options.h"
#include "builtin.h"
+#include "lockfile.h"
#include "run-command.h"
#include "diff.h"
#include "refs.h"
diff --git a/builtin/mv.c b/builtin/mv.c
index bf784cb..ea0b58a 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -3,8 +3,8 @@
*
* Copyright (C) 2006 Johannes Schindelin
*/
-#include "cache.h"
#include "builtin.h"
+#include "lockfile.h"
#include "dir.h"
#include "cache-tree.h"
#include "string-list.h"
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index e7e1c33..43b47f7 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -5,6 +5,7 @@
*/
#include "cache.h"
+#include "lockfile.h"
#include "object.h"
#include "tree.h"
#include "tree-walk.h"
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index afb8d99..47d4593 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1,4 +1,5 @@
#include "builtin.h"
+#include "lockfile.h"
#include "pack.h"
#include "refs.h"
#include "pkt-line.h"
diff --git a/builtin/reflog.c b/builtin/reflog.c
index 7c78b15..b6388f7 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -1,5 +1,5 @@
-#include "cache.h"
#include "builtin.h"
+#include "lockfile.h"
#include "commit.h"
#include "refs.h"
#include "dir.h"
diff --git a/builtin/reset.c b/builtin/reset.c
index 855d478..4c08ddc 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -8,6 +8,7 @@
* Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
*/
#include "builtin.h"
+#include "lockfile.h"
#include "tag.h"
#include "object.h"
#include "commit.h"
diff --git a/builtin/rm.c b/builtin/rm.c
index 2b61d3b..d8a9c86 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -3,8 +3,8 @@
*
* Copyright (C) Linus Torvalds 2006
*/
-#include "cache.h"
#include "builtin.h"
+#include "lockfile.h"
#include "dir.h"
#include "cache-tree.h"
#include "tree-walk.h"
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 6c95988..b0e3dc9 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -4,6 +4,7 @@
* Copyright (C) Linus Torvalds, 2005
*/
#include "cache.h"
+#include "lockfile.h"
#include "quote.h"
#include "cache-tree.h"
#include "tree-walk.h"
diff --git a/bundle.c b/bundle.c
index b2b89fe..891a3ca 100644
--- a/bundle.c
+++ b/bundle.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "bundle.h"
#include "object.h"
#include "commit.h"
diff --git a/cache-tree.c b/cache-tree.c
index 75a54fd..215202c 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "tree.h"
#include "tree-walk.h"
#include "cache-tree.h"
diff --git a/cache.h b/cache.h
index 0f1201e..7857b80 100644
--- a/cache.h
+++ b/cache.h
@@ -570,36 +570,11 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
#define REFRESH_IN_PORCELAIN 0x0020 /* user friendly output, not "needs update" */
extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
-/* String appended to a filename to derive the lockfile name: */
-#define LOCK_SUFFIX ".lock"
-#define LOCK_SUFFIX_LEN 5
-
-struct lock_file {
- struct lock_file *volatile next;
- volatile sig_atomic_t active;
- volatile int fd;
- volatile pid_t owner;
- char on_list;
- struct strbuf filename;
-};
-#define LOCK_DIE_ON_ERROR 1
-#define LOCK_NO_DEREF 2
-extern int unable_to_lock_error(const char *path, int err);
-extern void unable_to_lock_message(const char *path, int err,
- struct strbuf *buf);
-extern NORETURN void unable_to_lock_die(const char *path, int err);
-extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
-extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
-extern char *get_locked_file_path(struct lock_file *);
-extern int commit_lock_file_to(struct lock_file *, const char *path);
-extern int commit_lock_file(struct lock_file *);
-extern int reopen_lock_file(struct lock_file *);
extern void update_index_if_able(struct index_state *, struct lock_file *);
extern int hold_locked_index(struct lock_file *, int);
extern void set_alternate_index_output(const char *);
-extern int close_lock_file(struct lock_file *);
-extern void rollback_lock_file(struct lock_file *);
+
extern int delete_ref(const char *, const unsigned char *sha1, int delopt);
/* Environment bits from configuration mechanism */
diff --git a/config.c b/config.c
index 2110779..c31d4d2 100644
--- a/config.c
+++ b/config.c
@@ -6,6 +6,7 @@
*
*/
#include "cache.h"
+#include "lockfile.h"
#include "exec_cmd.h"
#include "strbuf.h"
#include "quote.h"
diff --git a/credential-store.c b/credential-store.c
index f9146e5..d435514 100644
--- a/credential-store.c
+++ b/credential-store.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "credential.h"
#include "string-list.h"
#include "parse-options.h"
diff --git a/fast-import.c b/fast-import.c
index 61ac7e0..44b6686 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -153,6 +153,7 @@ Format of STDIN stream:
#include "builtin.h"
#include "cache.h"
+#include "lockfile.h"
#include "object.h"
#include "blob.h"
#include "tree.h"
diff --git a/fetch-pack.c b/fetch-pack.c
index 7487aa7..655ee64 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "refs.h"
#include "pkt-line.h"
#include "commit.h"
diff --git a/lockfile.c b/lockfile.c
index 63f4e94..d27e61c 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -2,59 +2,9 @@
* Copyright (c) 2005, Junio C Hamano
*/
#include "cache.h"
+#include "lockfile.h"
#include "sigchain.h"
-/*
- * File write-locks as used by Git.
- *
- * For an overview of how to use the lockfile API, please see
- *
- * Documentation/technical/api-lockfile.txt
- *
- * This module keeps track of all locked files in lock_file_list for
- * use at cleanup. This list and the lock_file objects that comprise
- * it must be kept in self-consistent states at all time, because the
- * program can be interrupted any time by a signal, in which case the
- * signal handler will walk through the list attempting to clean up
- * any open lock files.
- *
- * A lockfile is owned by the process that created it. The lock_file
- * object has an "owner" field that records its owner. This field is
- * used to prevent a forked process from closing a lockfile created by
- * its parent.
- *
- * The possible states of a lock_file object are as follows:
- *
- * - Uninitialized. In this state the object's on_list field must be
- * zero but the rest of its contents need not be initialized. As
- * soon as the object is used in any way, it is irrevocably
- * registered in the lock_file_list, and on_list is set.
- *
- * - Locked, lockfile open (after hold_lock_file_for_update(),
- * hold_lock_file_for_append(), or reopen_lock_file()). In this
- * state:
- * - the lockfile exists
- * - active is set
- * - filename holds the filename of the lockfile
- * - fd holds a file descriptor open for writing to the lockfile
- * - owner holds the PID of the process that locked the file
- *
- * - Locked, lockfile closed (after successful close_lock_file()).
- * Same as the previous state, except that the lockfile is closed
- * and fd is -1.
- *
- * - Unlocked (after commit_lock_file(), commit_lock_file_to(),
- * rollback_lock_file(), a failed attempt to lock, or a failed
- * close_lock_file()). In this state:
- * - active is unset
- * - filename is empty (usually, though there are transitory
- * states in which this condition doesn't hold). Client code should
- * *not* rely on the filename being empty in this state.
- * - fd is -1
- * - the object is left registered in the lock_file_list, and
- * on_list is set.
- */
-
static struct lock_file *volatile lock_file_list;
static void remove_lock_files(void)
diff --git a/lockfile.h b/lockfile.h
new file mode 100644
index 0000000..9059e89
--- /dev/null
+++ b/lockfile.h
@@ -0,0 +1,84 @@
+#ifndef LOCKFILE_H
+#define LOCKFILE_H
+
+/*
+ * File write-locks as used by Git.
+ *
+ * For an overview of how to use the lockfile API, please see
+ *
+ * Documentation/technical/api-lockfile.txt
+ *
+ * This module keeps track of all locked files in lock_file_list for
+ * use at cleanup. This list and the lock_file objects that comprise
+ * it must be kept in self-consistent states at all time, because the
+ * program can be interrupted any time by a signal, in which case the
+ * signal handler will walk through the list attempting to clean up
+ * any open lock files.
+ *
+ * A lockfile is owned by the process that created it. The lock_file
+ * object has an "owner" field that records its owner. This field is
+ * used to prevent a forked process from closing a lockfile created by
+ * its parent.
+ *
+ * The possible states of a lock_file object are as follows:
+ *
+ * - Uninitialized. In this state the object's on_list field must be
+ * zero but the rest of its contents need not be initialized. As
+ * soon as the object is used in any way, it is irrevocably
+ * registered in the lock_file_list, and on_list is set.
+ *
+ * - Locked, lockfile open (after hold_lock_file_for_update(),
+ * hold_lock_file_for_append(), or reopen_lock_file()). In this
+ * state:
+ * - the lockfile exists
+ * - active is set
+ * - filename holds the filename of the lockfile
+ * - fd holds a file descriptor open for writing to the lockfile
+ * - owner holds the PID of the process that locked the file
+ *
+ * - Locked, lockfile closed (after successful close_lock_file()).
+ * Same as the previous state, except that the lockfile is closed
+ * and fd is -1.
+ *
+ * - Unlocked (after commit_lock_file(), commit_lock_file_to(),
+ * rollback_lock_file(), a failed attempt to lock, or a failed
+ * close_lock_file()). In this state:
+ * - active is unset
+ * - filename is empty (usually, though there are transitory
+ * states in which this condition doesn't hold). Client code should
+ * *not* rely on the filename being empty in this state.
+ * - fd is -1
+ * - the object is left registered in the lock_file_list, and
+ * on_list is set.
+ */
+
+struct lock_file {
+ struct lock_file *volatile next;
+ volatile sig_atomic_t active;
+ volatile int fd;
+ volatile pid_t owner;
+ char on_list;
+ struct strbuf filename;
+};
+
+/* String appended to a filename to derive the lockfile name: */
+#define LOCK_SUFFIX ".lock"
+#define LOCK_SUFFIX_LEN 5
+
+#define LOCK_DIE_ON_ERROR 1
+#define LOCK_NO_DEREF 2
+
+extern int unable_to_lock_error(const char *path, int err);
+extern void unable_to_lock_message(const char *path, int err,
+ struct strbuf *buf);
+extern NORETURN void unable_to_lock_die(const char *path, int err);
+extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
+extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
+extern char *get_locked_file_path(struct lock_file *);
+extern int commit_lock_file_to(struct lock_file *, const char *path);
+extern int commit_lock_file(struct lock_file *);
+extern int reopen_lock_file(struct lock_file *);
+extern int close_lock_file(struct lock_file *);
+extern void rollback_lock_file(struct lock_file *);
+
+#endif /* LOCKFILE_H */
diff --git a/merge-recursive.c b/merge-recursive.c
index 22315c3..5a0d58c 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -5,6 +5,7 @@
*/
#include "advice.h"
#include "cache.h"
+#include "lockfile.h"
#include "cache-tree.h"
#include "commit.h"
#include "blob.h"
diff --git a/merge.c b/merge.c
index 74ced7f..fcff632 100644
--- a/merge.c
+++ b/merge.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "commit.h"
#include "run-command.h"
#include "resolve-undo.h"
diff --git a/read-cache.c b/read-cache.c
index 9f137e7..8f3e9eb 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -5,6 +5,7 @@
*/
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
+#include "lockfile.h"
#include "cache-tree.h"
#include "refs.h"
#include "dir.h"
diff --git a/refs.c b/refs.c
index 5842dd0..18ddb71 100644
--- a/refs.c
+++ b/refs.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "refs.h"
#include "object.h"
#include "tag.h"
diff --git a/rerere.c b/rerere.c
index 20b18ad..1b0555f 100644
--- a/rerere.c
+++ b/rerere.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "string-list.h"
#include "rerere.h"
#include "xdiff-interface.h"
diff --git a/sequencer.c b/sequencer.c
index 5e8a207..1b9a35e 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "sequencer.h"
#include "dir.h"
#include "object.h"
diff --git a/sha1_file.c b/sha1_file.c
index c08c0cb..46ff273 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -8,6 +8,7 @@
*/
#include "cache.h"
#include "string-list.h"
+#include "lockfile.h"
#include "delta.h"
#include "pack.h"
#include "blob.h"
diff --git a/shallow.c b/shallow.c
index 33f3c7f..08e25a4 100644
--- a/shallow.c
+++ b/shallow.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "commit.h"
#include "tag.h"
#include "pkt-line.h"
diff --git a/test-scrap-cache-tree.c b/test-scrap-cache-tree.c
index 9ebcbca..6efee31 100644
--- a/test-scrap-cache-tree.c
+++ b/test-scrap-cache-tree.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "lockfile.h"
#include "tree.h"
#include "cache-tree.h"
--
2.1.0
^ permalink raw reply related [flat|nested] 55+ messages in thread
* Re: [PATCH v6 00/39] Lockfile correctness and refactoring
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (38 preceding siblings ...)
2014-09-26 10:08 ` [PATCH v6 39/39] lockfile.h: extract new header file for the functions in lockfile.c Michael Haggerty
@ 2014-09-26 16:31 ` Junio C Hamano
2014-09-30 9:55 ` Michael Haggerty
2014-09-26 19:10 ` Junio C Hamano
40 siblings, 1 reply; 55+ messages in thread
From: Junio C Hamano @ 2014-09-26 16:31 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> * Rebase to current master.
When you say this, could you be a bit more descriptive? Has the
series updated to use something new that appeared on 'master' since
the last series was posted and needed to be rebased? Or did you
just made sure that the series applied on top of the current master
still works, even though you have been running "rebase -i" on the
same fork point since the previous round?
If the former, naming what it now uses i.e. "rebased to current
master, to redo PATCH x,y,z using the new X recently graduated"
would be helpful.
If the latter, well, not rebasing is preferrable if the changes are
still viable on the previous fork point, to be absolutely honest.
Thanks.
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 00/39] Lockfile correctness and refactoring
2014-09-26 16:31 ` [PATCH v6 00/39] Lockfile correctness and refactoring Junio C Hamano
@ 2014-09-30 9:55 ` Michael Haggerty
0 siblings, 0 replies; 55+ messages in thread
From: Michael Haggerty @ 2014-09-30 9:55 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
On 09/26/2014 06:31 PM, Junio C Hamano wrote:
> Michael Haggerty <mhagger@alum.mit.edu> writes:
>
>> * Rebase to current master.
>
> When you say this, could you be a bit more descriptive? Has the
> series updated to use something new that appeared on 'master' since
> the last series was posted and needed to be rebased? Or did you
> just made sure that the series applied on top of the current master
> still works, even though you have been running "rebase -i" on the
> same fork point since the previous round?
>
> If the former, naming what it now uses i.e. "rebased to current
> master, to redo PATCH x,y,z using the new X recently graduated"
> would be helpful.
>
> If the latter, well, not rebasing is preferrable if the changes are
> still viable on the previous fork point, to be absolutely honest.
I have always routinely rebased my series to the current master each
time I reroll them. I thought that was helpful. Thanks for the info that
you prefer that I not do it. In the future I will only rebase to master
if I see that there would be nontrivial conflicts or if I would like to
take advantage of something that has graduated.
I've always tried to mention when the rebases were nontrivial. In this
case it was unproblematic.
As an aside, I've always found it wishy-washy to talk about "current
master" and "fork points" and stuff when (AFAIK) format-patch-generated
emails don't mention the fork point. So unless my cover letter specifies
the fork point as an unambiguous commit name, there is no basis *anyway*
to expect that you will apply the patch series in the same context that
I developed it.
Another victim of the format-patch information shredder :-(
Cheers,
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
^ permalink raw reply [flat|nested] 55+ messages in thread
* Re: [PATCH v6 00/39] Lockfile correctness and refactoring
2014-09-26 10:08 [PATCH v6 00/39] Lockfile correctness and refactoring Michael Haggerty
` (39 preceding siblings ...)
2014-09-26 16:31 ` [PATCH v6 00/39] Lockfile correctness and refactoring Junio C Hamano
@ 2014-09-26 19:10 ` Junio C Hamano
40 siblings, 0 replies; 55+ messages in thread
From: Junio C Hamano @ 2014-09-26 19:10 UTC (permalink / raw)
To: Michael Haggerty
Cc: Johannes Sixt, Torsten Bögershausen, Jeff King,
Ronnie Sahlberg, Jonathan Nieder, git
Michael Haggerty <mhagger@alum.mit.edu> writes:
> Next iteration of my lockfile fixes and refactoring. Thanks to
> Jonathan Nieder and Torsten Bögershausen for their comments about v5.
>
> I believe that this series addresses all of the comments from v1 [1],
> v2 [2], v3 [3], v4 [4], and v5 [5].
Looked quite good. Will replace---let's aim to merge this topic to
'next' soonish.
Thanks.
>
> Changes since v4:
>
> * Revise API documentation.
>
> * Split out a separate header file for the lockfile API: lockfile.h.
>
> * Change close_lock_file() to rollback on errors and make it into a
> NOOP if the file is already closed.
>
> * Don't set lk->on_list until the lock_file object is completely on
> the lock_file_list.
>
> * Delete some information from the docstring in lockfile.c (now
> lockfile.h) that is redundant with the API docs in api-lockfile.txt.
>
> * Remove the accidental extra call of git_config_clear() in
> git_config_set_multivar_in_file() when commit_lock_file() fails.
>
> * Adjust some comments, error messages, and commit messages.
>
> * Rebase to current master.
>
> [1] http://thread.gmane.org/gmane.comp.version-control.git/245609
> [2] http://thread.gmane.org/gmane.comp.version-control.git/245801
> [3] http://thread.gmane.org/gmane.comp.version-control.git/246222
> [4] http://thread.gmane.org/gmane.comp.version-control.git/256564
> [5] http://thread.gmane.org/gmane.comp.version-control.git/257159
>
> Michael Haggerty (39):
> unable_to_lock_die(): rename function from unable_to_lock_index_die()
> api-lockfile: revise and expand the documentation
> close_lock_file(): exit (successfully) if file is already closed
> rollback_lock_file(): do not clear filename redundantly
> rollback_lock_file(): exit early if lock is not active
> rollback_lock_file(): set fd to -1
> lockfile: unlock file if lockfile permissions cannot be adjusted
> hold_lock_file_for_append(): release lock on errors
> lock_file(): always initialize and register lock_file object
> lockfile.c: document the various states of lock_file objects
> cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN
> delete_ref_loose(): don't muck around in the lock_file's filename
> prepare_index(): declare return value to be (const char *)
> write_packed_entry_fn(): convert cb_data into a (const int *)
> lock_file(): exit early if lockfile cannot be opened
> remove_lock_file(): call rollback_lock_file()
> commit_lock_file(): inline temporary variable
> commit_lock_file(): die() if called for unlocked lockfile object
> close_lock_file(): if close fails, roll back
> commit_lock_file(): rollback lock file on failure to rename
> api-lockfile: document edge cases
> dump_marks(): remove a redundant call to rollback_lock_file()
> git_config_set_multivar_in_file(): avoid call to rollback_lock_file()
> lockfile: avoid transitory invalid states
> struct lock_file: declare some fields volatile
> try_merge_strategy(): remove redundant lock_file allocation
> try_merge_strategy(): use a statically-allocated lock_file object
> commit_lock_file(): use a strbuf to manage temporary space
> Change lock_file::filename into a strbuf
> resolve_symlink(): use a strbuf for internal scratch space
> resolve_symlink(): take a strbuf parameter
> trim_last_path_component(): replace last_path_elm()
> Extract a function commit_lock_file_to()
> Rename LOCK_NODEREF to LOCK_NO_DEREF
> lockfile.c: rename static functions
> get_locked_file_path(): new function
> hold_lock_file_for_append(): restore errno before returning
> Move read_index() definition to read-cache.c
> lockfile.h: extract new header file for the functions in lockfile.c
>
> Documentation/technical/api-lockfile.txt | 220 ++++++++++++++++++------
> builtin/add.c | 1 +
> builtin/apply.c | 1 +
> builtin/checkout-index.c | 2 +-
> builtin/checkout.c | 2 +-
> builtin/clone.c | 1 +
> builtin/commit.c | 17 +-
> builtin/describe.c | 1 +
> builtin/diff.c | 1 +
> builtin/gc.c | 2 +-
> builtin/merge.c | 16 +-
> builtin/mv.c | 2 +-
> builtin/read-tree.c | 1 +
> builtin/receive-pack.c | 1 +
> builtin/reflog.c | 4 +-
> builtin/reset.c | 1 +
> builtin/rm.c | 2 +-
> builtin/update-index.c | 3 +-
> bundle.c | 1 +
> cache-tree.c | 1 +
> cache.h | 20 +--
> config.c | 16 +-
> credential-store.c | 1 +
> fast-import.c | 5 +-
> fetch-pack.c | 1 +
> lockfile.c | 284 +++++++++++++++++--------------
> lockfile.h | 84 +++++++++
> merge-recursive.c | 1 +
> merge.c | 1 +
> read-cache.c | 21 ++-
> refs.c | 30 ++--
> rerere.c | 1 +
> sequencer.c | 1 +
> sha1_file.c | 1 +
> shallow.c | 7 +-
> test-scrap-cache-tree.c | 1 +
> 36 files changed, 492 insertions(+), 263 deletions(-)
> create mode 100644 lockfile.h
^ permalink raw reply [flat|nested] 55+ messages in thread