use std::{fs, io, path::Path};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum FileId {
#[cfg_attr(feature = "serde", serde(rename = "inode"))]
Inode {
#[cfg_attr(feature = "serde", serde(rename = "device"))]
device_id: u64,
#[cfg_attr(feature = "serde", serde(rename = "inode"))]
inode_number: u64,
},
#[cfg_attr(feature = "serde", serde(rename = "lowres"))]
LowRes {
#[cfg_attr(feature = "serde", serde(rename = "volume"))]
volume_serial_number: u32,
#[cfg_attr(feature = "serde", serde(rename = "index"))]
file_index: u64,
},
#[cfg_attr(feature = "serde", serde(rename = "highres"))]
HighRes {
#[cfg_attr(feature = "serde", serde(rename = "volume"))]
volume_serial_number: u64,
#[cfg_attr(feature = "serde", serde(rename = "file"))]
file_id: u128,
},
}
impl FileId {
#[must_use]
pub fn new_inode(device_id: u64, inode_number: u64) -> Self {
FileId::Inode {
device_id,
inode_number,
}
}
#[must_use]
pub fn new_low_res(volume_serial_number: u32, file_index: u64) -> Self {
FileId::LowRes {
volume_serial_number,
file_index,
}
}
#[must_use]
pub fn new_high_res(volume_serial_number: u64, file_id: u128) -> Self {
FileId::HighRes {
volume_serial_number,
file_id,
}
}
}
impl AsRef<FileId> for FileId {
fn as_ref(&self) -> &FileId {
self
}
}
#[cfg(target_family = "unix")]
pub fn get_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
use std::os::unix::fs::MetadataExt;
let metadata = fs::metadata(path.as_ref())?;
Ok(FileId::new_inode(metadata.dev(), metadata.ino()))
}
#[cfg(target_family = "unix")]
pub fn get_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
use std::os::unix::fs::MetadataExt;
let metadata = fs::symlink_metadata(path.as_ref())?;
Ok(FileId::new_inode(metadata.dev(), metadata.ino()))
}
#[cfg(target_family = "windows")]
pub fn get_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
let file = open_file(path)?;
get_file_info_ex(&file).or_else(|_| get_file_info(&file))
}
#[cfg(target_family = "windows")]
pub fn get_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
let file = open_file_no_follow(path)?;
get_file_info_ex(&file).or_else(|_| get_file_info(&file))
}
#[cfg(target_family = "windows")]
pub fn get_low_res_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
let file = open_file(path)?;
get_file_info(&file)
}
#[cfg(target_family = "windows")]
pub fn get_low_res_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
let file = open_file_no_follow(path)?;
get_file_info(&file)
}
#[cfg(target_family = "windows")]
pub fn get_high_res_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
let file = open_file(path)?;
get_file_info_ex(&file)
}
#[cfg(target_family = "windows")]
pub fn get_high_res_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
let file = open_file_no_follow(path)?;
get_file_info_ex(&file)
}
#[cfg(target_family = "windows")]
fn get_file_info_ex(file: &fs::File) -> Result<FileId, io::Error> {
use std::{
mem::{self, MaybeUninit},
os::windows::prelude::*,
};
use windows_sys::Win32::Storage::FileSystem::{
FILE_ID_INFO, FileIdInfo, GetFileInformationByHandleEx,
};
let mut info = MaybeUninit::<FILE_ID_INFO>::zeroed();
let ret = unsafe {
GetFileInformationByHandleEx(
file.as_raw_handle(),
FileIdInfo,
info.as_mut_ptr().cast(),
#[expect(clippy::cast_possible_truncation)]
{
mem::size_of::<FILE_ID_INFO>() as u32
},
)
};
if ret == 0 {
return Err(io::Error::last_os_error());
}
let info = unsafe { info.assume_init() };
Ok(FileId::new_high_res(
info.VolumeSerialNumber,
u128::from_le_bytes(info.FileId.Identifier),
))
}
#[cfg(target_family = "windows")]
fn get_file_info(file: &fs::File) -> Result<FileId, io::Error> {
use std::{mem::MaybeUninit, os::windows::prelude::*};
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle,
};
let mut info = MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::zeroed();
let ret = unsafe { GetFileInformationByHandle(file.as_raw_handle(), info.as_mut_ptr()) };
if ret == 0 {
return Err(io::Error::last_os_error());
}
let info = unsafe { info.assume_init() };
Ok(FileId::new_low_res(
info.dwVolumeSerialNumber,
(u64::from(info.nFileIndexHigh) << 32) | u64::from(info.nFileIndexLow),
))
}
#[cfg(target_family = "windows")]
fn open_file<P: AsRef<Path>>(path: P) -> io::Result<fs::File> {
use std::{fs::OpenOptions, os::windows::fs::OpenOptionsExt};
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS;
OpenOptions::new()
.access_mode(0)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(path)
}
#[cfg(target_family = "windows")]
fn open_file_no_follow<P: AsRef<Path>>(path: P) -> io::Result<fs::File> {
use std::{fs::OpenOptions, os::windows::fs::OpenOptionsExt};
use windows_sys::Win32::Storage::FileSystem::{
FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,
};
OpenOptions::new()
.access_mode(0)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)
}