| Crates.io | refind |
| lib.rs | refind |
| version | 0.1.2 |
| created_at | 2024-05-15 01:17:38.307093+00 |
| updated_at | 2024-05-15 02:47:01.196577+00 |
| description | Cross platform file locator by file ID. Keep track of files even after they're renamed or moved. |
| homepage | |
| repository | https://github.com/thewh1teagle/refind |
| max_upload_size | |
| id | 1240510 |
| size | 28,841 |
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/