[Python-checkins] python/dist/src/Lib posixpath.py, 1.62.6.1, 1.62.6.2

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Sun Jul 11 00:58:34 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17214/Lib

Modified Files:
      Tag: release23-maint
	posixpath.py 
Log Message:
posixpath.realpath() now detects loops from symlinks and returns the longest
path before recursion.


Index: posixpath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/posixpath.py,v
retrieving revision 1.62.6.1
retrieving revision 1.62.6.2
diff -C2 -d -r1.62.6.1 -r1.62.6.2
*** posixpath.py	12 May 2004 03:47:01 -0000	1.62.6.1
--- posixpath.py	10 Jul 2004 22:58:31 -0000	1.62.6.2
***************
*** 409,420 ****
      for i in range(2, len(bits)+1):
          component = join(*bits[0:i])
!         if islink(component):
!             resolved = os.readlink(component)
!             (dir, file) = split(component)
!             resolved = normpath(join(dir, resolved))
!             newpath = join(*([resolved] + bits[i:]))
!             return realpath(newpath)
  
      return filename
  
  supports_unicode_filenames = False
--- 409,444 ----
      for i in range(2, len(bits)+1):
          component = join(*bits[0:i])
!         # Resolve symbolic links.
! 	if islink(component):
!             resolved = _resolve_link(component)
!             if resolved is None:
!                 # Infinite loop -- return original component + rest of the path
!                 return join(*([component] + bits[i:]))
!             else:
!                 newpath = join(*([resolved] + bits[i:]))
!                 return realpath(newpath)
  
      return filename
  
+ 
+ def _resolve_link(path):
+     """Internal helper function.  Takes a path and follows symlinks
+     until we either arrive at something that isn't a symlink, or 
+     encounter a path we've seen before (meaning that there's a loop).
+     """
+     paths_seen = []
+     while islink(path):
+ 	if path in paths_seen:
+             # Already seen this path, so we must have a symlink loop
+             return None
+ 	paths_seen.append(path)
+         # Resolve where the link points to
+ 	resolved = os.readlink(path)
+         if not abspath(resolved):
+             dir = dirname(path)
+             path = normpath(join(dir, resolved))
+         else:
+             path = normpath(resolved)
+     return path
+ 
  supports_unicode_filenames = False



More information about the Python-checkins mailing list