![](https://private-user-images.githubusercontent.com/61390950/330634027-bc625185-bb8c-450f-af3a-47780e4bb21f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNTE2MTMsIm5iZiI6MTcyMjE1MTMxMywicGF0aCI6Ii82MTM5MDk1MC8zMzA2MzQwMjctYmM2MjUxODUtYmI4Yy00NTBmLWFmM2EtNDc3ODBlNGJiMjFmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDA3MjE1M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTRjOTBlYWZmYzcxMmZkMWYxMGUzZTdhODllZmQxNjBiODBlNjk1ZTUzMWFkNmUwOThjMDcxM2JjYTM0YzA0MmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.RgvZudkJ5x0Iyuk2Nm4YfTKcNw0yuL_bCubpw9UTauk)
Keep track of files even after they're renamed or moved.
refind
is cross platform Rust
crate for locating file by it's ID
.
Keep track of files even after they're renamed or moved.
Windows
and macOS
, with limited functionality on Linux
.
cargo add refind
Create file ID
fn main() {
let id = refind::get_id("<path>".into()).unwrap();
println!("ID: {}", id);
}
Find path from ID
fn main() {
let realpath = refind::find_path("<id">).unwrap();
println!("Path: {}", realpath.display());
}
See examples
In Windows
, the ID consists of a string representation of an unsigned 64-bit integer, e.g., 111111111111111
.
In macOS
, it's the device ID combined with the inode using a :
separator, e.g., 111111111:2222222222
.
macOS
has a special directory, .vol
, allowing file access via device number and file inode. It also retrieves file paths from descriptors.
refind
library creates a file ID with device ID and inode from stat
, facilitating path retrieval via .vol. Finally, it uses fcntl
with F_GETPATH
for realpath. see more Here.
To demonstrate, stat <path>
, copy device ID and inode, then use
GetFileInfo /.vol/{device id}/{file inode}
Windows
defines unique identifier for files and provide a way to get it via GetFileInformationByHandle
Later refind
uses OpenFileById
to open it by the ID, and uses GetFileInformationByHandleEx
with FILE_INFO_BY_HANDLE_CLASS
to get it's path.
To demonstrate, you can get file id with
fsutil file queryFileid <path>
And to get it's path from the ID you can use
fsutil file queryFileNamebyid <volume> fileID
See microsoft/extendedfileapis
Linux
resorts to a naive approach by traversing the filesystem from the home folder downwards, comparing file IDs. While relatively fast, it lacks the efficiency of macOS and Windows methods. see stackoverflow.com/questions/1406679/