Deleting a file based on disk ID

前端 未结 4 1699
清歌不尽
清歌不尽 2021-02-19 09:09

As described here, using SetFileInformationByHandle with FILE_DISPOSITION_INFO allows one to set a file with an open handle to be deleted upon all handles being closed.

4条回答
  •  我寻月下人不归
    2021-02-19 10:02

    There's a user mode version of the kernel mode ZwCreateFile called NTCreteFile which, among other things will give you all of the access rights you can't get with OpenFileById (but you can get with CreateFile). It can do everything CreateFile can do and more. For example, it can even create directories.

    The good part is, there's an immensely hacky (but entertaining) way of specifying a file ID in the POBJECT_ATTRIBUTES argument as well, so you get the best of all worlds...except that it's an even more awkward API to call than your run-of-the-mill awkward Windows APIs.

    There are two versions of the documentation. One at:

    https://msdn.microsoft.com/en-us/library/bb432380(v=vs.85).aspx

    and one at:

    https://msdn.microsoft.com/en-us/library/windows/hardware/ff556465(v=vs.85).aspx

    ...which links to the ZwCreateFile documentation at:

    https://msdn.microsoft.com/en-us/library/windows/hardware/ff566424(v=vs.85).aspx

    The reason I point this out is that the first article omits some of the goodies (like opening files by ID) that are documented in the last article. I have found this to be common and have also found that most of the documented Zwxxx functionality actually does exists in the equivalent, but incompletely documented NTxxx functions. So you gotta hold your mouth just right to get the requisite functionality.

提交回复
热议问题