代码之家  ›  专栏  ›  技术社区  ›  Jason R. Coombs

这个用于Windows的symlink遍历代码有什么问题吗?

  •  2
  • Jason R. Coombs  · 技术社区  · 15 年前

    在我努力解决 Python issue 1578269 我一直在努力以一种健壮的方式解决符号链接的目标。我按照建议使用getfinalPathNameByHandle开始 here 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()
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   user195808    15 年前

    不幸的是,我要到下周才能用vista进行测试,但是getfinalPathNameByHandle应该可以工作,即使是对于正在使用的文件——您注意到了什么问题? 在上面的代码中,忘记关闭文件句柄。