Skip to content

Commit

Permalink
lib: Add file_lock_set_unlink_on_free()
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed Jun 28, 2017
1 parent 2605b39 commit 68332e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/file-lock.c
Expand Up @@ -18,6 +18,7 @@ struct file_lock {
struct timeval locked_time;
int lock_type;
enum file_lock_method lock_method;
bool unlink_on_free;
};

static struct timeval lock_wait_start;
Expand Down Expand Up @@ -340,13 +341,23 @@ int file_lock_try_update(struct file_lock *lock, int lock_type)
return 1;
}

void file_lock_set_unlink_on_free(struct file_lock *lock, bool set)
{
lock->unlink_on_free = set;
}

void file_unlock(struct file_lock **_lock)
{
struct file_lock *lock = *_lock;
const char *error;

*_lock = NULL;

/* unlocking is unnecessary when the file is unlinked. or alternatively
the unlink() must be done before unlocking, because otherwise it
could be deleting the new lock. */
i_assert(!lock->unlink_on_free);

if (file_lock_do(lock->fd, lock->path, F_UNLCK,
lock->lock_method, 0, &error) == 0) {
/* this shouldn't happen */
Expand All @@ -362,6 +373,9 @@ void file_lock_free(struct file_lock **_lock)

*_lock = NULL;

if (lock->unlink_on_free)
i_unlink(lock->path);

file_lock_log_warning_if_slow(lock);
i_free(lock->path);
i_free(lock);
Expand Down
3 changes: 3 additions & 0 deletions src/lib/file-lock.h
Expand Up @@ -45,6 +45,9 @@ int file_wait_lock_error(int fd, const char *path, int lock_type,
/* Change the lock type. WARNING: This isn't an atomic operation!
The result is the same as file_unlock() + file_try_lock(). */
int file_lock_try_update(struct file_lock *lock, int lock_type);
/* When the lock is freed, unlink() the file automatically. This can be useful
for files that are only created to exist as lock files. */
void file_lock_set_unlink_on_free(struct file_lock *lock, bool set);

/* Unlock and free the lock. */
void file_unlock(struct file_lock **lock);
Expand Down

0 comments on commit 68332e3

Please sign in to comment.