pub struct TreeModel { /* private fields */ }Expand description
The model of a local tree that TreeModel::scan walked and hashed.
For each entry, the model keeps the name, the metadata, and the index of the parent directory. For each regular file, it also keeps the number of payload bytes that the hash pass read. The model holds no file content, and it serializes a dirtree or a dirmeta object again from its fields on each request. Its memory grows with the number of entries and the length of their names.
§Object source
The model is the ObjectSource of the objects of the tree.
object_names gives each object of the tree
once. The model holds no commit.
openof a regular file opens the file again on the blocking pool, with the open and the checks of the hash pass. These are the type check, and on Unix the identity check. It gives the file asObjectData::Content, whosesizeis the payload size that the hash pass read.openof a symlink opens no file.openof a dirtree or a dirmeta object serializes the object again from the model. Each of the two isObjectData::Encodedin therawencoding.content_sizegives the sum of the payload sizes of the hash pass, in each encoding. It reads no file.objectsanddetached_metadataname a commit, so each call returnsError::InvalidInput.
When the model sends a file, it gives the payload size of the hash pass and does not hash the file again. If a file changes after the hash pass, the server verifies the object and refuses it.
If a call names an object that the model does not hold, a commit
included, it returns Error::InvalidInput. If the open of a file fails,
the call returns Error::Walk with the path of the file and one of the
kinds that scan lists. The session returns that error
inside Error::Source.
Implementations§
Source§impl TreeModel
impl TreeModel
Sourcepub async fn scan(root: &Path, options: ScanOptions) -> Result<TreeModel>
pub async fn scan(root: &Path, options: ScanOptions) -> Result<TreeModel>
Walks the directory root and hashes each object of its tree.
The scan gives the checksum of each object of the tree: a content object for each regular file and symlink, and a dirtree and a dirmeta object for each directory.
§Walk
- The walk does not follow symlinks. The walk root must be a directory.
- The walk holds one directory open at a time. It reads a listing to its end and closes the directory before it enters a subdirectory.
- While it reads a listing, the walk reads the metadata of each entry
with
DirEntry::metadataand the target of each symlink. It reads only the walk root withsymlink_metadata. - Each name must be valid UTF-8 and must pass the dirtree name rule of
ostrya-core. Each symlink target must be valid UTF-8. - The walk takes regular files, directories, and symlinks. Any other entry stops the walk before the entry filter runs, so the filter never sees it. Other entries are device nodes, fifos, sockets, and Windows reparse points that are not symlinks.
- After the walk closes a directory, it runs the
EntryFilteron each entry in the order of the listing. Then it enters each kept subdirectory in turn, depth first. - The filter sees the root first, with the empty path.
EntryAction::Skipleaves out an entry, and for a directory its whole subtree. The walk does not open a skipped entry.Skipon the root stops the walk.- If a file has more than one hard link in the tree, the walk reads and hashes the file once for each of its paths.
§Hash pass
- The pass reads each kept regular file once, on the blocking pool of the runtime. It hashes the file over its file header and its payload.
- The pass starts each file as soon as the filter keeps it, in the order
of the walk, with at most
ScanOptions::hash_jobsfiles in flight. - A job reads its file in chunks of at most 64 KiB. Before each chunk, it checks the stop flag of the pass.
- At the first error of the walk or of a job, the pass sets the stop flag and starts no new job. It returns the error after each job in flight stops, so no file of the pass is open when the scan returns.
- If the future of the scan is dropped, the pass sets the stop flag. Each job in flight then stops at its next chunk.
- On Unix, a job opens its file with
O_NOFOLLOW | O_NONBLOCK. On Windows, it opens the file withFILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS. - One metadata read of the open file must show a regular file. On Unix, the device and inode numbers must also equal those that the walk read. If a parent directory of the file becomes a symlink after the walk, the open can reach another file, and this check refuses it. Windows has no identity check.
- The model records the number of payload bytes that each job read.
- After the walk and the jobs end, the scan hashes each directory bottom up: its dirtree object, with the entries sorted by name, and its dirmeta object.
§Errors
Error::InvalidInputifScanOptions::hash_jobsisSome(0). The walk does not start.Error::Walkfor each failure of the walk and of the hash pass. The error names the entry on the local filesystem, the walk root included. It keeps one of theseio::ErrorKindvalues:- the kind of the call that failed, for example
PermissionDeniedorNotFound InvalidInputif the walk root is not a directory, or if the filter returnsSkipfor the rootUnsupportedfor an entry that is not a regular file, a directory, or a symlink, and on Windows for a symlink whose targetstdcannot readInvalidDatafor a name that is not valid UTF-8 or that fails the name rule, and for a symlink target that is not valid UTF-8InvalidDatafor a change by the filter thatEntryMetadoes not allow, and for a file header thatostrya-corerefuses, for example a symlink target with a NUL byteInvalidDataon Windows for a symlink target that holds\or starts with a drive prefix, also if no filter runsInvalidDatafor a file whose type or identity changed after the walk read itInvalidDatafor a dirtree or a dirmeta object of more thanMAX_METADATA_SIZEbytes, and for a tree of more thanu32::MAXkept entries
- the kind of the call that failed, for example
If more than one entry fails, the error names one of the failing paths. If more than one hash job runs, the order of the failures is not fixed.
Examples found in repository?
139fn run(dir: &Path, count: u64, size: u64, compression: Compression) -> io::Result<()> {
140 fs::create_dir_all(dir)?;
141 write_files(dir, count, size)?;
142 ostrya_rt::block_on(async {
143 let model = TreeModel::scan(dir, ScanOptions::default())
144 .await
145 .map_err(io::Error::other)?;
146 let names: Vec<_> = model
147 .object_names()
148 .into_iter()
149 .filter(|name| name.ty == ObjectType::File)
150 .collect();
151 let session = PushSession::over_stream(
152 Cursor::new(scripted_replies().await?),
153 sink(),
154 &["main".to_string()],
155 SessionOptions::default(),
156 )
157 .await
158 .map_err(io::Error::other)?;
159 eprintln!("send pass: start");
160 let start = Instant::now();
161 session
162 .send(&model, &names, &[], compression)
163 .await
164 .map_err(io::Error::other)?;
165 let elapsed = start.elapsed();
166 let files = names.len().max(1) as f64;
167 println!(
168 "{} files of {size} bytes: {:.1} us per file, {:.3} s in all",
169 names.len(),
170 elapsed.as_secs_f64() * 1e6 / files,
171 elapsed.as_secs_f64()
172 );
173 if let Some(peak) = peak_rss() {
174 println!("VmHWM: {peak}");
175 }
176 Ok(())
177 })
178}Sourcepub fn root_dirtree(&self) -> Checksum
pub fn root_dirtree(&self) -> Checksum
Returns the dirtree checksum of the walk root.
Sourcepub fn root_dirmeta(&self) -> Checksum
pub fn root_dirmeta(&self) -> Checksum
Returns the dirmeta checksum of the walk root.
Sourcepub fn object_names(&self) -> Vec<ObjectName>
pub fn object_names(&self) -> Vec<ObjectName>
Returns the name of each object of the tree, once each.
The list holds the content object of each regular file and symlink, and the dirtree and dirmeta objects of each directory. It holds no commit object. The order is the order in which the walk reached the first source of each object.
Examples found in repository?
139fn run(dir: &Path, count: u64, size: u64, compression: Compression) -> io::Result<()> {
140 fs::create_dir_all(dir)?;
141 write_files(dir, count, size)?;
142 ostrya_rt::block_on(async {
143 let model = TreeModel::scan(dir, ScanOptions::default())
144 .await
145 .map_err(io::Error::other)?;
146 let names: Vec<_> = model
147 .object_names()
148 .into_iter()
149 .filter(|name| name.ty == ObjectType::File)
150 .collect();
151 let session = PushSession::over_stream(
152 Cursor::new(scripted_replies().await?),
153 sink(),
154 &["main".to_string()],
155 SessionOptions::default(),
156 )
157 .await
158 .map_err(io::Error::other)?;
159 eprintln!("send pass: start");
160 let start = Instant::now();
161 session
162 .send(&model, &names, &[], compression)
163 .await
164 .map_err(io::Error::other)?;
165 let elapsed = start.elapsed();
166 let files = names.len().max(1) as f64;
167 println!(
168 "{} files of {size} bytes: {:.1} us per file, {:.3} s in all",
169 names.len(),
170 elapsed.as_secs_f64() * 1e6 / files,
171 elapsed.as_secs_f64()
172 );
173 if let Some(peak) = peak_rss() {
174 println!("VmHWM: {peak}");
175 }
176 Ok(())
177 })
178}Trait Implementations§
Source§impl ObjectSource for TreeModel
impl ObjectSource for TreeModel
Source§fn objects<'a>(
&'a self,
commit: &'a Checksum,
) -> BoxFuture<'a, Result<Vec<ObjectName>>>
fn objects<'a>( &'a self, commit: &'a Checksum, ) -> BoxFuture<'a, Result<Vec<ObjectName>>>
commit reaches, and of the commit. Read moreSource§fn open<'a>(
&'a self,
name: &'a ObjectName,
_encoding: Encoding,
) -> BoxFuture<'a, Result<ObjectData>>
fn open<'a>( &'a self, name: &'a ObjectName, _encoding: Encoding, ) -> BoxFuture<'a, Result<ObjectData>>
name. Read moreSource§fn detached_metadata<'a>(
&'a self,
commit: &'a Checksum,
) -> BoxFuture<'a, Result<Option<Value>>>
fn detached_metadata<'a>( &'a self, commit: &'a Checksum, ) -> BoxFuture<'a, Result<Option<Value>>>
Source§fn content_size<'a>(
&'a self,
names: &'a [ObjectName],
_encoding: Encoding,
) -> BoxFuture<'a, Result<Option<u64>>>
fn content_size<'a>( &'a self, names: &'a [ObjectName], _encoding: Encoding, ) -> BoxFuture<'a, Result<Option<u64>>>
Source§fn detached_metadata_encoded<'a>(
&'a self,
commit: &'a Checksum,
) -> BoxFuture<'a, Result<Option<DetachedMetadata>>>
fn detached_metadata_encoded<'a>( &'a self, commit: &'a Checksum, ) -> BoxFuture<'a, Result<Option<DetachedMetadata>>>
commit, as a dict or as its bytes. Read more