file-lock.h revision 1107c86ff3fa4f29796c2e76134b78d0b4a0db50
98N/A#ifndef FILE_LOCK_H
911N/A#define FILE_LOCK_H
767N/A
98N/A#include <unistd.h>
98N/A#include <fcntl.h>
98N/A
98N/A#define DEFAULT_LOCK_TIMEOUT 120
98N/A
98N/Astruct file_lock;
98N/A
98N/Aenum file_lock_method {
98N/A FILE_LOCK_METHOD_FCNTL,
98N/A FILE_LOCK_METHOD_FLOCK,
98N/A FILE_LOCK_METHOD_DOTLOCK
98N/A};
98N/A
98N/A/* Parse lock method from given string. Returns TRUE if ok,
98N/A FALSE if name is unknown. */
98N/Abool file_lock_method_parse(const char *name, enum file_lock_method *method_r);
98N/A/* Convert lock method to string. */
98N/Aconst char *file_lock_method_to_str(enum file_lock_method method);
98N/A
98N/A/* Lock the file. Returns 1 if successful, 0 if file is already locked,
98N/A or -1 if error. lock_type is F_WRLCK or F_RDLCK. */
98N/Aint file_try_lock(int fd, const char *path, int lock_type,
98N/A enum file_lock_method lock_method,
98N/A struct file_lock **lock_r);
98N/A/* Like file_try_lock(), but return the error message as a string instead
98N/A of logging it. Also when returning 0 an error message is returned. */
98N/Aint file_try_lock_error(int fd, const char *path, int lock_type,
98N/A enum file_lock_method lock_method,
98N/A struct file_lock **lock_r, const char **error_r);
493N/A/* Like lock_try_lock(), but return 0 only after having tried to lock for
98N/A timeout_secs. */
98N/Aint file_wait_lock(int fd, const char *path, int lock_type,
814N/A enum file_lock_method lock_method,
98N/A unsigned int timeout_secs,
911N/A struct file_lock **lock_r);
911N/A/* Like file_wait_lock(), but return the error message as a string instead
911N/A of logging it. Also when returning 0 an error message is returned. */
911N/Aint file_wait_lock_error(int fd, const char *path, int lock_type,
98N/A enum file_lock_method lock_method,
98N/A unsigned int timeout_secs,
98N/A struct file_lock **lock_r, const char **error_r);
98N/A/* Change the lock type. */
98N/Aint file_lock_try_update(struct file_lock *lock, int lock_type);
156N/A
493N/A/* Unlock and free the lock. */
493N/Avoid file_unlock(struct file_lock **lock);
98N/A/* Free the lock without unlocking it (because you're closing the fd anyway). */
98N/Avoid file_lock_free(struct file_lock **lock);
705N/A
705N/A/* Returns human-readable string containing the process that has the file
720N/A currently locked. Returns "" if unknown, otherwise " (string)". */
720N/Aconst char *file_lock_find(int lock_fd, enum file_lock_method lock_method,
98N/A int lock_type);
606N/A
606N/A#endif
606N/A