这个用于Windows的符号链接遍历代码有任何问题吗?

时间:2009-10-23 22:24:19

标签: python windows winapi filesystems symlink-traversal

在努力解决Python issue 1578269时,我一直在努力尝试以健壮的方式解决符号链接的目标。我开始使用GetFinalPathNameByHandle作为推荐的here on stackoverflowMicrosoft,但事实证明,当使用目标时(例如使用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()

1 个答案:

答案 0 :(得分:0)

不幸的是,直到下周我都无法使用Vista进行测试,但GetFinalPathNameByHandle应该可以工作,即使对于正在使用的文件 - 您注意到的问题是什么? 在上面的代码中,您忘记关闭文件句柄。