notify-rs / notify

🔭 Cross-platform filesystem notification library for Rust.
https://docs.rs/notify
2.69k stars 215 forks source link

Open file with zero access mode read permissions in `file-id` crate #625

Closed mhnap closed 4 days ago

mhnap commented 2 months ago

As an improvement to the file-id crate, I would propose change: https://github.com/notify-rs/notify/blob/128bf6230c03d39dbb7f301ff7b20e594e34c3a2/file-id/src/lib.rs#L195 to:

.access_mode(0)

From https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#files:

The dwDesiredAccess parameter can be zero, allowing the application to query file attributes without accessing the file if the application is running with adequate security settings. This is useful to test for the existence of a file without opening it for read and/or write access, or to obtain other statistics about the file or directory.

For example, currently, I get an error trying to get info for C:\Users\Administrator\AppData\Local\Microsoft\Windows\UsrClass.dat file:

Os {
    code: 32,
    kind: Uncategorized,
    message: "The process cannot access the file because it is being used by another process.",
},
dfaust commented 2 weeks ago

Good suggestion. Thanks!