Search code examples
pythontartarfile

How to disable creating hardlinks


Using python's tarfile module, is there a way to specify the equivalent of tar's Q option (note: Haiku specific)?

tar --help
  -Q, --replace-hardlinks    replace hardlinks with corresponding symlink when
                             extracting

Currently the code is essentially:

tarFile = tarfile.open(archiveFile, 'r')
members = None
if subdir:
    members = [
        member for member in tarFile.getmembers()
        if member.name.startswith(subdir)
    ]
tarFile.extractall(targetBaseDir, members)
tarFile.close()

Solution

  • You can work around it by using the islink() method of the TarInfo objects returned by members. Something like the following:

    tarFile = tarfile.open(archiveFile, 'r')
       for member in tarFile.getmembers():
            if member.islnk():
                 # code for handling links
            else:
                tarFile.extract(targetBaseDir, member)
    tarFile.close()