[CRIU] [PATCH 2/9] mount: Add helper for path length calculations

Pavel Emelyanov xemul at parallels.com
Tue Nov 11 06:49:51 PST 2014


The path lenght is zero for the "/" one and strlen(path)
for all the others. This is done so to make it possible
to use this length to get tail-paths: if path_1 starts
with path_2 and both are absolute, then 

   path_1 + path_length(path_2)
	
would give the tail of the tail of path_1 relative to
path_2 even if the path_2 is just "/".

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 mount.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/mount.c b/mount.c
index d4db1ff..75fcfad 100644
--- a/mount.c
+++ b/mount.c
@@ -388,6 +388,25 @@ static struct mount_info *get_widest_peer(struct mount_info *m)
 	return NULL;
 }
 
+static inline int path_length(char *path)
+{
+	int off;
+
+	off = strlen(path);
+	/*
+	 * If we're pure / then set lenght to zero so that adding this
+	 * value as sub-path offset would produce the correct result.
+	 * E.g. the tail path of the "/foo/bar" relative to the "/foo"
+	 * will be the "/foo/bar" + len("/foo") == "/bar", while the
+	 * same relative to the "/" should be +0 to be the "/foo/bar",
+	 * not +1 and the "foo/bar".
+	 */
+	if (path[off - 1] == '/')
+		off--;
+
+	return off;
+}
+
 static int validate_shared(struct mount_info *m)
 {
 	struct mount_info *t, *ct, *cm, *tmp;
@@ -417,19 +436,10 @@ static int validate_shared(struct mount_info *m)
 
 	/* A set of childrent which ar visiable for both should be the same */
 
-	t_root_len = strlen(t->root);
-	m_root_len = strlen(m->root);
-	tpm = strlen(t->mountpoint);
-	mpm = strlen(m->mountpoint);
-
-	if (t->root[t_root_len - 1] == '/')
-		t_root_len--;
-	if (m->root[m_root_len - 1] == '/')
-		m_root_len--;
-	if (t->mountpoint[tpm - 1] == '/')
-		tpm--;
-	if (m->mountpoint[mpm - 1] == '/')
-		mpm--;
+	t_root_len = path_length(t->root);
+	m_root_len = path_length(m->root);
+	tpm = path_length(t->mountpoint);
+	mpm = path_length(m->mountpoint);
 
 	/* For example:
 	 * t->root = /		t->mp = ./zdtm/live/static/mntns_root_bind.test
-- 
1.8.4.2




More information about the CRIU mailing list