| Crates.io | helia-unixfs |
| lib.rs | helia-unixfs |
| version | 0.1.3 |
| created_at | 2025-10-11 04:10:45.628694+00 |
| updated_at | 2025-10-11 04:10:45.628694+00 |
| description | UnixFS filesystem implementation for Helia |
| homepage | https://github.com/cyberfly-io/rust-helia |
| repository | https://github.com/cyberfly-io/rust-helia |
| max_upload_size | |
| id | 1877844 |
| size | 250,819 |
A Rust implementation of the UnixFS filesystem for IPFS/Helia.
This crate provides a filesystem abstraction over IPFS, enabling file and directory operations with content-addressed storage. It implements the UnixFS specification, allowing you to create, read, update, and delete files and directories on IPFS.
Add this to your Cargo.toml:
[dependencies]
helia-unixfs = "0.1.0"
use helia_unixfs::{UnixFS, UnixFSInterface};
use bytes::Bytes;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let helia = helia::create_helia_default().await?;
let fs = UnixFS::new(Arc::new(helia));
// Add a file
let data = Bytes::from("Hello, IPFS!");
let cid = fs.add_bytes(data, None).await?;
println!("Added file: {}", cid);
// Read the file
let content = fs.cat(&cid, None).await?;
println!("File content: {:?}", content);
Ok(())
}
use helia_unixfs::{FileCandidate, AddOptions};
// Create a file with permissions and metadata
let file = FileCandidate {
path: "document.txt".to_string(),
content: Bytes::from("Important data"),
mode: Some(0o644), // rw-r--r--
mtime: None, // Uses current time
};
let cid = fs.add_file(file, Some(AddOptions {
pin: true, // Pin the content
..Default::default()
})).await?;
use helia_unixfs::{DirectoryCandidate, MkdirOptions};
// Create a directory
let dir_cid = fs.add_directory(None, None).await?;
// Create a subdirectory
let updated_dir_cid = fs.mkdir(
&dir_cid,
"documents",
Some(MkdirOptions {
mode: Some(0o755),
..Default::default()
})
).await?;
// Add a file to the directory
let file_cid = fs.add_bytes(Bytes::from("test"), None).await?;
let dir_with_file = fs.cp(&file_cid, &dir_cid, "test.txt", None).await?;
// List directory contents
let mut entries = fs.ls(&dir_with_file, None).await?;
while let Some(entry) = entries.next().await {
println!("Entry: {} ({})", entry.name, entry.cid);
}
use helia_unixfs::CatOptions;
// Read from offset 10, length 20
let options = CatOptions {
offset: Some(10),
length: Some(20),
};
let partial_data = fs.cat(&cid, Some(options)).await?;
// Remove a file from a directory
let updated_dir_cid = fs.rm(&dir_cid, "test.txt", None).await?;
use helia_unixfs::UnixFSStat;
let stat = fs.stat(&cid, None).await?;
match stat {
UnixFSStat::File(file_stat) => {
println!("File size: {} bytes", file_stat.size);
println!("Mode: {:?}", file_stat.mode);
}
UnixFSStat::Directory(dir_stat) => {
println!("Directory entries: {}", dir_stat.entries);
}
}
The crate is structured as follows:
UnixFSErrorUnixFS: Main filesystem implementationUnixFSInterface: Trait for filesystem operationsUnixFSType: Enum for file system entry types (File, Directory, Symlink, Raw)UnixFSStat: Statistics for files and directoriesUnixFSEntry: Directory entry with name, CID, size, and typeAddOptions: Options for adding content (pinning, chunking)CatOptions: Options for reading files (offset, length)LsOptions: Options for listing directoriesCpOptions: Options for copying filesMkdirOptions: Options for creating directories (mode, mtime)RmOptions: Options for removing entriesStatOptions: Options for getting statistics✅ Implemented:
⚠️ Limitations:
🔄 Future Enhancements:
Contributions are welcome! Please feel free to submit a Pull Request.
Licensed under either of
at your option.