Deleting a file based on disk ID

前端 未结 4 1709
清歌不尽
清歌不尽 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 09:55

    Assume the files are XXX and xxx and you want to delete XXX.

    1. MoveFile("XXX", "I think it's XXX")
    2. If XXX got renamed, then DeleteFile("I think it's XXX")
    3. Otherwise, DeleteFile("XXX"); MoveFile("I think it's XXX", "xxx")

    As to OpenFileById, as you noted, there is a potential ambiguity with a file with multiple names (aka hard links). Allowing DELETE access could cause havoc with this, with an unexpected name being deleted (if it were left to the file system to select which one). I suspect they opted for the simple case of never letting DELETE access be granted.

    A similar argument could be made for allowing hard links to directories. Sure, you could do it some of the time correctly, but once you created a cycle, things get a lot tougher...

提交回复
热议问题