/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "safe-mkstemp.h"
#include "mkdir-parents.h"
#include "file-lock.h"
#include "file-create-locked.h"
#include <unistd.h>
#include <fcntl.h>
/* Try mkdir() + lock creation multiple times. This allows the lock file
creation to work even while the directory is simultaneously being
rmdir()ed. */
static int
const struct file_create_settings *set,
{
int ret;
return -1;
}
return -1;
ret = 0;
} else {
ret = -1;
}
if (ret <= 0) {
/* the fd is closed next - no need to unlock */
}
return ret;
}
static int
const char **error_r)
{
if (p == NULL)
return 0;
int ret;
else {
}
return -1;
}
return 1;
}
static int
{
for (unsigned int i = 0; ret > 0; i++) {
else
i >= MAX_MKDIR_COUNT)
break;
return -1;
errno = orig_errno;
}
if (fd == -1) {
return -1;
}
ret = -1;
/* just created by somebody else */
ret = 0;
/* nobody should be deleting the temp file unless the
entire directory is deleted. */
"Temporary file %s was unexpectedly deleted",
} else {
}
} else {
return 1;
}
orig_errno = errno;
i_close_fd(&fd);
errno = orig_errno;
return ret;
}
const char **error_r)
{
unsigned int i;
for (i = 0; i < MAX_RETRY_COUNT; i++) {
if (fd != -1) {
if (ret > 0) {
/* successfully locked an existing file */
return fd;
}
i_close_fd(&fd);
if (ret < 0)
return -1;
return -1;
} else {
/* try to create the file */
if (ret < 0)
return -1;
if (ret > 0) {
/* successfully created a new locked file */
return fd;
}
/* the file was just created - try again opening and
locking it */
}
}
return -1;
}