python / python/cpython

`shutil.copytree()` incorrectly classifies relative symlinks as dangling

未关闭
#156,210 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:
Summary

shutil.copytree(..., symlinks=False, ignore_dangling_symlinks=True) tests a relative symlink target against the process current working directory rather than against the directory containing the symlink. Consequently, a valid relative symlink can be silently omitted from a successful copy, and a truly dangling symlink can still cause shutil.Error when the current working directory happens to contain a file with the same name as its target.

Affected code

Lib/shutil.py, _copytree():

linkto = os.readlink(srcname)
...
if not os.path.exists(linkto) and ignore_dangling_symlinks:
    continue

os.readlink() returns the raw target. A relative target is interpreted by the operating system relative to the symlink's parent directory, but os.path.exists(linkto) interprets it relative to the process current working
directory.

Reproducer
import os
import shutil
import tempfile

with tempfile.TemporaryDirectory() as root:
    src = os.path.join(root, "src")
    dst = os.path.join(root, "dst")
    os.mkdir(src)
    with open(os.path.join(src, "target"), "w") as f:
        f.write("data")
    os.symlink("target", os.path.join(src, "link"))

    old_cwd = os.getcwd()
    try:
        os.chdir(root)  # root/target does not exist
        shutil.copytree(src, dst, ignore_dangling_symlinks=True)
        print("exception: none")
    finally:
        os.chdir(old_cwd)

    print("source link exists?", os.path.lexists(os.path.join(src, "link")))
    print("destination entries:", sorted(os.listdir(dst)))
    print("destination link exists?", os.path.lexists(os.path.join(dst, "link")))

Actual output:

exception: none
source link exists? True
destination entries: ['target']
destination link exists? False

The source symlink is valid: src/link -> target resolves to src/target. With symlinks=False, the destination should contain a regular link file with the contents of src/target, in addition to target. Instead, the operation returns successfully with an incomplete destination tree.

The inverse case also fails: if src/link -> target is actually dangling but the current working directory contains target, it is not ignored and copytree() raises shutil.Error after creating the destination directory.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-156214

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Lib/shutil.py 中的 _copytree() 开始,并在进行更改之前检查所链接的 PR gh-156214。复现 issue 中的相对符号链接案例,然后在现有的 shutil 测试中添加或更新回归测试覆盖,确保无论当前工作目录是什么,有效链接都会被复制,而悬空链接会被忽略。

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

评估

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

把新 issue 发到你的邮箱

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