gpt4 book ai didi

filesystems - 如何通过 id 打开具有 DELETE 访问权限的文件?

转载 作者:行者123 更新时间:2023-12-02 21:51:25 25 4
gpt4 key购买 nike

使用 NT native 函数 NtCreateFile,可以使用 FILE_OPEN_BY_FILE_ID 创建选项按 id 打开文件。但是,这样做时,DELETE 访问标志似乎被忽略。如果我设置它,文件将正常打开,但任何删除或重命名文件的尝试都将失败(例如通过设置 FILE_DELETE_ON_CLOSE 或使用 FILE_RENAME_INFORMATION 类与 NtSetInformationFile)。

用这种方式打开的文件不能删除吗?有没有其他方法可以通过 id 而不是名称来删除文件?

最佳答案

除了 RbMm 的回答之外,我还发现了 Alex Carp 的一篇博文,Some Limitations Using Files Opened By ID ,这解释了这样做的基本原理。

Unfortunately the semantics for files opened by ID are a bit different from the semantics of the same files if they would have been opened by name. The file name namespace for example allows multiple names for a file (hardlinks) while the ID namespace does not. The different semantics of the different namespaces can make it so that some operations don't make sense.

For example because NTFS allows multiple names for a file if the file is opened by ID and an operation that changes the namespace is attempted, which name should be affected? To make this very clear, if file \Foo\f.txt and file \Bar\b.txt are hardlinks to the same file and I open the file by ID and I try to rename it, which name should change? How about if I try a delete?

简而言之,在 NTFS 模型中删除文件实际上意味着删除对文件的引用(也称为名称)。只有删除对它的所有引用后,作为副作用,文件本身才能被删除。很像许多编程语言中的引用计数。

假设有一个操作会获取文件 ID 并删除所有引用以及文件,但这将是一个非常不同的操作并且可能很棘手(例如,它需要对所有受影响的文件名执行权限检查,等等关闭所有相关句柄,防止新文件名引用被删除的文件等)。因此,从这方面来说,它不存在也就不足为奇了。

关于filesystems - 如何通过 id 打开具有 DELETE 访问权限的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com