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

标签 filesystems nt-native-api

使用 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/

相关文章:

linux - 在 Linux 上,完全清空一个目录而不删除它的正确方法是什么?

rust - 如何访问当前平台的文件路径分隔符?

php - 如何包含超过 2 个目录的文件?

database - 嵌入式数据库的选择?

windows - Windows 和 native API 中的系统调用?

language-agnostic - 内存映射文件的优点是什么?

c++ - 带extern “c”和汇编的未定义函数

windows - 删除由 NtCreateSymbolicLinkObject 创建的符号链接(symbolic link)

iphone - 如何在ios中访问Salesforce附件正文(base64二进制数据)?

c++ - 是否有任何关于 Windows NT Native API 的最新书籍或网站?