-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first draft with Directory trait for Archive and Dir
- Loading branch information
Showing
10 changed files
with
229 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use crate::fs::feature::git::GitCache; | ||
use std::fs; | ||
use std::io; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use log::*; | ||
|
||
use crate::fs::dir::{DotFilter, Files}; | ||
use crate::fs::File; | ||
|
||
pub trait Directory: Sized { | ||
/// Create a new Dir object filled with all the files in the directory | ||
/// pointed to by the given path. Fails if the directory can’t be read, or | ||
/// isn’t actually a directory, or if there’s an IO error that occurs at | ||
/// any point. | ||
/// | ||
/// The `read_dir` iterator doesn’t actually yield the `.` and `..` | ||
/// entries, so if the user wants to see them, we’ll have to add them | ||
/// ourselves after the files have been read. | ||
fn read_dir(path: PathBuf) -> io::Result<Self>; | ||
|
||
/// Produce an iterator of IO results of trying to read all the files in | ||
/// this directory. | ||
fn files<'dir, 'ig>( | ||
&'dir self, | ||
dots: DotFilter, | ||
git: Option<&'ig GitCache>, | ||
git_ignoring: bool, | ||
deref_links: bool, | ||
total_size: bool, | ||
) -> Files<'dir, 'ig>; | ||
|
||
/// Whether this directory contains a file with the given path. | ||
fn contains(&self, path: &Path) -> bool; | ||
/// Append a path onto the path specified by this directory. | ||
fn join(&self, child: &Path) -> PathBuf; | ||
} | ||
|
||
struct ArchiveEntry { | ||
// name: String, | ||
path: String, | ||
size: u64, | ||
// permissions | ||
// owner | ||
// file type | ||
// mtime | ||
} | ||
|
||
pub enum ArchiveFormat { | ||
Tar, | ||
Unknown, | ||
} | ||
|
||
trait ArchiveReader { | ||
fn read_dir(path: &PathBuf) -> io::Result<Vec<ArchiveEntry>>; | ||
} | ||
|
||
struct TarReader {} | ||
|
||
impl ArchiveReader for TarReader { | ||
fn read_dir(path: &PathBuf) -> io::Result<Vec<ArchiveEntry>> { | ||
let result = Vec::new(); | ||
let file_content = fs::File::open(path)?; | ||
tar::Archive::new(file_content).entries().map(|entries| { | ||
for entry in entries { | ||
if let Ok(e) = entry { | ||
let path = e.path().expect("TODO").to_string_lossy().to_string(); | ||
let size = e.size(); | ||
result.push(ArchiveEntry { path, size }); | ||
} | ||
} | ||
})?; | ||
Ok(result) | ||
} | ||
} | ||
|
||
impl ArchiveFormat { | ||
pub fn from_extension(extension: &str) -> Option<ArchiveFormat> { | ||
match extension { | ||
".tar" => Some(ArchiveFormat::Tar), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
pub struct Archive { | ||
//pub file: &'a File<'dir>, | ||
pub format: ArchiveFormat, | ||
|
||
contents: Vec<ArchiveEntry>, | ||
pub path: PathBuf, | ||
} | ||
|
||
impl Directory for Archive { | ||
fn read_dir(path: PathBuf) -> io::Result<Self> { | ||
let extension = path | ||
.extension() | ||
.unwrap_or(std::ffi::OsStr::new("")) | ||
.to_string_lossy(); | ||
let format = | ||
ArchiveFormat::from_extension(extension.as_ref()).unwrap_or(ArchiveFormat::Unknown); | ||
let contents = match format { | ||
ArchiveFormat::Tar => TarReader::read_dir(&path), | ||
ArchiveFormat::Unknown => { | ||
return Err(io::Error::new( | ||
io::ErrorKind::Unsupported, | ||
"Unsupported archive format", | ||
)) | ||
} | ||
}?; | ||
Ok(Archive { | ||
path, | ||
contents, | ||
format, | ||
}) | ||
} | ||
|
||
/// Produce an iterator of IO results of trying to read all the files in | ||
/// this directory. | ||
fn files<'dir, 'ig>( | ||
&'dir self, | ||
dots: DotFilter, | ||
git: Option<&'ig GitCache>, | ||
git_ignoring: bool, | ||
deref_links: bool, | ||
total_size: bool, | ||
) -> Files<'dir, 'ig> { | ||
Files { | ||
inner: self.contents.iter(), | ||
dir: self, | ||
dotfiles: dots.shows_dotfiles(), | ||
dots: dots.dots(), | ||
git, | ||
git_ignoring, | ||
deref_links, | ||
total_size, | ||
} | ||
} | ||
|
||
/// Whether this directory contains a file with the given path. | ||
fn contains(&self, path: &Path) -> bool {} | ||
/// Append a path onto the path specified by this directory. | ||
fn join(&self, child: &Path) -> PathBuf {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::options::parser::MatchedFlags; | ||
use crate::options::{flags, OptionsError}; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum ArchiveInspection { | ||
Always, | ||
Never, | ||
} | ||
|
||
impl ArchiveInspection { | ||
pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> { | ||
Ok(if matches.has(&flags::INSPECT_ARCHIVES)? { | ||
ArchiveInspection::Always | ||
} else { | ||
ArchiveInspection::Never | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters