从Python调用FSCTL_CREATE_OR_GET_OBJECT_ID [英] Calling FSCTL_CREATE_OR_GET_OBJECT_ID from Python

查看:134
本文介绍了从Python调用FSCTL_CREATE_OR_GET_OBJECT_ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取要在Python备份程序中使用的NTFS对象ID.我不知所措,但是设法创建了一个返回……某些东西的函数.

I'm trying to get NTFS object IDs to use in a Python backup program. I'm in way over my head, but managed to create a function that returns... something.

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

通过调用此函数获得的一些样本输出类似于↑·∟akòπ◄·δ%dΘπ╧hMêc▌Æ╧J¿/╧y╠┘ôπ↑·∟âkòπ◄·δ%dΘπ╧".就我的程序而言,这很合适,只要它与我要备份的每个文件都一致即可.但是我在这里做错什么吗?理想情况下,我想尽可能正确地实现这一点.

Some sample output from calling this function is a str like, "↑·∟âkòπ◄êδ %dΘπ╧hMêc▌Æ╧J¿/╧y╠┘ôπ↑·∟âkòπ◄êδ %dΘπ╧". That would be fine for the purposes of my program, as long as it's consistent for each file I'm backing up. But am I doing anything horribly wrong here? Ideally I'd like to implement this as correctly as possible.

推荐答案

在我的问题的输出str上使用binascii.hexify()会产生以下结果:"18fa1c836b95e31188eb002564e9e3cf684d8863dd92cf4aa82fcf79ccd993e318fa1c836b95e31188eb0025640000009".这与以下结果相同:

Using binascii.hexify() on the output str from my question yields: "18fa1c836b95e31188eb002564e9e3cf684d8863dd92cf4aa82fcf79ccd993e318fa1c836b95e31188eb002564e9e3cf00000000000000000000000000000000". This is the same result as:

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

因此我从fsutil.exe获得了独立的验证,证明我的Python函数正在产生正确的输出.

So I have independent verification from fsutil.exe that my Python function is producing the right output.

我仍然不知道Windows功能是否被绝对正确地使用,如果不正确,将不胜感激.

I still don't know whether the Windows functions were used absolutely correctly, and would appreciate any corrections if they weren't.

这篇关于从Python调用FSCTL_CREATE_OR_GET_OBJECT_ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆