bojand / infer

Small crate to infer file and MIME type by checking the magic number signature
MIT License
299 stars 28 forks source link

Don't touch access timestamp when inferring type from path #102

Open pmetras opened 3 weeks ago

pmetras commented 3 weeks ago

On Unix, when opening a file with std::fs::File::open(), the file is opened with O_RDONLY mode but the access timestamp in the file metadata is updated (see https://github.com/posborne/rust-systems-programming/blob/master/file-io.md and man 7 inode). In the case of infer, trying to infer a file type with infer::get_from_path should be without this side effect as it is on Windows where the file type can be deducted from the extension.

The way to do it is to open the file with the mode O_NOATIME, that can be done with the following code:

// We open the file without changing the access time in the metadata
let mode = (libc::O_RDONLY | libc::O_NOATIME).try_into().unwrap();
let mut file = OpenOptions::new().mode(mode).open(entry.path())?;

Please change infer::get_from_path to remove this side effect.