[elbe-devel] [PATCH] filesystem: make Filesyste.realpath() handle absolute paths in links

Torben Hohn torben.hohn at linutronix.de
Fri Jun 14 17:26:40 CEST 2019


the current implementation fails for absolute links:

>>> from elbepack import filesystem
>>> f = filesystem.Filesystem('xxx')
>>> f.mkdir_p('/usr/bin')
>>> f.symlink('/usr/bin/vim', '/usr/bin/omg')
>>> f.realpath('/usr/bin/omg')
'/home/torbenh/elbe/elbe/xxx/usr/bin/usr/bin/vim'
>>>

handle it by "initialising" path again, when an absolute path
is returned from os.readlink()

this still lacks catching recursive links, and i think, that .. is
not evaluated properly in any case.

Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
 elbepack/filesystem.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/elbepack/filesystem.py b/elbepack/filesystem.py
index 4c925d1c6..91dbe0879 100644
--- a/elbepack/filesystem.py
+++ b/elbepack/filesystem.py
@@ -102,9 +102,14 @@ class Filesystem(object):
                 new_path = os.sep.join(real_path)
                 if os.path.islink(new_path):
                     elements = os.readlink(new_path)
-                    for element in reversed(elements.split(os.sep)):
-                        path.append(element)
-                    real_path.pop()
+                    if os.path.isabs(elements):
+                        path = elements.split(os.sep)
+                        path.reverse()
+                        real_path = [self.path]
+                    else:
+                        for element in reversed(elements.split(os.sep)):
+                            path.append(element)
+                        real_path.pop()
         return os.sep.join(real_path)
 
     def symlink(self, src, path, allow_exists=False):
-- 
2.11.0




More information about the elbe-devel mailing list