[Python-Dev] [Python-checkins] cpython (2.7): Fix closes issue10761: tarfile.extractall failure when symlinked files are

Nadeem Vawda nadeem.vawda at gmail.com
Thu Apr 28 17:40:05 CEST 2011


On Thu, Apr 28, 2011 at 4:44 PM, Senthil Kumaran <orsenthil at gmail.com> wrote:
> On Thu, Apr 28, 2011 at 04:20:06PM +0200, Éric Araujo wrote:
>> >          if hasattr(os, "symlink") and hasattr(os, "link"):
>> >              # For systems that support symbolic and hard links.
>> >              if tarinfo.issym():
>> > +                if os.path.exists(targetpath):
>> > +                    os.unlink(targetpath)
>>
>> Is there a race condition here?
>
> The lock to avoid race conditions (if you were thinking along those
> lines) would usually be implemented at the higher level code which is
> using extractall in threads.
>
> Checking that no one else is accessing the file before unlinking may
> not be suitable for the library method and of course, we cannot check
> if someone is waiting to act on that file.

I think Éric is referring to the possibility of another process creating or
deleting targetpath between the calls to os.path.exists() and os.unlink().
This would result in symlink() or unlink() raising an exception.

The deletion case could be handled like this:

             if tarinfo.issym():
+                try:
+                    os.unlink(targetpath)
+                except OSError as e:
+                    if e.errno != errno.ENOENT:
+                        raise
                 os.symlink(tarinfo.linkname, targetpath)

I'm not sure what the best way of handling the creation case is. The obvious
solution would be to try the above code in a loop, repeating until we succeed
(or fail for a different reason), but this would not be guaranteed to
terminate.

Cheers,
Nadeem


More information about the Python-Dev mailing list