add file lock and unlock functions

pull/10/head
yuqing 2016-04-09 22:11:28 +08:00
parent 4df942b589
commit 6bd718079e
4 changed files with 64 additions and 26 deletions

View File

@ -1,7 +1,8 @@
Version 1.27 2016-04-08
Version 1.27 2016-04-09
* add function fd_set_cloexec
* php-fastcommon.spec.in support PHP 7
* add file lock and unlock functions
Version 1.26 2016-03-16
* add logger parameter: compress_log_days_before

View File

@ -90,7 +90,7 @@ int log_init_ex(LogContext *pContext)
pContext->log_level = LOG_INFO;
pContext->log_fd = STDERR_FILENO;
pContext->time_precision = LOG_TIME_PRECISION_SECOND;
strcpy(pContext->rotate_time_format, "%Y%m%d_%H%M%S");
strcpy(pContext->rotate_time_format, "%Y%m%d_%H%M%S");
pContext->log_buff = (char *)malloc(LOG_BUFF_SIZE);
if (pContext->log_buff == NULL)
@ -110,32 +110,11 @@ int log_init_ex(LogContext *pContext)
return 0;
}
static int do_lock_file(int fd, int cmd)
{
struct flock lock;
memset(&lock, 0, sizeof(lock));
lock.l_type = cmd;
lock.l_whence = SEEK_SET;
if (fcntl(fd, F_SETLKW, &lock) != 0)
{
fprintf(stderr, "call fcntl fail, "
"errno: %d, error info: %s\n",
errno, STRERROR(errno));
return errno != 0 ? errno : ENOMEM;
}
return 0;
}
#define log_file_lock(fd) do_lock_file(fd, F_WRLCK)
#define log_file_unlock(fd) do_lock_file(fd, F_UNLCK)
static int log_print_header(LogContext *pContext)
{
int result;
if ((result=log_file_lock(pContext->log_fd)) != 0)
if ((result=file_write_lock(pContext->log_fd)) != 0)
{
return result;
}
@ -144,7 +123,7 @@ static int log_print_header(LogContext *pContext)
if (pContext->current_size < 0)
{
result = errno != 0 ? errno : EACCES;
log_file_unlock(pContext->log_fd);
file_unlock(pContext->log_fd);
fprintf(stderr, "lseek file \"%s\" fail, " \
"errno: %d, error info: %s\n", \
@ -155,7 +134,7 @@ static int log_print_header(LogContext *pContext)
{
pContext->print_header_callback(pContext);
}
log_file_unlock(pContext->log_fd);
file_unlock(pContext->log_fd);
return 0;
}

View File

@ -2299,3 +2299,40 @@ bool is_power2(const int64_t n)
return i == n;
}
static inline int do_lock_file(int fd, int cmd)
{
struct flock lock;
int result;
memset(&lock, 0, sizeof(lock));
lock.l_type = cmd;
lock.l_whence = SEEK_SET;
do
{
if ((result=fcntl(fd, F_SETLKW, &lock)) != 0)
{
result = errno != 0 ? errno : ENOMEM;
fprintf(stderr, "call fcntl fail, "
"errno: %d, error info: %s\n",
result, STRERROR(result));
}
} while (result == EINTR);
return result;
}
int file_read_lock(int fd)
{
return do_lock_file(fd, F_RDLCK);
}
int file_write_lock(int fd)
{
return do_lock_file(fd, F_WRLCK);
}
int file_unlock(int fd)
{
return do_lock_file(fd, F_UNLCK);
}

View File

@ -552,6 +552,27 @@ int64_t get_current_time_us();
*/
bool is_power2(const int64_t n);
/** set file read lock
* parameters:
* fd: the file descriptor to lock
* return: error no, 0 for success, != 0 fail
*/
int file_read_lock(int fd);
/** set file write lock
* parameters:
* fd: the file descriptor to lock
* return: error no, 0 for success, != 0 fail
*/
int file_write_lock(int fd);
/** file unlock
* parameters:
* fd: the file descriptor to unlock
* return: error no, 0 for success, != 0 fail
*/
int file_unlock(int fd);
#ifdef __cplusplus
}
#endif