python / python/cpython

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

未关闭
#122,736 2 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 tarfile.py 中 extractfile() 和 _find_link_target() 附近的代码,然后运行提供的归档文件和脚本以复现 KeyError。将文档所述的链接行为与这个外部目标情况进行比较,并添加回归测试,明确预期结果。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。