A Rust library for parsing torrent files using libtorrent via FFI.
- Parse .torrent files and extract metadata
- Extract torrent name, files, piece layout, and total size
- Support for both single-file and multi-file torrents
- Proper error handling for invalid or corrupted torrent files
- Rust 1.70 or later
- libtorrent-rasterbar 2.0.x
- OpenSSL
- C++17 compiler
- FUSE 3.x (libfuse3-dev)
TorrentFS uses FUSE to mount the virtual filesystem. To allow non-root users to access the mount point, you need to configure FUSE:
-
Edit
/etc/fuse.conf:sudo sed -i 's/#user_allow_other/user_allow_other/' /etc/fuse.confOr manually uncomment the
user_allow_otherline in/etc/fuse.conf:user_allow_other -
Ensure your user is in the
fusegroup:sudo usermod -aG fuse $USER(Log out and back in for group changes to take effect)
-
On some systems, you may need to install FUSE development headers:
# Ubuntu/Debian sudo apt-get install libfuse3-dev # Fedora sudo dnf install fuse3-devel # Arch Linux sudo pacman -S fuse3
Add this to your Cargo.toml:
[dependencies]
torrentfs = "0.1"use torrentfs::{TorrentInfo, TorrentError};
fn main() -> Result<(), TorrentError> {
let info = TorrentInfo::from_file("example.torrent")?;
println!("Torrent: {}", info.name());
println!("Total Size: {} bytes", info.total_size());
println!("Piece Length: {} bytes", info.piece_length());
println!("Number of Pieces: {}", info.num_pieces());
println!("Number of Files: {}", info.num_files());
let files = info.files()?;
for file in files {
println!(" {} ({} bytes)", file.path, file.size);
}
let hash = info.info_hash()?;
print!("Info Hash: ");
for byte in &hash {
print!("{:02x}", byte);
}
println!();
Ok(())
}The main type for working with torrent metadata.
from_file(path)- Load a torrent file from diskname()- Get the torrent nametotal_size()- Get total size in bytespiece_length()- Get piece length in bytesnum_pieces()- Get number of piecesnum_files()- Get number of filesfiles()- Get list of files with paths and sizesinfo_hash()- Get the 20-byte SHA1 info hashmetadata()- Get all metadata in one struct
The library uses thiserror for error handling:
pub enum TorrentError {
InvalidFile(String),
ParseError(String),
IoError(std::io::Error),
NullPointer,
Unknown { code: i32, message: String },
}# Ensure libtorrent is installed
# On Ubuntu/Debian:
sudo apt-get install libtorrent-rasterbar-dev
# Build
cargo build
# Run tests
cargo test
# Run example
cargo run --example torrent_info -- /path/to/file.torrentMIT