#file #cross-platform #system #sync #search

refind

Cross platform file locator by file ID. Keep track of files even after they're renamed or moved.

3 releases

0.1.2 May 15, 2024
0.1.1 May 15, 2024
0.1.0 May 15, 2024

#539 in Filesystem

MIT license

14KB
238 lines

refind

Crates License

Keep track of files even after they're renamed or moved.

Introduction

refind is cross platform Rust crate for locating file by it's ID.

Keep track of files even after they're renamed or moved.

Supported platforms

Windows and macOS, with limited functionality on Linux.

Install

cargo add refind

Usage

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());
}

Examples

See examples

ID format

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.

How it works

macOS

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

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

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/

Dependencies

~0.2–35MB
~533K SLoC