Browse code

Fix leak in recursive-symlink-filtering algorithm

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/branches/amax-follow-symlinks@433 830e0280-6d2a-0410-9c65-932aecc39d9d

Aaron Maxwell authored on 2010/07/26 04:35:54
Showing 1 changed files
... ...
@@ -39,19 +39,22 @@ def check_args_type(args, type, verbose_type):
39 39
 def fswalk_follow_symlinks(path):
40 40
         '''
41 41
         Walk filesystem, following symbolic links (but without recursion), on python2.4 and later
42
+
43
+        If a recursive directory link is detected, emit a warning and skip.
42 44
         '''
43 45
         assert os.path.isdir(path) # only designed for directory argument
44 46
         walkdirs = set([path])
45
-        symlink_targets = set()
47
+        targets = set()
46 48
         for dirpath, dirnames, filenames in os.walk(path):
47 49
                 for dirname in dirnames:
48
-                        if os.path.islink(dirname):
49
-                                target = os.path.realpath(dirname)
50
-                                if target in symlink_targets:
51
-                                        warning(u"Skipping recursively symlinked directory %s" % dirname)
50
+                        current = os.path.join(dirpath, dirname)
51
+                        target = os.path.realpath(current)
52
+                        if os.path.islink(current):
53
+                                if target in targets:
54
+                                        warning("Skipping recursively symlinked directory %s" % dirname)
52 55
                                 else:
53
-                                        walkdirs.add(dirname)
54
-                                        symlink_targets.add(target)
56
+                                        walkdirs.add(current)
57
+                        targets.add(target)
55 58
         for walkdir in walkdirs:
56 59
                 for value in os.walk(walkdir):
57 60
                         yield value