Compare commits
1 Commits
8f156869f1
...
a73cb05a5c
| Author | SHA1 | Date |
|---|---|---|
|
|
a73cb05a5c |
|
|
@ -64,7 +64,6 @@ src/tests/test_thread_local
|
||||||
src/tests/test_memcpy
|
src/tests/test_memcpy
|
||||||
src/tests/mblock_benchmark
|
src/tests/mblock_benchmark
|
||||||
src/tests/cpool_benchmark
|
src/tests/cpool_benchmark
|
||||||
src/tests/test_fast_buffer
|
|
||||||
|
|
||||||
# other
|
# other
|
||||||
*.swp
|
*.swp
|
||||||
|
|
|
||||||
4
HISTORY
4
HISTORY
|
|
@ -1,10 +1,8 @@
|
||||||
|
|
||||||
Version 1.78 2025-08-07
|
Version 1.78 2025-08-03
|
||||||
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
||||||
* add files: spinlock.[hc]
|
* add files: spinlock.[hc]
|
||||||
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
|
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
|
||||||
* shared_func.[hc]: add functions short2hex, int2hex, long2hex etc.
|
|
||||||
* performance opt.: replace sprintf and snprintf as necessary
|
|
||||||
|
|
||||||
Version 1.77 2025-03-18
|
Version 1.77 2025-03-18
|
||||||
* impl. shorten_path for /./ and /../
|
* impl. shorten_path for /./ and /../
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx,
|
||||||
start = end;
|
start = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_combine_two_string(name_prefix, "array", '-', name);
|
snprintf(name, sizeof(name), "%s-array", name_prefix);
|
||||||
return fast_allocator_init_ex(&ctx->allocator, name,
|
return fast_allocator_init_ex(&ctx->allocator, name,
|
||||||
obj_size, NULL, regions, region - regions, 0,
|
obj_size, NULL, regions, region - regions, 0,
|
||||||
0.9999, reclaim_interval, need_lock);
|
0.9999, reclaim_interval, need_lock);
|
||||||
|
|
|
||||||
11
src/base64.c
11
src/base64.c
|
|
@ -24,7 +24,6 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "fc_memory.h"
|
#include "fc_memory.h"
|
||||||
#include "shared_func.h"
|
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -54,11 +53,15 @@ void base64_set_line_length(struct base64_context *context, const int length)
|
||||||
* Usually contains only a combination of chars \n and \r.
|
* Usually contains only a combination of chars \n and \r.
|
||||||
* Could be any chars not in set A-Z a-z 0-9 + /.
|
* Could be any chars not in set A-Z a-z 0-9 + /.
|
||||||
*/
|
*/
|
||||||
void base64_set_line_separator(struct base64_context *context,
|
void base64_set_line_separator(struct base64_context *context, \
|
||||||
const char *pLineSeparator)
|
const char *pLineSeparator)
|
||||||
{
|
{
|
||||||
context->line_sep_len = fc_safe_strcpy(context->
|
context->line_sep_len = snprintf(context->line_separator, \
|
||||||
line_separator, pLineSeparator);
|
sizeof(context->line_separator), "%s", pLineSeparator);
|
||||||
|
if (context->line_sep_len >= sizeof(context->line_separator))
|
||||||
|
{
|
||||||
|
context->line_sep_len = sizeof(context->line_separator) - 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void base64_init_ex(struct base64_context *context, const int nLineLength, \
|
void base64_init_ex(struct base64_context *context, const int nLineLength, \
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer,
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_safe_strcpy(writer->filename, filename);
|
snprintf(writer->filename, sizeof(writer->filename), "%s", filename);
|
||||||
writer->fd = open(writer->filename, O_WRONLY |
|
writer->fd = open(writer->filename, O_WRONLY |
|
||||||
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
||||||
if (writer->fd < 0)
|
if (writer->fd < 0)
|
||||||
|
|
|
||||||
|
|
@ -540,8 +540,6 @@ static inline int fc_fallocate(int fd, const int64_t size)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define FC_MACRO_STRINGIFY(x) #x
|
|
||||||
#define FC_MACRO_TOSTRING(x) FC_MACRO_STRINGIFY(x)
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -462,7 +462,8 @@ int conn_pool_parse_server_info(const char *pServerStr,
|
||||||
static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo,
|
static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo,
|
||||||
const char *ip_addr, const int port)
|
const char *ip_addr, const int port)
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(pServerInfo->ip_addr, ip_addr);
|
snprintf(pServerInfo->ip_addr, sizeof(pServerInfo->ip_addr),
|
||||||
|
"%s", ip_addr);
|
||||||
pServerInfo->port = port;
|
pServerInfo->port = port;
|
||||||
pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET;
|
pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET;
|
||||||
pServerInfo->sock = -1;
|
pServerInfo->sock = -1;
|
||||||
|
|
|
||||||
|
|
@ -26,28 +26,23 @@
|
||||||
#include "fc_memory.h"
|
#include "fc_memory.h"
|
||||||
#include "fast_buffer.h"
|
#include "fast_buffer.h"
|
||||||
|
|
||||||
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity,
|
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity)
|
||||||
const bool binary_mode, const bool check_capacity)
|
|
||||||
{
|
{
|
||||||
buffer->length = 0;
|
buffer->length = 0;
|
||||||
buffer->binary_mode = binary_mode;
|
|
||||||
if (init_capacity > 0)
|
if (init_capacity > 0)
|
||||||
{
|
{
|
||||||
buffer->alloc_size = init_capacity;
|
buffer->alloc_size = init_capacity;
|
||||||
buffer->check_capacity = check_capacity;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
buffer->alloc_size = 256;
|
buffer->alloc_size = 256;
|
||||||
buffer->check_capacity = true;
|
|
||||||
}
|
}
|
||||||
buffer->data = (char *)fc_malloc(buffer->alloc_size);
|
buffer->data = (char *)fc_malloc(buffer->alloc_size);
|
||||||
if (buffer->data == NULL)
|
if (buffer->data == NULL)
|
||||||
{
|
{
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
*(buffer->data) = '\0';
|
||||||
fast_buffer_set_null_terminator(buffer);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -93,10 +88,8 @@ int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity)
|
||||||
|
|
||||||
if (buffer->length > 0) {
|
if (buffer->length > 0) {
|
||||||
memcpy(buff, buffer->data, buffer->length);
|
memcpy(buff, buffer->data, buffer->length);
|
||||||
if (!buffer->binary_mode) {
|
|
||||||
*(buff + buffer->length) = '\0';
|
*(buff + buffer->length) = '\0';
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
free(buffer->data);
|
free(buffer->data);
|
||||||
buffer->data = buff;
|
buffer->data = buff;
|
||||||
|
|
@ -134,7 +127,7 @@ int fast_buffer_append(FastBuffer *buffer, const char *format, ...)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fast_buffer_set_null_terminator(buffer); //restore
|
*(buffer->data + buffer->length) = '\0'; //restore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -155,7 +148,7 @@ int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len)
|
||||||
|
|
||||||
memcpy(buffer->data + buffer->length, data, len);
|
memcpy(buffer->data + buffer->length, data, len);
|
||||||
buffer->length += len;
|
buffer->length += len;
|
||||||
fast_buffer_set_null_terminator(buffer);
|
*(buffer->data + buffer->length) = '\0';
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,6 +171,32 @@ int fast_buffer_append_binary(FastBuffer *buffer,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fast_buffer_append_int(FastBuffer *buffer, const int n)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
|
||||||
|
if ((result=fast_buffer_check(buffer, 16)) != 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer->length += sprintf(buffer->data + buffer->length, "%d", n);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
|
||||||
|
if ((result=fast_buffer_check(buffer, 32)) != 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer->length += sprintf(buffer->data + buffer->length, "%"PRId64, n);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int fast_buffer_append_file(FastBuffer *buffer, const char *filename)
|
int fast_buffer_append_file(FastBuffer *buffer, const char *filename)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,12 @@
|
||||||
#define __FAST_BUFFER_H__
|
#define __FAST_BUFFER_H__
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "shared_func.h"
|
#include "common_define.h"
|
||||||
|
|
||||||
typedef struct fast_buffer {
|
typedef struct fast_buffer {
|
||||||
char *data;
|
char *data;
|
||||||
int alloc_size;
|
int alloc_size;
|
||||||
int length;
|
int length;
|
||||||
bool binary_mode;
|
|
||||||
bool check_capacity;
|
|
||||||
} FastBuffer;
|
} FastBuffer;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
@ -41,39 +39,26 @@ static inline char *fast_buffer_data(FastBuffer *buffer)
|
||||||
return buffer->data;
|
return buffer->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity,
|
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity);
|
||||||
const bool binary_mode, const bool check_capacity);
|
|
||||||
|
|
||||||
static inline int fast_buffer_init1(FastBuffer *buffer, const int init_capacity)
|
|
||||||
{
|
|
||||||
const bool binary_mode = false;
|
|
||||||
const bool check_capacity = true;
|
|
||||||
return fast_buffer_init_ex(buffer, init_capacity,
|
|
||||||
binary_mode, check_capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int fast_buffer_init(FastBuffer *buffer)
|
static inline int fast_buffer_init(FastBuffer *buffer)
|
||||||
{
|
{
|
||||||
const int init_capacity = 0;
|
return fast_buffer_init_ex(buffer, 0);
|
||||||
return fast_buffer_init1(buffer, init_capacity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define fast_buffer_set_null_terminator(buffer) \
|
|
||||||
if (!buffer->binary_mode) *(buffer->data + buffer->length) = '\0'
|
|
||||||
|
|
||||||
#define fast_buffer_check(buffer, inc_len) \
|
|
||||||
((buffer)->check_capacity ? fast_buffer_check_inc_size(buffer, inc_len) : 0)
|
|
||||||
|
|
||||||
#define fast_buffer_clear(buffer) fast_buffer_reset(buffer)
|
#define fast_buffer_clear(buffer) fast_buffer_reset(buffer)
|
||||||
|
|
||||||
static inline void fast_buffer_reset(FastBuffer *buffer)
|
static inline void fast_buffer_reset(FastBuffer *buffer)
|
||||||
{
|
{
|
||||||
buffer->length = 0;
|
buffer->length = 0;
|
||||||
fast_buffer_set_null_terminator(buffer);
|
*buffer->data = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
void fast_buffer_destroy(FastBuffer *buffer);
|
void fast_buffer_destroy(FastBuffer *buffer);
|
||||||
|
|
||||||
|
#define fast_buffer_check(buffer, inc_len) \
|
||||||
|
fast_buffer_check_inc_size(buffer, inc_len)
|
||||||
|
|
||||||
int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity);
|
int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity);
|
||||||
|
|
||||||
static inline int fast_buffer_check_capacity(FastBuffer *buffer,
|
static inline int fast_buffer_check_capacity(FastBuffer *buffer,
|
||||||
|
|
@ -101,47 +86,9 @@ int fast_buffer_append_buff(FastBuffer *buffer,
|
||||||
int fast_buffer_append_binary(FastBuffer *buffer,
|
int fast_buffer_append_binary(FastBuffer *buffer,
|
||||||
const void *data, const int len);
|
const void *data, const int len);
|
||||||
|
|
||||||
static inline int fast_buffer_append_char(FastBuffer *buffer, const char ch)
|
int fast_buffer_append_int(FastBuffer *buffer, const int n);
|
||||||
{
|
|
||||||
int result;
|
|
||||||
|
|
||||||
if ((result=fast_buffer_check(buffer, 1)) != 0)
|
int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n);
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
*(buffer->data + buffer->length++) = ch;
|
|
||||||
fast_buffer_set_null_terminator(buffer);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int fast_buffer_append_int(FastBuffer *buffer, const int n)
|
|
||||||
{
|
|
||||||
int result;
|
|
||||||
|
|
||||||
if ((result=fast_buffer_check(buffer, 16)) != 0)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer->length += fc_itoa(n, buffer->data + buffer->length);
|
|
||||||
fast_buffer_set_null_terminator(buffer);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
|
|
||||||
{
|
|
||||||
int result;
|
|
||||||
|
|
||||||
if ((result=fast_buffer_check(buffer, 32)) != 0)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer->length += fc_itoa(n, buffer->data + buffer->length);
|
|
||||||
fast_buffer_set_null_terminator(buffer);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fast_buffer_append_file(FastBuffer *buffer, const char *filename);
|
int fast_buffer_append_file(FastBuffer *buffer, const char *filename);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -584,7 +584,7 @@ int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
|
||||||
|
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(mblock->info.name, name);
|
snprintf(mblock->info.name, sizeof(mblock->info.name), "%s", name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ int free_queue_init_ex2(struct fast_task_queue *queue, const char *name,
|
||||||
(int64_t)max_data_size);
|
(int64_t)max_data_size);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fc_combine_two_string(name, "task", '-', aname);
|
snprintf(aname, sizeof(aname), "%s-task", name);
|
||||||
return fast_mblock_init_ex1(&queue->allocator, aname,
|
return fast_mblock_init_ex1(&queue->allocator, aname,
|
||||||
queue->block_size, alloc_once, max_connections,
|
queue->block_size, alloc_once, max_connections,
|
||||||
(fast_mblock_object_init_func)task_alloc_init,
|
(fast_mblock_object_init_func)task_alloc_init,
|
||||||
|
|
|
||||||
|
|
@ -777,13 +777,14 @@ int64_t fc_hash_inc_value(const HashData *old_data, const int inc,
|
||||||
{
|
{
|
||||||
n = inc;
|
n = inc;
|
||||||
}
|
}
|
||||||
|
*new_value_len = sprintf(new_value, "%"PRId64, n);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
n = inc;
|
n = inc;
|
||||||
|
*new_value_len = sprintf(new_value, "%"PRId64, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
*new_value_len = fc_itoa(n, new_value);
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,7 @@ static int iniAnnotationFuncLocalIpGet(IniContext *context,
|
||||||
(int)(square_start - param), param);
|
(int)(square_start - param), param);
|
||||||
index = atoi(square_start + 1);
|
index = atoi(square_start + 1);
|
||||||
} else {
|
} else {
|
||||||
fc_safe_strcpy(name_part, param);
|
snprintf(name_part, sizeof(name_part) - 1, "%s", param);
|
||||||
index = -2;
|
index = -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -322,7 +322,7 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"NO set directives before, set value to %s",
|
"NO set directives before, set value to %s",
|
||||||
__LINE__, param);
|
__LINE__, param);
|
||||||
fc_strlcpy(output, param, FAST_INI_ITEM_VALUE_SIZE);
|
snprintf(output, FAST_INI_ITEM_VALUE_SIZE, "%s", param);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -652,7 +652,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||||
if (IS_URL_RESOURCE(szFilename))
|
if (IS_URL_RESOURCE(szFilename))
|
||||||
{
|
{
|
||||||
*pContext->config_path = '\0';
|
*pContext->config_path = '\0';
|
||||||
fc_safe_strcpy(full_filename, szFilename);
|
snprintf(full_filename, sizeof(full_filename), "%s", szFilename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -675,7 +675,8 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||||
|
|
||||||
memcpy(pContext->config_path, szFilename, len);
|
memcpy(pContext->config_path, szFilename, len);
|
||||||
*(pContext->config_path + len) = '\0';
|
*(pContext->config_path + len) = '\0';
|
||||||
fc_safe_strcpy(full_filename, szFilename);
|
snprintf(full_filename, sizeof(full_filename), \
|
||||||
|
"%s", szFilename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -698,8 +699,9 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||||
*(pContext->config_path + len) = '\0';
|
*(pContext->config_path + len) = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_combine_full_filename(pContext->config_path,
|
snprintf(full_filename, sizeof(full_filename), \
|
||||||
szFilename, full_filename);
|
"%s/%s", pContext->config_path, szFilename);
|
||||||
|
|
||||||
pLast = strrchr(szFilename, '/');
|
pLast = strrchr(szFilename, '/');
|
||||||
if (pLast != NULL)
|
if (pLast != NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -912,7 +914,7 @@ static int iniAddAnnotation(char *params)
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_combine_two_string(func_name, "init_annotation", '_', symbol);
|
snprintf(symbol, sizeof(symbol), "%s_init_annotation", func_name);
|
||||||
init_func = dlsym(dlhandle, symbol);
|
init_func = dlsym(dlhandle, symbol);
|
||||||
if (init_func == NULL)
|
if (init_func == NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -1011,11 +1013,13 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
|
||||||
strncasecmp(pLine+1, "include", 7) == 0 && \
|
strncasecmp(pLine+1, "include", 7) == 0 && \
|
||||||
(*(pLine+8) == ' ' || *(pLine+8) == '\t'))
|
(*(pLine+8) == ' ' || *(pLine+8) == '\t'))
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(pIncludeFilename, pLine + 9);
|
snprintf(pIncludeFilename, sizeof(pIncludeFilename),
|
||||||
|
"%s", pLine + 9);
|
||||||
fc_trim(pIncludeFilename);
|
fc_trim(pIncludeFilename);
|
||||||
if (IS_URL_RESOURCE(pIncludeFilename))
|
if (IS_URL_RESOURCE(pIncludeFilename))
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(full_filename, pIncludeFilename);
|
snprintf(full_filename, sizeof(full_filename),
|
||||||
|
"%s", pIncludeFilename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -1030,19 +1034,21 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
|
||||||
|
|
||||||
if (*pTrueFilename == '/')
|
if (*pTrueFilename == '/')
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(full_filename, pTrueFilename);
|
snprintf(full_filename, sizeof(full_filename), \
|
||||||
|
"%s", pTrueFilename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_combine_full_filename(pContext->config_path,
|
snprintf(full_filename, sizeof(full_filename), \
|
||||||
pTrueFilename, full_filename);
|
"%s/%s", pContext->config_path, \
|
||||||
|
pTrueFilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fileExists(full_filename))
|
if (!fileExists(full_filename))
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, " \
|
||||||
"include file \"%s\" not exists, "
|
"include file \"%s\" not exists, " \
|
||||||
"line: \"%s\"", __LINE__,
|
"line: \"%s\"", __LINE__, \
|
||||||
pTrueFilename, pLine);
|
pTrueFilename, pLine);
|
||||||
result = ENOENT;
|
result = ENOENT;
|
||||||
break;
|
break;
|
||||||
|
|
@ -2623,7 +2629,8 @@ static char *iniProccessFor(char *content, const int content_len,
|
||||||
char *pRemain;
|
char *pRemain;
|
||||||
int remainLen;
|
int remainLen;
|
||||||
|
|
||||||
valueLen = fc_itoa(i, value);
|
valueLen = sprintf(value, "%d", i);
|
||||||
|
|
||||||
pRemain = pForBlock;
|
pRemain = pForBlock;
|
||||||
remainLen = forBlockLen;
|
remainLen = forBlockLen;
|
||||||
while (remainLen >= tagLen)
|
while (remainLen >= tagLen)
|
||||||
|
|
@ -2810,7 +2817,7 @@ do { \
|
||||||
break; \
|
break; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
fc_safe_strcpy(targetItem.name, szItemName); \
|
snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \
|
||||||
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
|
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
|
||||||
pSection->count, sizeof(IniItem), iniCompareByItemName); \
|
pSection->count, sizeof(IniItem), iniCompareByItemName); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
|
||||||
55
src/logger.c
55
src/logger.c
|
|
@ -50,16 +50,16 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock);
|
||||||
|
|
||||||
static int check_and_mk_log_dir(const char *base_path)
|
static int check_and_mk_log_dir(const char *base_path)
|
||||||
{
|
{
|
||||||
char log_path[MAX_PATH_SIZE];
|
char data_path[MAX_PATH_SIZE];
|
||||||
|
|
||||||
fc_combine_full_filename(base_path, "logs", log_path);
|
snprintf(data_path, sizeof(data_path), "%s/logs", base_path);
|
||||||
if (!fileExists(log_path))
|
if (!fileExists(data_path))
|
||||||
{
|
{
|
||||||
if (mkdir(log_path, 0755) != 0)
|
if (mkdir(data_path, 0755) != 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "mkdir \"%s\" fail, "
|
fprintf(stderr, "mkdir \"%s\" fail, " \
|
||||||
"errno: %d, error info: %s\n",
|
"errno: %d, error info: %s\n", \
|
||||||
log_path, errno, STRERROR(errno));
|
data_path, errno, STRERROR(errno));
|
||||||
return errno != 0 ? errno : EPERM;
|
return errno != 0 ? errno : EPERM;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -249,7 +249,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
|
||||||
|
|
||||||
if (*(pContext->log_filename) == '\0')
|
if (*(pContext->log_filename) == '\0')
|
||||||
{
|
{
|
||||||
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
|
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
|
||||||
return log_open(pContext);
|
return log_open(pContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,7 +258,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
|
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
|
||||||
return log_reopen_ex(pContext);
|
return log_reopen_ex(pContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -385,11 +385,12 @@ static int log_delete_old_file(LogContext *pContext,
|
||||||
char full_filename[MAX_PATH_SIZE + 128];
|
char full_filename[MAX_PATH_SIZE + 128];
|
||||||
if (NEED_COMPRESS_LOG(pContext->compress_log_flags))
|
if (NEED_COMPRESS_LOG(pContext->compress_log_flags))
|
||||||
{
|
{
|
||||||
fc_concat_two_string(old_filename, GZIP_EXT_NAME_STR, full_filename);
|
snprintf(full_filename, sizeof(full_filename), "%s%s",
|
||||||
|
old_filename, GZIP_EXT_NAME_STR);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(full_filename, old_filename);
|
snprintf(full_filename, sizeof(full_filename), "%s", old_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlink(full_filename) != 0)
|
if (unlink(full_filename) != 0)
|
||||||
|
|
@ -589,9 +590,7 @@ static int log_get_matched_files(LogContext *pContext,
|
||||||
the_time = get_current_time() - days_before * 86400;
|
the_time = get_current_time() - days_before * 86400;
|
||||||
localtime_r(&the_time, &tm);
|
localtime_r(&the_time, &tm);
|
||||||
memset(filename_prefix, 0, sizeof(filename_prefix));
|
memset(filename_prefix, 0, sizeof(filename_prefix));
|
||||||
len = strlen(log_filename);
|
len = sprintf(filename_prefix, "%s.", log_filename);
|
||||||
memcpy(filename_prefix, log_filename, len);
|
|
||||||
*(filename_prefix + len++) = '.';
|
|
||||||
strftime(filename_prefix + len, sizeof(filename_prefix) - len,
|
strftime(filename_prefix + len, sizeof(filename_prefix) - len,
|
||||||
rotate_time_format_prefix, &tm);
|
rotate_time_format_prefix, &tm);
|
||||||
prefix_filename_len = strlen(filename_prefix);
|
prefix_filename_len = strlen(filename_prefix);
|
||||||
|
|
@ -652,8 +651,8 @@ static int log_delete_matched_old_files(LogContext *pContext,
|
||||||
log_get_file_path(pContext, log_filepath);
|
log_get_file_path(pContext, log_filepath);
|
||||||
for (i=0; i<filename_array.count; i++)
|
for (i=0; i<filename_array.count; i++)
|
||||||
{
|
{
|
||||||
fc_concat_two_string(log_filepath, filename_array.
|
snprintf(full_filename, sizeof(full_filename), "%s%s",
|
||||||
filenames[i], full_filename);
|
log_filepath, filename_array.filenames[i]);
|
||||||
if (unlink(full_filename) != 0)
|
if (unlink(full_filename) != 0)
|
||||||
{
|
{
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT)
|
||||||
|
|
@ -704,9 +703,7 @@ int log_delete_old_files(void *args)
|
||||||
the_time -= 86400;
|
the_time -= 86400;
|
||||||
localtime_r(&the_time, &tm);
|
localtime_r(&the_time, &tm);
|
||||||
memset(old_filename, 0, sizeof(old_filename));
|
memset(old_filename, 0, sizeof(old_filename));
|
||||||
len = strlen(pContext->log_filename);
|
len = sprintf(old_filename, "%s.", pContext->log_filename);
|
||||||
memcpy(old_filename, pContext->log_filename, len);
|
|
||||||
*(old_filename + len++) = '.';
|
|
||||||
strftime(old_filename + len, sizeof(old_filename) - len,
|
strftime(old_filename + len, sizeof(old_filename) - len,
|
||||||
pContext->rotate_time_format, &tm);
|
pContext->rotate_time_format, &tm);
|
||||||
if ((result=log_delete_old_file(pContext, old_filename)) != 0)
|
if ((result=log_delete_old_file(pContext, old_filename)) != 0)
|
||||||
|
|
@ -736,7 +733,6 @@ static void *log_gzip_func(void *args)
|
||||||
char log_filepath[MAX_PATH_SIZE];
|
char log_filepath[MAX_PATH_SIZE];
|
||||||
char full_filename[MAX_PATH_SIZE + 32];
|
char full_filename[MAX_PATH_SIZE + 32];
|
||||||
char output[512];
|
char output[512];
|
||||||
const char *gzip_cmd_filename;
|
|
||||||
int prefix_len;
|
int prefix_len;
|
||||||
int result;
|
int result;
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -764,10 +760,11 @@ static void *log_gzip_func(void *args)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
gzip_cmd_filename = get_gzip_command_filename();
|
snprintf(full_filename, sizeof(full_filename), "%s%s",
|
||||||
fc_concat_two_string(log_filepath, filename_array.
|
log_filepath, filename_array.filenames[i]);
|
||||||
filenames[i], full_filename);
|
snprintf(cmd, sizeof(cmd), "%s %s",
|
||||||
fc_combine_two_string(gzip_cmd_filename, full_filename, ' ', cmd);
|
get_gzip_command_filename(), full_filename);
|
||||||
|
|
||||||
result = getExecResult(cmd, output, sizeof(output));
|
result = getExecResult(cmd, output, sizeof(output));
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
|
|
@ -846,9 +843,7 @@ int log_rotate(LogContext *pContext)
|
||||||
localtime_r(¤t_time, &tm);
|
localtime_r(¤t_time, &tm);
|
||||||
|
|
||||||
memset(old_filename, 0, sizeof(old_filename));
|
memset(old_filename, 0, sizeof(old_filename));
|
||||||
len = strlen(pContext->log_filename);
|
len = sprintf(old_filename, "%s.", pContext->log_filename);
|
||||||
memcpy(old_filename, pContext->log_filename, len);
|
|
||||||
*(old_filename + len++) = '.';
|
|
||||||
strftime(old_filename + len, sizeof(old_filename) - len,
|
strftime(old_filename + len, sizeof(old_filename) - len,
|
||||||
pContext->rotate_time_format, &tm);
|
pContext->rotate_time_format, &tm);
|
||||||
if (access(old_filename, F_OK) == 0)
|
if (access(old_filename, F_OK) == 0)
|
||||||
|
|
@ -1051,12 +1046,8 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
|
||||||
|
|
||||||
if (caption != NULL)
|
if (caption != NULL)
|
||||||
{
|
{
|
||||||
buff_len = strlen(caption);
|
buff_len = sprintf(pContext->pcurrent_buff, "%s - ", caption);
|
||||||
memcpy(pContext->pcurrent_buff, caption, buff_len);
|
|
||||||
pContext->pcurrent_buff += buff_len;
|
pContext->pcurrent_buff += buff_len;
|
||||||
*pContext->pcurrent_buff++ = ' ';
|
|
||||||
*pContext->pcurrent_buff++ = '-';
|
|
||||||
*pContext->pcurrent_buff++ = ' ';
|
|
||||||
}
|
}
|
||||||
memcpy(pContext->pcurrent_buff, text, text_len);
|
memcpy(pContext->pcurrent_buff, text, text_len);
|
||||||
pContext->pcurrent_buff += text_len;
|
pContext->pcurrent_buff += text_len;
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ int fast_multi_sock_client_init_ex(FastMultiSockClient *client,
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<entry_count; i++) {
|
for (i=0; i<entry_count; i++) {
|
||||||
if ((result=fast_buffer_init1(&entries[i].recv_buffer,
|
if ((result=fast_buffer_init_ex(&entries[i].recv_buffer,
|
||||||
new_init_recv_buffer_size)) != 0)
|
new_init_recv_buffer_size)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ int write_to_pid_file(const char *pidFilename)
|
||||||
char buff[32];
|
char buff[32];
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
len = fc_itoa(getpid(), buff);
|
len = sprintf(buff, "%d", (int)getpid());
|
||||||
return writeToFile(pidFilename, buff, len);
|
return writeToFile(pidFilename, buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -294,18 +294,15 @@ FCServerInfo *fc_server_get_by_ip_port_ex(FCServerConfig *ctx,
|
||||||
static inline void fc_server_set_group_ptr_name(FCServerGroupInfo *ginfo,
|
static inline void fc_server_set_group_ptr_name(FCServerGroupInfo *ginfo,
|
||||||
const char *group_name)
|
const char *group_name)
|
||||||
{
|
{
|
||||||
int len;
|
|
||||||
|
|
||||||
ginfo->group_name.str = ginfo->name_buff;
|
ginfo->group_name.str = ginfo->name_buff;
|
||||||
len = strlen(group_name);
|
ginfo->group_name.len = snprintf(ginfo->name_buff,
|
||||||
if (len >= sizeof(ginfo->name_buff)) {
|
sizeof(ginfo->name_buff) - 1, "%s", group_name);
|
||||||
len = sizeof(ginfo->name_buff) - 1;
|
if (ginfo->group_name.len == 0) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(ginfo->name_buff, group_name, len);
|
fc_trim(ginfo->group_name.str);
|
||||||
*(ginfo->name_buff + len) = '\0';
|
ginfo->group_name.len = strlen(ginfo->group_name.str);
|
||||||
fc_trim(ginfo->name_buff);
|
|
||||||
ginfo->group_name.len = strlen(ginfo->name_buff);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
|
static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
|
||||||
|
|
@ -313,8 +310,8 @@ static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
|
||||||
{
|
{
|
||||||
ginfo->filter.ip_prefix.str = ginfo->filter.prefix_buff;
|
ginfo->filter.ip_prefix.str = ginfo->filter.prefix_buff;
|
||||||
if (ip_prefix != NULL) {
|
if (ip_prefix != NULL) {
|
||||||
ginfo->filter.ip_prefix.len = fc_safe_strcpy(
|
ginfo->filter.ip_prefix.len = snprintf(ginfo->filter.prefix_buff,
|
||||||
ginfo->filter.prefix_buff, ip_prefix);
|
sizeof(ginfo->filter.prefix_buff) - 1, "%s", ip_prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,6 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool g_set_cloexec = false;
|
bool g_set_cloexec = false;
|
||||||
const char *g_lower_hex_chars = "0123456789abcdef";
|
|
||||||
const char *g_upper_hex_chars = "0123456789ABCDEF";
|
|
||||||
|
|
||||||
char *formatDatetime(const time_t nTime, \
|
char *formatDatetime(const time_t nTime, \
|
||||||
const char *szDateFormat, \
|
const char *szDateFormat, \
|
||||||
|
|
@ -147,7 +145,7 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
|
||||||
|
|
||||||
if (szPath[0] == '/')
|
if (szPath[0] == '/')
|
||||||
{
|
{
|
||||||
fc_strlcpy(szAbsPath, szPath, pathSize);
|
snprintf(szAbsPath, pathSize, "%s", szPath);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -167,12 +165,11 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
|
||||||
|
|
||||||
if (szPath[0] != '\0')
|
if (szPath[0] != '\0')
|
||||||
{
|
{
|
||||||
fc_get_full_filepath_ex(cwd, strlen(cwd), szPath,
|
snprintf(szAbsPath, pathSize, "%s/%s", cwd, szPath);
|
||||||
strlen(szPath), szAbsPath, pathSize);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_strlcpy(szAbsPath, cwd, pathSize);
|
snprintf(szAbsPath, pathSize, "%s", cwd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,7 +205,8 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
filename = exeFilename;
|
filename = exeFilename;
|
||||||
for (i=0; i<3; i++)
|
for (i=0; i<3; i++)
|
||||||
{
|
{
|
||||||
fc_combine_full_filename(search_paths[i], filename, cwd);
|
snprintf(cwd, sizeof(cwd), "%s/%s", \
|
||||||
|
search_paths[i], filename);
|
||||||
if (fileExists(cwd))
|
if (fileExists(cwd))
|
||||||
{
|
{
|
||||||
strcpy(szPath, search_paths[i]);
|
strcpy(szPath, search_paths[i]);
|
||||||
|
|
@ -228,8 +226,8 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
|
snprintf(szAbsFilename, maxSize, "%s/%s", \
|
||||||
strlen(filename), szAbsFilename, maxSize);
|
szPath, filename);
|
||||||
return szAbsFilename;
|
return szAbsFilename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -243,8 +241,7 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
|
|
||||||
if (*szPath == '/')
|
if (*szPath == '/')
|
||||||
{
|
{
|
||||||
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
|
snprintf(szAbsFilename, maxSize, "%s/%s", szPath, filename);
|
||||||
strlen(filename), szAbsFilename, maxSize);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -264,13 +261,13 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
|
|
||||||
if (*szPath != '\0')
|
if (*szPath != '\0')
|
||||||
{
|
{
|
||||||
snprintf(szAbsFilename, maxSize, "%s/%s/%s",
|
snprintf(szAbsFilename, maxSize, "%s/%s/%s", \
|
||||||
cwd, szPath, filename);
|
cwd, szPath, filename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_get_full_filename_ex(cwd, strlen(cwd), filename,
|
snprintf(szAbsFilename, maxSize, "%s/%s", \
|
||||||
strlen(filename), szAbsFilename, maxSize);
|
cwd, filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -330,7 +327,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_combine_full_filepath(path, dirp->d_name, fullpath);
|
sprintf(fullpath, "%s/%s", path, dirp->d_name);
|
||||||
memset(&statbuf, 0, sizeof(statbuf));
|
memset(&statbuf, 0, sizeof(statbuf));
|
||||||
if (lstat(fullpath, &statbuf) < 0)
|
if (lstat(fullpath, &statbuf) < 0)
|
||||||
{
|
{
|
||||||
|
|
@ -339,7 +336,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
|
||||||
|
|
||||||
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
|
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
|
||||||
{
|
{
|
||||||
fc_combine_full_filename(fullpath, "cmdline", filepath);
|
sprintf(filepath, "%s/cmdline", fullpath);
|
||||||
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
|
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -360,11 +357,11 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
|
||||||
ptr = strrchr(buf, '/');
|
ptr = strrchr(buf, '/');
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(procname, buf);
|
snprintf(procname, 64, "%s", buf);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(procname, ptr + 1);
|
snprintf(procname, 64, "%s", ptr + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(procname, pTargetProg) == 0)
|
if (strcmp(procname, pTargetProg) == 0)
|
||||||
|
|
@ -533,17 +530,16 @@ char *bin2hex(const char *s, const int len, char *szHexBuff)
|
||||||
{
|
{
|
||||||
unsigned char *p;
|
unsigned char *p;
|
||||||
unsigned char *pEnd;
|
unsigned char *pEnd;
|
||||||
char *dest;
|
int nLen;
|
||||||
|
|
||||||
dest = szHexBuff;
|
nLen = 0;
|
||||||
pEnd = (unsigned char *)s + len;
|
pEnd = (unsigned char *)s + len;
|
||||||
for (p=(unsigned char *)s; p<pEnd; p++)
|
for (p=(unsigned char *)s; p<pEnd; p++)
|
||||||
{
|
{
|
||||||
*dest++ = g_lower_hex_chars[*p >> 4];
|
nLen += sprintf(szHexBuff + nLen, "%02x", *p);
|
||||||
*dest++ = g_lower_hex_chars[*p & 0x0F];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*dest = '\0';
|
szHexBuff[nLen] = '\0';
|
||||||
return szHexBuff;
|
return szHexBuff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1454,13 +1450,13 @@ int writeToFile(const char *filename, const char *buff, const int file_size)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int safeWriteToFile(const char *filename, const char *buff,
|
int safeWriteToFile(const char *filename, const char *buff, \
|
||||||
const int file_size)
|
const int file_size)
|
||||||
{
|
{
|
||||||
char tmpFilename[PATH_MAX];
|
char tmpFilename[PATH_MAX];
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
fc_combine_two_string(filename, "tmp", '.', tmpFilename);
|
snprintf(tmpFilename, sizeof(tmpFilename), "%s.tmp", filename);
|
||||||
if ((result=writeToFile(tmpFilename, buff, file_size)) != 0)
|
if ((result=writeToFile(tmpFilename, buff, file_size)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -1557,11 +1553,13 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path)
|
||||||
fname = strrchr(src_filename, '/');
|
fname = strrchr(src_filename, '/');
|
||||||
if (fname == NULL)
|
if (fname == NULL)
|
||||||
{
|
{
|
||||||
fc_combine_full_filename(dest_path, src_filename, dest_filename);
|
snprintf(dest_filename, sizeof(dest_filename),
|
||||||
|
"%s/%s", dest_path, src_filename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_combine_full_filename(dest_path, fname, dest_filename);
|
snprintf(dest_filename, sizeof(dest_filename),
|
||||||
|
"%s%s", dest_path, fname);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fc_copy_file(src_filename, dest_filename);
|
return fc_copy_file(src_filename, dest_filename);
|
||||||
|
|
@ -2568,6 +2566,7 @@ int get_time_item_from_str(const char *pValue, const char *item_name,
|
||||||
|
|
||||||
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
|
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
|
||||||
{
|
{
|
||||||
|
static unsigned char hex_chars[] = "0123456789ABCDEF";
|
||||||
const unsigned char *pSrc;
|
const unsigned char *pSrc;
|
||||||
const unsigned char *pEnd;
|
const unsigned char *pEnd;
|
||||||
char *pDest;
|
char *pDest;
|
||||||
|
|
@ -2590,8 +2589,8 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*pDest++ = '%';
|
*pDest++ = '%';
|
||||||
*pDest++ = g_upper_hex_chars[(*pSrc) >> 4];
|
*pDest++ = hex_chars[(*pSrc) >> 4];
|
||||||
*pDest++ = g_upper_hex_chars[(*pSrc) & 0x0F];
|
*pDest++ = hex_chars[(*pSrc) & 0x0F];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3232,7 +3231,7 @@ static void add_thousands_separator(char *str, const int len)
|
||||||
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
len = fc_ltostr(n, buff);
|
len = sprintf(buff, "%d", n);
|
||||||
if (thousands_separator)
|
if (thousands_separator)
|
||||||
{
|
{
|
||||||
add_thousands_separator(buff, len);
|
add_thousands_separator(buff, len);
|
||||||
|
|
@ -3243,7 +3242,7 @@ const char *int2str(const int n, char *buff, const bool thousands_separator)
|
||||||
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
|
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
len = fc_ltostr(n, buff);
|
len = sprintf(buff, "%"PRId64, n);
|
||||||
if (thousands_separator)
|
if (thousands_separator)
|
||||||
{
|
{
|
||||||
add_thousands_separator(buff, len);
|
add_thousands_separator(buff, len);
|
||||||
|
|
@ -4213,13 +4212,15 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
|
||||||
{
|
{
|
||||||
char full_filename[PATH_MAX];
|
char full_filename[PATH_MAX];
|
||||||
|
|
||||||
fc_combine_full_filename(file_path, redo_filename, full_filename);
|
snprintf(full_filename, sizeof(full_filename), "%s/%s",
|
||||||
|
file_path, redo_filename);
|
||||||
if ((fi->filename=fc_strdup(full_filename)) == NULL)
|
if ((fi->filename=fc_strdup(full_filename)) == NULL)
|
||||||
{
|
{
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_combine_full_filename(file_path, tmp_filename, full_filename);
|
snprintf(full_filename, sizeof(full_filename), "%s/%s",
|
||||||
|
file_path, tmp_filename);
|
||||||
if ((fi->tmp_filename=fc_strdup(full_filename)) == NULL)
|
if ((fi->tmp_filename=fc_strdup(full_filename)) == NULL)
|
||||||
{
|
{
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,6 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern bool g_set_cloexec;
|
extern bool g_set_cloexec;
|
||||||
extern const char *g_lower_hex_chars;
|
|
||||||
extern const char *g_upper_hex_chars;
|
|
||||||
|
|
||||||
static inline void fc_enable_fd_cloexec(const bool cloexec)
|
static inline void fc_enable_fd_cloexec(const bool cloexec)
|
||||||
{
|
{
|
||||||
|
|
@ -341,154 +339,6 @@ static inline double buff2double(const char *buff)
|
||||||
return *p;
|
return *p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int padding_hex(char *buff, const int len, const int padding_len)
|
|
||||||
{
|
|
||||||
char *p;
|
|
||||||
char *end;
|
|
||||||
char *stop;
|
|
||||||
int new_len;
|
|
||||||
int fill_len;
|
|
||||||
|
|
||||||
if (padding_len == len) {
|
|
||||||
return len;
|
|
||||||
} else if (padding_len > len) {
|
|
||||||
fill_len = padding_len - len;
|
|
||||||
memmove(buff + fill_len, buff, len + 1);
|
|
||||||
memset(buff, '0', fill_len);
|
|
||||||
return padding_len;
|
|
||||||
} else if (*buff != '0') {
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
end = buff + len;
|
|
||||||
if (padding_len <= 0) {
|
|
||||||
stop = end - 1;
|
|
||||||
} else {
|
|
||||||
stop = end - padding_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
p = buff + 1;
|
|
||||||
while (p < stop && *p == '0') {
|
|
||||||
++p;
|
|
||||||
}
|
|
||||||
|
|
||||||
new_len = end - p;
|
|
||||||
memmove(buff, p, new_len + 1);
|
|
||||||
return new_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int short2hex(const short n, char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
unsigned char *p;
|
|
||||||
|
|
||||||
p = (unsigned char *)buff;
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[n & 0x0F];
|
|
||||||
*p = '\0';
|
|
||||||
return padding_hex(buff, 4, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int short2HEX(const short n, char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
unsigned char *p;
|
|
||||||
|
|
||||||
p = (unsigned char *)buff;
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 12) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 8) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 4) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[n & 0x0F];
|
|
||||||
*p = '\0';
|
|
||||||
return padding_hex(buff, 4, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int int2hex(const int n, char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
unsigned char *p;
|
|
||||||
|
|
||||||
p = (unsigned char *)buff;
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 28) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 24) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 20) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 16) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[n & 0x0F];
|
|
||||||
*p = '\0';
|
|
||||||
return padding_hex(buff, 8, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int int2HEX(const int n, char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
unsigned char *p;
|
|
||||||
|
|
||||||
p = (unsigned char *)buff;
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 28) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 24) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 20) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 16) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 12) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 8) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 4) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[n & 0x0F];
|
|
||||||
*p = '\0';
|
|
||||||
return padding_hex(buff, 8, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int long2hex(const int64_t n,
|
|
||||||
char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
unsigned char *p;
|
|
||||||
|
|
||||||
p = (unsigned char *)buff;
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 60) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 56) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 52) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 48) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 44) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 40) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 36) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 32) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 28) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 24) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 20) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 16) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
|
|
||||||
*p++ = g_lower_hex_chars[n & 0x0F];
|
|
||||||
*p = '\0';
|
|
||||||
return padding_hex(buff, 16, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int long2HEX(const int64_t n,
|
|
||||||
char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
unsigned char *p;
|
|
||||||
|
|
||||||
p = (unsigned char *)buff;
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 60) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 56) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 52) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 48) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 44) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 40) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 36) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 32) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 28) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 24) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 20) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 16) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 12) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 8) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[(n >> 4) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[n & 0x0F];
|
|
||||||
*p = '\0';
|
|
||||||
return padding_hex(buff, 16, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** trim leading spaces ( \t\r\n)
|
/** trim leading spaces ( \t\r\n)
|
||||||
* parameters:
|
* parameters:
|
||||||
* pStr: the string to trim
|
* pStr: the string to trim
|
||||||
|
|
@ -1288,72 +1138,6 @@ static inline int resolve_path(const char *from, const char *filename,
|
||||||
size, NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS);
|
size, NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int fc_combine_two_string_ex(
|
|
||||||
const char *first_str, const int first_len,
|
|
||||||
const char *second_str, const int second_len,
|
|
||||||
const char seperator, char *combined_str, const int size)
|
|
||||||
{
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
if (first_len + 1 + second_len >= size) {
|
|
||||||
return snprintf(combined_str, size, "%s%c%s",
|
|
||||||
first_str, seperator, second_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(combined_str, first_str, first_len);
|
|
||||||
p = combined_str + first_len;
|
|
||||||
if (seperator != '\0') {
|
|
||||||
*p++ = seperator;
|
|
||||||
}
|
|
||||||
memcpy(p, second_str, second_len);
|
|
||||||
p += second_len;
|
|
||||||
*p = '\0';
|
|
||||||
return p - combined_str;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define fc_combine_two_string_s(first, second, seperator, combined, size) \
|
|
||||||
fc_combine_two_string_ex(first, strlen(first), second, strlen(second), \
|
|
||||||
seperator, combined, size)
|
|
||||||
|
|
||||||
#define fc_combine_two_string(first, second, seperator, combined) \
|
|
||||||
fc_combine_two_string_s(first, second, seperator, \
|
|
||||||
combined, sizeof(combined))
|
|
||||||
|
|
||||||
#define fc_concat_two_string(first, second, combined) \
|
|
||||||
fc_combine_two_string(first, second, '\0', combined)
|
|
||||||
|
|
||||||
static inline int fc_get_full_filename_ex(
|
|
||||||
const char *base_path_str, const int base_path_len,
|
|
||||||
const char *filename_str, const int filename_len,
|
|
||||||
char *full_filename, const int size)
|
|
||||||
{
|
|
||||||
const char seperator = '/';
|
|
||||||
return fc_combine_two_string_ex(base_path_str, base_path_len,
|
|
||||||
filename_str, filename_len, seperator, full_filename, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define fc_get_full_filename(base_path_str, base_path_len, \
|
|
||||||
filename_str, filename_len, full_filename) \
|
|
||||||
fc_get_full_filename_ex(base_path_str, base_path_len, \
|
|
||||||
filename_str, filename_len, full_filename, sizeof(full_filename))
|
|
||||||
|
|
||||||
#define fc_combine_full_filename(base_path, filename, full_filename) \
|
|
||||||
fc_get_full_filename(base_path, strlen(base_path), \
|
|
||||||
filename, strlen(filename), full_filename)
|
|
||||||
|
|
||||||
#define fc_get_full_filepath_ex(base_path_str, base_path_len, \
|
|
||||||
filepath_str, filepath_len, full_filename, size) \
|
|
||||||
fc_get_full_filename_ex(base_path_str, base_path_len, \
|
|
||||||
filepath_str, filepath_len, full_filename, size)
|
|
||||||
|
|
||||||
#define fc_get_full_filepath(base_path_str, base_path_len, \
|
|
||||||
filepath_str, filepath_len, full_filename) \
|
|
||||||
fc_get_full_filename(base_path_str, base_path_len, \
|
|
||||||
filepath_str, filepath_len, full_filename)
|
|
||||||
|
|
||||||
#define fc_combine_full_filepath(base_path, filepath, full_filename) \
|
|
||||||
fc_combine_full_filename(base_path, filepath, full_filename)
|
|
||||||
|
|
||||||
/** get gzip command full filename
|
/** get gzip command full filename
|
||||||
* return: the gzip command full filename
|
* return: the gzip command full filename
|
||||||
*/
|
*/
|
||||||
|
|
@ -1489,47 +1273,6 @@ bool fc_path_contains(const string_t *path, const string_t *needle,
|
||||||
*/
|
*/
|
||||||
int fc_itoa(int64_t n, char *buff);
|
int fc_itoa(int64_t n, char *buff);
|
||||||
|
|
||||||
static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
|
|
||||||
{
|
|
||||||
int len;
|
|
||||||
int fill_len;
|
|
||||||
|
|
||||||
len = fc_itoa(n, buff);
|
|
||||||
*(buff + len) = '\0';
|
|
||||||
if (padding_len <= len) {
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
fill_len = padding_len - len;
|
|
||||||
memmove(buff + fill_len, buff, len + 1);
|
|
||||||
memset(buff, '0', fill_len);
|
|
||||||
return padding_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int fc_ltostr(int64_t n, char *buff)
|
|
||||||
{
|
|
||||||
const int padding_len = 0;
|
|
||||||
return fc_ltostr_ex(n, buff, padding_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
|
|
||||||
{
|
|
||||||
int len;
|
|
||||||
|
|
||||||
len = strlen(src);
|
|
||||||
if (len < size) {
|
|
||||||
memcpy(dest, src, len + 1);
|
|
||||||
} else {
|
|
||||||
len = size - 1;
|
|
||||||
memcpy(dest, src, len);
|
|
||||||
*(dest + len) = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define fc_safe_strcpy(dest, src) fc_strlcpy(dest, src, sizeof(dest))
|
|
||||||
|
|
||||||
/** sleep in microseconds
|
/** sleep in microseconds
|
||||||
* parameters:
|
* parameters:
|
||||||
* microseconds: microseconds to sleep
|
* microseconds: microseconds to sleep
|
||||||
|
|
|
||||||
|
|
@ -1194,7 +1194,7 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fc_strlcpy(buff, ent->h_name, bufferSize);
|
snprintf(buff, bufferSize, "%s", ent->h_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buff;
|
return buff;
|
||||||
|
|
@ -1402,12 +1402,14 @@ int nbaccept(int sock, const int timeout, int *err_no)
|
||||||
int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
|
int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
|
||||||
{
|
{
|
||||||
sockaddr_convert_t convert;
|
sockaddr_convert_t convert;
|
||||||
|
char bind_ip_prompt[256];
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
memset(&convert, 0, sizeof(convert));
|
memset(&convert, 0, sizeof(convert));
|
||||||
convert.sa.addr.sa_family = af;
|
convert.sa.addr.sa_family = af;
|
||||||
if (bind_ipaddr == NULL || *bind_ipaddr == '\0')
|
if (bind_ipaddr == NULL || *bind_ipaddr == '\0')
|
||||||
{
|
{
|
||||||
|
*bind_ip_prompt = '\0';
|
||||||
if (af == AF_INET)
|
if (af == AF_INET)
|
||||||
{
|
{
|
||||||
convert.len = sizeof(convert.sa.addr4);
|
convert.len = sizeof(convert.sa.addr4);
|
||||||
|
|
@ -1427,15 +1429,11 @@ int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (bind(sock, &convert.sa.addr, convert.len) < 0) {
|
|
||||||
char bind_ip_prompt[256];
|
|
||||||
if (bind_ipaddr == NULL || *bind_ipaddr == '\0') {
|
|
||||||
*bind_ip_prompt = '\0';
|
|
||||||
} else {
|
|
||||||
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
|
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bind(sock, &convert.sa.addr, convert.len) < 0)
|
||||||
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"%sbind port %d failed, "
|
"%sbind port %d failed, "
|
||||||
"errno: %d, error info: %s.",
|
"errno: %d, error info: %s.",
|
||||||
|
|
@ -2579,14 +2577,10 @@ static inline int formatifmac(char *buff, const int buff_size,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dest = buff;
|
dest = buff + sprintf(buff, "%02x", *hwaddr);
|
||||||
*dest++ = g_lower_hex_chars[*hwaddr >> 4];
|
|
||||||
*dest++ = g_lower_hex_chars[*hwaddr & 0x0F];
|
|
||||||
for (ptr=hwaddr+1; ptr<end; ptr++)
|
for (ptr=hwaddr+1; ptr<end; ptr++)
|
||||||
{
|
{
|
||||||
*dest++ = ':';
|
dest += sprintf(dest, ":%02x", *ptr);
|
||||||
*dest++ = g_lower_hex_chars[*ptr >> 4];
|
|
||||||
*dest++ = g_lower_hex_chars[*ptr & 0x0F];
|
|
||||||
}
|
}
|
||||||
return dest - buff;
|
return dest - buff;
|
||||||
}
|
}
|
||||||
|
|
@ -2635,7 +2629,7 @@ static int getifmac(FastIFConfig *config)
|
||||||
fc_trim(output);
|
fc_trim(output);
|
||||||
if (*output != '\0')
|
if (*output != '\0')
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(config->mac, output);
|
snprintf(config->mac, sizeof(config->mac), "%s", output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2739,7 +2733,7 @@ int getifconfigs(FastIFConfig *if_configs, const int max_count, int *count)
|
||||||
return ENOSPC;
|
return ENOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(config->name, ifc->ifa_name);
|
sprintf(config->name, "%s", ifc->ifa_name);
|
||||||
(*count)++;
|
(*count)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -194,9 +194,9 @@ int get_boot_time(struct timeval *boot_time)
|
||||||
|
|
||||||
#define SET_MNT_FIELDS(left, fstypename, mntfromname, mntonname) \
|
#define SET_MNT_FIELDS(left, fstypename, mntfromname, mntonname) \
|
||||||
do { \
|
do { \
|
||||||
fc_safe_strcpy(left.f_fstypename, fstypename); \
|
snprintf(left.f_fstypename, sizeof(left.f_fstypename), "%s", fstypename); \
|
||||||
fc_safe_strcpy(left.f_mntfromname, mntfromname); \
|
snprintf(left.f_mntfromname, sizeof(left.f_mntfromname), "%s", mntfromname); \
|
||||||
fc_safe_strcpy(left.f_mntonname, mntonname); \
|
snprintf(left.f_mntonname, sizeof(left.f_mntonname), "%s", mntonname); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#ifdef OS_LINUX
|
||||||
|
|
@ -286,9 +286,12 @@ int get_mounted_filesystems(struct fast_statfs *stats,
|
||||||
toLowercase(fstypename);
|
toLowercase(fstypename);
|
||||||
if (get_device_type(fstypename, &stats[*count].device_type) == 0)
|
if (get_device_type(fstypename, &stats[*count].device_type) == 0)
|
||||||
{
|
{
|
||||||
fc_safe_strcpy(stats[*count].f_mntfromname, mntfromname);
|
snprintf(stats[*count].f_mntfromname, sizeof(stats[*count].
|
||||||
fc_safe_strcpy(stats[*count].f_mntonname, mntonname);
|
f_mntfromname), "%s", mntfromname);
|
||||||
fc_safe_strcpy(stats[*count].f_fstypename, fstypename);
|
snprintf(stats[*count].f_mntonname, sizeof(stats[*count].
|
||||||
|
f_mntonname), "%s", mntonname);
|
||||||
|
snprintf(stats[*count].f_fstypename, sizeof(stats[*count].
|
||||||
|
f_fstypename), "%s", fstypename);
|
||||||
(*count)++;
|
(*count)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -838,7 +841,8 @@ int get_processes(struct fast_process_info **processes, int *count)
|
||||||
for (i=0; i<nproc; i++)
|
for (i=0; i<nproc; i++)
|
||||||
{
|
{
|
||||||
process->field_count = 9;
|
process->field_count = 9;
|
||||||
fc_safe_strcpy(process->comm, procs[i].ki_comm);
|
snprintf(process->comm, sizeof(process->comm),
|
||||||
|
"%s", procs[i].ki_comm);
|
||||||
process->pid = procs[i].ki_pid;
|
process->pid = procs[i].ki_pid;
|
||||||
process->ppid = procs[i].ki_ppid;
|
process->ppid = procs[i].ki_ppid;
|
||||||
process->starttime = procs[i].ki_start;
|
process->starttime = procs[i].ki_start;
|
||||||
|
|
@ -1037,7 +1041,8 @@ static int get_block_size_by_write(const char *path, int *block_size)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_combine_full_filename(path, ".blksize-test.tmp", tmp_filename);
|
snprintf(tmp_filename, sizeof(tmp_filename),
|
||||||
|
"%s/.blksize-test.tmp", path);
|
||||||
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
|
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
|
||||||
O_DIRECT | O_CLOEXEC, 0755)) < 0)
|
O_DIRECT | O_CLOEXEC, 0755)) < 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo
|
||||||
test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \
|
test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \
|
||||||
test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \
|
test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \
|
||||||
test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \
|
test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \
|
||||||
test_thread_local test_memcpy test_fast_buffer mblock_benchmark cpool_benchmark
|
test_thread_local test_memcpy mblock_benchmark cpool_benchmark
|
||||||
|
|
||||||
all: $(ALL_PRGS)
|
all: $(ALL_PRGS)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,228 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2020 YuQing <384681@qq.com>
|
|
||||||
*
|
|
||||||
* This program is free software: you can use, redistribute, and/or modify
|
|
||||||
* it under the terms of the Lesser GNU General Public License, version 3
|
|
||||||
* or later ("LGPL"), as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the Lesser GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include "fastcommon/logger.h"
|
|
||||||
#include "fastcommon/fast_buffer.h"
|
|
||||||
#include "fastcommon/sched_thread.h"
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
DA_SLICE_TYPE_FILE = 'F', /* in file slice */
|
|
||||||
DA_SLICE_TYPE_CACHE = 'C', /* in memory cache */
|
|
||||||
DA_SLICE_TYPE_ALLOC = 'A' /* allocate slice (index and space allocate only) */
|
|
||||||
} DASliceType;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int64_t version;
|
|
||||||
uint64_t trunk_id; //0 for not inited
|
|
||||||
uint32_t length; //data length
|
|
||||||
uint32_t offset; //space offset
|
|
||||||
uint32_t size; //space size
|
|
||||||
} DAPieceFieldStorage;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int64_t version; //for stable sort only
|
|
||||||
uint64_t oid; //object ID
|
|
||||||
uint64_t fid; //field ID (key)
|
|
||||||
uint32_t extra; //such as slice offset
|
|
||||||
char op_type;
|
|
||||||
DASliceType slice_type;
|
|
||||||
DAPieceFieldStorage storage;
|
|
||||||
} DATrunkSpaceLogRecord;
|
|
||||||
|
|
||||||
static inline void log_pack_by_append(const DATrunkSpaceLogRecord
|
|
||||||
*record, FastBuffer *buffer, const bool have_extra_field)
|
|
||||||
{
|
|
||||||
fast_buffer_append_int64(buffer, (uint32_t)g_current_time);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->storage.version);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->oid);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->fid);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_char(buffer, record->op_type);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->storage.trunk_id);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->storage.length);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->storage.offset);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_int64(buffer, record->storage.size);
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_char(buffer, record->slice_type);
|
|
||||||
|
|
||||||
if (have_extra_field) {
|
|
||||||
fast_buffer_append_char(buffer, ' ');
|
|
||||||
fast_buffer_append_char(buffer, record->extra);
|
|
||||||
fast_buffer_append_char(buffer, '\n');
|
|
||||||
} else {
|
|
||||||
fast_buffer_append_char(buffer, '\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void log_pack_by_sprintf(const DATrunkSpaceLogRecord
|
|
||||||
*record, FastBuffer *buffer, const bool have_extra_field)
|
|
||||||
{
|
|
||||||
buffer->length += sprintf(buffer->data + buffer->length,
|
|
||||||
"%u %"PRId64" %"PRId64" %"PRId64" %c %"PRId64" %u %u %u %c",
|
|
||||||
(uint32_t)g_current_time, record->storage.version,
|
|
||||||
record->oid, record->fid, record->op_type,
|
|
||||||
record->storage.trunk_id, record->storage.length,
|
|
||||||
record->storage.offset, record->storage.size,
|
|
||||||
record->slice_type);
|
|
||||||
if (have_extra_field) {
|
|
||||||
buffer->length += sprintf(buffer->data + buffer->length,
|
|
||||||
" %u\n", record->extra);
|
|
||||||
} else {
|
|
||||||
*(buffer->data + buffer->length++) = '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#define BINLOG_FILENAME_PREFIX_STR "binlog."
|
|
||||||
#define BINLOG_FILENAME_PREFIX_LEN (sizeof(BINLOG_FILENAME_PREFIX_STR) - 1)
|
|
||||||
|
|
||||||
static inline int cache_binlog_filename_by_sprintf(
|
|
||||||
const char *data_path, const char *subdir_name,
|
|
||||||
const uint32_t subdirs, const uint64_t id,
|
|
||||||
char *full_filename, const int size)
|
|
||||||
{
|
|
||||||
int path_index;
|
|
||||||
path_index = id % subdirs;
|
|
||||||
return sprintf(full_filename, "%s/%s/%02X/%02X/%s%08"PRIX64,
|
|
||||||
data_path, subdir_name, path_index, path_index,
|
|
||||||
BINLOG_FILENAME_PREFIX_STR, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int cache_binlog_filename_by_append(
|
|
||||||
const char *data_path, const char *subdir_name,
|
|
||||||
const uint32_t subdirs, const uint64_t id,
|
|
||||||
char *full_filename, const int size)
|
|
||||||
{
|
|
||||||
int path_index;
|
|
||||||
int path_len;
|
|
||||||
int subdir_len;
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
path_index = id % subdirs;
|
|
||||||
path_len = strlen(data_path);
|
|
||||||
subdir_len = strlen(subdir_name);
|
|
||||||
p = full_filename;
|
|
||||||
memcpy(p, data_path, path_len);
|
|
||||||
p += path_len;
|
|
||||||
*p++ = '/';
|
|
||||||
memcpy(p, subdir_name, subdir_len);
|
|
||||||
p += subdir_len;
|
|
||||||
*p++ = '/';
|
|
||||||
*p++ = g_upper_hex_chars[(path_index >> 4) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[path_index & 0x0F];
|
|
||||||
*p++ = '/';
|
|
||||||
*p++ = g_upper_hex_chars[(path_index >> 4) & 0x0F];
|
|
||||||
*p++ = g_upper_hex_chars[path_index & 0x0F];
|
|
||||||
*p++ = '/';
|
|
||||||
memcpy(p, BINLOG_FILENAME_PREFIX_STR, BINLOG_FILENAME_PREFIX_LEN);
|
|
||||||
p += BINLOG_FILENAME_PREFIX_LEN;
|
|
||||||
if (id <= UINT32_MAX) {
|
|
||||||
p += int2HEX(id, p, 8);
|
|
||||||
} else {
|
|
||||||
p += long2HEX(id, p, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
return p - full_filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
const bool binary_mode = true;
|
|
||||||
const bool check_capacity = false;
|
|
||||||
const bool have_extra_field = false;
|
|
||||||
const int LOOP = 10 * 1000 * 1000;
|
|
||||||
int result;
|
|
||||||
int i;
|
|
||||||
int64_t start_time_us;
|
|
||||||
int append_time_us;
|
|
||||||
int sprintf_time_us;
|
|
||||||
double ratio;
|
|
||||||
FastBuffer buffer;
|
|
||||||
DATrunkSpaceLogRecord record;
|
|
||||||
|
|
||||||
log_init();
|
|
||||||
g_current_time = time(NULL);
|
|
||||||
if ((result=fast_buffer_init_ex(&buffer, 256,
|
|
||||||
binary_mode, check_capacity)) != 0)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&record, 0, sizeof(record));
|
|
||||||
record.op_type = 'C';
|
|
||||||
record.slice_type = DA_SLICE_TYPE_FILE;
|
|
||||||
record.storage.version = 1111;
|
|
||||||
record.oid = 9007211709265131LL;
|
|
||||||
record.fid = 0;
|
|
||||||
record.storage.trunk_id = 61;
|
|
||||||
record.storage.length = 62;
|
|
||||||
record.storage.offset = 12345;
|
|
||||||
record.storage.size = 64;
|
|
||||||
|
|
||||||
|
|
||||||
const char *data_path = "/opt/fastcfs/fdir/data";
|
|
||||||
const char *subdir_name = "binlog";
|
|
||||||
const uint32_t subdirs = 256;
|
|
||||||
uint64_t id = 123456;
|
|
||||||
char full_filename1[PATH_MAX];
|
|
||||||
char full_filename2[PATH_MAX];
|
|
||||||
|
|
||||||
start_time_us = get_current_time_us();
|
|
||||||
for (i=0; i<LOOP; i++) {
|
|
||||||
cache_binlog_filename_by_sprintf(data_path, subdir_name,
|
|
||||||
subdirs, ++id, full_filename1, sizeof(full_filename1));
|
|
||||||
fast_buffer_reset(&buffer);
|
|
||||||
log_pack_by_sprintf(&record, &buffer, have_extra_field);
|
|
||||||
}
|
|
||||||
sprintf_time_us = (get_current_time_us() - start_time_us);
|
|
||||||
|
|
||||||
start_time_us = get_current_time_us();
|
|
||||||
for (i=0; i<LOOP; i++) {
|
|
||||||
cache_binlog_filename_by_append(data_path, subdir_name,
|
|
||||||
subdirs, ++id, full_filename2, sizeof(full_filename2));
|
|
||||||
fast_buffer_reset(&buffer);
|
|
||||||
log_pack_by_append(&record, &buffer, have_extra_field);
|
|
||||||
}
|
|
||||||
append_time_us = (get_current_time_us() - start_time_us);
|
|
||||||
|
|
||||||
if (append_time_us > 0) {
|
|
||||||
ratio = (double)sprintf_time_us / (double)append_time_us;
|
|
||||||
} else {
|
|
||||||
ratio = 1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("sprintf time: %d ms, append time: %d ms, "
|
|
||||||
"sprintf time / append time: %d%%\n",
|
|
||||||
sprintf_time_us / 1000, append_time_us / 1000,
|
|
||||||
(int)(ratio * 100.00));
|
|
||||||
|
|
||||||
fast_buffer_destroy(&buffer);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -112,7 +112,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
setup_mblock_stat_task();
|
setup_mblock_stat_task();
|
||||||
|
|
||||||
if ((result=fast_buffer_init1(&buffer, 1024)) != 0) {
|
if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
fc_server_to_config_string(&ctx, &buffer);
|
fc_server_to_config_string(&ctx, &buffer);
|
||||||
|
|
|
||||||
|
|
@ -210,7 +210,7 @@ int fc_thread_pool_init_ex(FCThreadPool *pool, const char *name,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc_safe_strcpy(pool->name, name);
|
snprintf(pool->name, sizeof(pool->name), "%s", name);
|
||||||
pool->stack_size = stack_size;
|
pool->stack_size = stack_size;
|
||||||
pool->max_idle_time = max_idle_time;
|
pool->max_idle_time = max_idle_time;
|
||||||
if (min_idle_count > limit) {
|
if (min_idle_count > limit) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue