[lxc-devel] [lxc/master] 2016 09 25/lxc deslashify
brauner on Github
lxc-bot at linuxcontainers.org
Sun Sep 25 22:21:30 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 347 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160925/47d8c6a5/attachment.bin>
-------------- next part --------------
From 6dfbff859934390f3258b1e0a48016e98da4bb6a Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Sun, 25 Sep 2016 23:57:13 +0200
Subject: [PATCH 1/2] tools: lxc_deslashify() handle special cases
Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
src/lxc/criu.c | 2 +-
src/lxc/utils.c | 36 ++++++++++++++++++++++++++++--------
src/lxc/utils.h | 2 +-
3 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/src/lxc/criu.c b/src/lxc/criu.c
index 67d6fdc..f228736 100644
--- a/src/lxc/criu.c
+++ b/src/lxc/criu.c
@@ -282,7 +282,7 @@ static void exec_criu(struct criu_opts *opts)
}
}
- if (!lxc_deslashify(path)) {
+ if (!lxc_deslashify(&path)) {
ERROR("failed to deslashify %s", path);
free(path);
goto err;
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index c72c26a..4f5ff4e 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -716,21 +716,41 @@ char **lxc_normalize_path(const char *path)
return components;
}
-bool lxc_deslashify(char *path)
+bool lxc_deslashify(char **path)
{
- char **parts = NULL, *path2;
+ return false;
+ char *p;
+ char **parts = NULL;
+ size_t n, len;
- parts = lxc_normalize_path(path);
+ parts = lxc_normalize_path(*path);
if (!parts)
return false;
- path2 = lxc_string_join("/", (const char **) parts, *path == '/');
- lxc_free_array((void **) parts, free);
- if (!path2)
+ /* We'll end up here if path == "///" or path == "". */
+ if (!*parts) {
+ len = strlen(*path);
+ if (!len)
+ return true;
+ n = strcspn(*path, "/");
+ if (n == len) {
+ p = strdup("/");
+ if (!p)
+ return false;
+ free(*path);
+ *path = p;
+ return true;
+ }
+ }
+
+ p = lxc_string_join("/", (const char **)parts, **path == '/');
+ lxc_free_array((void **)parts, free);
+ if (!p)
return false;
- strncpy(path, path2, strlen(path));
- free(path2);
+ free(*path);
+ *path = p;
+
return true;
}
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index c2eef50..88719a0 100644
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -249,7 +249,7 @@ extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_pr
*/
extern char **lxc_normalize_path(const char *path);
/* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
-extern bool lxc_deslashify(char *path);
+extern bool lxc_deslashify(char **path);
extern char *lxc_append_paths(const char *first, const char *second);
/* Note: the following two functions use strtok(), so they will never
* consider an empty element, even if two delimiters are next to
From 7ad23a758fd90047295253b0fc2bffa3d006dcc3 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Sun, 25 Sep 2016 23:57:43 +0200
Subject: [PATCH 2/2] tests: add unit tests for lxc_deslashify()
Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
src/tests/lxc-test-utils.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/src/tests/lxc-test-utils.c b/src/tests/lxc-test-utils.c
index 4c1c373..5b213c8 100644
--- a/src/tests/lxc-test-utils.c
+++ b/src/tests/lxc-test-utils.c
@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -28,6 +29,37 @@
#include "lxctest.h"
#include "utils.h"
+void test_lxc_deslashify(void)
+{
+ char *s = strdup("/A///B//C/D/E/");
+ if (!s)
+ exit(EXIT_FAILURE);
+ lxc_test_assert_abort(lxc_deslashify(&s));
+ lxc_test_assert_abort(strcmp(s, "/A/B/C/D/E") == 0);
+ free(s);
+
+ s = strdup("/A");
+ if (!s)
+ exit(EXIT_FAILURE);
+ lxc_test_assert_abort(lxc_deslashify(&s));
+ lxc_test_assert_abort(strcmp(s, "/A") == 0);
+ free(s);
+
+ s = strdup("");
+ if (!s)
+ exit(EXIT_FAILURE);
+ lxc_test_assert_abort(lxc_deslashify(&s));
+ lxc_test_assert_abort(strcmp(s, "") == 0);
+ free(s);
+
+ s = strdup("//");
+ if (!s)
+ exit(EXIT_FAILURE);
+ lxc_test_assert_abort(lxc_deslashify(&s));
+ lxc_test_assert_abort(strcmp(s, "/") == 0);
+ free(s);
+}
+
void test_lxc_string_replace(void)
{
char *s;
@@ -84,6 +116,7 @@ int main(int argc, char *argv[])
{
test_lxc_string_replace();
test_lxc_string_in_array();
+ test_lxc_deslashify();
exit(EXIT_SUCCESS);
}
More information about the lxc-devel
mailing list