c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef FILE_DOTLOCK_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define FILE_DOTLOCK_H
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen#include <unistd.h>
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen#include <fcntl.h>
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenstruct dotlock;
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenstruct dotlock_settings {
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* Dotlock files are created by first creating a temp file and then
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen link()ing it to the dotlock. temp_prefix specifies the prefix to
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen use for temp files. It may contain a full path. Default is
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen ".temp.hostname.pid.". */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen const char *temp_prefix;
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* Use this suffix for dotlock filenames. Default is ".lock". */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen const char *lock_suffix;
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* Abort after this many seconds. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen unsigned int timeout;
8ecbb74bc4c7f6f6145da3525941d1d0e20e67f1Timo Sirainen /* Override the lock file when it and the file we're protecting is
8ecbb74bc4c7f6f6145da3525941d1d0e20e67f1Timo Sirainen older than stale_timeout. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen unsigned int stale_timeout;
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* Callback is called once in a while. stale is set to TRUE if stale
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen lock is detected and will be overridden in secs_left. If callback
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen returns FALSE then, the lock will not be overridden. */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen bool (*callback)(unsigned int secs_left, bool stale, void *context);
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen void *context;
860c6cf85bd2a3808ab3a480aa697bac24c291dcTimo Sirainen
860c6cf85bd2a3808ab3a480aa697bac24c291dcTimo Sirainen /* Rely on O_EXCL locking to work instead of using hardlinks.
860c6cf85bd2a3808ab3a480aa697bac24c291dcTimo Sirainen It's faster, but doesn't work with all NFS implementations. */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool use_excl_lock:1;
8da095519878426b012058e6f331a669f327f47fTimo Sirainen /* Flush NFS attribute cache before stating files. */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool nfs_flush:1;
fdcb22a688c4676face8db865736b217d9c07d19Timo Sirainen /* Use io_add_notify() to speed up finding out when an existing
fdcb22a688c4676face8db865736b217d9c07d19Timo Sirainen dotlock is deleted */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool use_io_notify:1;
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen};
94f9cf3436f949d6450e8cda523979fc1b11f103Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenenum dotlock_create_flags {
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* If lock already exists, fail immediately */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen DOTLOCK_CREATE_FLAG_NONBLOCK = 0x01,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* Don't actually create the lock file, only make sure it doesn't
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen exist. This is racy, so you shouldn't rely on it much. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen DOTLOCK_CREATE_FLAG_CHECKONLY = 0x02
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen};
892f02d4ab8f764f86015009aaf7437349398286Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenenum dotlock_replace_flags {
933a8cc7033e8a9bb7a1c7630fbc786b914f1510Timo Sirainen /* Check that lock file hasn't been overridden before renaming. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen DOTLOCK_REPLACE_FLAG_VERIFY_OWNER = 0x01,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen /* Don't close the file descriptor. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD = 0x02
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen};
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen/* Create dotlock. Returns 1 if successful, 0 if timeout or -1 if error.
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen When returning 0, errno is also set to EAGAIN. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenint file_dotlock_create(const struct dotlock_settings *set, const char *path,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen enum dotlock_create_flags flags,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen struct dotlock **dotlock_r);
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
6cb2c6ecddcdbeac9e6c73a292244747e12a793eTimo Sirainen/* Delete the dotlock file. Returns 1 if successful, 0 if the file had already
6cb2c6ecddcdbeac9e6c73a292244747e12a793eTimo Sirainen been deleted or reused by someone else, -1 if I/O error. */
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainenint ATTR_NOWARN_UNUSED_RESULT
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainenfile_dotlock_delete(struct dotlock **dotlock);
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen
892f02d4ab8f764f86015009aaf7437349398286Timo Sirainen/* Use dotlock as the new content for file. This provides read safety without
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen locks, but it's not very good for large files. Returns fd for lock file.
7698840ca7516916cba42d27d8bd09580ca393b5Timo Sirainen If locking timed out, returns -1 and errno = EAGAIN. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenint file_dotlock_open(const struct dotlock_settings *set, const char *path,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen enum dotlock_create_flags flags,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen struct dotlock **dotlock_r);
6f7617d14ae27473f55628fbdad2af2d75b909a9Timo Sirainen/* Like file_dotlock_open(), but use the given file permissions. */
6f7617d14ae27473f55628fbdad2af2d75b909a9Timo Sirainenint file_dotlock_open_mode(const struct dotlock_settings *set, const char *path,
6f7617d14ae27473f55628fbdad2af2d75b909a9Timo Sirainen enum dotlock_create_flags flags,
6f7617d14ae27473f55628fbdad2af2d75b909a9Timo Sirainen mode_t mode, uid_t uid, gid_t gid,
6f7617d14ae27473f55628fbdad2af2d75b909a9Timo Sirainen struct dotlock **dotlock_r);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenint file_dotlock_open_group(const struct dotlock_settings *set, const char *path,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen enum dotlock_create_flags flags,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen mode_t mode, gid_t gid, const char *gid_origin,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen struct dotlock **dotlock_r);
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen/* Replaces the file dotlock protects with the dotlock file itself. */
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainenint file_dotlock_replace(struct dotlock **dotlock,
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen enum dotlock_replace_flags flags);
933a8cc7033e8a9bb7a1c7630fbc786b914f1510Timo Sirainen/* Update dotlock's mtime. If you're keeping the dotlock for a long time,
933a8cc7033e8a9bb7a1c7630fbc786b914f1510Timo Sirainen it's a good idea to update it once in a while so others won't override it.
933a8cc7033e8a9bb7a1c7630fbc786b914f1510Timo Sirainen If the timestamp is less than a second old, it's not updated. */
933a8cc7033e8a9bb7a1c7630fbc786b914f1510Timo Sirainenint file_dotlock_touch(struct dotlock *dotlock);
021665c0147e5c655efb2c3f0eff2549b31f6b5aTimo Sirainen/* Returns TRUE if the lock is still ok, FALSE if it's been overridden. */
021665c0147e5c655efb2c3f0eff2549b31f6b5aTimo Sirainenbool file_dotlock_is_locked(struct dotlock *dotlock);
892f02d4ab8f764f86015009aaf7437349398286Timo Sirainen
d3dee2335ff3b0943085256fe341c2c13a678c4fTimo Sirainen/* Returns the lock file path. */
d3dee2335ff3b0943085256fe341c2c13a678c4fTimo Sirainenconst char *file_dotlock_get_lock_path(struct dotlock *dotlock);
d3dee2335ff3b0943085256fe341c2c13a678c4fTimo Sirainen
48f78a48f2e1cf299026544444666471ae16ad97Timo Sirainen#endif