storage_utils: move duplicated function from tools

Signed-off-by: 2xsec <dh48.jeong@samsung.com>
This commit is contained in:
2xsec 2018-08-18 18:05:32 +09:00
parent 34a10bfa34
commit 2b670dfeb0
4 changed files with 37 additions and 71 deletions

View File

@ -22,6 +22,7 @@
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
@ -457,6 +458,40 @@ bool unpriv_snap_allowed(struct lxc_storage *b, const char *t, bool snap,
return false;
}
uint64_t get_fssize(char *s)
{
uint64_t ret;
char *end;
ret = strtoull(s, &end, 0);
if (end == s) {
ERROR("Invalid blockdev size '%s', using default size", s);
return 0;
}
while (isblank(*end))
end++;
if (*end == '\0') {
ret *= 1024ULL * 1024ULL; /* MB by default */
} else if (*end == 'b' || *end == 'B') {
ret *= 1ULL;
} else if (*end == 'k' || *end == 'K') {
ret *= 1024ULL;
} else if (*end == 'm' || *end == 'M') {
ret *= 1024ULL * 1024ULL;
} else if (*end == 'g' || *end == 'G') {
ret *= 1024ULL * 1024ULL * 1024ULL;
} else if (*end == 't' || *end == 'T') {
ret *= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
} else {
ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end, s);
return 0;
}
return ret;
}
bool is_valid_storage_type(const char *type)
{
if (strcmp(type, "dir") == 0 ||

View File

@ -48,6 +48,7 @@ extern int find_fstype_cb(char *buffer, void *data);
extern const char *linkderef(const char *path, char *dest);
extern bool unpriv_snap_allowed(struct lxc_storage *b, const char *t, bool snap,
bool maybesnap);
extern uint64_t get_fssize(char *s);
extern bool is_valid_storage_type(const char *type);
extern int storage_destroy_wrapper(void *data);

View File

@ -17,7 +17,6 @@
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
@ -37,6 +36,7 @@
#include "arguments.h"
#include "log.h"
#include "storage_utils.h"
#include "utils.h"
#ifndef HAVE_GETSUBOPT
@ -143,7 +143,6 @@ static int do_clone_rename(struct lxc_container *c, char *newname);
static int do_clone_task(struct lxc_container *c, enum task task, int flags,
char **args);
static void free_mnts(void);
static uint64_t get_fssize(char *s);
/* Place an ephemeral container started with -e flag on a tmpfs. Restrictions
* are that you cannot request the data to be kept while placing the container
@ -538,39 +537,6 @@ static void free_mnts()
mnt_table_size = 0;
}
/* we pass fssize in bytes */
static uint64_t get_fssize(char *s)
{
uint64_t ret;
char *end;
ret = strtoull(s, &end, 0);
if (end == s) {
ERROR("Invalid blockdev size '%s', using default size", s);
return 0;
}
while (isblank(*end))
end++;
if (*end == '\0') {
ret *= 1024ULL * 1024ULL; /* MB by default */
} else if (*end == 'b' || *end == 'B') {
ret *= 1ULL;
} else if (*end == 'k' || *end == 'K') {
ret *= 1024ULL;
} else if (*end == 'm' || *end == 'M') {
ret *= 1024ULL * 1024ULL;
} else if (*end == 'g' || *end == 'G') {
ret *= 1024ULL * 1024ULL * 1024ULL;
} else if (*end == 't' || *end == 'T') {
ret *= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
} else {
ERROR("Invalid blockdev unit size '%c' in '%s', " "using default size", *end, s);
return 0;
}
return ret;
}
static int my_parser(struct lxc_arguments *args, int c, char *arg)
{
char *subopts = NULL;

View File

@ -18,7 +18,6 @@
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdint.h>
@ -37,7 +36,6 @@
lxc_log_define(lxc_create, lxc);
static int my_parser(struct lxc_arguments *args, int c, char *arg);
static uint64_t get_fssize(char *s);
static void create_helpfn(const struct lxc_arguments *args);
static bool validate_bdev_args(struct lxc_arguments *args);
@ -146,40 +144,6 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg)
return 0;
}
static uint64_t get_fssize(char *s)
{
uint64_t ret;
char *end;
ret = strtoull(s, &end, 0);
if (end == s) {
ERROR("Invalid blockdev size '%s', using default size", s);
return 0;
}
while (isblank(*end))
end++;
if (*end == '\0') {
ret *= 1024ULL * 1024ULL; /* MB by default */
} else if (*end == 'b' || *end == 'B') {
ret *= 1ULL;
} else if (*end == 'k' || *end == 'K') {
ret *= 1024ULL;
} else if (*end == 'm' || *end == 'M') {
ret *= 1024ULL * 1024ULL;
} else if (*end == 'g' || *end == 'G') {
ret *= 1024ULL * 1024ULL * 1024ULL;
} else if (*end == 't' || *end == 'T') {
ret *= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
} else {
ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end, s);
return 0;
}
return ret;
}
static void create_helpfn(const struct lxc_arguments *args)
{
char *argv[3], *path;