file-id-0.2.2/Cargo.lock0000644000000124070000000000100103250ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "bitflags" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "errno" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", "windows-sys", ] [[package]] name = "fastrand" version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "file-id" version = "0.2.2" dependencies = [ "serde", "tempfile", "windows-sys", ] [[package]] name = "libc" version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" [[package]] name = "linux-raw-sys" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" [[package]] name = "proc-macro2" version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] [[package]] name = "rustix" version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys", "windows-sys", ] [[package]] name = "serde" version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "syn" version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tempfile" version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" dependencies = [ "cfg-if", "fastrand", "rustix", "windows-sys", ] [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" [[package]] name = "windows_aarch64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" [[package]] name = "windows_i686_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" [[package]] name = "windows_i686_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" [[package]] name = "windows_x86_64_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" [[package]] name = "windows_x86_64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" file-id-0.2.2/Cargo.toml0000644000000024250000000000100103470ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.72" name = "file-id" version = "0.2.2" authors = ["Daniel Faust "] description = "Utility for reading inode numbers (Linux, MacOS) and file IDs (Windows)" homepage = "https://github.com/notify-rs/notify" documentation = "https://docs.rs/notify" readme = "README.md" keywords = [ "filesystem", "inode", "file", "index", ] categories = ["filesystem"] license = "MIT OR Apache-2.0" repository = "https://github.com/notify-rs/notify.git" [[bin]] name = "file-id" path = "bin/file_id.rs" [dependencies.serde] version = "1.0.89" features = ["derive"] optional = true [dev-dependencies.tempfile] version = "3.10.0" [target."cfg(windows)".dependencies.windows-sys] version = "0.52.0" features = [ "Win32_Storage_FileSystem", "Win32_Foundation", ] file-id-0.2.2/Cargo.toml.orig000064400000000000000000000013641046102023000140310ustar 00000000000000[package] name = "file-id" version = "0.2.2" description = "Utility for reading inode numbers (Linux, MacOS) and file IDs (Windows)" documentation = "https://docs.rs/notify" readme = "README.md" license = "MIT OR Apache-2.0" keywords = ["filesystem", "inode", "file", "index"] categories = ["filesystem"] authors = ["Daniel Faust "] rust-version.workspace = true edition.workspace = true homepage.workspace = true repository.workspace = true [[bin]] name = "file-id" path = "bin/file_id.rs" [dependencies] serde = { workspace = true, optional = true } [target.'cfg(windows)'.dependencies] windows-sys = { workspace = true, features = ["Win32_Storage_FileSystem", "Win32_Foundation"] } [dev-dependencies] tempfile.workspace = true file-id-0.2.2/LICENSE-APACHE000064400000000000000000000251271046102023000130710ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2023 Notify Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. file-id-0.2.2/LICENSE-MIT000064400000000000000000000020471046102023000125750ustar 00000000000000Copyright (c) 2023 Notify Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. file-id-0.2.2/README.md000064400000000000000000000011301046102023000124100ustar 00000000000000# File Id [![ยป Docs](https://flat.badgen.net/badge/api/docs.rs/df3600)][docs] A utility to read file IDs. Modern file systems assign a unique ID to each file. On Linux and MacOS it is called an `inode number`, on Windows it is called `file index`. Together with the `device id`, a file can be identified uniquely on a device at a given time. Keep in mind though, that IDs may be re-used at some point. ## Example ```rust let file_id = file_id::get_file_id(path).unwrap(); println!("{file_id:?}"); ``` ## Features - `serde` for serde support, off by default [docs]: https://docs.rs/file-id file-id-0.2.2/bin/file_id.rs000064400000000000000000000011311046102023000136430ustar 00000000000000use std::io; use file_id::FileId; fn main() { let path = std::env::args().nth(1).expect("no path given"); print_file_id(&path); } #[cfg(target_family = "unix")] fn print_file_id(path: &str) { print_result(file_id::get_file_id(path)); } #[cfg(target_family = "windows")] fn print_file_id(path: &str) { print_result(file_id::get_low_res_file_id(path)); print_result(file_id::get_high_res_file_id(path)); } fn print_result(result: io::Result) { match result { Ok(file_id) => println!("{file_id:?}"), Err(error) => println!("Error: {error}"), } } file-id-0.2.2/src/_bak_lib.rs000064400000000000000000000044551046102023000140250ustar 00000000000000use std::{fs, io, path::Path}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; // #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] // pub struct FileId { // #[cfg(target_family = "unix")] // device: u64, // #[cfg(target_family = "unix")] // inode: u64, // #[cfg(target_family = "windows")] // volume_serial_number: u64, // #[cfg(target_family = "windows")] // file_id: u128, // } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct FileId { pub device: u64, pub inode: u64, } impl FileId { pub fn new(device: u64, inode: u64) -> Self { Self { device, inode } } } #[cfg(target_family = "unix")] pub fn get_file_id(path: impl AsRef) -> io::Result { use std::os::unix::fs::MetadataExt; let metadata = fs::metadata(path.as_ref())?; Ok(FileId { device: metadata.dev(), inode: metadata.ino(), }) } #[cfg(target_family = "windows")] pub fn get_file_id(path: impl AsRef) -> io::Result { use winapi_util::{file::information, Handle}; let handle = Handle::from_path_any(path.as_ref())?; let info = information(&handle)?; Ok(FileId { device: info.volume_serial_number(), inode: info.file_index(), }) } // #[cfg(target_family = "windows")] // pub fn get_file_id(path: impl AsRef) -> io::Result { // use windows_sys::Win32::Foundation::HANDLE; // use windows_sys::Win32::Storage::FileSystem::GetFileInformationByHandleEx; // use windows_sys::Win32::Storage::FileSystem::FILE_INFO_BY_HANDLE_CLASS; // use windows_sys::Win32::Storage::FileSystem::FILE_ID_INFO; // let mut file_id_info: FILE_ID_INFO; // unsafe { // // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getfileinformationbyhandleex // GetFileInformationByHandleEx( // handle, // 0x12, // *file_id_info, // std::mem::size_of(FILE_ID_INFO) // ) // } // let handle = Handle::from_path_any(path.as_ref())?; // let info = information(&handle)?; // Ok(FileId { // file_index: info.file_index(), // volume_serial_number: info.volume_serial_number(), // }) // } file-id-0.2.2/src/lib.rs000064400000000000000000000146001046102023000130420ustar 00000000000000//! Utility for reading inode numbers (Linux, macOS) and file ids (Windows) that uniquely identify a file on a single computer. //! //! Modern file systems assign a unique ID to each file. On Linux and macOS it is called an `inode number`, //! on Windows it is called a `file id` or `file index`. //! Together with the `device id` (Linux, macOS) or the `volume serial number` (Windows), //! a file or directory can be uniquely identified on a single computer at a given time. //! //! Keep in mind though, that IDs may be re-used at some point. //! //! ## Example //! //! ``` //! let file = tempfile::NamedTempFile::new().unwrap(); //! //! let file_id = file_id::get_file_id(file.path()).unwrap(); //! println!("{file_id:?}"); //! ``` //! //! ## Example (Windows Only) //! //! ```ignore //! let file = tempfile::NamedTempFile::new().unwrap(); //! //! let file_id = file_id::get_low_res_file_id(file.path()).unwrap(); //! println!("{file_id:?}"); //! //! let file_id = file_id::get_high_res_file_id(file.path()).unwrap(); //! println!("{file_id:?}"); //! ``` use std::{fs, io, path::Path}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; /// Unique identifier of a file #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum FileId { /// Inode number, available on Linux and macOS. #[cfg_attr(feature = "serde", serde(rename = "inode"))] Inode { /// Device ID #[cfg_attr(feature = "serde", serde(rename = "device"))] device_id: u64, /// Inode number #[cfg_attr(feature = "serde", serde(rename = "inode"))] inode_number: u64, }, /// Low resolution file ID, available on Windows XP and above. /// /// Compared to the high resolution variant, only the lower parts of the IDs are stored. /// /// On Windows, the low resolution variant can be requested explicitly with the `get_low_res_file_id` function. /// /// Details: . #[cfg_attr(feature = "serde", serde(rename = "lowres"))] LowRes { /// Volume serial number #[cfg_attr(feature = "serde", serde(rename = "volume"))] volume_serial_number: u32, /// File index #[cfg_attr(feature = "serde", serde(rename = "index"))] file_index: u64, }, /// High resolution file ID, available on Windows Vista and above. /// /// On Windows, the high resolution variant can be requested explicitly with the `get_high_res_file_id` function. /// /// Details: . #[cfg_attr(feature = "serde", serde(rename = "highres"))] HighRes { /// Volume serial number #[cfg_attr(feature = "serde", serde(rename = "volume"))] volume_serial_number: u64, /// File ID #[cfg_attr(feature = "serde", serde(rename = "file"))] file_id: u128, }, } impl FileId { pub fn new_inode(device_id: u64, inode_number: u64) -> Self { FileId::Inode { device_id, inode_number, } } pub fn new_low_res(volume_serial_number: u32, file_index: u64) -> Self { FileId::LowRes { volume_serial_number, file_index, } } pub fn new_high_res(volume_serial_number: u64, file_id: u128) -> Self { FileId::HighRes { volume_serial_number, file_id, } } } /// Get the `FileId` for the file or directory at `path` #[cfg(target_family = "unix")] pub fn get_file_id(path: impl AsRef) -> io::Result { use std::os::unix::fs::MetadataExt; let metadata = fs::metadata(path.as_ref())?; Ok(FileId::new_inode(metadata.dev(), metadata.ino())) } /// Get the `FileId` for the file or directory at `path` #[cfg(target_family = "windows")] pub fn get_file_id(path: impl AsRef) -> io::Result { let file = open_file(path)?; unsafe { get_file_info_ex(&file).or_else(|_| get_file_info(&file)) } } /// Get the `FileId` with the low resolution variant for the file or directory at `path` #[cfg(target_family = "windows")] pub fn get_low_res_file_id(path: impl AsRef) -> io::Result { let file = open_file(path)?; unsafe { get_file_info(&file) } } /// Get the `FileId` with the high resolution variant for the file or directory at `path` #[cfg(target_family = "windows")] pub fn get_high_res_file_id(path: impl AsRef) -> io::Result { let file = open_file(path)?; unsafe { get_file_info_ex(&file) } } #[cfg(target_family = "windows")] unsafe fn get_file_info_ex(file: &fs::File) -> Result { use std::{mem, os::windows::prelude::*}; use windows_sys::Win32::{ Foundation::HANDLE, Storage::FileSystem::{FileIdInfo, GetFileInformationByHandleEx, FILE_ID_INFO}, }; let mut info: FILE_ID_INFO = mem::zeroed(); let ret = GetFileInformationByHandleEx( file.as_raw_handle() as HANDLE, FileIdInfo, &mut info as *mut FILE_ID_INFO as _, mem::size_of::() as u32, ); if ret == 0 { return Err(io::Error::last_os_error()); }; Ok(FileId::new_high_res( info.VolumeSerialNumber, u128::from_le_bytes(info.FileId.Identifier), )) } #[cfg(target_family = "windows")] unsafe fn get_file_info(file: &fs::File) -> Result { use std::{mem, os::windows::prelude::*}; use windows_sys::Win32::{ Foundation::HANDLE, Storage::FileSystem::{GetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION}, }; let mut info: BY_HANDLE_FILE_INFORMATION = mem::zeroed(); let ret = GetFileInformationByHandle(file.as_raw_handle() as HANDLE, &mut info); if ret == 0 { return Err(io::Error::last_os_error()); }; Ok(FileId::new_low_res( info.dwVolumeSerialNumber, ((info.nFileIndexHigh as u64) << 32) | (info.nFileIndexLow as u64), )) } #[cfg(target_family = "windows")] fn open_file>(path: P) -> io::Result { 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) }