file-dotlock.h revision 933a8cc7033e8a9bb7a1c7630fbc786b914f1510
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 /* Use this suffix for dotlock filenames. Default is ".lock". */
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 /* 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);
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. */
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen /* If lock already exists, fail immediately */
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. */
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen /* Check that lock file hasn't been overridden before renaming. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* Don't close the file descriptor. */
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/* 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);
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,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Replaces the file dotlock protects with the dotlock file itself. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenint file_dotlock_replace(struct dotlock **dotlock,
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/* Returns the lock file path. */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenconst char *file_dotlock_get_lock_path(struct dotlock *dotlock);