`add_file_from_memory` silently fails for symbolic links
還沒有人認領這個 Issue。
評估
研究方向
Start with the linkpath setter in libarchive/entry.py and trace how add_file_from_memory prepares entries for the CPIO writer. The report identifies entry_set_symlink as a possible fix and explains that older system libarchive versions may lack entry_set_link_to_symlink. Check the behavior with the reproduction script; done means the resulting CPIO entry retains the symlink target and its size.
由索引模型根據 Issue 內容生成。
描述
When creating CPIO archives, symlinks are discarded.
How to reproduce?
I created this simple test:
import libarchive
from ctypes import c_void_p
def print_archive_info(test_name, archive_bytes):
print(test_name)
with libarchive.memory_reader(archive_bytes) as archive:
for entry in archive:
print(f" entry.path: {entry.path}")
print(f" entry.size: {entry.size}")
print(f" entry.filetype: 0o{entry.filetype:o}")
print(f" entry.linkpath: {entry.linkpath}")
packed_chunks = []
def write_func(data):
packed_chunks.append(bytes(data))
return len(data)
path = "sbin"
linkpath = "usr/sbin"
entry_size = len(linkpath)
with libarchive.custom_writer(write_func, "cpio_newc") as archive:
archive.add_file_from_memory(
entry_path=path,
entry_size=len(linkpath),
entry_data=b"",
filetype=libarchive.entry.FileType.SYMBOLINK_LINK,
linkpath=linkpath.encode("utf-8"),
)
packed_bytes = b"".join(packed_chunks)
print_archive_info("libarchive standard method:", packed_bytes)
# fix that works:
packed_chunks = []
with libarchive.custom_writer(write_func, "cpio_newc") as archive:
new_entry = libarchive.entry.ArchiveEntry(
pathname=path,
size=len(linkpath),
filetype=libarchive.entry.FileType.SYMBOLINK_LINK,
linkpath=linkpath.encode("utf-8"),
)
libarchive.ffi.ffi('entry_set_symlink', [c_void_p], None)
libarchive.ffi.entry_set_symlink(new_entry._entry_p, linkpath.encode("utf-8"))
libarchive.ffi.write_header(archive._pointer, new_entry._entry_p)
libarchive.ffi.write_finish_entry(archive._pointer)
packed_bytes = b"".join(packed_chunks)
print_archive_info("libarchive fix method:", packed_bytes)
Which outputs the following:
$ python3 libarchive_symlink.py
libarchive standard method:
entry.path: sbin
entry.size: 0
entry.filetype: 0o120000
entry.linkpath:
libarchive fix method:
entry.path: sbin
entry.size: 8
entry.filetype: 0o120000
entry.linkpath: usr/sbin
You can see that using add_file_from_memory with the linkpath argument, the resulting symlink size is 0, and the symlink doesn't actually point to anything.
Investigations
I spent some time investigated this. Here is what happens when using add_file_from_memory:
- in the @linkpath.setter,
ffi.entry_update_link_utf8(self._entry_p, value)is called- in libarchive, this corresponds to archive_entry_update_link_utf8. As some other comments hint: "Set symlink if symlink is already set, else set hardlink". So at this point it sets the file to a hardlink.
- when the entry header is created, the CPIO writer runs into this in write_header:
/* Non-regular files don't store bodies. */
if (archive_entry_filetype(entry) != AE_IFREG)
archive_entry_set_size(entry, 0);
- then only if the file is a symlink, it will get its size and write the linkpath, etc
- as we mentioned, our file is a hardlink at this point, so its size gets set to
0, and no linkpath is inserted
Fix
As you can see in the test script above, I found a fix for this issue: if we call ffi.entry_set_symlink before writing the header file, then the file is converted from hardlink to symlink, and the CPIO writer actually writes the right size and linkpath as you would expect.
This entry_set_symlink needs to be imported from the C libarchive library. entry_set_link_to_symlink is also available in the latest libarchive, which would avoid having to copy the linkpath again. But older libarchive binaries won't have this function (was testing on MacOS 15.7.1 and the system libarchive didn't have it), which is why I used entry_set_symlink.
I am not sure if this issue also affect other format writers other than CPIO. If so, I think the call to entry_set_symlink could happen at the end of @linkpath.setter. If this issue is specific to the CPIO writers, then a more targeted fix should be developed.
- 主要語言
- Python
- 星號
- 84
- 分支
- 41
- 平均合併
- 6 天 12 小時
- 30 天內合併 PR
- 1
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
Changaco/python-libarchive-c 的其他 Issue
-
feature
難度 2/5 1-3 小時 新手友好度 51/100
Changaco/python-libarchive-c#144 ·
-
難度 4/5 3-5 天 新手友好度 25/100
Changaco/python-libarchive-c#129 ·
-
難度 4/5 3-5 天 新手友好度 20/100
Changaco/python-libarchive-c#122 · 1 則留言 ·
-
Type checking未關閉feature
難度 5/5 一週以上 新手友好度 35/100
Changaco/python-libarchive-c#111 ·
-
feature
難度 2/5 1-3 小時 新手友好度 35/100
Changaco/python-libarchive-c#100 · 2 則留言 ·
查看 Changaco/python-libarchive-c 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 76/100
microsoft/agent-lightning#661 ·
維護者通常 2 天內回覆
-
area/profiles comp/cli P3 type/feature
難度 2/5 1-3 小時 新手友好度 74/100
NousResearch/hermes-agent#146372 ·
維護者通常 1 天內回覆
-
[BUG] OpenAI-compatible embedding model alias breaks embedder construction可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 65/100
維護者通常 1 天內回覆
-
[oblt-aw][autodoc] Document obs-aw-automerge contract and fix automerge doc inconsistencies可能已有人在做 @github-actions 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
GeoSeries scale and skew tests fail with pytest 9.2.0可能已有人在做 @jiayuasu 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
維護者通常 1 天內回覆