Skip to content

Commit 5470596

Browse files
committed
utils: tests: Implement binary bytes conversion function
size_to_byte function just converts with 1000(K), 1000*K, 1000*M. But this function converts with 1024(KiB), 1024*KiB(MiB), and 1024*MiB(GiB). Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 225e896 commit 5470596

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

include/fluent-bit/flb_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void flb_utils_split_free_entry(struct flb_split_entry *entry);
4949
void flb_utils_split_free(struct mk_list *list);
5050
int flb_utils_timer_consume(flb_pipefd_t fd);
5151
int64_t flb_utils_size_to_bytes(const char *size);
52+
int64_t flb_utils_size_to_binary_bytes(const char *size);
5253
int64_t flb_utils_hex2int(char *hex, int len);
5354
int flb_utils_time_to_seconds(const char *time);
5455
int flb_utils_pipe_byte_consume(flb_pipefd_t fd);

src/flb_utils.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,92 @@ int64_t flb_utils_size_to_bytes(const char *size)
607607
return (int64_t)val;
608608
}
609609

610+
int64_t flb_utils_size_to_binary_bytes(const char *size)
611+
{
612+
int i;
613+
int len;
614+
int plen = 0;
615+
double val;
616+
char tmp[4] = {0};
617+
int64_t KiB = 1024;
618+
int64_t MiB = 1024 * KiB;
619+
int64_t GiB = 1024 * MiB;
620+
621+
if (!size) {
622+
return -1;
623+
}
624+
625+
if (strcasecmp(size, "false") == 0) {
626+
return 0;
627+
}
628+
629+
len = strlen(size);
630+
val = atof(size);
631+
632+
if (len == 0) {
633+
return -1;
634+
}
635+
636+
for (i = len - 1; i >= 0; i--) {
637+
if (isalpha(size[i])) {
638+
plen++;
639+
}
640+
else {
641+
break;
642+
}
643+
}
644+
645+
if (plen == 0) {
646+
return (int64_t)val;
647+
}
648+
else if (plen > 3) {
649+
return -1;
650+
}
651+
652+
for (i = 0; i < plen; i++) {
653+
tmp[i] = toupper(size[len - plen + i]);
654+
}
655+
656+
if (plen == 2) {
657+
if (tmp[1] != 'B') {
658+
return -1;
659+
}
660+
}
661+
if (plen == 3) {
662+
if (tmp[1] != 'I' || tmp[2] != 'B') {
663+
return -1;
664+
}
665+
}
666+
667+
if (tmp[0] == 'K') {
668+
/* set upper bound (2**64/KiB)/2 to avoid overflows */
669+
if (val >= 9223372036854775.0 || val <= -9223372036854774.0)
670+
{
671+
return -1;
672+
}
673+
return (int64_t)(val * KiB);
674+
}
675+
else if (tmp[0] == 'M') {
676+
/* set upper bound (2**64/MiB)/2 to avoid overflows */
677+
if (val >= 9223372036854.0 || val <= -9223372036853.0) {
678+
return -1;
679+
}
680+
return (int64_t)(val * MiB);
681+
}
682+
else if (tmp[0] == 'G') {
683+
/* set upper bound (2**64/GiB)/2 to avoid overflows */
684+
if (val >= 9223372036.0 || val <= -9223372035.0) {
685+
return -1;
686+
}
687+
return (int64_t)(val * GiB);
688+
}
689+
else {
690+
return -1;
691+
}
692+
693+
return (int64_t)val;
694+
}
695+
610696
int64_t flb_utils_hex2int(char *hex, int len)
611697
{
612698
int i = 0;

tests/internal/utils.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,45 @@ void test_size_to_bytes()
798798
}
799799
}
800800

801+
struct size_to_bytes_check size_to_binary_bytes_checks[] = {
802+
{"922337.63", 922337},
803+
{"2K",2048},
804+
{"5.7263K", 5863},
805+
{"5.7263KB", 5863},
806+
{"5.7263KiB", 5863},
807+
{"9223372036854775.23K", -1},
808+
{"1M", 1048576},
809+
{"1.1M", 1153433},
810+
{"1.1MB", 1153433},
811+
{"1.1MiB", 1153433},
812+
{"3.592M", 3766484},
813+
{"52.752383M", 55314882},
814+
{"52.752383MB", 55314882},
815+
{"52.752383MiB", 55314882},
816+
{"9223372036854.42M", -1},
817+
{"492.364G",528671819431},
818+
{"492.364GB",528671819431},
819+
{"492.364GiB",528671819431},
820+
{"1.2973G", 1392965268},
821+
{"9223372036.78G", -1},
822+
};
823+
824+
void test_size_to_binary_bytes()
825+
{
826+
int i;
827+
int size;
828+
int64_t ret;
829+
struct size_to_bytes_check *u;
830+
831+
size = sizeof(size_to_binary_bytes_checks) / sizeof(struct size_to_bytes_check);
832+
for (i = 0; i < size; i++) {
833+
u = &size_to_binary_bytes_checks[i];
834+
835+
ret = flb_utils_size_to_binary_bytes(u->size);
836+
TEST_CHECK_(ret == u->ret, "ret = %zu, u->ret = %zu", ret, u->ret);
837+
}
838+
}
839+
801840
TEST_LIST = {
802841
/* JSON maps iteration */
803842
{ "url_split", test_url_split },
@@ -815,5 +854,6 @@ TEST_LIST = {
815854
{ "test_flb_utils_split_quoted_errors", test_flb_utils_split_quoted_errors},
816855
{ "test_flb_utils_get_machine_id", test_flb_utils_get_machine_id },
817856
{ "test_size_to_bytes", test_size_to_bytes },
857+
{ "test_size_to_bianry_bytes", test_size_to_binary_bytes },
818858
{ 0 }
819859
};

0 commit comments

Comments
 (0)