在我解决Python issue 1578269的努力中,我一直在努力以一种健壮的方式解决符号链接的目标。我按照建议使用getfinalpathnamebyhandlehere on stackoverflow和Microsoft,但是当目标正在使用时(比如pagefile.sys),这项技术失败了。
所以,我编写了一个新的例程来使用createfile和deviceiocontrol来完成这项工作(看起来这就是explorer所做的)。以下是jaraco.windows.filesystem中的相关代码。
问题是,有没有更好的技术来可靠地解决windows中的符号链接?你能找出这个实现中的任何问题吗?
def relpath(path, start=os.path.curdir):
"""
Like os.path.relpath, but actually honors the start path
if supplied. See http://bugs.python.org/issue7195
"""
return os.path.normpath(os.path.join(start, path))
def trace_symlink_target(link):
"""
Given a file that is known to be a symlink, trace it to its ultimate
target.
Raises TargetNotPresent when the target cannot be determined.
Raises ValueError when the specified link is not a symlink.
"""
if not is_symlink(link):
raise ValueError("link must point to a symlink on the system")
while is_symlink(link):
orig = os.path.dirname(link)
link = _trace_symlink_immediate_target(link)
link = relpath(link, orig)
return link
def _trace_symlink_immediate_target(link):
handle = CreateFile(
link,
0,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
None,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
None,
)
res = DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, None, 10240)
bytes = create_string_buffer(res)
p_rdb = cast(bytes, POINTER(REPARSE_DATA_BUFFER))
rdb = p_rdb.contents
if not rdb.tag == IO_REPARSE_TAG_SYMLINK:
raise RuntimeError("Expected IO_REPARSE_TAG_SYMLINK, but got %d" % rdb.tag)
return rdb.get_print_name()
最佳答案
不幸的是,我要到下周才能用vista进行测试,但是getfinalpathnamebyhandle应该可以工作,即使对于正在使用的文件-你注意到了什么问题?
在上面的代码中,您忘记关闭文件句柄。
关于python - Windows的此符号链接(symbolic link)遍历代码是否存在任何问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1616245/