python / python/cpython

tarfile: Extracting symlink using extractfile results in Keyerror: linkname not found

Aperta
#122,736 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

I have the following tar archive containing some files and a symlink to a file that is not part of the tar archive.

drwxr-xr-x guif/users        0 2024-08-06 15:51 test/
-rw-r--r-- guif/users        0 2024-08-06 15:50 test/file3
lrwxrwxrwx guif/users        0 2024-08-06 15:51 test/mylink -> /usr/bin/env
-rw-r--r-- guif/users        0 2024-08-06 15:50 test/file1
-rw-r--r-- guif/users        0 2024-08-06 15:50 test/file2

I want to extract the symlink. With the getmember method I receive a valid member of the tar archive.
Using this member in the extractfile however results in an error.

#!/usr/bin/env python

import tarfile


def main():
    with tarfile.open('./test.tar') as tar_file:
        member = tar_file.getmember('test/mylink')
        file_content = tar_file.extractfile(member)


if __name__ == '__main__':
    main()

Traceback (most recent call last):
  File "/home/guif/repos/private/tmp/./main.py", line 13, in <module>
    main()
  File "/home/guif/repos/private/tmp/./main.py", line 9, in main
    file_content = tar_file.extractfile(member)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/l014xp1qxdl6gim3zc0jv3mpxhbp346s-python3-3.12.4/lib/python3.12/tarfile.py", line 2385, in extractfile
    return self.extractfile(self._find_link_target(tarinfo))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/l014xp1qxdl6gim3zc0jv3mpxhbp346s-python3-3.12.4/lib/python3.12/tarfile.py", line 2725, in _find_link_target
    raise KeyError("linkname %r not found" % linkname)
KeyError: "linkname 'test//usr/bin/env' not found"

As you can see 'test//usr/bin/env can not be found.
The path does not look like the symlink I try to extract but more like <symlink name>/<link target>

From the doc:

Extract a member from the archive as a file object. `member' may be
a filename or a TarInfo object. If `member' is a regular file or
a link, an io.BufferedReader object is returned. For all other
existing members, None is returned. If `member' does not appear
in the archive, KeyError is raised.
CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Leggi tarfile.py intorno a extractfile() e _find_link_target(), quindi esegui l'archivio e lo script forniti per riprodurre il KeyError. Confronta il comportamento documentato per i link con questo caso di destinazione esterna e aggiungi una copertura di regressione che renda esplicito il risultato previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.