From ce8d6217fe7ed4d40af23242be4eaef8e53385a3 Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Wed, 26 Aug 2020 10:43:31 +0200
Subject: Fix tree node path generation for detached sub trees

The function sqfs_tree_node_get_path is used in several places within
rdsquashfs to produce a path for a tree node, either when describing
the file system, or when unpacking it. Unpacking can be done on
sub-trees as well as the entire tree, in which case the root of the
sub-tree has its parent pointer removed, so the full path terminates
at the new root.

This works with directories, since they receive special case handling
anyway, but fails if the sub-tree to unpack is only a single file
because the sqfs_tree_node_get_path function assumes that we are at the
tree root and returns "/" as a path, which gets normalized to "".

This commit adds a workaround to the function to simply use the nodes
name (if available) in that case instead.

The describe case in rdsquashfs is unaffacted, since it always starts
at the root. Likewise, the sqfs2tar case should also be unaffacted,
since it already employs special case handling for the [sub] tree root
node.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 lib/common/get_path.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/common/get_path.c b/lib/common/get_path.c
index bdc6c3f..72ed1bd 100644
--- a/lib/common/get_path.c
+++ b/lib/common/get_path.c
@@ -15,8 +15,12 @@ char *sqfs_tree_node_get_path(const sqfs_tree_node_t *node)
 	char *str, *ptr;
 	size_t len = 0;
 
-	if (node->parent == NULL)
+	if (node->parent == NULL) {
+		if (node->name[0] != '\0')
+			return strdup((const char *)node->name);
+
 		return strdup("/");
+	}
 
 	for (it = node; it != NULL && it->parent != NULL; it = it->parent) {
 		len += strlen((const char *)it->name) + 1;
-- 
cgit v1.2.3