Merge branch 'replication_quorum'
commit
68360c1bd1
5
HISTORY
5
HISTORY
|
|
@ -1,4 +1,9 @@
|
|||
|
||||
Version 1.59 2022-07-21
|
||||
* open file with flag O_CLOEXEC
|
||||
* add global var g_set_cloexec and macro FC_SET_CLOEXEC
|
||||
* add function fc_get_first_lines
|
||||
|
||||
Version 1.58 2022-06-04
|
||||
* add function conn_pool_connect_server_ex1 to support service name
|
||||
* add function conn_pool_get_connection_ex to support service name
|
||||
|
|
|
|||
3
INSTALL
3
INSTALL
|
|
@ -12,6 +12,5 @@ Chinese language: http://www.fastken.com/
|
|||
# the command lines as:
|
||||
|
||||
git clone https://github.com/happyfish100/libfastcommon.git
|
||||
cd libfastcommon; git checkout V1.0.57
|
||||
cd libfastcommon; git checkout V1.0.59
|
||||
./make.sh clean && ./make.sh && ./make.sh install
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
%define CommitVersion %(echo $COMMIT_VERSION)
|
||||
|
||||
Name: libfastcommon
|
||||
Version: 1.0.58
|
||||
Version: 1.0.59
|
||||
Release: 1%{?dist}
|
||||
Summary: c common functions library extracted from my open source projects FastDFS
|
||||
License: LGPL
|
||||
|
|
|
|||
|
|
@ -1237,8 +1237,8 @@ static PHPFileContext *fetch_file_context(const char *filename)
|
|||
|
||||
static int fc_open_file(PHPFileContext *ctx)
|
||||
{
|
||||
if ((ctx->fd = open(ctx->filename, O_WRONLY |
|
||||
O_CREAT | O_APPEND, 0644)) < 0)
|
||||
if ((ctx->fd = open(ctx->filename, O_WRONLY | O_CREAT |
|
||||
O_APPEND | O_CLOEXEC, 0644)) < 0)
|
||||
{
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"open file \"%s\" to write fail, "
|
||||
|
|
|
|||
|
|
@ -49,7 +49,8 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer,
|
|||
}
|
||||
|
||||
snprintf(writer->filename, sizeof(writer->filename), "%s", filename);
|
||||
writer->fd = open(writer->filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
|
||||
writer->fd = open(writer->filename, O_WRONLY |
|
||||
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
||||
if (writer->fd < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : EIO;
|
||||
|
|
|
|||
|
|
@ -181,7 +181,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
|
|||
return result;
|
||||
}
|
||||
|
||||
static inline void conn_pool_get_key(const ConnectionInfo *conn, char *key, int *key_len)
|
||||
static inline void conn_pool_get_key(const ConnectionInfo *conn,
|
||||
char *key, int *key_len)
|
||||
{
|
||||
*key_len = sprintf(key, "%s_%u", conn->ip_addr, conn->port);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,13 +123,14 @@ int id_generator_init_extra_ex(struct idg_context *context, const char *filename
|
|||
mid = ntohl(ip_addr.s_addr) & ((1 << mid_bits) - 1);
|
||||
}
|
||||
|
||||
if ((context->fd = open(filename, O_RDWR)) < 0)
|
||||
if ((context->fd = open(filename, O_RDWR | O_CLOEXEC)) < 0)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
{
|
||||
mode_t old_mode;
|
||||
old_mode = umask(0);
|
||||
if ((context->fd=open(filename, O_RDWR | O_CREAT, mode)) < 0)
|
||||
if ((context->fd=open(filename, O_RDWR | O_CREAT |
|
||||
O_CLOEXEC, mode)) < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : EACCES;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -158,8 +158,8 @@ static int log_print_header(LogContext *pContext)
|
|||
static int log_open(LogContext *pContext)
|
||||
{
|
||||
int result;
|
||||
if ((pContext->log_fd = open(pContext->log_filename, O_WRONLY | \
|
||||
O_CREAT | O_APPEND | pContext->fd_flags, 0644)) < 0)
|
||||
if ((pContext->log_fd = open(pContext->log_filename, O_WRONLY | O_CREAT |
|
||||
O_APPEND | O_CLOEXEC | pContext->fd_flags, 0644)) < 0)
|
||||
{
|
||||
fprintf(stderr, "open log file \"%s\" to write fail, " \
|
||||
"errno: %d, error info: %s\n", \
|
||||
|
|
|
|||
|
|
@ -114,9 +114,9 @@ static inline void fc_timedwait_sec(pthread_mutex_t *lock,
|
|||
{
|
||||
struct timespec ts;
|
||||
|
||||
PTHREAD_MUTEX_LOCK(lock);
|
||||
ts.tv_sec = get_current_time() + timeout;
|
||||
ts.tv_nsec = 0;
|
||||
PTHREAD_MUTEX_LOCK(lock);
|
||||
pthread_cond_timedwait(cond, lock, &ts);
|
||||
PTHREAD_MUTEX_UNLOCK(lock);
|
||||
}
|
||||
|
|
@ -128,9 +128,9 @@ static inline void fc_timedwait_ms(pthread_mutex_t *lock,
|
|||
struct timespec ts;
|
||||
|
||||
expires_ms = get_current_time_ms() + timeout_ms;
|
||||
PTHREAD_MUTEX_LOCK(lock);
|
||||
ts.tv_sec = expires_ms / 1000;
|
||||
ts.tv_nsec = (expires_ms % 1000) * (1000 * 1000);
|
||||
PTHREAD_MUTEX_LOCK(lock);
|
||||
pthread_cond_timedwait(cond, lock, &ts);
|
||||
PTHREAD_MUTEX_UNLOCK(lock);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,9 +127,9 @@ static int sched_init_entries(ScheduleEntry *entries, const int count)
|
|||
|
||||
if (pEntry->interval <= 0)
|
||||
{
|
||||
logError("file: "__FILE__", line: %d, " \
|
||||
"shedule interval %d <= 0", \
|
||||
__LINE__, pEntry->interval);
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"shedule id: %d, interval %d <= 0",
|
||||
__LINE__, pEntry->id, pEntry->interval);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@
|
|||
#endif
|
||||
#endif
|
||||
|
||||
bool g_set_cloexec = false;
|
||||
|
||||
char *formatDatetime(const time_t nTime, \
|
||||
const char *szDateFormat, \
|
||||
char *buff, const int buff_size)
|
||||
|
|
@ -335,7 +337,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
|
|||
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
|
||||
{
|
||||
sprintf(filepath, "%s/cmdline", fullpath);
|
||||
if ((fd = open(filepath, O_RDONLY))<0)
|
||||
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
@ -734,7 +736,7 @@ int fc_get_file_line_count_ex(const char *filename,
|
|||
return ENOMEM;
|
||||
}
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
fd = open(filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
result = errno != 0 ? errno : EACCES;
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
|
|
@ -1314,7 +1316,7 @@ int getFileContent(const char *filename, char **buff, int64_t *file_size)
|
|||
}
|
||||
}
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
fd = open(filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
{
|
||||
*buff = NULL;
|
||||
|
|
@ -1368,7 +1370,7 @@ int getFileContentEx(const char *filename, char *buff,
|
|||
return EINVAL;
|
||||
}
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
fd = open(filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
{
|
||||
*size = 0;
|
||||
|
|
@ -1406,7 +1408,7 @@ int writeToFile(const char *filename, const char *buff, const int file_size)
|
|||
int fd;
|
||||
int result;
|
||||
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (fd < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : EIO;
|
||||
|
|
@ -1480,7 +1482,7 @@ int fc_copy_file(const char *src_filename, const char *dest_filename)
|
|||
int bytes;
|
||||
char buff[16 * 1024];
|
||||
|
||||
src_fd = open(src_filename, O_RDONLY);
|
||||
src_fd = open(src_filename, O_RDONLY | O_CLOEXEC);
|
||||
if (src_fd < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : ENOENT;
|
||||
|
|
@ -1490,7 +1492,8 @@ int fc_copy_file(const char *src_filename, const char *dest_filename)
|
|||
return result;
|
||||
}
|
||||
|
||||
dest_fd = open(dest_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
dest_fd = open(dest_filename, O_WRONLY | O_CREAT |
|
||||
O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (dest_fd < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : EIO;
|
||||
|
|
@ -3709,9 +3712,11 @@ int fc_get_first_line(const char *filename, char *buff,
|
|||
|
||||
read_bytes = buff_size - 1;
|
||||
if ((result=getFileContentEx(filename, buff, 0, &read_bytes)) != 0) {
|
||||
line->len = 0;
|
||||
return result;
|
||||
}
|
||||
if (read_bytes == 0) {
|
||||
line->len = 0;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
|
|
@ -3721,6 +3726,7 @@ int fc_get_first_line(const char *filename, char *buff,
|
|||
"file: %s, line no: 1, "
|
||||
"expect new line char \"\\n\"",
|
||||
__LINE__, filename);
|
||||
line->len = 0;
|
||||
return EINVAL;
|
||||
}
|
||||
line->str = buff;
|
||||
|
|
@ -3728,6 +3734,54 @@ int fc_get_first_line(const char *filename, char *buff,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int fc_get_first_lines(const char *filename, char *buff,
|
||||
const int buff_size, string_t *lines, int *count)
|
||||
{
|
||||
int result;
|
||||
int target_count;
|
||||
int64_t read_bytes;
|
||||
char *p;
|
||||
char *end;
|
||||
char *line_end;
|
||||
|
||||
if (*count <= 0) {
|
||||
lines->len = 0;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
read_bytes = buff_size - 1;
|
||||
if ((result=getFileContentEx(filename, buff, 0, &read_bytes)) != 0) {
|
||||
*count = 0;
|
||||
lines->len = 0;
|
||||
return result;
|
||||
}
|
||||
if (read_bytes == 0) {
|
||||
*count = 0;
|
||||
lines->len = 0;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
target_count = *count;
|
||||
*count = 0;
|
||||
p = buff;
|
||||
end = buff + read_bytes;
|
||||
while (p < end) {
|
||||
line_end = (char *)memchr(p, '\n', end - p);
|
||||
if (line_end == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
p = line_end + 1;
|
||||
if (++(*count) == target_count) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
lines->str = buff;
|
||||
lines->len = p - buff;
|
||||
return (*count > 0 ? 0 : ENOENT);
|
||||
}
|
||||
|
||||
int fc_get_last_line(const char *filename, char *buff,
|
||||
const int buff_size, int64_t *file_size, string_t *line)
|
||||
{
|
||||
|
|
@ -3736,10 +3790,12 @@ int fc_get_last_line(const char *filename, char *buff,
|
|||
int result;
|
||||
|
||||
if ((result=getFileSize(filename, file_size)) != 0) {
|
||||
line->len = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (*file_size == 0) {
|
||||
line->len = 0;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
|
|
@ -3752,9 +3808,11 @@ int fc_get_last_line(const char *filename, char *buff,
|
|||
if ((result=getFileContentEx(filename, buff,
|
||||
offset, &read_bytes)) != 0)
|
||||
{
|
||||
line->len = 0;
|
||||
return result;
|
||||
}
|
||||
if (read_bytes == 0) {
|
||||
line->len = 0;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
|
|
@ -3779,16 +3837,19 @@ int fc_get_last_lines(const char *filename, char *buff,
|
|||
int result;
|
||||
|
||||
if (*count <= 0) {
|
||||
lines->len = 0;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if ((result=getFileSize(filename, &file_size)) != 0) {
|
||||
*count = 0;
|
||||
lines->len = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (file_size == 0) {
|
||||
*count = 0;
|
||||
lines->len = 0;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
|
|
@ -3801,10 +3862,13 @@ int fc_get_last_lines(const char *filename, char *buff,
|
|||
if ((result=getFileContentEx(filename, buff,
|
||||
offset, &read_bytes)) != 0)
|
||||
{
|
||||
*count = 0;
|
||||
lines->len = 0;
|
||||
return result;
|
||||
}
|
||||
if (read_bytes == 0) {
|
||||
*count = 0;
|
||||
lines->len = 0;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,10 +41,20 @@
|
|||
normalize_path_ex(from, filename, full_filename, size, \
|
||||
NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS)
|
||||
|
||||
#define FC_SET_CLOEXEC(fd) \
|
||||
if (g_set_cloexec) fd_set_cloexec(fd)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern bool g_set_cloexec;
|
||||
|
||||
static inline void fc_enable_fd_cloexec(const bool cloexec)
|
||||
{
|
||||
g_set_cloexec = cloexec;
|
||||
}
|
||||
|
||||
/** lowercase the string
|
||||
* parameters:
|
||||
* src: input string, will be changed
|
||||
|
|
@ -1146,6 +1156,9 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path);
|
|||
int fc_get_first_line(const char *filename, char *buff,
|
||||
const int buff_size, string_t *line);
|
||||
|
||||
int fc_get_first_lines(const char *filename, char *buff,
|
||||
const int buff_size, string_t *lines, int *count);
|
||||
|
||||
int fc_get_last_line(const char *filename, char *buff,
|
||||
const int buff_size, int64_t *file_size, string_t *line);
|
||||
|
||||
|
|
@ -1198,7 +1211,7 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
|
|||
|
||||
static inline int fc_safe_write_file_open(SafeWriteFileInfo *fi)
|
||||
{
|
||||
const int flags = O_WRONLY | O_CREAT | O_TRUNC;
|
||||
const int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
|
||||
int result;
|
||||
|
||||
if ((fi->fd=open(fi->tmp_filename, flags, 0644)) < 0) {
|
||||
|
|
|
|||
|
|
@ -1012,6 +1012,7 @@ int socketCreateEx2(int af, const char *server_ip,
|
|||
return -1;
|
||||
}
|
||||
|
||||
FC_SET_CLOEXEC(sock);
|
||||
SET_SOCKOPT_NOSIGPIPE(sock);
|
||||
if (flags != 0)
|
||||
{
|
||||
|
|
@ -1402,6 +1403,7 @@ int socketServer2(int af, const char *bind_ipaddr, const int port, int *err_no)
|
|||
return -1;
|
||||
}
|
||||
|
||||
FC_SET_CLOEXEC(sock);
|
||||
SET_SOCKOPT_NOSIGPIPE(sock);
|
||||
|
||||
result = 1;
|
||||
|
|
@ -1476,7 +1478,8 @@ int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \
|
|||
recv_func = tcprecvdata_ex;
|
||||
}
|
||||
|
||||
write_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
write_fd = open(filename, O_WRONLY | O_CREAT |
|
||||
O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (write_fd < 0)
|
||||
{
|
||||
return errno != 0 ? errno : EACCES;
|
||||
|
|
@ -1544,7 +1547,7 @@ int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \
|
|||
break;
|
||||
}
|
||||
|
||||
read_fd = open(filename, O_RDONLY);
|
||||
read_fd = open(filename, O_RDONLY | O_CLOEXEC);
|
||||
if (read_fd < 0)
|
||||
{
|
||||
return errno != 0 ? errno : EACCES;
|
||||
|
|
@ -1627,7 +1630,8 @@ int tcprecvfile_ex(int sock, const char *filename, const int64_t file_bytes, \
|
|||
recv_func = tcprecvdata_ex;
|
||||
}
|
||||
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
fd = open(filename, O_WRONLY | O_CREAT |
|
||||
O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (fd < 0)
|
||||
{
|
||||
return errno != 0 ? errno : EACCES;
|
||||
|
|
@ -1761,7 +1765,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
|
|||
int64_t remain_bytes;
|
||||
#endif
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
fd = open(filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
{
|
||||
*total_send_bytes = 0;
|
||||
|
|
@ -1779,7 +1783,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
|
|||
|
||||
if (flags & O_NONBLOCK)
|
||||
{
|
||||
if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) == -1)
|
||||
if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) < 0)
|
||||
{
|
||||
*total_send_bytes = 0;
|
||||
return errno != 0 ? errno : EACCES;
|
||||
|
|
@ -1873,7 +1877,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
|
|||
|
||||
if (flags & O_NONBLOCK) //restore
|
||||
{
|
||||
if (fcntl(sock, F_SETFL, flags) == -1)
|
||||
if (fcntl(sock, F_SETFL, flags) < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : EACCES;
|
||||
}
|
||||
|
|
@ -2124,7 +2128,7 @@ int tcpsetnonblockopt(int fd)
|
|||
return errno != 0 ? errno : EACCES;
|
||||
}
|
||||
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
|
||||
{
|
||||
logError("file: "__FILE__", line: %d, " \
|
||||
"fcntl failed, errno: %d, error info: %s.", \
|
||||
|
|
|
|||
|
|
@ -843,7 +843,7 @@ int get_device_block_size(const char *device, int *block_size)
|
|||
int fd;
|
||||
size_t bs;
|
||||
|
||||
if ((fd=open(device, O_RDONLY)) < 0) {
|
||||
if ((fd=open(device, O_RDONLY | O_CLOEXEC)) < 0) {
|
||||
result = errno != 0 ? errno : ENOENT;
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"open device %s fail, errno: %d, error info: %s",
|
||||
|
|
@ -885,7 +885,9 @@ static int get_block_size_by_write(const char *path, int *block_size)
|
|||
|
||||
snprintf(tmp_filename, sizeof(tmp_filename),
|
||||
"%s/.blksize-test.tmp", path);
|
||||
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT | O_DIRECT, 0755)) < 0) {
|
||||
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
|
||||
O_DIRECT | O_CLOEXEC, 0755)) < 0)
|
||||
{
|
||||
result = errno != 0 ? errno : ENOENT;
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"open file %s fail, errno: %d, error info: %s",
|
||||
|
|
|
|||
Loading…
Reference in New Issue