Blame lockfile.h

Packit 4511e4
#ifndef LOCKFILE_H
Packit 4511e4
#define LOCKFILE_H
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * File write-locks as used by Git.
Packit 4511e4
 *
Packit 4511e4
 * The lockfile API serves two purposes:
Packit 4511e4
 *
Packit 4511e4
 * * Mutual exclusion and atomic file updates. When we want to change
Packit 4511e4
 *   a file, we create a lockfile `<filename>.lock`, write the new
Packit 4511e4
 *   file contents into it, and then rename the lockfile to its final
Packit 4511e4
 *   destination `<filename>`. We create the `<filename>.lock` file
Packit 4511e4
 *   with `O_CREAT|O_EXCL` so that we can notice and fail if somebody
Packit 4511e4
 *   else has already locked the file, then atomically rename the
Packit 4511e4
 *   lockfile to its final destination to commit the changes and
Packit 4511e4
 *   unlock the file.
Packit 4511e4
 *
Packit 4511e4
 * * Automatic cruft removal. If the program exits after we lock a
Packit 4511e4
 *   file but before the changes have been committed, we want to make
Packit 4511e4
 *   sure that we remove the lockfile. This is done by remembering the
Packit 4511e4
 *   lockfiles we have created in a linked list and setting up an
Packit 4511e4
 *   `atexit(3)` handler and a signal handler that clean up the
Packit 4511e4
 *   lockfiles. This mechanism ensures that outstanding lockfiles are
Packit 4511e4
 *   cleaned up if the program exits (including when `die()` is
Packit 4511e4
 *   called) or if the program is terminated by a signal.
Packit 4511e4
 *
Packit 4511e4
 * Please note that lockfiles only block other writers. Readers do not
Packit 4511e4
 * block, but they are guaranteed to see either the old contents of
Packit 4511e4
 * the file or the new contents of the file (assuming that the
Packit 4511e4
 * filesystem implements `rename(2)` atomically).
Packit 4511e4
 *
Packit 4511e4
 * Most of the heavy lifting is done by the tempfile module (see
Packit 4511e4
 * "tempfile.h").
Packit 4511e4
 *
Packit 4511e4
 * Calling sequence
Packit 4511e4
 * ----------------
Packit 4511e4
 *
Packit 4511e4
 * The caller:
Packit 4511e4
 *
Packit 4511e4
 * * Allocates a `struct lock_file` with whatever storage duration you
Packit 4511e4
 *   desire. The struct does not have to be initialized before being
Packit 4511e4
 *   used, but it is good practice to do so using by setting it to
Packit 4511e4
 *   all-zeros (or using the LOCK_INIT macro). This puts the object in a
Packit 4511e4
 *   consistent state that allows you to call rollback_lock_file() even
Packit 4511e4
 *   if the lock was never taken (in which case it is a noop).
Packit 4511e4
 *
Packit 4511e4
 * * Attempts to create a lockfile by calling `hold_lock_file_for_update()`.
Packit 4511e4
 *
Packit 4511e4
 * * Writes new content for the destination file by either:
Packit 4511e4
 *
Packit 4511e4
 *   * writing to the file descriptor returned by the
Packit 4511e4
 *     `hold_lock_file_for_*()` functions (also available via
Packit 4511e4
 *     `lock->fd`).
Packit 4511e4
 *
Packit 4511e4
 *   * calling `fdopen_lock_file()` to get a `FILE` pointer for the
Packit 4511e4
 *     open file and writing to the file using stdio.
Packit 4511e4
 *
Packit 4511e4
 *   Note that the file descriptor returned by hold_lock_file_for_update()
Packit 4511e4
 *   is marked O_CLOEXEC, so the new contents must be written by the
Packit 4511e4
 *   current process, not a spawned one.
Packit 4511e4
 *
Packit 4511e4
 * When finished writing, the caller can:
Packit 4511e4
 *
Packit 4511e4
 * * Close the file descriptor and rename the lockfile to its final
Packit 4511e4
 *   destination by calling `commit_lock_file()` or
Packit 4511e4
 *   `commit_lock_file_to()`.
Packit 4511e4
 *
Packit 4511e4
 * * Close the file descriptor and remove the lockfile by calling
Packit 4511e4
 *   `rollback_lock_file()`.
Packit 4511e4
 *
Packit 4511e4
 * * Close the file descriptor without removing or renaming the
Packit 4511e4
 *   lockfile by calling `close_lock_file_gently()`, and later call
Packit 4511e4
 *   `commit_lock_file()`, `commit_lock_file_to()`,
Packit 4511e4
 *   `rollback_lock_file()`, or `reopen_lock_file()`.
Packit 4511e4
 *
Packit 4511e4
 * After the lockfile is committed or rolled back, the `lock_file`
Packit 4511e4
 * object can be discarded or reused.
Packit 4511e4
 *
Packit 4511e4
 * If the program exits before `commit_lock_file()`,
Packit 4511e4
 * `commit_lock_file_to()`, or `rollback_lock_file()` is called, the
Packit 4511e4
 * tempfile module will close and remove the lockfile, thereby rolling
Packit 4511e4
 * back any uncommitted changes.
Packit 4511e4
 *
Packit 4511e4
 * If you need to close the file descriptor you obtained from a
Packit 4511e4
 * `hold_lock_file_for_*()` function yourself, do so by calling
Packit 4511e4
 * `close_lock_file_gently()`. See "tempfile.h" for more information.
Packit 4511e4
 *
Packit 4511e4
 *
Packit 4511e4
 * Under the covers, a lockfile is just a tempfile with a few helper
Packit 4511e4
 * functions. In particular, the state diagram and the cleanup
Packit 4511e4
 * machinery are all implemented in the tempfile module.
Packit 4511e4
 *
Packit 4511e4
 *
Packit 4511e4
 * Error handling
Packit 4511e4
 * --------------
Packit 4511e4
 *
Packit 4511e4
 * The `hold_lock_file_for_*()` functions return a file descriptor on
Packit 4511e4
 * success or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see
Packit 4511e4
 * "flags" below). On errors, `errno` describes the reason for
Packit 4511e4
 * failure. Errors can be reported by passing `errno` to
Packit 4511e4
 * `unable_to_lock_message()` or `unable_to_lock_die()`.
Packit 4511e4
 *
Packit 4511e4
 * Similarly, `commit_lock_file`, `commit_lock_file_to`, and
Packit 4511e4
 * `close_lock_file` return 0 on success. On failure they set `errno`
Packit 4511e4
 * appropriately and return -1. The `commit` variants (but not `close`)
Packit 4511e4
 * do their best to delete the temporary file before returning.
Packit 4511e4
 */
Packit 4511e4
Packit 4511e4
#include "tempfile.h"
Packit 4511e4
Packit 4511e4
struct lock_file {
Packit 4511e4
	struct tempfile *tempfile;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
#define LOCK_INIT { NULL }
Packit 4511e4
Packit 4511e4
/* String appended to a filename to derive the lockfile name: */
Packit 4511e4
#define LOCK_SUFFIX ".lock"
Packit 4511e4
#define LOCK_SUFFIX_LEN 5
Packit 4511e4
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Flags
Packit 4511e4
 * -----
Packit 4511e4
 *
Packit 4511e4
 * The following flags can be passed to `hold_lock_file_for_update()`.
Packit 4511e4
 */
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * If a lock is already taken for the file, `die()` with an error
Packit 4511e4
 * message. If this flag is not specified, trying to lock a file that
Packit 4511e4
 * is already locked silently returns -1 to the caller, or ...
Packit 4511e4
 */
Packit 4511e4
#define LOCK_DIE_ON_ERROR 1
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * ... this flag can be passed instead to return -1 and give the usual
Packit 4511e4
 * error message upon an error.
Packit 4511e4
 */
Packit 4511e4
#define LOCK_REPORT_ON_ERROR 4
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Usually symbolic links in the destination path are resolved. This
Packit 4511e4
 * means that (1) the lockfile is created by adding ".lock" to the
Packit 4511e4
 * resolved path, and (2) upon commit, the resolved path is
Packit 4511e4
 * overwritten. However, if `LOCK_NO_DEREF` is set, then the lockfile
Packit 4511e4
 * is created by adding ".lock" to the path argument itself. This
Packit 4511e4
 * option is used, for example, when detaching a symbolic reference,
Packit 4511e4
 * which for backwards-compatibility reasons, can be a symbolic link
Packit 4511e4
 * containing the name of the referred-to-reference.
Packit 4511e4
 */
Packit 4511e4
#define LOCK_NO_DEREF 2
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Attempt to create a lockfile for the file at `path` and return a
Packit 4511e4
 * file descriptor for writing to it, or -1 on error. If the file is
Packit 4511e4
 * currently locked, retry with quadratic backoff for at least
Packit 4511e4
 * timeout_ms milliseconds. If timeout_ms is 0, try exactly once; if
Packit 4511e4
 * timeout_ms is -1, retry indefinitely. The flags argument and error
Packit 4511e4
 * handling are described above.
Packit 4511e4
 */
Packit 4511e4
extern int hold_lock_file_for_update_timeout(
Packit 4511e4
		struct lock_file *lk, const char *path,
Packit 4511e4
		int flags, long timeout_ms);
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Attempt to create a lockfile for the file at `path` and return a
Packit 4511e4
 * file descriptor for writing to it, or -1 on error. The flags
Packit 4511e4
 * argument and error handling are described above.
Packit 4511e4
 */
Packit 4511e4
static inline int hold_lock_file_for_update(
Packit 4511e4
		struct lock_file *lk, const char *path,
Packit 4511e4
		int flags)
Packit 4511e4
{
Packit 4511e4
	return hold_lock_file_for_update_timeout(lk, path, flags, 0);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Return a nonzero value iff `lk` is currently locked.
Packit 4511e4
 */
Packit 4511e4
static inline int is_lock_file_locked(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	return is_tempfile_active(lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Append an appropriate error message to `buf` following the failure
Packit 4511e4
 * of `hold_lock_file_for_update()` to lock `path`. `err` should be the
Packit 4511e4
 * `errno` set by the failing call.
Packit 4511e4
 */
Packit 4511e4
extern void unable_to_lock_message(const char *path, int err,
Packit 4511e4
				   struct strbuf *buf);
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Emit an appropriate error message and `die()` following the failure
Packit 4511e4
 * of `hold_lock_file_for_update()` to lock `path`. `err` should be the
Packit 4511e4
 * `errno` set by the failing
Packit 4511e4
 * call.
Packit 4511e4
 */
Packit 4511e4
extern NORETURN void unable_to_lock_die(const char *path, int err);
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Associate a stdio stream with the lockfile (which must still be
Packit 4511e4
 * open). Return `NULL` (*without* rolling back the lockfile) on
Packit 4511e4
 * error. The stream is closed automatically when
Packit 4511e4
 * `close_lock_file_gently()` is called or when the file is committed or
Packit 4511e4
 * rolled back.
Packit 4511e4
 */
Packit 4511e4
static inline FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
Packit 4511e4
{
Packit 4511e4
	return fdopen_tempfile(lk->tempfile, mode);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Return the path of the lockfile. The return value is a pointer to a
Packit 4511e4
 * field within the lock_file object and should not be freed.
Packit 4511e4
 */
Packit 4511e4
static inline const char *get_lock_file_path(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	return get_tempfile_path(lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static inline int get_lock_file_fd(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	return get_tempfile_fd(lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static inline FILE *get_lock_file_fp(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	return get_tempfile_fp(lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Return the path of the file that is locked by the specified
Packit 4511e4
 * lock_file object. The caller must free the memory.
Packit 4511e4
 */
Packit 4511e4
extern char *get_locked_file_path(struct lock_file *lk);
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * If the lockfile is still open, close it (and the file pointer if it
Packit 4511e4
 * has been opened using `fdopen_lock_file()`) without renaming the
Packit 4511e4
 * lockfile over the file being locked. Return 0 upon success. On
Packit 4511e4
 * failure to `close(2)`, return a negative value (the lockfile is not
Packit 4511e4
 * rolled back). Usually `commit_lock_file()`, `commit_lock_file_to()`,
Packit 4511e4
 * or `rollback_lock_file()` should eventually be called.
Packit 4511e4
 */
Packit 4511e4
static inline int close_lock_file_gently(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	return close_tempfile_gently(lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Re-open a lockfile that has been closed using `close_lock_file_gently()`
Packit 4511e4
 * but not yet committed or rolled back. This can be used to implement
Packit 4511e4
 * a sequence of operations like the following:
Packit 4511e4
 *
Packit 4511e4
 * * Lock file.
Packit 4511e4
 *
Packit 4511e4
 * * Write new contents to lockfile, then `close_lock_file_gently()` to
Packit 4511e4
 *   cause the contents to be written to disk.
Packit 4511e4
 *
Packit 4511e4
 * * Pass the name of the lockfile to another program to allow it (and
Packit 4511e4
 *   nobody else) to inspect the contents you wrote, while still
Packit 4511e4
 *   holding the lock yourself.
Packit 4511e4
 *
Packit 4511e4
 * * `reopen_lock_file()` to reopen the lockfile. Make further updates
Packit 4511e4
 *   to the contents.
Packit 4511e4
 *
Packit 4511e4
 * * `commit_lock_file()` to make the final version permanent.
Packit 4511e4
 */
Packit 4511e4
static inline int reopen_lock_file(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	return reopen_tempfile(lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Commit the change represented by `lk`: close the file descriptor
Packit 4511e4
 * and/or file pointer if they are still open and rename the lockfile
Packit 4511e4
 * to its final destination. Return 0 upon success. On failure, roll
Packit 4511e4
 * back the lock file and return -1, with `errno` set to the value
Packit 4511e4
 * from the failing call to `close(2)` or `rename(2)`. It is a bug to
Packit 4511e4
 * call `commit_lock_file()` for a `lock_file` object that is not
Packit 4511e4
 * currently locked.
Packit 4511e4
 */
Packit 4511e4
extern int commit_lock_file(struct lock_file *lk);
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Like `commit_lock_file()`, but rename the lockfile to the provided
Packit 4511e4
 * `path`. `path` must be on the same filesystem as the lock file.
Packit 4511e4
 */
Packit 4511e4
static inline int commit_lock_file_to(struct lock_file *lk, const char *path)
Packit 4511e4
{
Packit 4511e4
	return rename_tempfile(&lk->tempfile, path);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Roll back `lk`: close the file descriptor and/or file pointer and
Packit 4511e4
 * remove the lockfile. It is a NOOP to call `rollback_lock_file()`
Packit 4511e4
 * for a `lock_file` object that has already been committed or rolled
Packit 4511e4
 * back.
Packit 4511e4
 */
Packit 4511e4
static inline void rollback_lock_file(struct lock_file *lk)
Packit 4511e4
{
Packit 4511e4
	delete_tempfile(&lk->tempfile);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
#endif /* LOCKFILE_H */