file-dotlock.h revision d3dee2335ff3b0943085256fe341c2c13a678c4f
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen#ifndef __FILE_DOTLOCK_H
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen#define __FILE_DOTLOCK_H
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen#include <unistd.h>
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen#include <fcntl.h>
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainenstruct dotlock;
cf1c3e6833fc5031d89db48dad46ed025beda4e7Timo Sirainen
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstruct dotlock_settings {
46d283ef537885386ab3fc72d1831054ea5f986bTimo Sirainen /* Dotlock files are created by first creating a temp file and then
c4ac0f222613903b7f8c9e0825198396e6bf793eTimo Sirainen link()ing it to the dotlock. temp_prefix specifies the prefix to
46d283ef537885386ab3fc72d1831054ea5f986bTimo Sirainen use for temp files. It may contain a full path. Default is
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen ".temp.hostname.pid.". */
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen const char *temp_prefix;
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen /* Use this suffix for dotlock filenames. Default is ".lock". */
f330fcc3307d48954b6f8909349546773368a041Timo Sirainen const char *lock_suffix;
d389c93ada174a8fc6edf995b4f829d38e8fe567Timo Sirainen
f24b245f49b030d7dba3bcb2463cbc05b98c9d1dTimo Sirainen /* Abort after this many seconds. */
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen unsigned int timeout;
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen /* If file specified in path doesn't change in stale_timeout seconds
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen and it's still locked, override the lock file. */
f23baa3b53b1dd4eb19729e99a43937fa3c7f309Timo Sirainen unsigned int stale_timeout;
8732bdd21579472feb40da8ffc99b8fd3b341417Timo Sirainen /* If file is older than this, override the lock immediately. */
7f098e28ddad259d9fbe76e18347c722bb005189Timo Sirainen unsigned int immediate_stale_timeout;
7f098e28ddad259d9fbe76e18347c722bb005189Timo Sirainen
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen /* Callback is called once in a while. stale is set to TRUE if stale
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen lock is detected and will be overridden in secs_left. If callback
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen returns FALSE then, the lock will not be overridden. */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen int (*callback)(unsigned int secs_left, int stale, void *context);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen void *context;
71fbc9af425dc9afc2fb58f911bfe4c5b4edba9bTimo Sirainen
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen /* Rely on O_EXCL locking to work instead of using hardlinks.
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen It's faster, but doesn't work with all NFS implementations. */
8732bdd21579472feb40da8ffc99b8fd3b341417Timo Sirainen unsigned int use_excl_lock:1;
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen};
2d7da70c0e6768afeb1200c95f3e1293f2e502c6Timo Sirainen
2d7da70c0e6768afeb1200c95f3e1293f2e502c6Timo Sirainenenum dotlock_create_flags {
2d7da70c0e6768afeb1200c95f3e1293f2e502c6Timo Sirainen /* If lock already exists, fail immediately */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen DOTLOCK_CREATE_FLAG_NONBLOCK = 0x01,
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* Don't actually create the lock file, only make sure it doesn't
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen exist. This is racy, so you shouldn't rely on it much. */
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen DOTLOCK_CREATE_FLAG_CHECKONLY = 0x02
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen};
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainenenum dotlock_replace_flags {
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen /* Check that lock file hasn't been overwritten before renaming. */
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen DOTLOCK_REPLACE_FLAG_VERIFY_OWNER = 0x01,
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen /* Don't close the file descriptor. */
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD = 0x02
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen};
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen/* Create dotlock. Returns 1 if successful, 0 if timeout or -1 if error.
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen When returning 0, errno is also set to EAGAIN. */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenint file_dotlock_create(const struct dotlock_settings *set, const char *path,
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen enum dotlock_create_flags flags,
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen struct dotlock **dotlock_r);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen/* Delete the dotlock file. Returns 1 if successful, 0 if the file was already
ea1ae54fcfbd8e911978c149f9e7265a45b6380aTimo Sirainen been deleted or reused by someone else, -1 if error. */
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainenint file_dotlock_delete(struct dotlock **dotlock);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen/* Use dotlock as the new content for file. This provides read safety without
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen locks, but it's not very good for large files. Returns fd for lock file.
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen If locking timed out, returns -1 and errno = EAGAIN. */
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainenint file_dotlock_open(const struct dotlock_settings *set, const char *path,
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen enum dotlock_create_flags flags,
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainen struct dotlock **dotlock_r);
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen/* Replaces the file dotlock protects with the dotlock file itself. */
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainenint file_dotlock_replace(struct dotlock **dotlock,
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainen enum dotlock_replace_flags flags);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainen/* Returns the lock file path. */
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainenconst char *file_dotlock_get_lock_path(struct dotlock *dotlock);
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainen
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen#endif
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen