在Python中创建NTFS连接点

在Python中创建NTFS连接点,python,windows,ntfs,junction,Python,Windows,Ntfs,Junction,有没有办法在Python中创建NTFS连接点?我知道我可以调用junction实用程序,但最好不要依赖外部工具。您不想依赖外部工具,但不介意依赖特定环境?我认为您可以安全地假设,如果您运行的是NTFS,那么junction实用程序可能就在那里 但是,如果你的意思是你不想打电话给外部程序,我发现这些东西非常宝贵。它允许您直接从Python调用Windows DLL。我敢肯定,它现在已经出现在标准Python版本中了 您只需找出CreateJunction()(或任何Windows调用的)API调用

有没有办法在Python中创建NTFS连接点?我知道我可以调用
junction
实用程序,但最好不要依赖外部工具。

您不想依赖外部工具,但不介意依赖特定环境?我认为您可以安全地假设,如果您运行的是NTFS,那么junction实用程序可能就在那里

但是,如果你的意思是你不想打电话给外部程序,我发现这些东西非常宝贵。它允许您直接从Python调用Windows DLL。我敢肯定,它现在已经出现在标准Python版本中了

您只需找出
CreateJunction()
(或任何Windows调用的)API调用所在的Windows DLL,然后设置参数和调用。幸运的是,微软似乎并不十分支持它。您可以反汇编SysInternals
junction
程序或
linkd
或其他工具之一,以了解它们是如何实现的


我,我很懒,我只需要调用
junction
作为外部进程:-)

您可以使用python win32 API模块,例如

import win32file

win32file.CreateSymbolicLink(srcDir, targetDir, 1)
有关更多详细信息,请参阅

如果您不想依赖于此,您可以始终使用ctypes并直接调用CreateSymbolicLinl win32 API,这是一个简单的调用

下面是使用ctypes的示例调用

import ctypes

kdll = ctypes.windll.LoadLibrary("kernel32.dll")

kdll.CreateSymbolicLinkA("d:\testdir", "d:\testdir_link", 1)
表示支持的最小客户端Windows Vista

我在a中回答了这个问题,所以我将把我的答案复制到下面。在写了这个答案之后,我最终只写了一个python模块(如果您可以调用一个只使用ctypes python的模块)来创建、读取和检查可以在中找到的连接。希望有帮助

此外,与使用CreateSymbolicLinkAAPI的答案不同,链接的实现应该适用于支持连接的任何Windows版本。CreateSymbolicLinkA仅在Vista+中受支持

回答:

或者,如果要使用pywin32,可以使用前面提到的方法,要读取,请使用:

from win32file import *
from winioctlcon import FSCTL_GET_REPARSE_POINT

__all__ = ['islink', 'readlink']

# Win32file doesn't seem to have this attribute.
FILE_ATTRIBUTE_REPARSE_POINT = 1024
# To make things easier.
REPARSE_FOLDER = (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)

# For the parse_reparse_buffer function
SYMBOLIC_LINK = 'symbolic'
MOUNTPOINT = 'mountpoint'
GENERIC = 'generic'

def islink(fpath):
    """ Windows islink implementation. """
    if GetFileAttributes(fpath) & REPARSE_FOLDER:
        return True
    return False


def parse_reparse_buffer(original, reparse_type=SYMBOLIC_LINK):
    """ Implementing the below in Python:

    typedef struct _REPARSE_DATA_BUFFER {
        ULONG  ReparseTag;
        USHORT ReparseDataLength;
        USHORT Reserved;
        union {
            struct {
                USHORT SubstituteNameOffset;
                USHORT SubstituteNameLength;
                USHORT PrintNameOffset;
                USHORT PrintNameLength;
                ULONG Flags;
                WCHAR PathBuffer[1];
            } SymbolicLinkReparseBuffer;
            struct {
                USHORT SubstituteNameOffset;
                USHORT SubstituteNameLength;
                USHORT PrintNameOffset;
                USHORT PrintNameLength;
                WCHAR PathBuffer[1];
            } MountPointReparseBuffer;
            struct {
                UCHAR  DataBuffer[1];
            } GenericReparseBuffer;
        } DUMMYUNIONNAME;
    } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;

    """
    # Size of our data types
    SZULONG = 4 # sizeof(ULONG)
    SZUSHORT = 2 # sizeof(USHORT)

    # Our structure.
    # Probably a better way to iterate a dictionary in a particular order,
    # but I was in a hurry, unfortunately, so I used pkeys.
    buffer = {
        'tag' : SZULONG,
        'data_length' : SZUSHORT,
        'reserved' : SZUSHORT,
        SYMBOLIC_LINK : {
            'substitute_name_offset' : SZUSHORT,
            'substitute_name_length' : SZUSHORT,
            'print_name_offset' : SZUSHORT,
            'print_name_length' : SZUSHORT,
            'flags' : SZULONG,
            'buffer' : u'',
            'pkeys' : [
                'substitute_name_offset',
                'substitute_name_length',
                'print_name_offset',
                'print_name_length',
                'flags',
            ]
        },
        MOUNTPOINT : {
            'substitute_name_offset' : SZUSHORT,
            'substitute_name_length' : SZUSHORT,
            'print_name_offset' : SZUSHORT,
            'print_name_length' : SZUSHORT,
            'buffer' : u'',
            'pkeys' : [
                'substitute_name_offset',
                'substitute_name_length',
                'print_name_offset',
                'print_name_length',
            ]
        },
        GENERIC : {
            'pkeys' : [],
            'buffer': ''
        }
    }

    # Header stuff
    buffer['tag'] = original[:SZULONG]
    buffer['data_length'] = original[SZULONG:SZUSHORT]
    buffer['reserved'] = original[SZULONG+SZUSHORT:SZUSHORT]
    original = original[8:]

    # Parsing
    k = reparse_type
    for c in buffer[k]['pkeys']:
        if type(buffer[k][c]) == int:
            sz = buffer[k][c]
            bytes = original[:sz]
            buffer[k][c] = 0
            for b in bytes:
                n = ord(b)
                if n:
                    buffer[k][c] += n
            original = original[sz:]

    # Using the offset and length's grabbed, we'll set the buffer.
    buffer[k]['buffer'] = original
    return buffer

def readlink(fpath):
    """ Windows readlink implementation. """
    # This wouldn't return true if the file didn't exist, as far as I know.
    if not islink(fpath):
        return None

    # Open the file correctly depending on the string type.
    handle = CreateFileW(fpath, GENERIC_READ, 0, None, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, 0) \
                if type(fpath) == unicode else \
            CreateFile(fpath, GENERIC_READ, 0, None, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, 0)

    # MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16384 = (16*1024)
    buffer = DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, None, 16*1024)
    # Above will return an ugly string (byte array), so we'll need to parse it.

    # But first, we'll close the handle to our file so we're not locking it anymore.
    CloseHandle(handle)

    # Minimum possible length (assuming that the length of the target is bigger than 0)
    if len(buffer) < 9:
        return None
    # Parse and return our result.
    result = parse_reparse_buffer(buffer)
    offset = result[SYMBOLIC_LINK]['substitute_name_offset']
    ending = offset + result[SYMBOLIC_LINK]['substitute_name_length']
    rpath = result[SYMBOLIC_LINK]['buffer'][offset:ending].replace('\x00','')
    if len(rpath) > 4 and rpath[0:4] == '\\??\\':
        rpath = rpath[4:]
    return rpath

def realpath(fpath):
    from os import path
    while islink(fpath):
        rpath = readlink(fpath)
        if not path.isabs(rpath):
            rpath = path.abspath(path.join(path.dirname(fpath), rpath))
        fpath = rpath
    return fpath


def example():
    from os import system, unlink
    system('cmd.exe /c echo Hello World > test.txt')
    system('mklink test-link.txt test.txt')
    print 'IsLink: %s' % islink('test-link.txt')
    print 'ReadLink: %s' % readlink('test-link.txt')
    print 'RealPath: %s' % realpath('test-link.txt')
    unlink('test-link.txt')
    unlink('test.txt')

if __name__=='__main__':
    example()

如果您将其放入正在发布的内容中,因为这种形式的符号链接仅在Vista+上受支持。

自Python 3.5以来,
\u winapi
模块中有一个函数
CreateJunction

import _winapi
_winapi.CreateJunction(source, target)

根据Charles接受的答案,这里是函数的改进(和跨平台)版本(Python2.7和3.5+)

  • islink()现在还检测Windows下的文件符号链接(与POSIX等价物一样)
  • parse_repasse_buffer()和readlink()现在实际检测正确解码路径所需的重分析点类型(NTFS连接、符号链接或通用)
  • readlink()不再因NTFS连接或目录符号链接上的访问被拒绝而失败(除非您确实没有读取属性的权限)

导入操作系统
导入结构
导入系统
如果sys.platform==“win32”:
从Win32文件导入*
从winioctlcon导入FSCTL\u获取\u重新分析\u点
__全部链接=['islink','readlink']
#Win32file似乎没有此属性。
文件\属性\重新分析\点=1024
#它们在win32\lib\winnt.py中定义,但具有错误的值
IO_重新分析_标签_安装_点=0xA0000003#连接
IO_重新分析_标记_符号链接=0xA000000C
def islink(路径):
"""
跨平台islink实现。
支持Windows NT符号链接和重分析点。
"""
如果sys.platform!=“win32”或sys.getwindowsversion()[0]<6:
返回os.path.islink(路径)
返回bool(os.path.exists(path)和GetFileAttributes(path)&
文件\属性\重新分析\点==文件\属性\重新分析\点)
def解析\重新解析\缓冲区(buf):
“”“在Python中实现以下功能:
typedef结构\u重新分析\u数据\u缓冲区{
乌龙重铺标签;
USHORT-repassedatalength;
乌肖特保留;
联合{
结构{
USHORT替代物偏移量;
USHORT替代漆包线;
USHORT PrintNameOffset;
USHORT PrintNameLength;
乌龙旗;
WCHAR路径缓冲区[1];
}符号缓冲区;
结构{
USHORT替代物偏移量;
USHORT替代漆包线;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR路径缓冲区[1];
}mountpointrepassebuffer;
结构{
UCHAR-DataBuffer[1];
}通用内存缓冲区;
}Dummy名称;
}重新分析数据缓冲区,*准备数据缓冲区;
"""
#看https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntifs/ns-ntifs-_reparse_data_buffer

数据={'tag':struct.unpack('ctypes从2.5版开始包含在Python中。junction命令在Vista和Win7上不存在。它已被mklink取代。它作为系统内部工具junction存在。@Charles,一揽子断言很少是个好主意。您可以使用
DeviceIoControl
,传递
SET\u Repasse\u POINT
,创建联接。很抱歉这样做。我不是说你做不到,我是说Windows API不提供函数调用来在一条指令中创建它…我认为连接是从Win2K开始的,但不是正式的(或者很好的)由MS支持,考虑到缺乏关于如何操作的文档。新的符号链接看起来好多了,特别是因为你可以对文件进行链接,而且(我认为)它们现在可以跨网络。连接不是符号链接的子集。连接仅适用于目录。这个答案不正确,并且为文件创建了符号链接(仅适用于Vista和更高版本)而不是目录的连接点(适用于Windows 2000中的NTFS)和更高版本。不幸的是,在Python中没有真正简单的实现方法。参考Mike McQuaid的评论,投票被否决。我正在寻找连接点,即指向direc的硬链接
import _winapi
_winapi.CreateJunction(source, target)
import os
import struct
import sys

if sys.platform == "win32":
    from win32file import *
    from winioctlcon import FSCTL_GET_REPARSE_POINT

__all__ = ['islink', 'readlink']

# Win32file doesn't seem to have this attribute.
FILE_ATTRIBUTE_REPARSE_POINT = 1024

# These are defined in win32\lib\winnt.py, but with wrong values
IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003  # Junction
IO_REPARSE_TAG_SYMLINK = 0xA000000C

def islink(path):
    """
    Cross-platform islink implementation.

    Supports Windows NT symbolic links and reparse points.

    """
    if sys.platform != "win32" or sys.getwindowsversion()[0] < 6:
        return os.path.islink(path)
    return bool(os.path.exists(path) and GetFileAttributes(path) &
                FILE_ATTRIBUTE_REPARSE_POINT == FILE_ATTRIBUTE_REPARSE_POINT)


def parse_reparse_buffer(buf):
    """ Implementing the below in Python:

    typedef struct _REPARSE_DATA_BUFFER {
        ULONG  ReparseTag;
        USHORT ReparseDataLength;
        USHORT Reserved;
        union {
            struct {
                USHORT SubstituteNameOffset;
                USHORT SubstituteNameLength;
                USHORT PrintNameOffset;
                USHORT PrintNameLength;
                ULONG Flags;
                WCHAR PathBuffer[1];
            } SymbolicLinkReparseBuffer;
            struct {
                USHORT SubstituteNameOffset;
                USHORT SubstituteNameLength;
                USHORT PrintNameOffset;
                USHORT PrintNameLength;
                WCHAR PathBuffer[1];
            } MountPointReparseBuffer;
            struct {
                UCHAR  DataBuffer[1];
            } GenericReparseBuffer;
        } DUMMYUNIONNAME;
    } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;

    """
    # See https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntifs/ns-ntifs-_reparse_data_buffer

    data = {'tag': struct.unpack('<I', buf[:4])[0],
            'data_length': struct.unpack('<H', buf[4:6])[0],
            'reserved': struct.unpack('<H', buf[6:8])[0]}
    buf = buf[8:]

    if data['tag'] in (IO_REPARSE_TAG_MOUNT_POINT, IO_REPARSE_TAG_SYMLINK):
        keys = ['substitute_name_offset',
                'substitute_name_length',
                'print_name_offset',
                'print_name_length']
        if data['tag'] == IO_REPARSE_TAG_SYMLINK:
            keys.append('flags')

        # Parsing
        for k in keys:
            if k == 'flags':
                fmt, sz = '<I', 4
            else:
                fmt, sz = '<H', 2
            data[k] = struct.unpack(fmt, buf[:sz])[0]
            buf = buf[sz:]

    # Using the offset and lengths grabbed, we'll set the buffer.
    data['buffer'] = buf

    return data


def readlink(path):
    """
    Cross-platform implenentation of readlink.

    Supports Windows NT symbolic links and reparse points.

    """
    if sys.platform != "win32":
        return os.readlink(path)

    # This wouldn't return true if the file didn't exist
    if not islink(path):
        # Mimic POSIX error
        raise OSError(22, 'Invalid argument', path)

    # Open the file correctly depending on the string type.
    if type(path) is type(u''):
        createfilefn = CreateFileW
    else:
        createfilefn = CreateFile
    # FILE_FLAG_OPEN_REPARSE_POINT alone is not enough if 'path'
    # is a symbolic link to a directory or a NTFS junction.
    # We need to set FILE_FLAG_BACKUP_SEMANTICS as well.
    # See https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea
    handle = createfilefn(path, GENERIC_READ, 0, None, OPEN_EXISTING,
                          FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, 0)

    # MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16384 = (16 * 1024)
    buf = DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, None, 16 * 1024)
    # Above will return an ugly string (byte array), so we'll need to parse it.

    # But first, we'll close the handle to our file so we're not locking it anymore.
    CloseHandle(handle)

    # Minimum possible length (assuming that the length is bigger than 0)
    if len(buf) < 9:
        return type(path)()
    # Parse and return our result.
    result = parse_reparse_buffer(buf)
    if result['tag'] in (IO_REPARSE_TAG_MOUNT_POINT, IO_REPARSE_TAG_SYMLINK):
        offset = result['substitute_name_offset']
        ending = offset + result['substitute_name_length']
        rpath = result['buffer'][offset:ending].decode('UTF-16-LE')
    else:
        rpath = result['buffer']
    if len(rpath) > 4 and rpath[0:4] == '\\??\\':
        rpath = rpath[4:]
    return rpath