Compare commits

...

5 Commits

Author SHA1 Message Date
Hongcai Deng f6bc853094
Merge a16fde8070 into 02227ff318 2026-09-05 11:43:57 +08:00
YuQing 02227ff318 upgrade version to 1.0.88 2026-09-04 11:10:27 +08:00
YuQing 9493e1ad47 add functions: parse_timestamp and iniGetTimestampValueEx 2026-09-04 09:53:07 +08:00
YuQing 15424dfc1c local_host_ip_addrs_to_string: fix buffer overflow 2026-09-03 15:21:36 +08:00
Hongcai Deng a16fde8070 fix: compile error when build .so using .a
```
src/libfastcommon.a(hash.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
```

env: Ubuntu 14.04, gcc 4.8
2017-01-29 14:20:18 +08:00
10 changed files with 157 additions and 8 deletions

View File

@ -1,4 +1,9 @@
Version 1.88 2026-09-04
* local_host_ip_addrs_to_string: fix buffer overflow
* shared_func.[hc]: add function parse_timestamp
* ini_file_reader.[hc]: add function iniGetTimestampValueEx
Version 1.87 2026-07-10
* ini_file_readers can refer env variable such as %{env::FASTDFS_IPADDR}

View File

@ -10,7 +10,7 @@ libfastcommon是在github开源的⼀个C函数库。它提供了ini⽂件解析
## ini_file_reader的主要特点
### 1、⽀持section
例如:
例如:
```
[workers]
threads = 4

View File

@ -3,7 +3,7 @@
%define CommitVersion %(echo $COMMIT_VERSION)
Name: libfastcommon
Version: 1.0.87
Version: 1.0.88
Release: 1%{?dist}
Summary: c common functions library extracted from my open source projects FastDFS
License: LGPL

View File

@ -68,7 +68,7 @@ libfastcommon.a: $(FAST_STATIC_OBJS)
.c:
$(COMPILE) -o $@ $< $(FAST_STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
.c.o:
$(COMPILE) -c -o $@ $< $(INC_PATH)
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
.c.lo:
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
install:

View File

@ -22,7 +22,7 @@
#define FC_MAJOR_VERSION 1
#define FC_MINOR_VERSION 0
#define FC_PATCH_VERSION 87
#define FC_PATCH_VERSION 88
#define FC_VERSION_INT FC_VERSION_TO_INT(FC_MAJOR_VERSION, \
FC_MINOR_VERSION, FC_PATCH_VERSION)

View File

@ -3531,3 +3531,26 @@ int iniGetPercentValueEx(IniFullContext *ini_ctx,
return 0;
}
int64_t iniGetTimestampValueEx(const char *szSectionName,
const char *szItemName, IniContext *pContext,
const int64_t nDefaultValue, const int nDefaultUnitTS,
const bool bRetryGlobal)
{
char *pValue;
int64_t nValue;
pValue = iniGetStrValueEx(szSectionName,
szItemName, pContext, bRetryGlobal);
if (pValue == NULL)
{
return nDefaultValue;
}
if (parse_timestamp(pValue, nDefaultUnitTS, &nValue) != 0)
{
return nDefaultValue;
}
return nValue;
}

View File

@ -140,6 +140,10 @@ extern "C" {
#define iniGetPercentValue(ini_ctx, item_name, item_value, default_value) \
iniGetPercentValueEx(ini_ctx, item_name, item_value, default_value, false)
#define iniGetTimestampValue(szSectionName, szItemName, pContext, nDefaultValue) \
iniGetTimestampValueEx(szSectionName, szItemName, \
pContext, nDefaultValue, 1, false)
#define iniGetByteValue(szSectionName, szItemName, pContext, nDefaultValue) \
iniGetByteValueEx(szSectionName, szItemName, \
pContext, nDefaultValue, 1, false)
@ -424,6 +428,22 @@ int64_t iniGetByteCorrectValueEx(IniFullContext *pIniContext,
const int nDefaultUnitBytes, const int64_t nMinValue,
const int64_t nMaxValue, const bool bRetryGlobal);
/** get item timestamp value (64 bits integer)
* parameters:
* szSectionName: the section name, NULL or empty string for
* global section
* szItemName: the item name
* pContext: the ini context
* nDefaultValue: the default value
* nDefaultUnitBytes: the default timestamp unit, such as 1 for second, 86400 for day
* bRetryGlobal: if fetch from global section when the item not exist
* return: int64 value, return nDefaultValue when the item not exist
*/
int64_t iniGetTimestampValueEx(const char *szSectionName,
const char *szItemName, IniContext *pContext,
const int64_t nDefaultValue, const int nDefaultUnitTS,
const bool bRetryGlobal);
/** get item boolean value
* parameters:
* szSectionName: the section name, NULL or empty string for

View File

@ -70,12 +70,21 @@ const char *local_host_ip_addrs_to_string(char *buff, const int size)
len = snprintf(buff, size, "local_host_ip_count: %d,",
g_local_host_ip_count);
if (len >= size)
{
return buff;
}
pEnd = g_local_host_ip_addrs +
IP_ADDRESS_SIZE * g_local_host_ip_count;
for (p=g_local_host_ip_addrs; p<pEnd; p+=IP_ADDRESS_SIZE)
{
len += snprintf(buff + len, size - len, " %s", p);
}
{
len += snprintf(buff + len, size - len, " %s", p);
if (len >= size)
{
break;
}
}
return buff;
}

View File

@ -2484,6 +2484,88 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes)
return result;
}
int parse_timestamp(const char *str, const int default_unit_secs, int64_t *seconds)
{
char *pReservedEnd;
int result;
pReservedEnd = NULL;
*seconds = strtoll(str, &pReservedEnd, 10);
if (*seconds < 0)
{
logError("file: "__FILE__", line: %d, "
"timestamp: %"PRId64" < 0, input string: %s",
__LINE__, *seconds, str);
return EINVAL;
}
if (pReservedEnd == NULL || *pReservedEnd == '\0')
{
*seconds *= default_unit_secs;
return 0;
}
if (*pReservedEnd == ' ' || *pReservedEnd == '\t')
{
do {
++pReservedEnd;
} while (*pReservedEnd == ' ' || *pReservedEnd == '\t');
if (*pReservedEnd == '\0')
{
*seconds *= default_unit_secs;
return 0;
}
}
if (*(pReservedEnd + 1) != '\0')
{
logError("file: "__FILE__", line: %d, "
"unkown timestamp unit: \"%s\", input string: \"%s\"",
__LINE__, pReservedEnd, str);
return EINVAL;
}
switch (*pReservedEnd)
{
case 's':
result = 0;
break;
case 'm':
*seconds *= 60;
result = 0;
break;
case 'h':
*seconds *= 3600;
result = 0;
break;
case 'd':
*seconds *= 86400;
result = 0;
break;
case 'W':
*seconds *= 7 * 86400;
result = 0;
break;
case 'M':
*seconds *= 30 * 86400;
result = 0;
break;
case 'Y':
*seconds *= 365 * 86400;
result = 0;
break;
default:
result = EINVAL;
logError("file: "__FILE__", line: %d, "
"unkown timestamp unit: \"%s\", input string: \"%s\"",
__LINE__, pReservedEnd, str);
break;
}
return result;
}
int set_rand_seed()
{
time_t ts;

View File

@ -979,12 +979,22 @@ int fc_compare_int64_ptr(const int64_t *n1, const int64_t *n2);
/** parse bytes
* parameters:
* pStr: the string to parse
* default_unit_bytes: default unit if not specified the unit like MB etc.
* default_unit_bytes: default unit if not specified, the unit like MB etc.
* bytes: store the parsed bytes
* return: error no , 0 success, != 0 fail
*/
int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes);
/** parse timestamp
* parameters:
* str: the string to parse
* default_unit_secs: default unit if not specified
* seconds: store the parsed seconds
* return: error no , 0 success, != 0 fail
*/
int parse_timestamp(const char *str, const int default_unit_secs, int64_t *seconds);
/** set rand seed
* return: error no , 0 success, != 0 fail
*/