[lxc-devel] [lxc/master] storage_utils: move duplicated function from tools

2xsec on Github lxc-bot at linuxcontainers.org
Sat Aug 18 09:08:42 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 485 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180818/af265531/attachment.bin>
-------------- next part --------------
From 2b670dfeb01e571c341891632886344f26bd676a Mon Sep 17 00:00:00 2001
From: 2xsec <dh48.jeong at samsung.com>
Date: Sat, 18 Aug 2018 18:05:32 +0900
Subject: [PATCH] storage_utils: move duplicated function from tools

Signed-off-by: 2xsec <dh48.jeong at samsung.com>
---
 src/lxc/storage/storage_utils.c | 35 ++++++++++++++++++++++++++++++++
 src/lxc/storage/storage_utils.h |  1 +
 src/lxc/tools/lxc_copy.c        | 36 +--------------------------------
 src/lxc/tools/lxc_create.c      | 36 ---------------------------------
 4 files changed, 37 insertions(+), 71 deletions(-)

diff --git a/src/lxc/storage/storage_utils.c b/src/lxc/storage/storage_utils.c
index b0a190438..da1ed7fcc 100644
--- a/src/lxc/storage/storage_utils.c
+++ b/src/lxc/storage/storage_utils.c
@@ -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 ||
diff --git a/src/lxc/storage/storage_utils.h b/src/lxc/storage/storage_utils.h
index 3c6bbb0d7..905233a24 100644
--- a/src/lxc/storage/storage_utils.h
+++ b/src/lxc/storage/storage_utils.h
@@ -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);
 
diff --git a/src/lxc/tools/lxc_copy.c b/src/lxc/tools/lxc_copy.c
index 33e4f9a76..2d6080134 100644
--- a/src/lxc/tools/lxc_copy.c
+++ b/src/lxc/tools/lxc_copy.c
@@ -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;
diff --git a/src/lxc/tools/lxc_create.c b/src/lxc/tools/lxc_create.c
index 8423ff30d..ae72f929e 100644
--- a/src/lxc/tools/lxc_create.c
+++ b/src/lxc/tools/lxc_create.c
@@ -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;


More information about the lxc-devel mailing list