[elbe-devel] [PATCH v3 2/8] Add 'realpath' method to 'Filesystem'
dion at linutronix.de
dion at linutronix.de
Wed May 29 16:24:54 CEST 2019
From: Olivier Dion <dion at linutronix.de>
Signed-off-by: Olivier Dion <dion at linutronix.de>
---
elbepack/filesystem.py | 42 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/elbepack/filesystem.py b/elbepack/filesystem.py
index d9f3e50e..cd5e8133 100644
--- a/elbepack/filesystem.py
+++ b/elbepack/filesystem.py
@@ -84,6 +84,48 @@ class Filesystem(object):
def mkdir(self, path):
os.makedirs(self.fname(path))
+ # Taken from 'posixpath.py'
+ def _joinrealpath(self, path, rest, seen):
+ if os.path.isabs(rest):
+ rest = rest[1:]
+ path = self.path
+
+ while rest:
+ name, _, rest = rest.partition(os.sep)
+ if not name or name == os.curdir:
+ continue
+ if name == os.pardir:
+ if path:
+ # Block going outside of RFS
+ if path == self.path:
+ continue
+ path, name = os.path.split(path)
+ if name == os.pardir:
+ path = os.path.join(path, os.pardir, os.pardir)
+ else:
+ path = os.pardir
+ continue
+ newpath = os.path.join(path, name)
+ if not os.path.islink(newpath):
+ path = newpath
+ continue
+ if newpath in seen:
+ path = seen[newpath]
+ if path is not None:
+ continue
+ return os.path.join(newpath, rest), False
+ seen[newpath] = None
+ path, ok = self._joinrealpath(path, os.readlink(newpath), seen)
+ if not ok:
+ return os.path.join(path, rest), False
+ seen[newpath] = path
+
+ return path, True
+
+ def realpath(self, path):
+ path, ok = self._joinrealpath(self.path, path, {})
+ return os.path.abspath(path)
+
def symlink(self, src, path, allow_exists=False):
try:
os.symlink(src, self.fname(path))
--
2.11.0
More information about the elbe-devel
mailing list