file-create-locked.c revision 02c335c23bf5fa225a467c19f2c063fb0dc7b8c3
/* Copyright (c) 2015-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "safe-mkstemp.h"
#include "file-lock.h"
#include "file-create-locked.h"
#include <unistd.h>
#include <fcntl.h>
#define MAX_RETRY_COUNT 1000
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
{
else
if (fd == -1) {
return -1;
}
/* just created by somebody else */
ret = 0;
/* our temp file was just deleted by somebody else,
retry creating it. */
ret = 0;
} 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;
}