This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hynek
Recipients hynek, neologix
Date 2012-05-10.19:28:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1336678120.54.0.411663637267.issue14773@psf.upfronthosting.co.za>
In-reply-to
Content
I'm implementing a safe rmtree using fwalk. Everything works perfectly except for one thing: if the directory contains dangling symlinks, fwalk goes belly-up:

$ ls -l test/
total 0
lrwxrwxrwx 1 vagrant vagrant 4 May 10 16:36 doesntwork -> this

$ ./python
Python 3.3.0a3+ (default:b32baa5b7626+, May 10 2012, 14:56:20) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
[71253 refs]
>>> list(os.fwalk('test'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/p/Lib/os.py", line 342, in fwalk
    for x in _fwalk(topfd, top, topdown, onerror, followlinks):
  File "/home/vagrant/p/Lib/os.py", line 361, in _walk
    if st.S_ISDIR(fstatat(topfd, name).st_mode):
FileNotFoundError: [Errno 2] No such file or directory


Unfortunately this makes it impossible to implement rmtree. The reason is the following code:

for name in names:
    # Here, we don't use AT_SYMLINK_NOFOLLOW to be consistent with
    # walk() which reports symlinks to directories as directories. We do
    # however check for symlinks before recursing into a subdirectory.
    if st.S_ISDIR(fstatat(topfd, name).st_mode):
        dirs.append(name)
    else:
        nondirs.append(name)

The "unsafe" walk tree uses os.path.isdir() instead of os.fstatat() and handles this case gracefully.

A simple try-except protection with a symlink check fixes it and the tests pass. This is a blocker for #4489. I have expanded the test of the WalkerTests suite.

Tested on Linux (= works) and OS X (= skipped).
History
Date User Action Args
2012-05-10 19:28:40hyneksetrecipients: + hynek, neologix
2012-05-10 19:28:40hyneksetmessageid: <1336678120.54.0.411663637267.issue14773@psf.upfronthosting.co.za>
2012-05-10 19:28:39hyneklinkissue14773 messages
2012-05-10 19:28:39hynekcreate