Skip to content

Commit 1e75173

Browse files
authored
Update lib.rs
1 parent f30914b commit 1e75173

1 file changed

Lines changed: 52 additions & 20 deletions

File tree

source-code/src/lib.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct HFS {
5353
impl HFS {
5454
pub fn new(db_path: &Path, cybersecurity: bool, key: Option<Key>, compression_type: Option<String>, noatime: bool) -> Result<Self> {
5555
let db = sled::open(db_path)
56-
.with_context(|| format!("Failed to open database at {}", db_path.display()))?;
56+
.with_context(|| format!("Failed to open database at {}", db_path.display()))?;
5757

5858
let crypto = if cybersecurity {
5959
let key = key.ok_or_else(|| anyhow::anyhow!("Cybersecurity mode requires a key"))?;
@@ -64,11 +64,11 @@ impl HFS {
6464

6565
let compression = Compression::new(match compression_type.as_deref() {
6666
Some("zlib") => CompressionType::Zlib,
67-
#[cfg(feature = "zstd")]
68-
Some("zstd") => CompressionType::Zstd,
69-
#[cfg(feature = "lz4")]
70-
Some("lz4") => CompressionType::Lz4,
71-
_ => CompressionType::None,
67+
#[cfg(feature = "zstd")]
68+
Some("zstd") => CompressionType::Zstd,
69+
#[cfg(feature = "lz4")]
70+
Some("lz4") => CompressionType::Lz4,
71+
_ => CompressionType::None,
7272
});
7373

7474
let dedup = Deduplication::new(&db)?;
@@ -103,7 +103,7 @@ impl HFS {
103103
};
104104
batch.insert(
105105
format!("inode:{}", ROOT_INO).as_bytes(),
106-
bincode::serialize(&Inode { attr: root_attr.into(), parent: 0 })?,
106+
bincode::serialize(&Inode { attr: root_attr.into(), parent: 0 })?,
107107
);
108108
db.apply_batch(batch)?;
109109
ROOT_INO + 1
@@ -124,17 +124,17 @@ impl HFS {
124124
Ok(Self {
125125
db,
126126
next_ino: AtomicU64::new(next_ino),
127-
crypto,
128-
compression,
129-
dedup,
130-
versioning,
131-
audit,
132-
quota,
133-
xattr,
134-
repair,
135-
cache,
136-
noatime,
137-
background_repair_sender: Some(tx),
127+
crypto,
128+
compression,
129+
dedup,
130+
versioning,
131+
audit,
132+
quota,
133+
xattr,
134+
repair,
135+
cache,
136+
noatime,
137+
background_repair_sender: Some(tx),
138138
})
139139
}
140140

@@ -276,7 +276,7 @@ impl HFS {
276276
}
277277
let bytes_to_write = (FS_BLOCK_SIZE as usize - block_start).min(data.len() - pos);
278278
block[block_start..block_start + bytes_to_write]
279-
.copy_from_slice(&data[pos..pos + bytes_to_write]);
279+
.copy_from_slice(&data[pos..pos + bytes_to_write]);
280280
self.put_block(ino, block_idx, &block)?;
281281
pos += bytes_to_write;
282282
}
@@ -346,11 +346,43 @@ impl HFS {
346346

347347
pub(crate) fn with_batch<F>(&self, f: F) -> Result<(), HfsError>
348348
where
349-
F: FnOnce(&mut sled::Batch) -> Result<(), HfsError>,
349+
F: FnOnce(&mut sled::Batch) -> Result<(), HfsError>,
350350
{
351351
let mut batch = sled::Batch::default();
352352
f(&mut batch)?;
353353
self.db.apply_batch(batch)?;
354354
Ok(())
355355
}
356356
}
357+
358+
// Public API for filesystem creation
359+
pub fn format(db_path: &Path, encryption: bool, block_size: Option<u32>) -> Result<(), HfsError> {
360+
let db = sled::open(db_path)?;
361+
let mut batch = sled::Batch::default();
362+
batch.insert(b"next_ino", bincode::serialize(&(ROOT_INO + 1))?);
363+
let root_attr = fuser::FileAttr {
364+
ino: ROOT_INO,
365+
size: 0,
366+
blocks: 0,
367+
atime: std::time::UNIX_EPOCH,
368+
mtime: std::time::UNIX_EPOCH,
369+
ctime: std::time::UNIX_EPOCH,
370+
crtime: std::time::UNIX_EPOCH,
371+
kind: fuser::FileType::Directory,
372+
perm: 0o755,
373+
nlink: 2,
374+
uid: 0,
375+
gid: 0,
376+
rdev: 0,
377+
blksize: block_size.unwrap_or(FS_BLOCK_SIZE),
378+
flags: 0,
379+
};
380+
let inode = Inode { attr: root_attr.into(), parent: 0 };
381+
batch.insert(
382+
format!("inode:{}", ROOT_INO).as_bytes(),
383+
bincode::serialize(&inode)?,
384+
);
385+
db.apply_batch(batch)?;
386+
db.flush()?;
387+
Ok(())
388+
}

0 commit comments

Comments
 (0)