file-dotlock.h revision 933a8cc7033e8a9bb7a1c7630fbc786b914f1510
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch#ifndef __FILE_DOTLOCK_H
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#define __FILE_DOTLOCK_H
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include <unistd.h>
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen#include <fcntl.h>
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainenstruct dotlock;
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstruct dotlock_settings {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* Dotlock files are created by first creating a temp file and then
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen link()ing it to the dotlock. temp_prefix specifies the prefix to
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen use for temp files. It may contain a full path. Default is
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen ".temp.hostname.pid.". */
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen const char *temp_prefix;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen /* Use this suffix for dotlock filenames. Default is ".lock". */
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainen const char *lock_suffix;
cd2cd224d3216a243d55c71c298a5b7684de0ac4Timo Sirainen
cd2cd224d3216a243d55c71c298a5b7684de0ac4Timo Sirainen /* Abort after this many seconds. */
c1faff067b29fb48426cb84260adba563e93189aTimo Sirainen unsigned int timeout;
f6c1297c26b355c4aec2a08978f51ec3efecb351Timo Sirainen /* Override the lock file when it and the file we're protecting is
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen older than stale_timeout. */
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen unsigned int stale_timeout;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen /* Callback is called once in a while. stale is set to TRUE if stale
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen lock is detected and will be overridden in secs_left. If callback
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen returns FALSE then, the lock will not be overridden. */
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen bool (*callback)(unsigned int secs_left, bool stale, void *context);
ef2b2ef2e6a6eb5e4667f2e63faae8a3b646e8baTimo Sirainen void *context;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen /* Rely on O_EXCL locking to work instead of using hardlinks.
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen It's faster, but doesn't work with all NFS implementations. */
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen unsigned int use_excl_lock:1;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen};
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainenenum dotlock_create_flags {
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen /* If lock already exists, fail immediately */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen DOTLOCK_CREATE_FLAG_NONBLOCK = 0x01,
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen /* Don't actually create the lock file, only make sure it doesn't
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen exist. This is racy, so you shouldn't rely on it much. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen DOTLOCK_CREATE_FLAG_CHECKONLY = 0x02
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen};
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenenum dotlock_replace_flags {
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen /* Check that lock file hasn't been overridden before renaming. */
44f93baa7b8dca7d00bf187cd3db1c15eed384d2Timo Sirainen DOTLOCK_REPLACE_FLAG_VERIFY_OWNER = 0x01,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* Don't close the file descriptor. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD = 0x02
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen};
4ea6c43a08b37f270bd54b5809142246fd118263Timo Sirainen
bf9ea5404a0094a8fb8199b677d81f803512c44eTimo Sirainen/* Create dotlock. Returns 1 if successful, 0 if timeout or -1 if error.
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen When returning 0, errno is also set to EAGAIN. */
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainenint file_dotlock_create(const struct dotlock_settings *set, const char *path,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen enum dotlock_create_flags flags,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen struct dotlock **dotlock_r);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Delete the dotlock file. Returns 1 if successful, 0 if the file was already
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen been deleted or reused by someone else, -1 if error. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenint file_dotlock_delete(struct dotlock **dotlock);
4ea6c43a08b37f270bd54b5809142246fd118263Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Use dotlock as the new content for file. This provides read safety without
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen locks, but it's not very good for large files. Returns fd for lock file.
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen If locking timed out, returns -1 and errno = EAGAIN. */
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainenint file_dotlock_open(const struct dotlock_settings *set, const char *path,
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen enum dotlock_create_flags flags,
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen struct dotlock **dotlock_r);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Replaces the file dotlock protects with the dotlock file itself. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenint file_dotlock_replace(struct dotlock **dotlock,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen enum dotlock_replace_flags flags);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Update dotlock's mtime. If you're keeping the dotlock for a long time,
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen it's a good idea to update it once in a while so others won't override it.
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen If the timestamp is less than a second old, it's not updated. */
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainenint file_dotlock_touch(struct dotlock *dotlock);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Returns the lock file path. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenconst char *file_dotlock_get_lock_path(struct dotlock *dotlock);
7d87a87b360ecac47fe10e7ca5c7e1433dd63004Timo Sirainen
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen#endif
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen