file-dotlock.h revision d3dee2335ff3b0943085256fe341c2c13a678c4f
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.". */
94ba4820927b906b333e39445c1508a29387c3aaTimo Sirainen /* Use this suffix for dotlock filenames. Default is ".lock". */
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. */
8732bdd21579472feb40da8ffc99b8fd3b341417Timo Sirainen /* If file is older than this, override the lock immediately. */
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);
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. */
2d7da70c0e6768afeb1200c95f3e1293f2e502c6Timo Sirainen /* If lock already exists, fail immediately */
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 /* Check that lock file hasn't been overwritten before renaming. */
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen /* Don't close the file descriptor. */
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/* 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/* 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/* Replaces the file dotlock protects with the dotlock file itself. */
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainenint file_dotlock_replace(struct dotlock **dotlock,
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainen/* Returns the lock file path. */
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainenconst char *file_dotlock_get_lock_path(struct dotlock *dotlock);