pub struct Writer { /* private fields */ }
Expand description
A basic SquashFS writer.
This provides a simple interface for writing archives. The user calls open
,
add
to add each node, and finish
to finish writing. This is
intended for writing archives that are generated by code or otherwise not reflected by files in
a file system – if you want to archive a tree of files from disk, TreeProcessor
handles
directory tracking so that you don’t have to do it yourself.
Each node must be written before its parent, and an error will be raised if this invariant
is not maintained – however, this is not detected until finish
is called.
let writer = Writer::open("archive.sfs")?;
let mut ids = HashMap::new();
for i in 0..5 {
let mut content = format!("This is the content of file {}.txt.", i).as_bytes();
let source = Source::defaults(SourceData::File(Box::new(content)));
let id = writer.add(source)?;
ids.insert(OsString::from(format!("{}.txt", i)), id);
}
writer.add(Source::defaults(SourceData::Dir(Box::new(ids.into_iter()))))?;
writer.finish()?;
Implementations§
Source§impl Writer
impl Writer
Sourcepub fn open<T: AsRef<Path>>(path: T) -> Result<Self>
pub fn open<T: AsRef<Path>>(path: T) -> Result<Self>
Open a new output file for writing.
If the file exists, it will be overwritten.
Sourcepub fn add(&mut self, source: Source) -> Result<u32>
pub fn add(&mut self, source: Source) -> Result<u32>
Add the provided Source
to the archive.
This writes file data and xattrs to the archive directly, while storing directory tree
information to write when finish
is called.
The returned value is the inode number of the added Source
. If the file is to be added
to a directory (that is, almost always), this number needs to be stored so that it can be
provided when the directory is added. In the current implementation, inode numbers start
at 1 for the first file and count steadily upward, but this behavior may change without
warning.