Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
e3db0162
Unverified
Commit
e3db0162
authored
Oct 18, 2017
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
utils: parse_byte_size_string()
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
b037bc67
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
152 additions
and
0 deletions
+152
-0
utils.c
src/lxc/utils.c
+67
-0
utils.h
src/lxc/utils.h
+2
-0
lxc-test-utils.c
src/tests/lxc-test-utils.c
+83
-0
No files found.
src/lxc/utils.c
View file @
e3db0162
...
...
@@ -47,6 +47,7 @@
#include "log.h"
#include "lxclock.h"
#include "namespace.h"
#include "parse.h"
#include "utils.h"
#ifndef O_PATH
...
...
@@ -2390,3 +2391,69 @@ uint64_t lxc_getpagesize(void)
return
pgsz
;
}
int
parse_byte_size_string
(
const
char
*
s
,
int64_t
*
converted
)
{
int
ret
,
suffix_len
;
long
long
int
conv
;
int64_t
mltpl
,
overflow
;
char
*
end
;
char
dup
[
LXC_NUMSTRLEN64
+
2
];
char
suffix
[
3
];
if
(
!
s
||
!
strcmp
(
s
,
""
))
return
-
EINVAL
;
end
=
stpncpy
(
dup
,
s
,
sizeof
(
dup
));
if
(
*
end
!=
'\0'
)
return
-
EINVAL
;
if
(
isdigit
(
*
(
end
-
1
)))
suffix_len
=
0
;
else
if
(
isalpha
(
*
(
end
-
1
)))
suffix_len
=
1
;
else
return
-
EINVAL
;
if
((
end
-
2
)
==
dup
&&
!
isdigit
(
*
(
end
-
2
)))
return
-
EINVAL
;
if
(
isalpha
(
*
(
end
-
2
)))
{
if
(
suffix_len
==
1
)
suffix_len
++
;
else
return
-
EINVAL
;
}
if
(
suffix_len
>
0
)
{
memcpy
(
suffix
,
end
-
suffix_len
,
suffix_len
);
*
(
suffix
+
suffix_len
)
=
'\0'
;
*
(
end
-
suffix_len
)
=
'\0'
;
}
dup
[
lxc_char_right_gc
(
dup
,
strlen
(
dup
))]
=
'\0'
;
ret
=
lxc_safe_long_long
(
dup
,
&
conv
);
if
(
ret
<
0
)
return
-
ret
;
if
(
suffix_len
!=
2
)
{
*
converted
=
conv
;
return
0
;
}
if
(
!
strcmp
(
suffix
,
"kB"
))
mltpl
=
1024
;
else
if
(
!
strcmp
(
suffix
,
"MB"
))
mltpl
=
1024
*
1024
;
else
if
(
!
strcmp
(
suffix
,
"GB"
))
mltpl
=
1024
*
1024
*
1024
;
else
return
-
EINVAL
;
overflow
=
conv
*
mltpl
;
if
(
conv
!=
0
&&
(
overflow
/
conv
)
!=
mltpl
)
return
-
ERANGE
;
*
converted
=
overflow
;
return
0
;
}
src/lxc/utils.h
View file @
e3db0162
...
...
@@ -421,6 +421,8 @@ extern int lxc_safe_int(const char *numstr, int *converted);
extern
int
lxc_safe_long
(
const
char
*
numstr
,
long
int
*
converted
);
extern
int
lxc_safe_long_long
(
const
char
*
numstr
,
long
long
int
*
converted
);
extern
int
lxc_safe_ulong
(
const
char
*
numstr
,
unsigned
long
*
converted
);
/* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
extern
int
parse_byte_size_string
(
const
char
*
s
,
int64_t
*
converted
);
/* Switch to a new uid and gid. */
extern
int
lxc_switch_uid_gid
(
uid_t
uid
,
gid_t
gid
);
...
...
src/tests/lxc-test-utils.c
View file @
e3db0162
...
...
@@ -380,6 +380,88 @@ void test_lxc_string_in_array(void)
lxc_test_assert_abort
(
lxc_string_in_array
(
"XYZ"
,
(
const
char
*
[]){
"BERTA"
,
"ARQWE(9"
,
"C8Zhkd"
,
"7U"
,
"XYZ"
,
"UOIZ9"
,
"=)()"
,
NULL
}));
}
void
test_parse_byte_size_string
(
void
)
{
int
ret
;
int64_t
n
;
ret
=
parse_byte_size_string
(
"0"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
0
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1 "
,
&
n
);
if
(
ret
==
0
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1B"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1kB"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1024
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1MB"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1048576
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1GB"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1073741824
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1TB"
,
&
n
);
if
(
ret
==
0
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1 B"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1 kB"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1024
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1 MB"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1048576
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1 GB"
,
&
n
);
if
(
ret
<
0
)
exit
(
EXIT_FAILURE
);
if
(
n
!=
1073741824
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"1 TB"
,
&
n
);
if
(
ret
==
0
)
exit
(
EXIT_FAILURE
);
ret
=
parse_byte_size_string
(
"asdf"
,
&
n
);
if
(
ret
==
0
)
exit
(
EXIT_FAILURE
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
test_lxc_string_replace
();
...
...
@@ -389,6 +471,7 @@ int main(int argc, char *argv[])
test_lxc_safe_uint
();
test_lxc_safe_int
();
test_lxc_safe_long
();
test_parse_byte_size_string
();
exit
(
EXIT_SUCCESS
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment