从Python调用FSCTL_CREATE_或_GET_OBJECT_ID

2024-04-18 23:49:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让NTFS对象id在Python备份程序中使用。我有点不知所措,但设法创建了一个函数,返回。。。什么。在

import sys
import win32file
import winioctlcon


def object_id(filename):
    """
    NTFS OBJECT_ID
    """
    fhandle = win32file.CreateFileW(
        # FileName
        filename,

        # DesiredAccess
        win32file.GENERIC_READ,

        # ShareMode
        win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,

        # SecurityAttributes
        None,

        # CreationDisposition
        win32file.OPEN_EXISTING,

        # FlagsAndAttributes
        0
        )

    obj_id = win32file.DeviceIoControl(
        # Device : PyHANDLE
        # Handle to a file, device, or volume
        fhandle,

        # IoControlCode : int
        # IOControl Code to use, from winioctlcon
        winioctlcon.FSCTL_CREATE_OR_GET_OBJECT_ID,

        # InBuffer : str/buffer
        # The input data for the operation, can be None for some operations.
        None,

        # OutBuffer : int/buffer
        # Size of the buffer to allocate for output, or a writeable buffer as
        # returned by win32file::AllocateReadBuffer.
        64,

        # Overlapped=None : PyOVERLAPPED An overlapped object for async
        # operations. Device handle must have been opened with
        # FILE_FLAG_OVERLAPPED.
        None
        )
    fhandle.Close()
    return obj_id

调用此函数的一些示例输出是类似str的“↑·∟–kòπ◄Θδ%dΘπ╧hMêc▌╧J╧y╠┘π↑·∟–kòπ◄Θπ╧”。这对于我的程序来说是可以的,只要我备份的每个文件都是一致的。但我有没有做错什么可怕的事?理想情况下,我希望尽可能正确地实现这一点。在


Tags: to函数import程序noneidforobject
2条回答

使用比纳西.hexify()在我问题的输出str上生成:“18fa1c836b95e31188eb002564e9e3cf684d8863dd92cf4aa82fcf79ccd993e318fa1c836b95e31188eb002564e9e3cf00000000000000000000000000000000”。这与以下结果相同:

C:\Windows\system32>fsutil.exe objectid query "myfile.txt"
Object ID :        18fa1c836b95e31188eb002564e9e3cf
BirthVolume ID :   684d8863dd92cf4aa82fcf79ccd993e3
BirthObjectId ID : 18fa1c836b95e31188eb002564e9e3cf
Domain ID :        00000000000000000000000000000000

所以我有独立的证据fsutil.exe文件我的Python函数产生了正确的输出。在

我仍然不知道Windows函数是否被完全正确地使用,如果不正确的话,我将非常感激

是的。这是100%有效的。在

返回三个OBJECT_ID

对象ID:3FB73FE2-6BF2-3F3F-EA3F-2025643F3F

出生量ID:684DEA63-A6C6-2D4A-BF2F-2D79A62BF470

出生对象ID:3FB73FE2-6BF2-3F3F-EA3F-2025643F3F

这一产出与预期相符。:)

相关问题 更多 >