pub struct FileTree { /* private fields */ }
Expand description
Creates a directory structure suitable for storing large numbers of files. Optionally deletes the created directory and files when dropped.
Slots for new files are allocated using get_new_file()
. This struct will
create new subdirectories as needed to ensure that no subdirectory contains
more than 1,000 files/subdirectories.
Implementations§
Source§impl FileTree
impl FileTree
Sourcepub fn new_in(path: PathBuf, persistent: bool) -> Result<FileTree>
pub fn new_in(path: PathBuf, persistent: bool) -> Result<FileTree>
Create a new directory structure under path
. If persistent
is
false
the directory and all it’s contents will be deleted when
the returned FileTree
is dropped
§Examples
Create a new temporary data structure and make sure the base path exists
use file_tree::FileTree;
use std::env::temp_dir;
let file_tree = FileTree::new_in(temp_dir(), false).unwrap();
assert!(file_tree.get_root().exists());
§Errors
If persistent
is false
, the directory will be created using
tempdir::TempDir
, and any related errors will be returned here
Sourcepub fn new(persistent: bool) -> Result<FileTree>
pub fn new(persistent: bool) -> Result<FileTree>
Create a new directory structure. If persistent
is false
the
directory and all it’s contents will be deleted when the returned
FileTree
is dropped.
§Examples
Create a new temporary data structure and make sure the base path exists
use file_tree::FileTree;
let file_tree = FileTree::new(false).unwrap();
assert!(file_tree.get_root().exists());
§Errors
If persistent
is false
, the directory will be created using
tempdir::TempDir
, and any related errors will be returned here
Examples found in repository?
3fn main() {
4 let mut file_tree = FileTree::new(false).unwrap();
5
6 let writeable_path = file_tree.get_new_file().unwrap();
7 assert_eq!(
8 writeable_path,
9 file_tree.get_root().join("000/000/000/000000000000")
10 );
11
12 let writeable_path_2 = file_tree.get_new_file().unwrap();
13 assert_eq!(
14 writeable_path_2,
15 file_tree.get_root().join("000/000/000/000000000001")
16 );
17}
Sourcepub fn from_existing(path: PathBuf) -> FileTree
pub fn from_existing(path: PathBuf) -> FileTree
Creates a FileTree
from an existing directory structure. path
should
be equivalent to the result of calling get_root()
on the previous
(persistent) FileTree
.
§Examples
Re-create a FileTree
using an existing file structure
use file_tree::FileTree;
use std::fs::File;
// create a `FileTree` with one file
let mut ft = FileTree::new(true).unwrap();
let file_path = ft.get_new_file().unwrap();
File::create(file_path.clone()).unwrap();
let base = ft.get_root();
drop(ft);
// create a `FileTree` using the existing path, and make sure that the
// files we pull back don't overwrite the existing one
let mut ft2 = FileTree::from_existing(base);
let file2 = ft2.get_new_file().unwrap();
assert_eq!(file_path.file_name().unwrap(), "000000000000");
assert_eq!(file2.file_name().unwrap(), "000000000001");
Sourcepub fn get_new_file(&mut self) -> Result<PathBuf>
pub fn get_new_file(&mut self) -> Result<PathBuf>
Returns a PathBuf pointing to an available slot in the file tree. The
file pointed to by the returned PathBuf
will not be created by
this method call, but a new directory will be created if necessary.
This method will ensure that the file pointed to by the returned
PathBuf
does not exist. If this struct was created using an existing
directory structure existing files will be skipped over when generating
new file names to return.
File paths are generated such that each new leaf directory (starting
with 000/000/000/
) will be filled entirely before creating a new
directory (next would be 000/000/001/
).
§Examples
Retrieve two distinct file paths via get_new_file()
use file_tree::FileTree;
let mut file_tree = FileTree::new(false).unwrap();
let writeable_path = file_tree.get_new_file().unwrap();
assert_eq!(
writeable_path,
file_tree.get_root().join("000/000/000/000000000000")
);
let writeable_path_2 = file_tree.get_new_file().unwrap();
assert_eq!(
writeable_path_2,
file_tree.get_root().join("000/000/000/000000000001")
);
§Errors
If a new subdirectory is required, fs::create_dir_all
will be called.
Any errors from that call will be returned here
Examples found in repository?
3fn main() {
4 let mut file_tree = FileTree::new(false).unwrap();
5
6 let writeable_path = file_tree.get_new_file().unwrap();
7 assert_eq!(
8 writeable_path,
9 file_tree.get_root().join("000/000/000/000000000000")
10 );
11
12 let writeable_path_2 = file_tree.get_new_file().unwrap();
13 assert_eq!(
14 writeable_path_2,
15 file_tree.get_root().join("000/000/000/000000000001")
16 );
17}
Sourcepub fn get_root(&self) -> PathBuf
pub fn get_root(&self) -> PathBuf
Return the root path for the file tree
Examples found in repository?
3fn main() {
4 let mut file_tree = FileTree::new(false).unwrap();
5
6 let writeable_path = file_tree.get_new_file().unwrap();
7 assert_eq!(
8 writeable_path,
9 file_tree.get_root().join("000/000/000/000000000000")
10 );
11
12 let writeable_path_2 = file_tree.get_new_file().unwrap();
13 assert_eq!(
14 writeable_path_2,
15 file_tree.get_root().join("000/000/000/000000000001")
16 );
17}