1use std::{fs, io, path::Path};
41
42#[cfg(feature = "serde")]
43use serde::{Deserialize, Serialize};
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48pub enum FileId {
49 #[cfg_attr(feature = "serde", serde(rename = "inode"))]
51 Inode {
52 #[cfg_attr(feature = "serde", serde(rename = "device"))]
54 device_id: u64,
55
56 #[cfg_attr(feature = "serde", serde(rename = "inode"))]
58 inode_number: u64,
59 },
60
61 #[cfg_attr(feature = "serde", serde(rename = "lowres"))]
69 LowRes {
70 #[cfg_attr(feature = "serde", serde(rename = "volume"))]
72 volume_serial_number: u32,
73
74 #[cfg_attr(feature = "serde", serde(rename = "index"))]
76 file_index: u64,
77 },
78
79 #[cfg_attr(feature = "serde", serde(rename = "highres"))]
85 HighRes {
86 #[cfg_attr(feature = "serde", serde(rename = "volume"))]
88 volume_serial_number: u64,
89
90 #[cfg_attr(feature = "serde", serde(rename = "file"))]
92 file_id: u128,
93 },
94}
95
96impl FileId {
97 #[must_use]
98 pub fn new_inode(device_id: u64, inode_number: u64) -> Self {
99 FileId::Inode {
100 device_id,
101 inode_number,
102 }
103 }
104
105 #[must_use]
106 pub fn new_low_res(volume_serial_number: u32, file_index: u64) -> Self {
107 FileId::LowRes {
108 volume_serial_number,
109 file_index,
110 }
111 }
112
113 #[must_use]
114 pub fn new_high_res(volume_serial_number: u64, file_id: u128) -> Self {
115 FileId::HighRes {
116 volume_serial_number,
117 file_id,
118 }
119 }
120}
121
122impl AsRef<FileId> for FileId {
123 fn as_ref(&self) -> &FileId {
124 self
125 }
126}
127
128#[cfg(target_family = "unix")]
130pub fn get_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
131 use std::os::unix::fs::MetadataExt;
132
133 let metadata = fs::metadata(path.as_ref())?;
134
135 Ok(FileId::new_inode(metadata.dev(), metadata.ino()))
136}
137
138#[cfg(target_family = "unix")]
140pub fn get_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
141 use std::os::unix::fs::MetadataExt;
142
143 let metadata = fs::symlink_metadata(path.as_ref())?;
144
145 Ok(FileId::new_inode(metadata.dev(), metadata.ino()))
146}
147
148#[cfg(target_family = "windows")]
150pub fn get_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
151 let file = open_file(path)?;
152
153 get_file_info_ex(&file).or_else(|_| get_file_info(&file))
154}
155
156#[cfg(target_family = "windows")]
158pub fn get_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
159 let file = open_file_no_follow(path)?;
160
161 get_file_info_ex(&file).or_else(|_| get_file_info(&file))
162}
163
164#[cfg(target_family = "windows")]
166pub fn get_low_res_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
167 let file = open_file(path)?;
168
169 get_file_info(&file)
170}
171
172#[cfg(target_family = "windows")]
174pub fn get_low_res_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
175 let file = open_file_no_follow(path)?;
176
177 get_file_info(&file)
178}
179
180#[cfg(target_family = "windows")]
182pub fn get_high_res_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
183 let file = open_file(path)?;
184
185 get_file_info_ex(&file)
186}
187
188#[cfg(target_family = "windows")]
190pub fn get_high_res_file_id_no_follow(path: impl AsRef<Path>) -> io::Result<FileId> {
191 let file = open_file_no_follow(path)?;
192
193 get_file_info_ex(&file)
194}
195
196#[cfg(target_family = "windows")]
197fn get_file_info_ex(file: &fs::File) -> Result<FileId, io::Error> {
198 use std::{
199 mem::{self, MaybeUninit},
200 os::windows::prelude::*,
201 };
202 use windows_sys::Win32::Storage::FileSystem::{
203 FILE_ID_INFO, FileIdInfo, GetFileInformationByHandleEx,
204 };
205
206 let mut info = MaybeUninit::<FILE_ID_INFO>::zeroed();
207 let ret = unsafe {
213 GetFileInformationByHandleEx(
214 file.as_raw_handle(),
215 FileIdInfo,
216 info.as_mut_ptr().cast(),
217 #[expect(clippy::cast_possible_truncation)]
218 {
219 mem::size_of::<FILE_ID_INFO>() as u32
220 },
221 )
222 };
223 if ret == 0 {
224 return Err(io::Error::last_os_error());
225 }
226
227 let info = unsafe { info.assume_init() };
229 Ok(FileId::new_high_res(
230 info.VolumeSerialNumber,
231 u128::from_le_bytes(info.FileId.Identifier),
232 ))
233}
234
235#[cfg(target_family = "windows")]
236fn get_file_info(file: &fs::File) -> Result<FileId, io::Error> {
237 use std::{mem::MaybeUninit, os::windows::prelude::*};
238 use windows_sys::Win32::Storage::FileSystem::{
239 BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle,
240 };
241
242 let mut info = MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::zeroed();
243 let ret = unsafe { GetFileInformationByHandle(file.as_raw_handle(), info.as_mut_ptr()) };
247 if ret == 0 {
248 return Err(io::Error::last_os_error());
249 }
250
251 let info = unsafe { info.assume_init() };
253 Ok(FileId::new_low_res(
254 info.dwVolumeSerialNumber,
255 (u64::from(info.nFileIndexHigh) << 32) | u64::from(info.nFileIndexLow),
256 ))
257}
258
259#[cfg(target_family = "windows")]
260fn open_file<P: AsRef<Path>>(path: P) -> io::Result<fs::File> {
261 use std::{fs::OpenOptions, os::windows::fs::OpenOptionsExt};
262 use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS;
263
264 OpenOptions::new()
265 .access_mode(0)
266 .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
267 .open(path)
268}
269
270#[cfg(target_family = "windows")]
271fn open_file_no_follow<P: AsRef<Path>>(path: P) -> io::Result<fs::File> {
272 use std::{fs::OpenOptions, os::windows::fs::OpenOptionsExt};
273 use windows_sys::Win32::Storage::FileSystem::{
274 FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,
275 };
276
277 OpenOptions::new()
278 .access_mode(0)
279 .custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT)
280 .open(path)
281}