tarfile: Extracting symlink using extractfile results in Keyerror: linkname not found
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Đọc tarfile.py xung quanh extractfile() và _find_link_target(), sau đó chạy archive và script được cung cấp để tái hiện KeyError. So sánh hành vi được tài liệu hóa đối với các liên kết với trường hợp đích bên ngoài này và bổ sung kiểm thử hồi quy làm rõ kết quả dự kiến.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- tooling
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100